package com.xzzn.ems.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.xzzn.common.enums.StrategyStatus;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.common.utils.bean.BeanUtils;
import com.xzzn.ems.domain.EmsStrategy;
import com.xzzn.ems.domain.vo.StrategyTimeConfigVo;
import com.xzzn.ems.domain.vo.SyncStrategyTimeConfigVo;
import com.xzzn.ems.mapper.EmsStrategyCurveMapper;
import com.xzzn.ems.mapper.EmsStrategyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper;
import com.xzzn.ems.domain.EmsStrategyTimeConfig;
import com.xzzn.ems.service.IEmsStrategyTimeConfigService;

/**
 * 时间配置Service业务层处理
 *
 * @author xzzn
 * @date 2025-07-11
 */
@Service
public class EmsStrategyTimeConfigServiceImpl implements IEmsStrategyTimeConfigService
{
    @Autowired
    private EmsStrategyTimeConfigMapper emsStrategyTimeConfigMapper;
    @Autowired
    private EmsStrategyCurveMapper emsStrategyCurveMapper;
    @Autowired
    private EmsStrategyMapper emsStrategyMapper;

    /**
     * 查询时间配置
     *
     * @param id 时间配置主键
     * @return 时间配置
     */
    @Override
    public EmsStrategyTimeConfig selectEmsStrategyTimeConfigById(Long id)
    {
        return emsStrategyTimeConfigMapper.selectEmsStrategyTimeConfigById(id);
    }

    /**
     * 查询时间配置列表
     *
     * @param emsStrategyTimeConfig 时间配置
     * @return 时间配置
     */
    @Override
    public List<EmsStrategyTimeConfig> selectEmsStrategyTimeConfigList(EmsStrategyTimeConfig emsStrategyTimeConfig)
    {
        return emsStrategyTimeConfigMapper.selectEmsStrategyTimeConfigList(emsStrategyTimeConfig);
    }

    /**
     * 新增时间配置
     *
     * @param timeConfigList 时间配置
     * @return 结果
     */
    @Override
    public boolean insertEmsStrategyTimeConfig(List<EmsStrategyTimeConfig> timeConfigList)
    {
        if (timeConfigList != null) {
            for (EmsStrategyTimeConfig strategyTimeConfig : timeConfigList) {
                Long id = strategyTimeConfig.getId();
                Long strategyId = strategyTimeConfig.getStrategyId();
                String siteId = strategyTimeConfig.getSiteId();
                strategyTimeConfig.setIsPost(1);//默认未下发
                // 新增
                if (id == null) {
                    strategyTimeConfig.setCreateTime(DateUtils.getNowDate());
                    emsStrategyTimeConfigMapper.insertEmsStrategyTimeConfig(strategyTimeConfig);
                } else {//更新
                    strategyTimeConfig.setUpdateTime(DateUtils.getNowDate());
                    emsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(strategyTimeConfig);
                }
                // 处理曲线图：物理删除该策略的曲线图
                if (StringUtils.isNotEmpty(siteId) && strategyId != null) {
                    emsStrategyCurveMapper.physicalDeleteCurve(strategyId,siteId);
                }
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * 修改时间配置
     *
     * @param emsStrategyTimeConfig 时间配置
     * @return 结果
     */
    @Override
    public int updateEmsStrategyTimeConfig(EmsStrategyTimeConfig emsStrategyTimeConfig)
    {
        emsStrategyTimeConfig.setUpdateTime(DateUtils.getNowDate());
        return emsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(emsStrategyTimeConfig);
    }

    /**
     * 批量删除时间配置
     *
     * @param ids 需要删除的时间配置主键
     * @return 结果
     */
    @Override
    public int deleteEmsStrategyTimeConfigByIds(Long[] ids)
    {
        return emsStrategyTimeConfigMapper.deleteEmsStrategyTimeConfigByIds(ids);
    }

    /**
     * 删除时间配置信息
     *
     * @param id 时间配置主键
     * @return 结果
     */
    @Override
    public int deleteEmsStrategyTimeConfigById(Long id)
    {
        return emsStrategyTimeConfigMapper.deleteEmsStrategyTimeConfigById(id);
    }

    /**
     * 获取策略时间配置显示数据
     * @param emsStrategyTimeConfig 时间配置
     * @return
     */
    public List<StrategyTimeConfigVo> getStrategyTimeList(EmsStrategyTimeConfig emsStrategyTimeConfig)
    {
        return emsStrategyTimeConfigMapper.getStrategyTimeList(emsStrategyTimeConfig);
    }

    @Override
    public void dealStrategyTimeData(String content, String operateType) {
        if (StringUtils.isEmpty(content)) {
            return;
        }
        String siteId = "";
        EmsStrategyTimeConfig timeConfig = new EmsStrategyTimeConfig();
        SyncStrategyTimeConfigVo syncVo = JSON.parseObject(content, SyncStrategyTimeConfigVo.class);
        BeanUtils.copyProperties(syncVo, timeConfig);
        // 先校验策略是否存在，不存在则插入
        dealStrategyData(timeConfig, syncVo);
        switch (operateType) {
            case "INSERT":
                // 新增策略运行数据
                emsStrategyTimeConfigMapper.insertEmsStrategyTimeConfig(timeConfig);
                break;
            case "UPDATE":
                // 根据站点 + strategyId + 月份判断是否存在数据存在则更新否则插入
                if (checkExistConfig(timeConfig)) {
                    emsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(timeConfig);
                } else {
                    timeConfig.setCreateTime(DateUtils.getNowDate());
                    emsStrategyTimeConfigMapper.insertEmsStrategyTimeConfig(timeConfig);
                }
                break;
            default:
                // 未知操作类型-不同步
                break;
        }
    }

    private boolean checkExistConfig(EmsStrategyTimeConfig timeConfig) {
        boolean result = true;
        String siteId = timeConfig.getSiteId();
        Long strategyId = timeConfig.getStrategyId();
        Long month = timeConfig.getMonth();
        EmsStrategyTimeConfig emsStrategyTimeConfig = emsStrategyTimeConfigMapper.getExistTimeConfig(siteId,strategyId,month);
        if (emsStrategyTimeConfig == null) {
            result = false;
        }
        return result;
    }

    private void dealStrategyData(EmsStrategyTimeConfig temp, SyncStrategyTimeConfigVo syncVo) {
        // 主策略
        String mainStrategyName =  syncVo.getStrategyName();
        EmsStrategy strategy = emsStrategyMapper.getStrategyByName(mainStrategyName);
        if (strategy != null) {
            temp.setStrategyId(strategy.getId());
        } else {
            strategy = createStrategyEntity(mainStrategyName,   syncVo.getStrategyType());
            emsStrategyMapper.insertEmsStrategy(strategy);
            temp.setStrategyId(strategy.getId());
        }
    }

    private EmsStrategy createStrategyEntity(String strategyName, Long type) {
        EmsStrategy strategy = new EmsStrategy();
        strategy.setStrategyType(type);
        strategy.setStrategyName(strategyName);
        strategy.setStatus(StrategyStatus.RUNNING.getCode());
        strategy.setCreateTime(DateUtils.getNowDate());
        return  strategy;
    }
}
