/*
 * 文 件 名:  GeekOpenCbController
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/2/27
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.ruoyi.ems.controller;

import com.alibaba.fastjson.JSON;
import com.huashe.common.exception.BusinessException;
import com.ruoyi.ems.config.BaCtlConfig;
import com.ruoyi.ems.core.BaCtlEnergyTemplate;
import com.ruoyi.ems.handle.BaCtlHandler;
import com.ruoyi.ems.handle.GeekOpenCbHandler;
import com.ruoyi.ems.handle.Keka86BsHandler;
import com.ruoyi.ems.model.CallData;
import com.ruoyi.ems.model.CallResponse;
import com.ruoyi.ems.model.ModbusCommand;
import com.ruoyi.ems.model.idenergy.CodesValReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Test
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/2/27]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@RestController
@CrossOrigin(allowedHeaders = "*", allowCredentials = "false")
@RequestMapping("/test")
@Api(value = "TestController", description = "测试Api")
public class TestController {
    /**
     * 日志
     */
    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Qualifier("geekOpenCbHandler")
    @Resource
    private GeekOpenCbHandler geekOpenCbHandler;

    @Qualifier("baCtlHandler")
    @Resource
    private BaCtlHandler baCtlHandler;

    @Qualifier("keka86BsHandler")
    @Resource
    private Keka86BsHandler keka86BsHandler;

    @Resource
    private BaCtlConfig baCtlConfig;

    /**
     * msgHandle 消息处理
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/msgHandle", method = RequestMethod.POST)
    @ApiOperation(value = "/msgHandle", notes = "mqtt消息处理")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> msgHandle(@RequestParam(name = "deviceCode") String deviceCode,
        @RequestBody String jsonObject) {
        CallResponse<Void> res = null;

        try {
            geekOpenCbHandler.msgHandle(deviceCode, jsonObject);
            res = new CallResponse<>(0, "success");
        }
        catch (BusinessException e) {
            res = new CallResponse<>(500, e.getMessage());
        }
        catch (Exception e) {
            log.error("geekOpenCbAbilityCall fail!", e);
            res = new CallResponse<>(501, "内部错误:" + e.getMessage());
        }

        return res;
    }

    /**
     * 小时产出
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/collect", method = RequestMethod.GET)
    @ApiOperation(value = "/collect", notes = "采集触发")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> collect() {
        CallResponse<Void> res = null;

        try {
            baCtlHandler.meterCollect();
            res = new CallResponse<>(0, "success");
        }
        catch (BusinessException e) {
            res = new CallResponse<>(500, e.getMessage());
        }
        catch (Exception e) {
            log.error("collect fail!", e);
            res = new CallResponse<>(501, "内部错误:" + e.getMessage());
        }

        return res;
    }

    /**
     * 小时产出
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/meterHourProd", method = RequestMethod.GET)
    @ApiOperation(value = "/meterHourProd", notes = "小时计量触发")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> meterHourProd() {
        CallResponse<Void> res = null;

        try {
            baCtlHandler.meterHourProd();
            res = new CallResponse<>(0, "success");
        }
        catch (BusinessException e) {
            res = new CallResponse<>(500, e.getMessage());
        }
        catch (Exception e) {
            log.error("meterHourProd fail!", e);
            res = new CallResponse<>(501, "内部错误:" + e.getMessage());
        }

        return res;
    }

    /**
     * 小时产出
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/baGetCodesVal", method = RequestMethod.GET)
    @ApiOperation(value = "/baGetCodesVal", notes = "BA采集")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public String baGetCodesVal(@RequestParam("pointId") String pointId) {
        String ret = null;

        try {
            BaCtlEnergyTemplate template = new BaCtlEnergyTemplate(baCtlConfig.getUrl());
            CallData<String> callData = template.getCodesVal(new CodesValReq(StringUtils.split(pointId, ",")));
            ret = callData.getResPayload();
        }
        catch (Exception e) {
            log.error("baGetCodesVal fail!", e);
            ret = JSON.toJSONString(new CallResponse<>(500, e.getMessage()));
        }

        return ret;
    }

    /**
     * 小时产出
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/baXfCollect", method = RequestMethod.GET)
    @ApiOperation(value = "/baXfCollect", notes = "BA新风采集")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public String xfCollect() {
        String ret = null;

        try {
            baCtlHandler.wtCollect();
            baCtlHandler.wpCollect();
            baCtlHandler.lightCollect();
        }
        catch (Exception e) {
            log.error("xfCollect fail!", e);
            ret = JSON.toJSONString(new CallResponse<>(500, e.getMessage()));
        }

        return ret;
    }

    /**
     * keka86BsSet
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/keka86BsSet", method = RequestMethod.GET)
    @ApiOperation(value = "/keka86BsSet", notes = "keka86BsSet")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public String keka86BsSet(@RequestParam("gatewayId") String gatewayId, @RequestParam("lightId") Integer lightId,
        @RequestParam("state") Integer state) {
        String ret = null;

        try {
            ModbusCommand payload = keka86BsHandler.buildControlCommand(lightId, state);
            keka86BsHandler.sendMqttHex("/sc/dtu/ctl/" + gatewayId, payload);
            ret = JSON.toJSONString(new CallResponse<>(0, "success"));
        }
        catch (Exception e) {
            log.error("keka86BsSet fail!", e);
            ret = JSON.toJSONString(new CallResponse<>(500, e.getMessage()));
        }

        return ret;
    }

    /**
     * keka86BsSet
     *
     * @return 数据列表
     */
    @RequestMapping(value = "/keka86BsGet", method = RequestMethod.GET)
    @ApiOperation(value = "/keka86BsGet", notes = "keka86BsGet")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public String keka86BsGet(@RequestParam("gatewayId") String gatewayId, @RequestParam("lightId") Integer lightId) {
        String ret = null;

        try {
            ModbusCommand payload = keka86BsHandler.buildReadCommand(lightId);
            keka86BsHandler.sendMqttHex("/sc/dtu/ctl/" + gatewayId, payload);
            ret = JSON.toJSONString(new CallResponse<>(0, "success"));
        }
        catch (Exception e) {
            log.error("keka86BsGet fail!", e);
            ret = JSON.toJSONString(new CallResponse<>(500, e.getMessage()));
        }

        return ret;
    }
}