package com.qmth.distributed.print.api;

import com.alibaba.fastjson.JSON;
import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.TeachStudentBatchParam;
import com.qmth.distributed.print.business.bean.params.TeachStudentParams;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.TeachStudentService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * @Description: 教学学生 前端控制器
 * @Author: CaoZixuan
 * @Date: 2022-02-10
 */
@Api(tags = "教学学生Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.teach}/student")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class TeachStudentController {
    @Resource
    private TeachStudentService teachStudentService;

    @ApiOperation(value = "教学学生-查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findTeachStudentPage(@ApiParam(value = "教学班级id", required = true) @RequestParam String teachClazzId,
                                       @ApiParam(value = "学生信息（学号、姓名）") @RequestParam(required = false) String studentInfo,
                                       @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                       @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(teachStudentService.teachStudentPage(SystemConstant.convertIdToLong(teachClazzId), studentInfo, pageNumber, pageSize));
    }

    @ApiOperation(value = "教学学生-创建教学学生")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "创建成功", response = EditResult.class)})
    public Result createTeachStudent(@Valid @RequestBody TeachStudentParams teachStudentParams, BindingResult bindingResult) throws IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        SystemConstant.verifyDBFields(teachStudentParams, teachStudentParams.getClass());
        return ResultUtil.ok(teachStudentService.createTeachStudent(teachStudentParams.getStudentName(), teachStudentParams.getStudentCode(), teachStudentParams.getTeachClazzId(), requestUser));
    }

    @ApiOperation(value = "教学学生-从课程班级批量添加学生")
    @RequestMapping(value = "/create_batch", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "创建成功", response = EditResult.class)})
    public Result createTeachStudentBatchByDictionary(@Valid @RequestBody TeachStudentBatchParam teachStudentBatchParam, BindingResult bindingResult) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        Set<Long> basicStudentIdSet = teachStudentBatchParam.getBasicStudentIdSet();
        if (basicStudentIdSet == null || basicStudentIdSet.size() == 0) {
            throw ExceptionResultEnum.ERROR.exception("请选择要删除的对象");
        }
        Long teachClazzId = teachStudentBatchParam.getTeachClazzId();
        Long schoolId = SystemConstant.convertIdToLong(String.valueOf(ServletUtil.getRequestHeaderSchoolId()));
        teachStudentService.createTeachStudentBatch(basicStudentIdSet, teachClazzId, schoolId, requestUser);
        return ResultUtil.ok();
    }


    @ApiOperation(value = "教学学生-批量导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    public Result teachStudentImport(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                                     @ApiParam(value = "教学班级id", required = true) @RequestParam String teachClazzId) throws IOException, NoSuchFieldException {

        Long schoolId = SystemConstant.convertIdToLong(ServletUtil.getRequestHeaderSchoolId().toString());
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok((Object) teachStudentService.importTeachStudent(file, SystemConstant.convertIdToLong(teachClazzId), schoolId, requestUser));
    }

    @ApiOperation(value = "教学学生-批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    public Result deleteTeachStudent(@ApiParam(value = "选择的要删除的学生id集合", required = true) @RequestParam List<Long> idList) {
        return ResultUtil.ok(teachStudentService.deleteTeachStudentBatch(idList));
    }
}
