package com.qmth.themis.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.themis.business.cache.bean.ExamCacheBean;
import com.qmth.themis.business.cache.bean.ExamPaperCacheBean;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.MqDto;
import com.qmth.themis.business.entity.TBAttachment;
import com.qmth.themis.business.entity.TBTaskHistory;
import com.qmth.themis.business.entity.TBUser;
import com.qmth.themis.business.entity.TEExamPaper;
import com.qmth.themis.business.enums.*;
import com.qmth.themis.business.service.*;
import com.qmth.themis.business.util.MqUtil;
import com.qmth.themis.business.util.OssUtil;
import com.qmth.themis.business.util.ServletUtil;
import com.qmth.themis.common.enums.ExceptionResultEnum;
import com.qmth.themis.common.exception.BusinessException;
import com.qmth.themis.common.util.Result;
import com.qmth.themis.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: 考试试卷 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/6/25
 */
@Api(tags = "考试试卷Controller")
@RestController
@RequestMapping("/${prefix.url.admin}/exam/paper")
public class TEExamPaperController {

    private final static Logger log = LoggerFactory.getLogger(TEExamPaperController.class);

    @Resource
    TEExamPaperService teExamPaperService;

    @Resource
    TBTaskHistoryService taskHistoryService;

    @Resource
    TEExamService teExamService;

    @Resource
    TBAttachmentService tbAttachmentService;

    @Resource
    MqDtoService mqDtoService;

    @Resource
    OssUtil ossUtil;

    @Resource
    MqUtil mqUtil;

    @Resource
    TEExamCourseService examCourseService;

    @ApiOperation(value = "考试试卷查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "考试科目信息", response = TEExamPaper.class)})
    public Result query(@ApiParam(value = "考试批次id", required = true) @RequestParam Long examId,
                        @ApiParam(value = "科目编码", required = true) @RequestParam String courseCode) {
        if (Objects.isNull(examId) || Objects.equals(examId, "")) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        if (Objects.isNull(courseCode) || Objects.equals(courseCode, "")) {
            throw new BusinessException(ExceptionResultEnum.COURSE_CODE_IS_NULL);
        }
        QueryWrapper<TEExamPaper> teExamPaperQueryWrapper = new QueryWrapper<>();
        teExamPaperQueryWrapper.lambda().eq(TEExamPaper::getExamId, examId).eq(TEExamPaper::getCourseCode, courseCode);
        return ResultUtil.ok(teExamPaperService.list(teExamPaperQueryWrapper));
    }

    @ApiOperation(value = "考试试卷参数修改接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @Transactional
    @ApiResponses({@ApiResponse(code = 200, message = "{\"success\":true}", response = Result.class)})
    public Result save(@ApiParam(value = "考试试卷信息", required = true) @RequestBody List<TEExamPaper> teExamPaperList) {
        if (Objects.isNull(teExamPaperList) || teExamPaperList.size() == 0) {
            throw new BusinessException(ExceptionResultEnum.PAPER_INFO_IS_NULL);
        }
        try {
            TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
            teExamPaperList.forEach(s -> {
                s.setUpdateId(tbUser.getId());
                teExamPaperService.saveOrUpdate(s);
            });
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            if (e instanceof DuplicateKeyException) {
                String errorColumn = e.getCause().toString();
                String columnStr = errorColumn.substring(errorColumn.lastIndexOf("key") + 3, errorColumn.length())
                        .replaceAll("'", "");
                throw new BusinessException(
                        "机构id[" + teExamPaperList.get(0).getExamId() + "]下的" + FieldUniqueEnum.convertToCode(columnStr)
                                + "数据不允许重复插入");
            } else if (e instanceof BusinessException) {
                throw new BusinessException(e.getMessage());
            } else {
                throw new RuntimeException(e);
            }
        }
        teExamPaperList.forEach(s -> {
            if (Objects.isNull(teExamPaperService.updateExamPaperCacheBean(s.getId()))) {
                teExamPaperService.deleteExamPaperCacheBean(s.getId());
            }
            ExamPaperCacheBean paper = teExamPaperService.getExamPaperCacheBean(s.getId());
            examCourseService.deleteExamCourseCacheBean(paper.getExamId(), paper.getCourseCode());
        });
        return ResultUtil.ok(true);
    }

    @ApiOperation(value = "考试试卷数据包上传接口")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @Transactional
    @ApiResponses({@ApiResponse(code = 200, message = "{\"taskId\":0}", response = Result.class)})
    public Result importData(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                             @ApiParam(value = "批次ID", required = true) @RequestParam Long examId,
                             @ApiParam(value = "客观题乱序", required = false) @RequestParam Boolean objectiveShuffle,
                             @ApiParam(value = "选项乱序", required = false) @RequestParam Boolean optionShuffle,
                             @ApiParam(value = "音频播放次数", required = false) @RequestParam Integer audioPlayCount,
                             @ApiParam(value = "解析试卷", required = true) @RequestParam Boolean processPaper,
                             @ApiParam(value = "解析标答", required = true) @RequestParam Boolean processAnswer) {
        if (file == null) {
            throw new BusinessException(ExceptionResultEnum.ATTACHMENT_IS_NULL);
        }
        if (examId == null) {
            throw new BusinessException(ExceptionResultEnum.EXAM_ID_IS_NULL);
        }
        Map<String, Object> mapParameter = ossUtil.getAliYunOssPrivateDomain().getMap();
        TBAttachment tbAttachment = null;
        TBTaskHistory tbTaskHistory = null;
        Map<String, Object> transMap = new HashMap<String, Object>();
        try {
            TBUser tbUser = (TBUser) ServletUtil.getRequestAccount();
            tbAttachment = tbAttachmentService
                    .saveAttachment(file, ServletUtil.getRequestMd5(), ServletUtil.getRequestPath(), mapParameter,
                            UploadFileEnum.file, tbUser.getOrgId(), tbUser.getId());
            if (Objects.isNull(tbAttachment)) {
                throw new BusinessException(ExceptionResultEnum.ATTACHMENT_ERROR);
            } else {
                //往任务表里插一条数据
                tbTaskHistory = new TBTaskHistory(TaskTypeEnum.IMPORT_EXAM_PAPER, examId, TaskStatusEnum.INIT,
                        SystemConstant.IMPORT_INIT, 0d, tbAttachment.getName(), tbAttachment.getRemark(),
                        tbUser.getId(), tbUser.getOrgId());
                tbTaskHistory.setExamId(examId);
                taskHistoryService.save(tbTaskHistory);
                transMap.put("tbTaskHistory", tbTaskHistory);
            }
            transMap.put(SystemConstant.EXAM_ID, examId);
            transMap.put(SystemConstant.CREATE_ID, tbUser.getId());
            transMap.put(SystemConstant.ORG_ID, tbUser.getOrgId());
            //先查询考试相关信息
            ExamCacheBean examCacheBean = teExamService.getExamCacheBean(examId);
            if (Objects.isNull(examCacheBean)) {
                throw new BusinessException(ExceptionResultEnum.EXAM_NO);
            }
//            TEExam teExam = teExamService.cacheConvert(examCacheBean);
            transMap.put("objectiveShuffle", objectiveShuffle);
            transMap.put("optionShuffle", optionShuffle);
            transMap.put("audioPlayCount", audioPlayCount);
            transMap.put("processPaper", processPaper);
            transMap.put("processAnswer", processAnswer);
            transMap.put(SystemConstant.REMARK, tbAttachment.getRemark());
            //mq发送消息start
            MqDto mqDto = new MqDto(mqUtil.getMqGroupDomain().getTopic(), MqTagEnum.EXAM_PAPER_IMPORT.name(), transMap,
                    MqTagEnum.EXAM_PAPER_IMPORT, String.valueOf(tbTaskHistory.getId()), tbUser.getName());
            mqDtoService.assembleSendOneWayMsg(mqDto);
            //mq发送消息end
        } catch (Exception e) {
            log.error(SystemConstant.LOG_ERROR, e);
            if (Objects.nonNull(tbAttachment)) {
                tbAttachmentService.deleteAttachment(mapParameter, UploadFileEnum.file, tbAttachment);
            }
            if (e instanceof BusinessException) {
                throw new BusinessException(e.getMessage());
            } else {
                throw new RuntimeException(e);
            }
        }
        return ResultUtil.ok(Collections.singletonMap(SystemConstant.TASK_ID, tbTaskHistory.getId()));
    }
}
