package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.TBViolationResult;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.entity.TBViolation;
import com.qmth.sop.business.service.SysUserService;
import com.qmth.sop.business.service.TBViolationService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.ViolationStatusEnum;
import com.qmth.sop.common.enums.ViolationTypeEnum;
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;

/**
 * sop违规登记表 控制器
 *
 * @author: shudonghui
 * @date: 2023-08-09 10:33:08
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "sop违规登记表 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_VIOLATION)
public class TBViolationController {

    @Resource
    TBViolationService tBViolationService;
    @Resource
    SysUserService sysUserService;

    @ApiOperation(value = "sop违规登记表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "sop违规登记表查询结果", response = TBViolationResult.class)})
    public Result query(
            @ApiParam(value = "服务单元", required = false) @RequestParam(required = false) Long serviceId,
            @ApiParam(value = "违规类型", required = false) @RequestParam(required = false) ViolationTypeEnum type,
            @ApiParam(value = "跟进状态", required = false) @RequestParam(required = false) ViolationStatusEnum status,
            @ApiParam(value = "节点负责人", required = false) @RequestParam(required = false) Long userId,
            @ApiParam(value = "客户名称（模糊查询）", required = false) @RequestParam(required = false) String custom,
            @ApiParam(value = "SOP流水号（模糊查询）", required = false) @RequestParam(required = false) String sopNo,
            @ApiParam(value = "登记人", required = false) @RequestParam(required = false) Long createId,
            @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) {
        IPage<TBViolationResult> resultIPage = tBViolationService.query(new Page<>(pageNumber, pageSize), serviceId, type, status, userId, custom, sopNo, createId, startTime, endTime);
        return ResultUtil.ok(resultIPage);
    }

    @ApiOperation(value = "sop违规登记表新增修改接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result save(@Valid @ApiParam(value = "sop违规登记表信息", required = true) @RequestBody(required = true) TBViolation tBViolation, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tBViolationService.saveTBViolation(tBViolation));
    }

    //关闭违规登记
    @ApiOperation(value = "关闭违规登记")
    @RequestMapping(value = "/close", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "关闭成功")})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result close(@ApiParam(value = "违规登记id", required = true) @RequestParam(required = true) long id) {
        TBViolation tBViolation = tBViolationService.getById(id);
        tBViolation.setStatus(ViolationStatusEnum.CLOSE);
        tBViolation.setCloseTime(System.currentTimeMillis());
        return ResultUtil.ok(tBViolationService.saveTBViolation(tBViolation));
    }

    //重启违规登记
    @ApiOperation(value = "重启违规登记")
    @RequestMapping(value = "/restart", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "重启成功")})
    @OperationLog(logType = LogTypeEnum.UPDATE)
    public Result restart(@ApiParam(value = "违规登记id", required = true) @RequestParam(required = true) long id) {
        TBViolation tBViolation = tBViolationService.getById(id);
        tBViolation.setStatus(ViolationStatusEnum.RESTART);
        return ResultUtil.ok(tBViolationService.saveTBViolation(tBViolation));
    }

    //根据code的获取详细信息
    @ApiOperation(value = "sop违规登记表获取单个接口")
    @RequestMapping(value = "/getByCode", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "sop违规登记表信息", response = TBViolationResult.class)})
    public Result getByCode(@ApiParam(value = "sop违规登记表code", required = true) @RequestParam String code) {
        return ResultUtil.ok(tBViolationService.getByCode(code));
    }

    //获取创建者list
    @ApiOperation(value = "获取创建者list")
    @RequestMapping(value = "/creator/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "创建者list", response = SysUser.class)})
    public Result getCreatorList() {
        return ResultUtil.ok(sysUserService.lambdaQuery().inSql(SysUser::getId, "select DISTINCT create_id from t_b_violation").list());
    }

    @ApiOperation(value = "获取创建者list")
    @RequestMapping(value = "/user/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "创建者list", response = SysUser.class)})
    public Result getUserList() {

        return ResultUtil.ok(sysUserService.lambdaQuery().inSql(SysUser::getId, "select DISTINCT user_id from t_b_violation").list());
    }
}