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.ProjectExchangeResult;
import com.qmth.sop.business.bean.result.TBProjectExchangeViewResult;
import com.qmth.sop.business.entity.TBProjectExchange;
import com.qmth.sop.business.service.TBProjectExchangeService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.*;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * <p>
 * 项目变更计划表 前端控制器
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
@Api(tags = "项目变更计划Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_PROJECT_EXCHANGE)
public class TBProjectExchangeController {
    private final static Logger log = LoggerFactory.getLogger(TBProjectExchangeController.class);

    @Resource
    TBProjectExchangeService tbProjectExchangeService;

    @ApiOperation(value = "项目变更计划申请接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result save(@Valid @ApiParam(value = "项目变更计划申请信息", required = true) @RequestBody TBProjectExchange tbProjectExchange, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tbProjectExchangeService.saveProjectExchange(tbProjectExchange));
    }

    @ApiOperation(value = "项目变更计划列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = ProjectExchangeResult.class)})
    public Result list(@ApiParam(value = "服务单元id") @RequestParam(required = false) Long serviceId,
                       @ApiParam(value = "提交人id") @RequestParam(required = false) Long createUserId,
                       @ApiParam(value = "变更类型") @RequestParam(required = false) DelayWarnTypeEnum type,
                       @ApiParam(value = "客户类型") @RequestParam(required = false) ProductTypeEnum customType,
                       @ApiParam(value = "流程状态") @RequestParam(required = false) ProjectExchangeFlowStatusEnum flowStatus,
                       @ApiParam(value = "客户名称") @RequestParam(required = false) String customName,
                       @ApiParam(value = "变更申请编号") @RequestParam(required = false) String exchangeNo,
                       @ApiParam(value = "提交开始时间") @RequestParam(required = false) Long startTime,
                       @ApiParam(value = "提交结束时间") @RequestParam(required = false) Long endTime,
                       @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(tbProjectExchangeService.list(new Page<>(pageNumber, pageSize), serviceId, createUserId, type, customType, flowStatus, customName, exchangeNo, startTime, endTime));
    }

    @ApiOperation(value = "项目变更计划审核接口")
    @RequestMapping(value = "/flow/approve", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    public Result flowApprove(@ApiParam(value = "流程任务id", required = true) @RequestParam Long taskId,
                              @ApiParam(value = "流程审批", required = true) @RequestParam FlowApprovePassEnum flowApprove,
                              @ApiParam(value = "审核结果", required = true) @RequestParam ProjectExchangeApproveEnum projectExchangeApprove,
                              @ApiParam(value = "flow备注") @RequestParam(required = false) String remark,
                              @ApiParam(value = "审核用户id") @RequestParam(required = false) Long userId) throws InterruptedException {
        return ResultUtil.ok(tbProjectExchangeService.flowApprove(taskId, flowApprove, projectExchangeApprove, remark, userId));
    }

    @ApiOperation(value = "项目变更计划详情接口")
    @RequestMapping(value = "/view", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = TBProjectExchangeViewResult.class)})
    public Result view(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
        return ResultUtil.ok(tbProjectExchangeService.view(id));
    }
}
