package com.qmth.distributed.print.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.result.WorkResult;
import com.qmth.distributed.print.business.entity.ExamTaskDetail;
import com.qmth.distributed.print.business.service.ExamTaskDetailService;
import com.qmth.distributed.print.business.service.ExamTaskService;
import com.qmth.distributed.print.business.service.GradeInitializeService;
import com.qmth.teachcloud.common.bean.dto.mark.MarkSettingDto;
import com.qmth.teachcloud.common.bean.vo.PaperInfoVo;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.GradeAnalyzePaperStatusEnum;
import com.qmth.teachcloud.common.util.ExamTaskUtil;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import com.qmth.teachcloud.common.util.ServletUtil;
import com.qmth.teachcloud.mark.service.MarkPaperService;
import io.swagger.annotations.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Description: 工作台 前端控制器
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/4/8
 */
@Api(tags = "工作台Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_WORK)
@Validated
public class WorkController {

    @Resource
    private ExamTaskService examTaskService;
    @Resource
    private ExamTaskDetailService examTaskDetailService;
    @Resource
    private MarkPaperService markPaperService;
    @Resource
    private GradeInitializeService gradeInitializeService;

    @ApiOperation(value = "获取待办接口")
    @RequestMapping(value = "/exam/task/ready", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = WorkResult.class)})
    public Result getFlowTaskReady(@ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                   @ApiParam(value = "数量", required = true) @RequestParam @Min(2) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(examTaskService.getFlowTaskReadyList(new Page<>(pageNumber, pageSize), SystemConstant.getHeadOrUserSchoolId(), sysUser.getOrgId(), sysUser.getId()));
    }

    @ApiOperation(value = "获取待办计数接口")
    @RequestMapping(value = "/exam/task/ready_count", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = WorkResult.class)})
    public Result getFlowTaskReadyCount() {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(examTaskService.getFlowTaskReadyList(new Page<>(SystemConstant.PAGE_NUMBER, SystemConstant.PAGE_SIZE), SystemConstant.getHeadOrUserSchoolId(), sysUser.getOrgId(), sysUser.getId()).getRecords().size());
    }

    /**
     * 阅卷待办任务-数量
     *
     * @return
     */
    @ApiOperation(value = "阅卷待办任务-数量")
    @RequestMapping(value = "/structure/task/ready_count", method = RequestMethod.POST)
    public Result countStructure() {
        return ResultUtil.ok(markPaperService.countByPropositionTeacherId(false));
    }

    /**
     * 阅卷待办任务-数量
     *
     * @return
     */
    @ApiOperation(value = "阅卷待办任务-列表")
    @RequestMapping(value = "/structure/task/ready", method = RequestMethod.POST)
    public Result list(@RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(2) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        Long schoolId = Long.parseLong(ServletUtil.getRequestHeaderSchoolId().toString());
        IPage<MarkSettingDto> markSettingDtoIPage = markPaperService.listPaperSetting(null, null, null, false, pageNumber, pageSize);
        for (MarkSettingDto record : markSettingDtoIPage.getRecords()) {
            ExamTaskDetail examTaskDetail = examTaskDetailService.findByExamIdAndCourseCodeAndPaperNumber(record.getExamId(), record.getCourseCode(), record.getPaperNumber());
            if (examTaskDetail != null) {
                List<PaperInfoVo> paperInfoVos = ExamTaskUtil.parsePaperAttachmentPath(examTaskDetail.getPaperAttachmentIds(), record.getPaperType());
                if (CollectionUtils.isNotEmpty(paperInfoVos)) {
                    record.setCardId(Long.valueOf(paperInfoVos.get(0).getCardId()));
                }
            }
        }
        return ResultUtil.ok(markSettingDtoIPage);
    }

    /**
     * 教研待办任务-数量
     */
    @ApiOperation(value = "教研待办任务-数量")
    @RequestMapping(value = "/grade/task/ready_count", method = RequestMethod.POST)
    public Result countReport() {
        return ResultUtil.ok(gradeInitializeService.countByPropositionTeacherId(GradeAnalyzePaperStatusEnum.SETTING_GRADE_PAPER_PARAM, false));
    }

    /**
     * 教研待办任务
     */
    @ApiOperation(value = "教研待办任务-列表")
    @RequestMapping(value = "/grade/task/ready", method = RequestMethod.POST)
    public Result gradeList(@RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                            @RequestParam @Min(2) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(gradeInitializeService.listNeedAnalyzedPaperToWork((SysUser) ServletUtil.getRequestUser(), pageNumber, pageSize, GradeAnalyzePaperStatusEnum.SETTING_GRADE_PAPER_PARAM, false));
    }
}

