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.TBDelayWarnResult;
import com.qmth.sop.business.entity.TBDelayWarn;
import com.qmth.sop.business.service.TBDelayWarnService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.DelayWarnStatusEnum;
import com.qmth.sop.common.enums.DelayWarnTypeEnum;
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.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;

/**
 * 延期预警跟进表 控制器
 *
 * @author: shudonghui
 * @date: 2023-08-08 16:02:21
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "延期预警跟进表 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DELAY_WARN)
public class TBDelayWarnController {

    @Resource
    TBDelayWarnService tBDelayWarnService;

    @ApiOperation(value = "延期预警跟进表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "延期预警跟进表查询结果", response = TBDelayWarnResult.class)})
    public Result query(
            @ApiParam(value = "预警类型", required = false) @RequestParam(required = false) DelayWarnTypeEnum type,
            @ApiParam(value = "跟进状态", required = false) @RequestParam(required = false) DelayWarnStatusEnum status,
            @ApiParam(value = "服务单元", required = false) @RequestParam(required = false) Long serviceId,
            @ApiParam(value = "客户名称（模糊查询）", required = false) @RequestParam(required = false) String custom,
            @ApiParam(value = "预警字段（模糊查询）", required = false) @RequestParam(required = false) String fieldObj,
            @ApiParam(value = "SOP流水号（模糊查询）", required = false) @RequestParam(required = false) String sopNo,
            @ApiParam(value = "预警时间开始", required = false) @RequestParam(required = false) Long startTime,
            @ApiParam(value = "预警时间结束", required = false) @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(tBDelayWarnService.query(new Page<>(pageNumber, pageSize), type, status, serviceId, custom, fieldObj, sopNo, startTime, endTime, null));
    }

    @ApiOperation(value = "延期预警跟进表获取单个接口")
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "延期预警跟进表信息", response = TBDelayWarnResult.class)})
    public Result get(@ApiParam(value = "延期预警跟进表id", required = true) @RequestParam long id) {
        return ResultUtil.ok(tBDelayWarnService.get(id));
    }

    //关闭延期预警跟进
    @ApiOperation(value = "关闭延期预警跟进")
    @RequestMapping(value = "/close", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "关闭成功")})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result close(@ApiParam(value = "延期预警跟进表id", required = true) @RequestParam long id) {
        TBDelayWarn tBDelayWarn = tBDelayWarnService.getById(id);
        tBDelayWarn.setStatus(DelayWarnStatusEnum.CLOSE);
        tBDelayWarn.setCloseTime(System.currentTimeMillis());
        tBDelayWarnService.saveTBDelayWarn(tBDelayWarn);
        return ResultUtil.ok();
    }

    //重启延期预警跟进
    @ApiOperation(value = "重启延期预警跟进")
    @RequestMapping(value = "/restart", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "重启成功")})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result restart(@ApiParam(value = "延期预警跟进表id", required = true) @RequestParam long id) {
        TBDelayWarn tBDelayWarn = tBDelayWarnService.getById(id);
        tBDelayWarn.setStatus(DelayWarnStatusEnum.RESTART);
        tBDelayWarnService.saveTBDelayWarn(tBDelayWarn);
        return ResultUtil.ok();
    }
}