package com.njcn.gather.freqConverter.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.freqConverter.pojo.param.PqFreqConverterParam;
import com.njcn.gather.freqConverter.pojo.po.PqFreqConverterConfig;
import com.njcn.gather.freqConverter.pojo.vo.TolerantPointVO;
import com.njcn.gather.freqConverter.service.IFreqConverterService;
import com.njcn.gather.freqConverter.service.IPqFreqConverterConfigService;
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;

/**
 * @author caozehui
 * @data 2026-04-13
 */
@Slf4j
@Api(tags = "变频器")
@RestController
@RequestMapping("/freqConverter")
@RequiredArgsConstructor
public class FreqConverterController extends BaseController {
    private final IFreqConverterService freqConverterService;
    private final IPqFreqConverterConfigService pqFreqConverterConfigService;


    @OperateInfo
    @PostMapping("/list")
    @ApiOperation("分页查询变频器列表")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<PqFreqConverterConfig>> list(@RequestBody @Validated PqFreqConverterParam.QueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<PqFreqConverterConfig> result = pqFreqConverterConfigService.listPqFreqConverterConfigs(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    @OperateInfo(operateType = OperateType.ADD)
    @PostMapping("/add")
    @ApiOperation("新增变频器")
    @ApiImplicitParam(name = "pqDevParam", value = "被检设备", required = true)
    public HttpResult<Boolean> add(@RequestBody @Validated PqFreqConverterParam param) {
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，新增数据为：{}", methodDescribe, param);
        boolean result = pqFreqConverterConfigService.addPqFreqConverterConfig(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 = "updateParam", value = "被检设备", required = true)
    public HttpResult<Boolean> update(@RequestBody @Validated PqFreqConverterParam.UpdateParam param) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，修改数据为：{}", methodDescribe, param);
        boolean result = pqFreqConverterConfigService.updatePqFreqConverterConfig(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 = "param", value = "删除参数", required = true)
    public HttpResult<Boolean> delete(@RequestBody @Validated List<String> ids) {
        String methodDescribe = getMethodDescribe("delete");
        LogUtil.njcnDebug(log, "{}，删除ID数据为：{}", methodDescribe, String.join(StrUtil.COMMA, ids));
        boolean result = pqFreqConverterConfigService.deletePqFreqConverterConfig(ids);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @GetMapping("/result")
    @ApiOperation("查询变频器测试结果")
    @ApiImplicitParam(name = "param", value = "查询参数", required = true)
    public HttpResult<List<TolerantPointVO>> result(@RequestParam("converterId") String converterId) {
        String methodDescribe = getMethodDescribe("result");
        LogUtil.njcnDebug(log, "{}，查询ID数据为：{}", methodDescribe, converterId);
        List<TolerantPointVO> tolerantPoints = freqConverterService.getDipPoints(converterId);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, tolerantPoints, methodDescribe);
    }

//    @GetMapping("/scurve")
//    @ApiOperation("获取绘制特性曲线的point点")
//    @ApiImplicitParam(name = "converterId", value = "变频器ID", required = true)
//    public HttpResult<List<TolerantPointVO>> getScurvePoints(@RequestParam("converterId") String converterId) {
//        String methodDescribe = getMethodDescribe("getScurvePoints");
//        LogUtil.njcnDebug(log, "{}，查询ID数据为：{}", methodDescribe, converterId);
//        List<TolerantPointVO> tolerantPoints = freqConverterService.getFeaturesScurvePoints(converterId);
//        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, tolerantPoints, methodDescribe);
//    }
}
