package com.xzzn.ems.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsStrategyRunning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xzzn.ems.mapper.EmsFaultProtectionPlanMapper;
import com.xzzn.ems.domain.EmsFaultProtectionPlan;
import com.xzzn.ems.service.IEmsFaultProtectionPlanService;

/**
 * 故障告警保护方案Service业务层处理
 *
 * @author xzzn
 * @date 2025-10-24
 */
@Service
public class EmsFaultProtectionPlanServiceImpl implements IEmsFaultProtectionPlanService
{
    @Autowired
    private EmsFaultProtectionPlanMapper emsFaultProtectionPlanMapper;

    /**
     * 查询故障告警保护方案
     *
     * @param id 故障告警保护方案主键
     * @return 故障告警保护方案
     */
    @Override
    public EmsFaultProtectionPlan selectEmsFaultProtectionPlanById(Long id)
    {
        return emsFaultProtectionPlanMapper.selectEmsFaultProtectionPlanById(id);
    }

    /**
     * 查询故障告警保护方案列表
     *
     * @param emsFaultProtectionPlan 故障告警保护方案
     * @return 故障告警保护方案
     */
    @Override
    public List<EmsFaultProtectionPlan> selectEmsFaultProtectionPlanList(EmsFaultProtectionPlan emsFaultProtectionPlan)
    {
        return emsFaultProtectionPlanMapper.selectEmsFaultProtectionPlanList(emsFaultProtectionPlan);
    }

    /**
     * 新增故障告警保护方案
     *
     * @param emsFaultProtectionPlan 故障告警保护方案
     * @return 结果
     */
    @Override
    public int insertEmsFaultProtectionPlan(EmsFaultProtectionPlan emsFaultProtectionPlan)
    {
        emsFaultProtectionPlan.setCreateTime(DateUtils.getNowDate());
        return emsFaultProtectionPlanMapper.insertEmsFaultProtectionPlan(emsFaultProtectionPlan);
    }

    /**
     * 修改故障告警保护方案
     *
     * @param emsFaultProtectionPlan 故障告警保护方案
     * @return 结果
     */
    @Override
    public int updateEmsFaultProtectionPlan(EmsFaultProtectionPlan emsFaultProtectionPlan)
    {
        emsFaultProtectionPlan.setUpdateTime(DateUtils.getNowDate());
        return emsFaultProtectionPlanMapper.updateEmsFaultProtectionPlan(emsFaultProtectionPlan);
    }

    /**
     * 批量删除故障告警保护方案
     *
     * @param ids 需要删除的故障告警保护方案主键
     * @return 结果
     */
    @Override
    public int deleteEmsFaultProtectionPlanByIds(Long[] ids)
    {
        return emsFaultProtectionPlanMapper.deleteEmsFaultProtectionPlanByIds(ids);
    }

    /**
     * 删除故障告警保护方案信息
     *
     * @param id 故障告警保护方案主键
     * @return 结果
     */
    @Override
    public int deleteEmsFaultProtectionPlanById(Long id)
    {
        return emsFaultProtectionPlanMapper.deleteEmsFaultProtectionPlanById(id);
    }

    @Override
    public void dealSyncData(String content, String operateType) {
        if (StringUtils.isEmpty(content)) {
            return;
        }

        EmsFaultProtectionPlan faultProtectionPlan = JSON.parseObject(content, EmsFaultProtectionPlan.class);
        switch(operateType) {
            case "INSERT":
                insertEmsFaultProtectionPlan(faultProtectionPlan);
                break;
            case "UPDATE":
                updateEmsFaultProtectionPlan(faultProtectionPlan);
                break;
            case "DELETE":
                // 根据站点+faultName+faultLevel 删除
                String siteId = faultProtectionPlan.getSiteId();
                String faultName = faultProtectionPlan.getFaultName();
                Integer faultLevel = faultProtectionPlan.getFaultLevel();
                emsFaultProtectionPlanMapper.deleteEmsFaultProtectionPlan(siteId, faultName, faultLevel);
                break;
            default:
                    break;
        }
    }

    private static Long[] convertJsonToLongArray(String json) {
        // 解析 JSON 为 JSONObject
        JSONObject jsonObject = JSON.parseObject(json);
        // 获取 "id" 对应的 JSONArray
        JSONArray idArray = jsonObject.getJSONArray("id");

        if (idArray == null) {
            return new Long[0]; // 数组不存在时返回空数组
        }

        // 转换为 Long 数组
        return idArray.toArray(new Long[0]);
    }
}
