package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.enums.ClassGroupStudentStatusEnum;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.service.ClassGroupStudentMapperService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@RequestMapping("classGroupStudent")
@Api(tags = "班级学生服务")
@RestController
public class ClassGroupStudentController extends BaseController {

    @Autowired
    private ClassGroupStudentMapperService classGroupStudentMapperService;


    @ApiOperation(value = "删除班级学生")
    @PostMapping("/del")
    @PreAuthorize("@pcs.hasPermissions('classGroupStudent/del')")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "classGroupId", value = "班级id", required = true, dataType = "int")})
    public HttpResponseResult del(Integer userId, Integer classGroupId) throws Exception {
        return succeed(classGroupStudentMapperService.delClassGroupStudent(userId, classGroupId,false));
    }

    @ApiOperation(value = "查询班级所有学生")
    @PostMapping("/findAllStudent")
    @PreAuthorize("@pcs.hasPermissions('classGroupStudent/findAllStudent')")
    @ApiImplicitParams({@ApiImplicitParam(name = "classGroupId", value = "班级id", required = true, dataType = "int")})
    public HttpResponseResult findAllStudent(Integer classGroupId) throws Exception {
        return succeed(classGroupStudentMapperService.findClassStudentList(classGroupId, ClassGroupStudentStatusEnum.NORMAL));
    }

    @ApiOperation(value = "调整班级（小班课）")
    @PostMapping("/adjustClassGroup")
    @PreAuthorize("@pcs.hasPermissions('classGroupStudent/adjustClassGroup')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "学生userId", required = true, dataType = "int"),
            @ApiImplicitParam(name = "oldClassGroupId", value = "原班级id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "classGroupId", value = "新班级id", required = true, dataType = "int")
    })
    public HttpResponseResult adjustClassGroup(Integer userId, Integer oldClassGroupId, Integer classGroupId) throws Exception {
        return succeed(classGroupStudentMapperService.adjustClassGroup(userId, oldClassGroupId, classGroupId));
    }


    @ApiOperation(value = "添加学生（通用）")
    @PostMapping("/addStudents")
    @PreAuthorize("@pcs.hasPermissions('classGroupStudent/addStudents')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classGroupId", value = "小课班班级id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userIdsStr", value = "学生UserId,逗号分隔", required = true, dataType = "String")
    })
    public HttpResponseResult addStudents(Integer classGroupId, String userIdsStr) throws Exception {
        return succeed(classGroupStudentMapperService.addStudents(classGroupId, userIdsStr, GroupType.MUSIC));
    }

    /**
     * 获取节假日
     * @param year
     * @return
     */
    @ApiOperation(value = "获取节假日")
    @GetMapping("/queryHoliday")
    @PreAuthorize("@pcs.hasPermissions('classGroupStudent/queryHoliday')")
    public Object queryHoliday(String year) throws IOException {
        return succeed(classGroupStudentMapperService.queryHoliday(year));
    }

    @ApiOperation(value = "修改班级学员")
    @PostMapping("/updateClassGroupStudents")
    @PreAuthorize("@pcs.hasPermissions('classGroupStudent/updateClassGroupStudents')")
    public HttpResponseResult updateClassGroupStudents(Long classGroupId, String studentIds){
        if(Objects.isNull(classGroupId)){
            return failed("请指定班级");
        }
        Set<Integer> studentIdSet = new HashSet<>();
        if(StringUtils.isNotBlank(studentIds)){
            studentIdSet = Arrays.stream(studentIds.split(",")).map(Integer::valueOf).collect(Collectors.toSet());
        }
        classGroupStudentMapperService.updateClassGroupStudents(classGroupId, studentIdSet);
        return succeed();
    }
}
