package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.analyze.GradePaperDimensionParam;
import com.qmth.distributed.print.business.bean.result.analyze.GradePaperDimensionResult;
import com.qmth.teachcloud.common.enums.DimensionEnum;
import com.qmth.distributed.print.business.service.GradePaperDimensionService;
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 java.io.IOException;

/**
 * <p>
 * 分析-考察点维度表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2022-05-20
 */
@Api(tags = "分析课程维度管理Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.grade}/paper/dimension")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class GradePaperDimensionController {
    @Resource
    private GradePaperDimensionService gradePaperDimensionService;

    @ApiOperation(value = "成绩分析维度-查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradePaperDimensionResult.class)})
    public Result findGradePaperDimensionList(@ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                                              @ApiParam(value = "试卷类型", required = true) @RequestParam String paperType) {

        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (SystemConstant.isOneNull(paperNumber, paperType)) {
            throw ExceptionResultEnum.ERROR.exception("试卷参数不完整");
        }
        return ResultUtil.ok(gradePaperDimensionService.findGradePaperDimensionList(paperNumber, paperType, requestUser));
    }

    @ApiOperation(value = "成绩分析维度-新建维度")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    public Result saveGradePaperDimensionBatch(@Valid @RequestBody GradePaperDimensionParam gradePaperDimensionParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        gradePaperDimensionService.saveGradePaperDimensionBatch(gradePaperDimensionParam, requestUser);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "成绩分析维度-导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result gradePaperDimensionImport(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                                            @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber,
                                            @ApiParam(value = "试卷类型", required = true) @RequestParam String paperType,
                                            @ApiParam(value = "试卷名称", required = true) @RequestParam String paperName,
                                            @ApiParam(value = "维度类型", required = true) @RequestParam DimensionEnum dimensionType) throws IOException, NoSuchFieldException {

        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (SystemConstant.isOneNull(file, paperNumber, paperType, paperName, dimensionType)) {
            throw ExceptionResultEnum.ERROR.exception("参数不完整");
        }

        switch (dimensionType) {
            case KNOWLEDGE:
                gradePaperDimensionService.importGradePaperKnowledgeDimension(file, paperNumber, paperType, paperName, requestUser);
                break;
            case ABILITY:
                gradePaperDimensionService.importGradePaperAbilityDimension(file, paperNumber, paperType, paperName, requestUser);
                break;
        }
        return ResultUtil.ok();
    }

    @ApiOperation(value = "成绩分析维度-模板导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result gradePaperDimensionExport(@ApiParam(value = "课程编号", required = true) @RequestParam String courseCode,
                                            @ApiParam(value = "课程名称", required = true) @RequestParam String courseName,
                                            @ApiParam(value = "维度类型", required = true) @RequestParam DimensionEnum dimensionType) throws Exception {

        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        if (SystemConstant.isOneNull(courseCode,courseName, dimensionType)) {
            throw ExceptionResultEnum.ERROR.exception("参数不完整");
        }
        gradePaperDimensionService.exportGradePaperDimensionTemplate(courseCode,courseName,dimensionType);
        return ResultUtil.ok();
    }
}
