package com.njcn.gather.source.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
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.source.pojo.param.PqSourceParam;
import com.njcn.gather.source.pojo.po.PqSource;
import com.njcn.gather.source.pojo.po.SourceInitialize;
import com.njcn.gather.source.pojo.po.SourceParam;
import com.njcn.gather.source.service.IPqSourceService;
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-28
 */
@Slf4j
@Api(tags = "检测源管理")
@RestController
@RequestMapping("/pqSource")
@RequiredArgsConstructor
public class PqSourceController extends BaseController {
    private final IPqSourceService pqSourceService;

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

    @OperateInfo
    @GetMapping("/getById")
    @ApiOperation("根据id查询检测源")
    @ApiImplicitParam(name = "id", value = "查询参数", required = true)
    public HttpResult<PqSource> getPqSourceById(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("getById");
        LogUtil.njcnDebug(log, "{}，查询ID为：{}", methodDescribe, id);
        PqSource result = pqSourceService.getPqSourceById(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 PqSourceParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, param);
        boolean result = pqSourceService.addPqSource(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 PqSourceParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, param);
        boolean result = pqSourceService.updatePqSource(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 = pqSourceService.deletePqSource(ids);
        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<Map<String, Object>>> getAllPqSource(@RequestParam("patternId") String patternId) {
        String methodDescribe = getMethodDescribe("getAllPqSource");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, patternId);
        List<Map<String, Object>> result = pqSourceService.listAllPqSource(patternId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo
    @GetMapping("/getSourceParam")
    @ApiOperation("按照检测源ID获取源参数")
    @ApiImplicitParam(name = "pqSourceId", value = "检测源ID", required = true)
    public HttpResult<List<SourceParam>> getSourceParam(@RequestParam("sourceId") String sourceId) {
        String methodDescribe = getMethodDescribe("getParam");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, sourceId);
        List<SourceParam> result = pqSourceService.getSourceParam(sourceId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }
}

