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.ems.domain.EmsStrategyRunning;
import com.xzzn.ems.domain.vo.StrategyRunningVo;
import com.xzzn.ems.mapper.EmsStrategyRunningMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xzzn.ems.mapper.EmsStrategyMapper;
import com.xzzn.ems.domain.EmsStrategy;
import com.xzzn.ems.service.IEmsStrategyService;

/**
 * 策略Service业务层处理
 *
 * @author xzzn
 * @date 2025-07-10
 */
@Service
public class EmsStrategyServiceImpl implements IEmsStrategyService
{
    @Autowired
    private EmsStrategyMapper emsStrategyMapper;
    @Autowired
    private EmsStrategyRunningMapper emsStrategyRunningMapper;

    @Override
    public List<StrategyRunningVo> selectEmsStrategyRunningList(String siteId) {
        return emsStrategyRunningMapper.getRunningList(siteId);
    }

    @Override
    public int stopRunningStrategy(Long id) {
        return emsStrategyRunningMapper.stopEmsStrategyRunning(id);
    }

    @Override
    public List<EmsStrategy> getMainStrategyList() {
        return emsStrategyMapper.getStrategyListByType(1L);
    }

    @Override
    public List<EmsStrategy> getAuxStrategyList() {
        return emsStrategyMapper.getStrategyListByType(2L);
    }

    @Override
    public int configStrategy(EmsStrategyRunning emsStrategyRunning) {
        // 校验是否已存在已运行的相同策略
        EmsStrategyRunning existStrategy = emsStrategyRunningMapper.selectEmsStrategyRunning(emsStrategyRunning);
        if (existStrategy != null) {
            return -1;
        }
        // 传策略组id-更新
        if (emsStrategyRunning.getId() != null){
            emsStrategyRunning.setUpdateTime(DateUtils.getNowDate());
            return emsStrategyRunningMapper.updateEmsStrategyRunning(emsStrategyRunning);
        }
        // 否则新增
        emsStrategyRunning.setStatus("1");
        emsStrategyRunning.setCreateTime(DateUtils.getNowDate());
        return emsStrategyRunningMapper.insertEmsStrategyRunning(emsStrategyRunning);
    }

    @Override
    public void dealStrategyRunningData(String content, String operateType) {
        if (StringUtils.isEmpty(content)) {
            return;
        }
        String siteId = "";
        EmsStrategyRunning emsStrategyRunning = new EmsStrategyRunning();
        switch (operateType) {
            case "INSERT":
                StrategyRunningVo insertVo = JSON.parseObject(content, StrategyRunningVo.class);
                BeanUtils.copyProperties(insertVo, emsStrategyRunning);
                // 先校验策略是否存在，不存在则插入
                dealStrategyData(emsStrategyRunning, insertVo);
                // 新增策略运行数据
                emsStrategyRunningMapper.insertEmsStrategyRunning(emsStrategyRunning);
                break;
            case "STOP":
                EmsStrategyRunning vo = JSON.parseObject(content, EmsStrategyRunning.class);
                siteId = vo.getSiteId();
                // 停止站点正在运行的策略即可
                EmsStrategyRunning runningStrategy = emsStrategyRunningMapper.getRunningStrategy(siteId);
                emsStrategyRunningMapper.stopEmsStrategyRunning(runningStrategy.getId());
                break;
            case "UPDATE":
                StrategyRunningVo updateVo = JSON.parseObject(content, StrategyRunningVo.class);
                siteId = updateVo.getSiteId();
                // 获取该站点是否有正在运行的策略，无则新增，有则更新
                EmsStrategyRunning existStrategy = emsStrategyRunningMapper.getRunningStrategy(siteId);
                if (existStrategy == null) {
                    BeanUtils.copyProperties(updateVo, emsStrategyRunning);
                    // 先校验策略是否存在，不存在则插入
                    dealStrategyData(emsStrategyRunning, updateVo);
                    emsStrategyRunningMapper.insertEmsStrategyRunning(emsStrategyRunning);
                } else {
                     // 校验更新的主副策略是否存在，不存在则插入，存在则获取id
                    dealStrategyData(existStrategy, updateVo);
                    emsStrategyRunning.setCreateTime(DateUtils.getNowDate());
                    emsStrategyRunningMapper.updateEmsStrategyRunning(existStrategy);
                }
                break;
            default:
                // 未知操作类型-不同步
                break;
        }
    }

    private void dealStrategyData(EmsStrategyRunning emsStrategyRunning, StrategyRunningVo insertVo) {
        // 主策略
        String mainStrategyName =  insertVo.getMainStrategyName();
        EmsStrategy mainStrategy = emsStrategyMapper.getStrategyByName(mainStrategyName);
        if (mainStrategy != null) {
            emsStrategyRunning.setMainStrategyId(mainStrategy.getId());
        } else {
            mainStrategy = createStrategyEntity(mainStrategyName,   1L);
            emsStrategyMapper.insertEmsStrategy(mainStrategy);
            emsStrategyRunning.setMainStrategyId(mainStrategy.getId());
        }
        // 副策略
        String auxStrategyName = insertVo.getAuxStrategyName();
        EmsStrategy auxStrategy = emsStrategyMapper.getStrategyByName(auxStrategyName);
        if (auxStrategy != null) {
            emsStrategyRunning.setAuxiliaryStrategyId(auxStrategy.getId());
        } else {
            auxStrategy = createStrategyEntity(mainStrategyName,   2L);
            emsStrategyMapper.insertEmsStrategy(auxStrategy);
            emsStrategyRunning.setAuxiliaryStrategyId(auxStrategy.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;
    }
}
