package com.qmth.distributed.print.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.bean.dto.TCStatisticsDto;
import com.qmth.distributed.print.business.bean.result.EditResult;
import com.qmth.distributed.print.business.enums.StatisticsStatusEnum;
import com.qmth.distributed.print.business.service.PrintCommonService;
import com.qmth.distributed.print.business.service.TCStatisticsService;
import com.qmth.distributed.print.business.templete.execute.AsyncStatisticsDataImportService;
import com.qmth.teachcloud.common.bean.params.ArraysParams;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.entity.TBTask;
import com.qmth.teachcloud.common.enums.TaskTypeEnum;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 命题统计表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2021-09-09
 */
@Api(tags = "命题统计Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/${prefix.url.statistics}")
@Validated
//@Aac(auth = BOOL.FALSE, strict = BOOL.FALSE)
public class TCStatisticsController {
    private final static Logger log = LoggerFactory.getLogger(TCStatisticsController.class);

    @Resource
    TCStatisticsService tcStatisticsService;

    @Resource
    PrintCommonService printCommonService;

    @Resource
    AsyncStatisticsDataImportService asyncStatisticsDataImportService;

    @ApiOperation(value = "命题计划统计-导入")
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public Result dataImport(@ApiParam(value = "上传文件", required = true) @RequestParam MultipartFile file,
                             @ApiParam(value = "考试id", required = true) @RequestParam String examId) throws Exception {
        Map<String, Object> map = printCommonService.saveTask(file, TaskTypeEnum.STATISTICS_IMPORT);
        map.put("examId",examId);
        asyncStatisticsDataImportService.importTask(map);
        TBTask tbTask = Objects.nonNull(map.get(SystemConstant.TASK)) ? (TBTask) map.get(SystemConstant.TASK) : null;
        return Objects.nonNull(tbTask) ? ResultUtil.ok(tbTask.getId()) : ResultUtil.error("创建任务失败");
    }

    @ApiOperation(value = "命题计划统计-列表")
    @ApiResponses({@ApiResponse(code = 200, message = "命题统计信息", response = TCStatisticsDto.class)})
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@ApiParam(value = "学期id") @RequestParam(required = false) String semesterId,
                       @ApiParam(value = "考试id") @RequestParam(required = false) String examId,
                       @ApiParam(value = "学院id") @RequestParam(required = false) String collegeId,
                       @ApiParam(value = "部门id") @RequestParam(required = false) String teachingRoomId,
                       @ApiParam(value = "完成状态") @RequestParam(required = false) StatisticsStatusEnum status,
                       @ApiParam(value = "课程名称") @RequestParam(required = false) String courseName,
                       @ApiParam(value = "任课老师名称") @RequestParam(required = false) String teacherName,
                       @ApiParam(value = "页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @ApiParam(value = "数量", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        return ResultUtil.ok(tcStatisticsService.findExamTaskPrintStatistic(SystemConstant.convertIdToLong(semesterId), SystemConstant.convertIdToLong(examId), SystemConstant.convertIdToLong(collegeId), SystemConstant.convertIdToLong(teachingRoomId), status, courseName, teacherName, sysUser.getSchoolId(), sysUser.getId(),pageNumber,pageSize));
    }

    @ApiOperation(value = "命题计划统计-刷新数据")
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    @RequestMapping(value = "/freshen", method = RequestMethod.POST)
    public Result freshen(@ApiParam(value = "考试id", required = true) @RequestParam String examId) {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        tcStatisticsService.refreshTCStatisticByExamIdAndRequestUser(SystemConstant.convertIdToLong(examId),sysUser);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "命题计划统计-删除数据")
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = EditResult.class)})
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result delete(@Valid @RequestBody ArraysParams arraysParams, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tcStatisticsService.deleteData(Arrays.asList(arraysParams.getIds())));
    }
}
