package com.qmth.distributed.print.api;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.entity.ExamPaperStructure;
import com.qmth.distributed.print.business.service.ExamPaperStructureService;
import com.qmth.distributed.print.business.templete.execute.AsyncCloudMarkingTaskService;
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.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 试卷结构 前端控制器
 * </p>
 *
 * @author xf
 */
@Api(tags = "试卷结构Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.exam}/structure")
public class ExamPaperStructureController {

    @Resource
    private ExamPaperStructureService examPaperStructureService;

    @Resource
    private AsyncCloudMarkingTaskService asyncCloudMarkingTaskService;

    /**
     * 查询
     *
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@ApiParam(value = "学期ID", required = false) @RequestParam(required = false) Long semesterId,
                       @ApiParam(value = "考试ID", required = false) @RequestParam(required = false) Long examId,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<ExamPaperStructure> examPaperStructureIPage = examPaperStructureService.listByPropositionTeacherId(semesterId, examId, pageNumber, pageSize, null);
        return ResultUtil.ok(examPaperStructureIPage);
    }


    /**
     * 上传
     *
     * @param md5   文件md5
     * @param files 文件数组
     * @return Result
     */
    @ApiOperation(value = "上传试卷结构、标答")
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public Result upload(@RequestParam("examPaperStructure") String examPaperStructure,
                         @RequestParam("md5") String md5,
                         @RequestParam("keys") String keys,
                         @RequestParam("files") MultipartFile[] files) {
        ExamPaperStructure examPaper = examPaperStructureService.upload(examPaperStructure, md5, keys, files);
        return ResultUtil.ok(String.valueOf(examPaper.getId()), null);
    }

    @ApiOperation(value = "评卷参数-提交")
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = EditResult.class)})
    public Result submitExamPaperParams(@RequestBody String evaluationParameters) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        ExamPaperStructure examPaperStructure = examPaperStructureService.submitExamPaperParams(evaluationParameters, sysUser);
        // 异步同步到云阅卷
        asyncCloudMarkingTaskService.syncPaperStructureAndGroup(examPaperStructure);
        return ResultUtil.ok();
    }

    /**
     * 上传标答文件
     *
     * @param md5  文件md5
     * @param file 文件数组
     * @return Result
     */
    @ApiOperation(value = "上传试卷标答文件")
    @RequestMapping(value = "/upload_answer", method = RequestMethod.POST)
    public Result uploadAnswer(@RequestParam("id") String id,
                               @RequestParam("md5") String md5,
                               @RequestParam("file") MultipartFile file) {
        ExamPaperStructure examPaperStructure = examPaperStructureService.uploadAnswer(id, md5, file);
        // 异步同步到云阅卷
        asyncCloudMarkingTaskService.syncPaperAndAnswer(examPaperStructure);
        return ResultUtil.ok(true);
    }

    /**
     * 保存客观题答案
     *
     * @param body body
     */
    @ApiOperation(value = "更新客观题答案")
    @RequestMapping(value = "/update_objective_answer", method = RequestMethod.POST)
    public Result updateObjectiveAnswer(@RequestBody String body) {
        JSONObject object = JSONObject.parseObject(body, JSONObject.class);
        Long id = object.getLong("id");
        String objectiveStructure = object.getString("objectiveStructure");
        ExamPaperStructure examPaperStructure = examPaperStructureService.updateObjectiveAnswer(id, objectiveStructure);
        // 异步同步到云阅卷
        asyncCloudMarkingTaskService.syncObjectiveStructure(examPaperStructure);
        return ResultUtil.ok(true);
    }

    /**
     * 试卷结构预览
     *
     * @param id id
     */
    @ApiOperation(value = "试卷结构预览")
    @RequestMapping(value = "/preview_structure", method = RequestMethod.POST)
    public Result preStructure(@RequestParam("id") Long id) {
        List<Map> list = examPaperStructureService.preStructure(id);
        return ResultUtil.ok(list);
    }


    @ApiOperation(value = "评卷参数设置-获取题卡图片")
    @RequestMapping(value = "/find_jpg_file", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = Result.class)})
    public Result findCardJpgFileByPaperNumber(@ApiParam(value = "考试id", required = true) @RequestParam String examId,
                                               @ApiParam(value = "课程代码", required = true) @RequestParam String courseCode,
                                               @ApiParam(value = "试卷编号", required = true) @RequestParam String paperNumber) {
        return ResultUtil.ok(examPaperStructureService.findCardJpgFileByPaperNumber(SystemConstant.convertIdToLong(examId), courseCode, paperNumber));
    }
}

