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.params.FlowApproveParam;
import com.qmth.sop.business.bean.result.TBQualityProblemApplyResult;
import com.qmth.sop.business.entity.SysUser;
import com.qmth.sop.business.entity.TBQualityProblemApply;
import com.qmth.sop.business.service.TBQualityProblemApplyService;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.InfluenceDegreeEnum;
import com.qmth.sop.common.enums.LogTypeEnum;
import com.qmth.sop.common.enums.QualityProblemReasonEnum;
import com.qmth.sop.common.enums.QualityProblemTypeEnum;
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;

/**
 * 质量问题反馈表 控制器
 *
 * @author: shudonghui
 * @date: 2023-08-14 14:00:01
 * @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_QUALITY_PROBLEM_APPLY)
public class TBQualityProblemApplyController {

    @Resource
    TBQualityProblemApplyService tBQualityProblemApplyService;

    @ApiOperation(value = "质量问题反馈表查询接口")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "质量问题反馈表查询结果", response = TBQualityProblemApplyResult.class)})
    public Result query(
            @ApiParam(value = "服务单元", required = false) @RequestParam(required = false) Long serviceId,
            @ApiParam(value = "责任人", required = false) @RequestParam(required = false) Long userId,
            @ApiParam(value = "问题类型", required = false) @RequestParam(required = false) QualityProblemTypeEnum type,
            @ApiParam(value = "问题归因", required = false) @RequestParam(required = false) QualityProblemReasonEnum reason,
            @ApiParam(value = "影响度", required = false) @RequestParam(required = false) InfluenceDegreeEnum degree,
            @ApiParam(value = "客户名称（模糊查询）", required = false) @RequestParam(required = false) String custom,
            @ApiParam(value = "质量问题编号（模糊查询） ", required = false) @RequestParam(required = false) String problemNo,
            @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(tBQualityProblemApplyService.query(new Page<>(pageNumber, pageSize), serviceId, userId, type, reason, degree, custom, problemNo, startTime, endTime));
    }

    @ApiOperation(value = "质量问题反馈表新增修改接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.EDIT)
    public Result save(@Valid @ApiParam(value = "质量问题反馈表信息", required = true) @RequestBody TBQualityProblemApply tbQualityProblemApply, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tBQualityProblemApplyService.saveTBQualityProblemApply(tbQualityProblemApply));
    }

    @ApiOperation(value = "质量问题反馈表审核接口")
    @RequestMapping(value = "/approve", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.UN_KNOW)
    public Result submit(@Valid @ApiParam(value = "质量问题反馈审核信息", required = true) @RequestBody FlowApproveParam flowApproveParam, BindingResult bindingResult) throws InterruptedException {
        if (bindingResult.hasErrors()) {
            return ResultUtil.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultUtil.ok(tBQualityProblemApplyService.flowApprove(flowApproveParam));
    }

    @ApiOperation(value = "质量问题反馈表删除接口")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
    @OperationLog(logType = LogTypeEnum.DELETE)
    public Result delete(@ApiParam(value = "质量问题反馈表id", required = true) @RequestParam(required = true) long[] ids) {
        return ResultUtil.ok(tBQualityProblemApplyService.delete(ids));
    }

//
//    @Aac(auth= BOOL.FALSE)
//    @ApiOperation(value = "质量问题反馈表获取单个接口")
//    @RequestMapping(value = "/get", method = RequestMethod.POST)
//    @ApiResponses({@ApiResponse(code = 200, message = "质量问题反馈表信息", response = TBQualityProblemApply.class)})
//    public Result get(@ApiParam(value = "质量问题反馈表id", required = true) @RequestParam long id) {
//        return ResultUtil.ok(tBQualityProblemApplyService.getById(id));
//    }

    //获取责任人接口
    @ApiOperation(value = "质量问题反馈表获取责任人接口")
    @RequestMapping(value = "/user/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "质量问题反馈表获取责任人接口", response = SysUser.class)})
    public Result userList() {
        return ResultUtil.ok(tBQualityProblemApplyService.userList());
    }
}