package com.xzzn.ems.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsStrategyTemp;
import com.xzzn.ems.domain.vo.StrategyCurveVo;
import com.xzzn.ems.domain.vo.StrategyPowerDataVo;
import com.xzzn.ems.mapper.EmsStrategyTempMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xzzn.ems.mapper.EmsStrategyCurveMapper;
import com.xzzn.ems.domain.EmsStrategyCurve;
import com.xzzn.ems.service.IEmsStrategyCurveService;

/**
 * 策曲线Service业务层处理
 *
 * @author xzzn
 * @date 2025-07-11
 */
@Service
public class EmsStrategyCurveServiceImpl implements IEmsStrategyCurveService
{
    @Autowired
    private EmsStrategyCurveMapper emsStrategyCurveMapper;
    @Autowired
    private EmsStrategyTempMapper emsStrategyTempMapper;

    /**
     * 查询策曲线
     *
     * @param id 策曲线主键
     * @return 策曲线
     */
    @Override
    public EmsStrategyCurve selectEmsStrategyCurveById(Long id)
    {
        return emsStrategyCurveMapper.selectEmsStrategyCurveById(id);
    }

    /**
     * 查询策曲线列表
     *
     * @param emsStrategyCurve 策曲线
     * @return 策曲线
     */
    @Override
    public List<EmsStrategyCurve> selectEmsStrategyCurveList(EmsStrategyCurve emsStrategyCurve)
    {
        return emsStrategyCurveMapper.selectEmsStrategyCurveList(emsStrategyCurve);
    }

    /**
     * 新增策曲线
     *
     * @param emsStrategyCurve 策曲线
     * @return 结果
     */
    @Override
    public int insertEmsStrategyCurve(EmsStrategyCurve emsStrategyCurve)
    {
        emsStrategyCurve.setCreateTime(DateUtils.getNowDate());
        return emsStrategyCurveMapper.insertEmsStrategyCurve(emsStrategyCurve);
    }

    /**
     * 修改策曲线
     *
     * @param emsStrategyCurve 策曲线
     * @return 结果
     */
    @Override
    public int updateEmsStrategyCurve(EmsStrategyCurve emsStrategyCurve)
    {
        emsStrategyCurve.setUpdateTime(DateUtils.getNowDate());
        return emsStrategyCurveMapper.updateEmsStrategyCurve(emsStrategyCurve);
    }

    /**
     * 批量删除策曲线
     *
     * @param ids 需要删除的策曲线主键
     * @return 结果
     */
    @Override
    public int deleteEmsStrategyCurveByIds(Long[] ids)
    {
        return emsStrategyCurveMapper.deleteEmsStrategyCurveByIds(ids);
    }

    /**
     * 删除策曲线信息
     *
     * @param id 策曲线主键
     * @return 结果
     */
    @Override
    public int deleteEmsStrategyCurveById(Long id)
    {
        return emsStrategyCurveMapper.deleteEmsStrategyCurveById(id);
    }

    @Override
    public List<StrategyCurveVo> getStrategyCurveList(EmsStrategyTemp tempConfig) {
        List<StrategyCurveVo> dataList = new ArrayList();
        String siteId = tempConfig.getSiteId();
        Long strategyId = tempConfig.getStrategyId();
        // 获取该策略的所有模板配置
        List<Map<String,String>> tempList = emsStrategyTempMapper.getTempNameList(strategyId, siteId);

        if (tempList != null && !tempList.isEmpty()) {
            for (Map<String,String> map : tempList) {
                String tempName = map.get("templateName");
                String tempId = map.get("templateId");

                if (StringUtils.isNotEmpty(tempId)) {
                    List<EmsStrategyCurve> taskList = emsStrategyCurveMapper.getTemplateCurve(tempId);

                    for (int i = 0; i < taskList.size(); i++) {
                        StrategyCurveVo vo = new StrategyCurveVo();
                        vo.setTemplateName(tempName);
                        vo.setTemplateId(tempId);
                        vo.setStrategyId(taskList.get(i).getStrategyId());
                        vo.setSiteId(taskList.get(i).getSiteId());
                        vo.setMonth(taskList.get(i).getMonth());
                        // 时间功率
                        String powerJson = taskList.get(i).getPowerData();
                        List<StrategyPowerDataVo> powerList = JSON.parseArray(powerJson, StrategyPowerDataVo.class);
                        vo.setPowerList(powerList);

                        dataList.add(vo);
                    }
                }
            }
        }
        return dataList;
    }
}
