package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.TeachCourseParams;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.service.TeachCourseService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.log.LevelEnum;
import com.qmth.teachcloud.common.enums.log.OperationTypeEnum;
import com.qmth.teachcloud.common.enums.log.OperationUnitEnum;
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}/course")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class TeachCourseController {
    @Resource
    private TeachCourseService teachCourseService;

    @ApiOperation(value = "教学课程-查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    @OperationLogDetail(detail = "根据查询条件课程名称[{{courseName}}]模糊查询教学课程，查询第[{{pageNumber}}]页，每页查[{{pageSize}}]个", operationType = OperationTypeEnum.SELECT, operationUnit = OperationUnitEnum.TEACH_COURSE)
    public Result findTeachCoursePage(@ApiParam(value = "课程名称（模糊查询）") @RequestParam(required = false) String courseName,
                                      @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) {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(teachCourseService.teachCoursePage(courseName, pageNumber, pageSize, requestUser.getId()));
    }

    @ApiOperation(value = "教学课程-创建教学课程")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "创建成功", response = EditResult.class)})
    @OperationLogDetail(detail = "创建教学课程，参数[{{teachCourseParams}}]", level = LevelEnum.TERTIARY, operationType = OperationTypeEnum.EDIT, operationUnit = OperationUnitEnum.TEACH_COURSE)
    public Result createTeachCourse(@Valid @RequestBody TeachCourseParams teachCourseParams, BindingResult bindingResult) throws IllegalAccessException {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SystemConstant.verifyDBFields(teachCourseParams, teachCourseParams.getClass());
        return ResultUtil.ok(teachCourseService.createTeachCourse(teachCourseParams.getBasicCourseId(), requestUser));
    }

    @ApiOperation(value = "教学课程-查找基础课程")
    @RequestMapping(value = "/find_basic_course", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    @OperationLogDetail(detail = "用户查找可被创建课程的基础课程信息", operationType = OperationTypeEnum.SELECT)
    public Result findBasicCourse() {
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(teachCourseService.findBasicCourseByUser(requestUser));
    }

    @ApiOperation(value = "教学课程-批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功", response = EditResult.class)})
    @OperationLogDetail(detail = "用户根据教学课程id集合[{{idList}}]批量删除教学课程", level = LevelEnum.TERTIARY, operationType = OperationTypeEnum.DELETE, operationUnit = OperationUnitEnum.TEACH_COURSE)
    public Result deleteTeachCourse(@ApiParam(value = "选择的要删除的课程id集合", required = true) @RequestParam List<Long> idList) {
        return ResultUtil.ok(teachCourseService.deleteTeachCourseBatch(idList));
    }
}
