package com.qmth.distributed.print.api;

import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.annotation.BOOL;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.params.analyze.GradePaperStructParam;
import com.qmth.distributed.print.business.bean.result.analyze.GradePaperStructResult;
import com.qmth.distributed.print.business.service.GradePaperStructService;
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/struct")
@Validated
//@Aac(strict = BOOL.FALSE, auth = BOOL.FALSE)
public class GradePaperStructController {
    @Resource
    private GradePaperStructService gradePaperStructService;

    @ApiOperation(value = "成绩分析试卷结构-查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = GradePaperStructResult.class)})
    public Result findGradePaperStructList(@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(gradePaperStructService.findGradePaperStructureResultList(paperNumber, paperType, requestUser));
    }

    @ApiOperation(value = "成绩分析试卷结构-新建试卷结构")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    public Result saveGradePaperStructBatch(@Valid @RequestBody GradePaperStructParam gradePaperStructParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        SysUser requestUser = (SysUser) ServletUtil.getRequestUser();
        gradePaperStructService.saveGradePaperStructBatch(gradePaperStructParam, requestUser);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "成绩分析试卷结构-导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result gradePaperStructImport(@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) throws IOException, NoSuchFieldException {

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

    @ApiOperation(value = "成绩分析试卷结构-模板导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result gradePaperStructExport(@ApiParam(value = "课程编号", required = true) @RequestParam String paperNumber,
                                         @ApiParam(value = "课程名称", required = true) @RequestParam String paperType) throws Exception {

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