package com.qmth.sop.server.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.business.bean.params.DingSubmitParam;
import com.qmth.sop.business.bean.query.DingSubmitQuery;
import com.qmth.sop.business.bean.result.DingSubmitResult;
import com.qmth.sop.business.entity.TBTask;
import com.qmth.sop.business.service.TBDingSubmitService;
import com.qmth.sop.business.service.TBTaskService;
import com.qmth.sop.business.templete.execute.AsyncDingSubmitExportService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.TaskTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: 考勤提交前端控制器
 * @Author: CaoZixuan
 * @Date: 2023-08-29
 */
@Api(tags = "考勤提交Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DING_SUBMIT)
public class TBDingSubmitController {
    @Resource
    private TBDingSubmitService tbDingSubmitService;
    @Resource
    private TBTaskService tbTaskService;
    @Resource
    private AsyncDingSubmitExportService asyncDingSubmitExportService;

    @ApiOperation(value = "考勤提交-分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = DingSubmitResult.class)})
    public Result findDingSubmitPage(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                     @ApiParam(value = "考勤提交状态") @RequestParam(required = false) String dingSubmitStatusDesc,
                                     @ApiParam(value = "提交人id") @RequestParam(required = false) String submitUserId,
                                     @ApiParam(value = "档案名称") @RequestParam(required = false) String archivesName,
                                     @ApiParam(value = "供应商id") @RequestParam(required = false) String supplierId,
                                     @ApiParam(value = "客户名") @RequestParam(required = false) String customName,
                                     @ApiParam(value = "sop号") @RequestParam(required = false) String sopNo,
                                     @ApiParam(value = "违规工时限制") @RequestParam(required = false) Integer violationHoursLimit,
                                     @ApiParam(value = "需处理异常考勤限制") @RequestParam(required = false) Integer dingExceptionLimit,
                                     @ApiParam(value = "补卡限制") @RequestParam(required = false) Integer remainLimit,
                                     @ApiParam(value = "异常限制") @RequestParam(required = false) Integer exceptionLimit,
                                     @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) {

        return ResultUtil.ok(tbDingSubmitService.findDingSubmitPage(SystemConstant.convertIdToLong(serviceUnitId),
                dingSubmitStatusDesc, SystemConstant.convertIdToLong(submitUserId), archivesName,
                SystemConstant.convertIdToLong(supplierId), customName, sopNo, violationHoursLimit,
                dingExceptionLimit, remainLimit, exceptionLimit, pageNumber, pageSize));
    }

    @ApiOperation(value = "考勤提交-小计")
    @RequestMapping(value = "/sub_total", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "查询成功", response = DingSubmitResult.class)})
    public Result findDingSubmitSubTotal(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                         @ApiParam(value = "考勤提交状态") @RequestParam(required = false) String dingSubmitStatusDesc,
                                         @ApiParam(value = "提交人id") @RequestParam(required = false) String submitUserId,
                                         @ApiParam(value = "档案名称") @RequestParam(required = false) String archivesName,
                                         @ApiParam(value = "供应商id") @RequestParam(required = false) String supplierId,
                                         @ApiParam(value = "客户名") @RequestParam(required = false) String customName,
                                         @ApiParam(value = "sop号") @RequestParam(required = false) String sopNo,
                                         @ApiParam(value = "违规工时限制") @RequestParam(required = false) Integer violationHoursLimit,
                                         @ApiParam(value = "需处理异常考勤限制") @RequestParam(required = false) Integer dingExceptionLimit,
                                         @ApiParam(value = "补卡限制") @RequestParam(required = false) Integer remainLimit,
                                         @ApiParam(value = "异常限制") @RequestParam(required = false) Integer exceptionLimit) {

        return ResultUtil.ok(tbDingSubmitService.findDingSubmitSubTotal(SystemConstant.convertIdToLong(serviceUnitId),
                dingSubmitStatusDesc, SystemConstant.convertIdToLong(submitUserId), archivesName,
                SystemConstant.convertIdToLong(supplierId), customName, sopNo, violationHoursLimit,
                dingExceptionLimit, remainLimit, exceptionLimit));
    }

    @ApiOperation(value = "考勤提交-批量提交")
    @RequestMapping(value = "/submit_batch", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result submitBatch(@Valid @RequestBody List<DingSubmitParam> dingSubmitParamList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        tbDingSubmitService.submitDingBatch(dingSubmitParamList);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "考勤提交-考勤撤回")
    @RequestMapping(value = "/apply_withdraw", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result applyWithdraw(@Valid @RequestBody DingSubmitParam dingSubmitParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        tbDingSubmitService.applyWithdraw(dingSubmitParam);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "考勤提交-取消撤回")
    @RequestMapping(value = "/cancel_withdraw", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result submitBatch(@Valid @RequestBody DingSubmitParam dingSubmitParam, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        tbDingSubmitService.cancelWithdraw(dingSubmitParam);
        return ResultUtil.ok();
    }

    @ApiOperation(value = "考勤提交-同意撤回")
    @RequestMapping(value = "/agree_withdraw", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "更新成功", response = Result.class)})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result agreeWithdraw(@ApiParam(value = "考勤提交记录id", required = true) @RequestParam() String dingSubmitId) {
        tbDingSubmitService.agreeWithdraw(SystemConstant.convertIdToLong(dingSubmitId));
        return ResultUtil.ok();
    }


    @ApiOperation(value = "考勤提交-批量导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EXPORT)
    public Result exportDingSubmit(@ApiParam(value = "服务单元id") @RequestParam(required = false) String serviceUnitId,
                                   @ApiParam(value = "考勤提交状态") @RequestParam(required = false) String dingSubmitStatusDesc,
                                   @ApiParam(value = "提交人id") @RequestParam(required = false) String submitUserId,
                                   @ApiParam(value = "档案名称") @RequestParam(required = false) String archivesName,
                                   @ApiParam(value = "供应商id") @RequestParam(required = false) String supplierId,
                                   @ApiParam(value = "客户名") @RequestParam(required = false) String customName,
                                   @ApiParam(value = "sop号") @RequestParam(required = false) String sopNo,
                                   @ApiParam(value = "违规工时限制") @RequestParam(required = false) Integer violationHoursLimit,
                                   @ApiParam(value = "需处理异常考勤限制") @RequestParam(required = false) Integer dingExceptionLimit,
                                   @ApiParam(value = "补卡限制") @RequestParam(required = false) Integer remainLimit,
                                   @ApiParam(value = "异常限制") @RequestParam(required = false) Integer exceptionLimit) throws Exception {
        Map<String, Object> map = tbTaskService.saveTask(TaskTypeEnum.DING_SUBMIT_EXPORT);
        DingSubmitQuery query = new DingSubmitQuery();
        query.setServiceUnitId(SystemConstant.convertIdToLong(serviceUnitId));
        query.setDingSubmitStatusDesc(dingSubmitStatusDesc);
        query.setSubmitUserId(SystemConstant.convertIdToLong(submitUserId));
        query.setArchivesName(archivesName);
        query.setSupplierId(SystemConstant.convertIdToLong(supplierId));
        query.setCustomName(customName);
        query.setSopNo(sopNo);
        query.setViolationHoursLimit(violationHoursLimit);
        query.setDingExceptionLimit(dingExceptionLimit);
        query.setRemainLimit(remainLimit);
        query.setExceptionLimit(exceptionLimit);
        map.put(SystemConstant.QUERY, query);

        asyncDingSubmitExportService.exportTask(map);
        TBTask tbTask = (TBTask) map.get(SystemConstant.TASK);
        return ResultUtil.ok(tbTask.getId());
    }
}