package com.qmth.sop.server.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.DingApplyDoneResult;
import com.qmth.sop.business.bean.result.DingApplyUnDoneResult;
import com.qmth.sop.business.bean.result.TBDingApplyViewResult;
import com.qmth.sop.business.entity.TBDingApply;
import com.qmth.sop.business.service.TBDingApplyService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.DingExceptionApproveEnum;
import com.qmth.sop.common.enums.LogTypeEnum;
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;

/**
 * <p>
 * 考勤补卡申请表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
@Api(tags = "考勤异常申请Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DING_EXCEPTION_APPLY)
public class TBDingApplyController {

    @Resource
    TBDingApplyService tbDingApplyService;

    @ApiOperation(value = "考勤异常申请接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.ADD)
    public Result save(@Valid @ApiParam(value = "考勤异常申请信息", required = true) @RequestBody TBDingApply tbDingApply, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbDingApplyService.saveDingApply(tbDingApply));
    }

    @ApiOperation(value = "考勤异常待审核接口")
    @RequestMapping(value = "/flow/task/un_done/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = DingApplyUnDoneResult.class)})
    public Result flowTaskUnDoneList(@ApiParam(value = "服务单元id") @RequestParam(required = false) Long serviceId,
                                     @ApiParam(value = "姓名") @RequestParam(required = false) String name,
                                     @ApiParam(value = "供应商id") @RequestParam(required = false) Long supplierId,
                                     @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                     @ApiParam(value = "异常开始时间") @RequestParam(required = false) Long startTime,
                                     @ApiParam(value = "异常结束时间") @RequestParam(required = false) Long endTime,
                                     @ApiParam(value = "申请开始时间") @RequestParam(required = false) Long applyStartTime,
                                     @ApiParam(value = "申请结束时间") @RequestParam(required = false) Long applyEndTime,
                                     @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) throws Exception {
        return ResultUtil.ok(tbDingApplyService.flowTaskUnDoneList(new Page<>(pageNumber, pageSize), serviceId, name, supplierId, customName, startTime, endTime, applyStartTime, applyEndTime));
    }

    @ApiOperation(value = "考勤异常已审核接口")
    @RequestMapping(value = "/flow/task/done/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = DingApplyDoneResult.class)})
    public Result flowTaskDoneList(@ApiParam(value = "服务单元id") @RequestParam(required = false) Long serviceId,
                                   @ApiParam(value = "姓名") @RequestParam(required = false) String name,
                                   @ApiParam(value = "供应商id") @RequestParam(required = false) Long supplierId,
                                   @ApiParam(value = "审核结果") @RequestParam(required = false) DingExceptionApproveEnum dingExceptionApprove,
                                   @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                                   @ApiParam(value = "异常开始时间") @RequestParam(required = false) Long startTime,
                                   @ApiParam(value = "异常结束时间") @RequestParam(required = false) Long endTime,
                                   @ApiParam(value = "申请开始时间") @RequestParam(required = false) Long applyStartTime,
                                   @ApiParam(value = "申请结束时间") @RequestParam(required = false) Long applyEndTime,
                                   @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) throws Exception {
        return ResultUtil.ok(tbDingApplyService.flowTaskDoneList(new Page<>(pageNumber, pageSize), serviceId, name, supplierId, dingExceptionApprove, customName, startTime, endTime, applyStartTime, applyEndTime));
    }

    @ApiOperation(value = "考勤异常审核接口")
    @RequestMapping(value = "/flow/approve", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result flowApprove(@ApiParam(value = "流程任务id", required = true) @RequestParam Long taskId,
                              @ApiParam(value = "审核结果", required = true) @RequestParam DingExceptionApproveEnum dingExceptionApprove,
                              @ApiParam(value = "审核用户id") @RequestParam(required = false) Long userId) throws InterruptedException {
        return ResultUtil.ok(tbDingApplyService.flowApprove(taskId, dingExceptionApprove, userId));
    }

    @ApiOperation(value = "考勤异常批量审核接口")
    @RequestMapping(value = "/flow/batch/approve", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result flowBatchApprove(@ApiParam(value = "流程任务id数组", required = true) @RequestParam List<Long> taskIds,
                                   @ApiParam(value = "审核结果", required = true) @RequestParam DingExceptionApproveEnum dingExceptionApprove,
                                   @ApiParam(value = "审核用户id") @RequestParam(required = false) Long userId) throws InterruptedException {
        return ResultUtil.ok(tbDingApplyService.flowBatchApprove(taskIds, dingExceptionApprove, userId));
    }

    @ApiOperation(value = "考勤异常详情接口")
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = TBDingApplyViewResult.class)})
    public Result view(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
        return ResultUtil.ok(tbDingApplyService.view(id));
    }
}
