package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.TeachClazzParams;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.TeachClazzService;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
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 javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

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

    @ApiOperation(value = "教学班级-查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findTeachClazzPage(@ApiParam(value = "教学课程id") @RequestParam(required = false) String teachCourseId,
                                     @ApiParam(value = "班级名称（模糊查询）") @RequestParam(required = false) String teachClazzName,
                                     @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(teachClazzService.teachClazzPage(SystemConstant.convertIdToLong(teachCourseId), teachClazzName, pageNumber, pageSize));
    }

    @ApiOperation(value = "教学班级-编辑教学班级")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "创建成功", response = EditResult.class)})
    public Result createTeachClazz(@Valid @RequestBody TeachClazzParams teachClazzParams, BindingResult bindingResult) throws IllegalAccessException {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SystemConstant.verifyDBFields(teachClazzParams, teachClazzParams.getClass());
        return ResultUtil.ok(teachClazzService.editTeachClazz(teachClazzParams, requestUser));
    }

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

    @ApiOperation(value = "教学班级-查找教学课程")
    @RequestMapping(value = "/find_teach_course", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findTeachCourse() {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(teachClazzService.findTeachCourseByUserId(requestUser.getId()));
    }
}
