package com.njcn.gather.script.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.script.pojo.param.PqScriptDtlsParam;
import com.njcn.gather.script.pojo.param.PqScriptIssueParam;
import com.njcn.gather.script.pojo.param.PqScriptParam;
import com.njcn.gather.script.pojo.param.ScriptParam;
import com.njcn.gather.script.pojo.po.PqScript;
import com.njcn.gather.script.pojo.po.PqScriptCheckData;
import com.njcn.gather.script.pojo.po.PqScriptDtls;
import com.njcn.gather.script.pojo.po.SourceIssue;
import com.njcn.gather.script.service.IPqScriptDtlsService;
import com.njcn.gather.script.service.IPqScriptService;
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-18
 */
@Slf4j
@Api(tags = "检测脚本管理")
@RestController
@RequestMapping("/pqScript")
@RequiredArgsConstructor
public class PqScriptController extends BaseController {
    private final IPqScriptService pqScriptService;
    private final IPqScriptDtlsService pqScriptDtlsService;

    @OperateInfo
    @PostMapping("/list")
    @ApiOperation("分页查询检测脚本")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<PqScript>> list(@RequestBody @Validated PqScriptParam.QueryParam param) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        Page<PqScript> result = pqScriptService.listPqScript(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增检测脚本")
    @ApiImplicitParam(name = "param", value = "检测脚本", required = true)
    public HttpResult<String> add(@RequestBody @Validated PqScriptParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, param);
        String id = pqScriptService.addPqScript(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, id, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改检测脚本")
    @ApiImplicitParam(name = "param", value = "检测脚本", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated PqScriptParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, param);
        boolean result = pqScriptService.updatePqScript(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 = pqScriptService.deletePqScript(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo
    @GetMapping("/getScriptDtlsByScriptId")
    @ApiOperation("根据脚本id查询检测脚本详情")
    @ApiImplicitParam(name = "id", value = "检测脚本id", required = true)
    public HttpResult<List<PqScriptDtls>> getScriptDtlsByScriptId(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, id);
        List<PqScriptDtls> result = pqScriptDtlsService.listPqScriptDtlByScriptId(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.UPDATE)
    @GetMapping("/upgradeToTemplate")
    @ApiOperation("升级为模板")
    @ApiImplicitParam(name = "id", value = "检测脚本id", required = true)
    public HttpResult<Boolean> upgradeToTemplate(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("upgradeToTemplate");
        LogUtil.njcnDebug(log, "{}，升级ID数据为：{}", methodDescribe, id);
        boolean result = pqScriptService.upgradeToTemplate(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @OperateInfo
    @GetMapping("/getAll")
    @ApiOperation("获取指定模式下的所有检测脚本")
    @ApiImplicitParam(name = "patternId", value = "模式Id", required = true)
    public HttpResult<List<PqScript>> getAllPqScript(@RequestParam("patternId") String patternId) {
        String methodDescribe = getMethodDescribe("getAllPqScript");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, patternId);
        List<PqScript> result = pqScriptService.listAllPqScript(patternId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    @OperateInfo
    @PostMapping("/dlsDetails")
    @ApiOperation("根据脚本id查询检测脚本详情")
    @ApiImplicitParam(name = "param", value = "检测脚本", required = true)
    public HttpResult<List<SourceIssue>> dlsDetails(@RequestBody PqScriptIssueParam param) {
        String methodDescribe = getMethodDescribe("dlsDetails");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, param);
        List<SourceIssue> sourceIssues = pqScriptDtlsService.listSourceIssue(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, sourceIssues, methodDescribe);
    }


    @OperateInfo
    @PostMapping("/addScriptDtls")
    @ApiOperation("添加检测脚本")
    @ApiImplicitParam(name = "sourceIssue", value = "检测脚本", required = true)
    public HttpResult<Boolean> addScriptDtls(@RequestBody PqScriptDtlsParam sourceIssue) {
        String methodDescribe = getMethodDescribe("addScriptDtls");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sourceIssue);
        Boolean b = pqScriptDtlsService.saveScriptDtls(sourceIssue);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }

    @OperateInfo
    @PostMapping("/scriptDtlsCheckDataList")
    @ApiOperation("根据通讯参数生成装置下发原始数据公式")
    @ApiImplicitParam(name = "sourceIssue", value = "检测脚本", required = true)
    public HttpResult<List<PqScriptDtlsParam.CheckData>> scriptDtlsCheckDataList(@RequestBody PqScriptDtlsParam sourceIssue) {
        String methodDescribe = getMethodDescribe("scriptDtlsCheckDataList");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sourceIssue);
        List<PqScriptDtlsParam.CheckData> checkData = pqScriptDtlsService.scriptDtlsCheckDataList(sourceIssue);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, checkData, methodDescribe);
    }


    @OperateInfo
    @PostMapping("/deleteDtls")
    @ApiOperation("删除脚本")
    @ApiImplicitParam(name = "sourceIssue", value = "检测脚本", required = true)
    public HttpResult<Boolean> deleteDtls(@RequestBody @Validated ScriptParam sourceIssue) {
        String methodDescribe = getMethodDescribe("deleteDtls");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sourceIssue);
        Boolean b = pqScriptDtlsService.deleteDtls(sourceIssue);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }


    @OperateInfo
    @PostMapping("/updateDtls")
    @ApiOperation("启用关闭脚本")
    @ApiImplicitParam(name = "sourceIssue", value = "检测脚本", required = true)
    public HttpResult<Boolean> updateDtls(@RequestBody @Validated ScriptParam sourceIssue) {
        String methodDescribe = getMethodDescribe("updateDtls");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sourceIssue);
        Boolean b = pqScriptDtlsService.updateDtls(sourceIssue);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, b, methodDescribe);
    }

    @OperateInfo
    @PostMapping("/checkDataList")
    @ApiOperation("通讯脚本回显")
    @ApiImplicitParam(name = "sourceIssue", value = "检测脚本", required = true)
    public HttpResult<List<PqScriptCheckData>> checkDataList(@RequestBody @Validated ScriptParam sourceIssue) {
        String methodDescribe = getMethodDescribe("checkDataList");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sourceIssue);
        List<PqScriptCheckData> checkData = pqScriptDtlsService.checkDataList(sourceIssue);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, checkData, methodDescribe);
    }

    @OperateInfo
    @PostMapping("/getScriptDtlsDesc")
    @ApiOperation("通讯脚本回显")
    @ApiImplicitParam(name = "param", value = "检测脚本", required = true)
    public HttpResult<String> getScriptDtlsDesc(@RequestBody @Validated ScriptParam.Desc param) {
        String methodDescribe = getMethodDescribe("getScriptDtlsDesc");
        String checkData = pqScriptDtlsService.getScriptDtlsDesc(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, checkData, methodDescribe);
    }
}

