package com.njcn.gather.err.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.LogUtil;
import com.njcn.gather.err.pojo.param.PqErrSysParam;
import com.njcn.gather.err.pojo.po.PqErrSys;
import com.njcn.gather.err.pojo.vo.PqErrSysDtlsVO;
import com.njcn.gather.err.service.IPqErrSysService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.HttpResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author caozehui
 * @date 2024-11-27
 */
@Slf4j
@Api(tags = "误差体系管理")
@RestController
@RequestMapping("/pqErrSys")
@RequiredArgsConstructor
public class PqErrSysController extends BaseController {
    private final IPqErrSysService pqErrSysService;

    @OperateInfo
    @PostMapping("/list")
    @ApiOperation("分页查询误差体系")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<Page<PqErrSys>> list(@RequestBody @Validated PqErrSysParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        Page<PqErrSys> result = pqErrSysService.listPqErrSys(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo
    @GetMapping("/getById")
    @ApiOperation("根据id查询误差体系")
    @ApiImplicitParam(name = "id", value = "查询参数", required = true)
    public HttpResult<PqErrSys> getPqErrSysById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        LogUtil.njcnDebug(log, "{}，查询ID为：{}", methodDescribe, id);
        PqErrSys result = pqErrSysService.getPqErrSysById(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增误差体系")
    @ApiImplicitParam(name = "param", value = "误差体系", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated PqErrSysParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, param);
        boolean result = pqErrSysService.addPqErrSys(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改误差体系")
    @ApiImplicitParam(name = "param", value = "误差体系", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated PqErrSysParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, param);
        boolean result = pqErrSysService.updatePqErrSys(param);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo(operateType = OperateType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("删除误差体系")
    @ApiImplicitParam(name = "ids", value = "误差体系id", required = true)
    public HttpResult<Boolean> delete(@RequestBody List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，删除ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = pqErrSysService.deletePqErrSys(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo()
    @GetMapping("/getDetail")
    @ApiOperation("查看误差体系项详情")
    @ApiImplicitParam(name = "id", value = "误差体系id", required = true)
    public HttpResult<List<PqErrSysDtlsVO>> getDetail(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getDetail");
        LogUtil.njcnDebug(log, "{}，查看ID为：{}", methodDescribe, id);
        List<PqErrSysDtlsVO> result = pqErrSysService.getDetail(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo
    @GetMapping("/getAll")
    @ApiOperation("获取所有误差体系")
    public HttpResult<List<Map<String, Object>>> getAllPqErrSys() {
        String methodDescribe = getMethodDescribe("getAllPqErrSys");
        LogUtil.njcnDebug(log, "{}", methodDescribe);
        List<Map<String, Object>> result = pqErrSysService.listAllPqErrSys();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.ADD)
    @GetMapping("/copy")
    @ApiOperation("复制误差体系")
    @ApiImplicitParam(name = "id", value = "误差体系id", required = true)
    public HttpResult<Boolean> copy(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("copy");
        LogUtil.njcnDebug(log, "{}，复制ID为：{}", methodDescribe, id);
        boolean result = pqErrSysService.copyPqErrSys(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo
    @GetMapping("/getTestItems")
    @ApiOperation("根据误差体系id获取测试项")
    @ApiImplicitParam(name = "id", value = "误差体系id", required = true)
    public HttpResult<Map<String, String>> getTestItems(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getTestItems");
        LogUtil.njcnDebug(log, "{}，获取测试项ID为：{}", methodDescribe, id);
        Map<String, String> result = pqErrSysService.getTestItems(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}

