/*
 * 文 件 名:  ChargingController
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  充电桩适配控制器
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/12/25
 * 修改内容:  新建充电桩能力调用API入口
 */
package com.ruoyi.ems.controller;

import com.ruoyi.ems.charging.handler.ChargingHandler;
import com.ruoyi.ems.charging.service.ChargingDataService;
import com.ruoyi.ems.model.AbilityPayload;
import com.ruoyi.ems.model.CallResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 充电桩适配控制器
 * 提供充电桩系统、主机、充电枪的能力调用API
 * <h3>支持的能力列表：</h3>
 * <h4>系统级能力 (M_W2_SYS_CHARGING, objType=3)：</h4>
 * <ul>
 *   <li>setAllPilesPowerLimit - 批量设置功率限制，参数: 30-100</li>
 *   <li>stopAllCharging - 全部停止充电</li>
 *   <li>clearCache - 清除设备编码缓存</li>
 *   <li>getCacheStats - 获取缓存统计信息</li>
 * </ul>
 * <h4>充电主机能力 (M_W2_DEV_CHARGING_HOST, objType=2)：</h4>
 * <ul>
 *   <li>setMaxOutputPower - 设置最大输出功率，参数: 30-100</li>
 *   <li>enablePile - 启用充电桩</li>
 *   <li>disablePile - 禁用充电桩</li>
 * </ul>
 * <h4>充电枪能力 (M_W2_DEV_CHARGING_PILE, objType=2)：</h4>
 * <ul>
 *   <li>remoteStop - 远程停止充电</li>
 *   <li>readRealtimeData - 主动读取实时数据</li>
 * </ul>
 *
 * @author lvwenbin
 * @version [版本号, 2025/12/25]
 */
@RestController
@CrossOrigin(allowedHeaders = "*", allowCredentials = "false")
@RequestMapping("/charging")
@Api(value = "ChargingController", description = "充电桩适配接口")
public class ChargingController {

    private static final Logger log = LoggerFactory.getLogger(ChargingController.class);

    @Resource
    private ChargingHandler chargingHandler;

    @Resource
    private ChargingDataService chargingDataService;

    /**
     * 充电桩能力调用入口
     *
     * @param abilityPayload 能力调用参数
     * @return 调用结果
     */
    @PostMapping("/ct/abilityCall")
    @ApiOperation(value = "充电桩能力调用", notes = "统一的充电桩能力调用入口")
    @ApiResponses({ @ApiResponse(code = 200, message = "success"),
        @ApiResponse(code = 400, message = "{code:****,message:'fail'}")
    })
    public CallResponse<Void> abilityCall(@RequestBody AbilityPayload abilityPayload) {
        CallResponse<Void> res;

        try {
            log.info("收到充电桩能力调用请求: {}", abilityPayload);
            res = chargingHandler.call(abilityPayload);
        }
        catch (Exception e) {
            log.error("充电桩能力调用失败!", e);
            res = new CallResponse<>(501, "内部错误:" + e.getMessage());
        }

        return res;
    }

    /**
     * 清除设备编码缓存
     *
     * @return 操作结果
     */
    @PostMapping("/cache/clear")
    @ApiOperation(value = "清除缓存", notes = "清除设备编码缓存，设备配置变更后需要调用")
    public CallResponse<Void> clearCache() {
        try {
            chargingDataService.clearDeviceCodeCache();
            return new CallResponse<>(0, "缓存已清除");
        }
        catch (Exception e) {
            log.error("清除缓存失败", e);
            return new CallResponse<>(-1, e.getMessage());
        }
    }

    /**
     * 刷新充电桩在线状态
     *
     * @return 操作结果
     */
    @PostMapping("/system/refreshOnline")
    @ApiOperation(value = "刷新在线状态", notes = "刷新所有充电桩的在线状态")
    public CallResponse<Void> refreshOnline() {
        try {
            chargingHandler.refreshOnline();
            return new CallResponse<>(0, "在线状态刷新完成");
        }
        catch (Exception e) {
            log.error("刷新在线状态失败", e);
            return new CallResponse<>(-1, e.getMessage());
        }
    }
}
