package cn.cslg.report.controller.InvalidReReport;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.model.dto.invalidReReport.CheckReFeaturesDTO;
import cn.cslg.report.common.model.dto.invalidReReport.InvalidReasonDTO;
import cn.cslg.report.common.model.dto.invalidReReport.InvalidReasonFieldValueDTO;
import cn.cslg.report.common.model.vo.invalidReReport.QueryInvalidReasonVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.service.business.InvalidReReport.InvalidReasonFieldValueService;
import cn.cslg.report.service.business.InvalidReReport.InvalidReasonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 无效理由据管理的控制层
 *
 * @Author lrj
 * @Data 2023/4/1
 */
@Tag(name = "无效理由和证据管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(Constants.REPORT_API + "/invalidReason")
public class InvalidReasonController {
    private final InvalidReasonService invalidReasonService;
    private final InvalidReasonFieldValueService invalidReasonFieldValueService;
    @Operation(summary = "添加无效理由和证据")
    @PostMapping("/addInvalidReason")
    public String addInvalidReason(@RequestBody InvalidReasonDTO invalidReasonDTO) {
        if(invalidReasonDTO != null) {
            invalidReasonService.addInvalidReason(invalidReasonDTO);
            return Response.success("新增成功");
        } else {
            return Response.error("参数不能为空");
        }
    }

    @Operation(summary = "删除无效理由和证据")
    @PostMapping("/deleteInvalidReason")
    public String deleteInvalidReason(@RequestBody List<Integer> ids) {
        if(ids != null && ids.size() != 0){
            invalidReasonService.deleteInvalidReason(ids);
            return Response.success("删除成功");
        } else {
            return Response.error("参数不能为空");
        }
    }

    @Operation(summary = "更新无效理由和证据")
    @PostMapping("/updateInvalidReason")
    public String updateInvalidReason(@RequestBody InvalidReasonDTO invalidReasonDTO) throws IOException {
             invalidReasonService.updateInvalidReason(invalidReasonDTO);
        return Response.success();
    }

    @Operation(summary = "查找无效理由和证据")
    @GetMapping("/queryInvalidReason")
    public String queryInvalidReason(Integer reportId) throws IOException {
        if(reportId != null && reportId > 0) {
            List<QueryInvalidReasonVO> queryInvalidReasonVOS = invalidReasonService.queryInvalidReason(reportId);
            return Response.success(queryInvalidReasonVOS);
        } else {
            return Response.error("参数不能为空");
        }
    }

    @Operation(summary = "查询无效理由和证据详情")
    @GetMapping("/queryInvalidReasonDetail")
    public String queryInvalidReasonDetail(Integer id) throws IOException {
        return Response.success(invalidReasonService.queryInvalidReasonDetail(id));
    }


    @Operation(summary = "测试检测插入特征是否重复")
    @PostMapping("/testCheckReFeature")
    public String testCheckReFeature(@RequestBody CheckReFeaturesDTO checkReFeaturesDTO) throws IOException {
        Boolean isFlag = invalidReasonService.checkReFeatures(checkReFeaturesDTO);
        return Response.success(isFlag);
    }

    @Operation(summary = "添加或更新无效理由自定义字段值")
    @PostMapping("/addOrUpdateValue")
    public String addOrUpdateValue(@RequestBody List<InvalidReasonFieldValueDTO> invalidReasonFieldValueDTOS) throws IOException {
        Boolean isFlag = invalidReasonFieldValueService.addOrUpdate(invalidReasonFieldValueDTOS);
        if(!isFlag){
            Response.error("添加失败");
        }
        return Response.success("添加成功");
    }
}
