package com.ruoyi.ems.report.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ems.base.domain.EmsBaseMonitorWorkUnit;
import com.ruoyi.ems.base.domain.EmsBaseWorkUnit;
import com.ruoyi.ems.base.mapper.EmsBaseMonitorWorkUnitMapper;
import com.ruoyi.ems.base.mapper.EmsBaseWorkUnitMapper;
import com.ruoyi.ems.report.domain.EnergyStatisticalReport;
import com.ruoyi.ems.report.domain.PeaksValleysConsumptionReport;
import com.ruoyi.ems.report.mapper.EmsReportMapper;
import com.ruoyi.ems.report.service.IEmsReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 能源报表Service业务层处理
 *
 * @author Yinq
 * @date 2024-05-22
 */
@Service
public class EmsReportServiceImpl implements IEmsReportService {
    @Autowired
    private EmsReportMapper emsReportMapper;

    @Autowired
    private EmsBaseMonitorWorkUnitMapper emsBaseMonitorWorkUnitMapper;

    @Autowired
    private EmsBaseWorkUnitMapper emsBaseWorkUnitMapper;


    /**
     * 统计能耗报表
     *
     * @param hashMap
     * @return
     */
    @Override
    public List<EnergyStatisticalReport> energyStatisticalReportList(Map hashMap) {
        if (!hashMap.containsKey("dateType") && !hashMap.containsKey("energyType")) {
            return null;
        }
        hashMap.put("timeSub", Integer.parseInt(String.valueOf(hashMap.get("dateType"))));
        List<EnergyStatisticalReport> reportList = new ArrayList<>();

        if (Objects.equals(String.valueOf(hashMap.get("energyType")), "2")){
            reportList = emsReportMapper.energyStatisticalDnbReportList(hashMap);
        }
        return reportList;
    }

    /**
     * 峰平谷耗量报表
     *
     * @param hashMap
     * @return
     */
    @Override
    public List<PeaksValleysConsumptionReport> peaksValleysConsumptionReportList(Map hashMap) {
        if (!hashMap.containsKey("dateType")) {
            return null;
        }
        return emsReportMapper.peaksValleysConsumptionReportList(hashMap);
    }

    /**
     * 能源预览接口
     *
     * @param hashMap
     * @return
     */
    @Override
    public List<Map<String, Object>> energyPreviewReportList(Map hashMap) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        if (!hashMap.containsKey("startTime") || !hashMap.containsKey("endTime")) {
            return resultList;
        }

        EmsBaseWorkUnit workUnit = new EmsBaseWorkUnit();
        List<EmsBaseWorkUnit> workUnits = emsBaseWorkUnitMapper.selectEmsBaseWorkUnitList(workUnit);

        HashMap<String, String> selectMap = new HashMap<>();
        selectMap.put("dateType", String.valueOf(hashMap.get("dateType")));
        selectMap.put("energyType", String.valueOf(hashMap.get("energyType")));
        selectMap.put("beginCollectTime", String.valueOf(hashMap.get("startTime")));
        selectMap.put("endCollectTime", String.valueOf(hashMap.get("endTime")));

        for (EmsBaseWorkUnit unit : workUnits) {
            HashMap<String, Object> typeResultMap = new HashMap<>();
            typeResultMap.put("unitName", unit.getWorkUnitName());
            selectMap.put("workUnitCode", unit.getWorkUnitCode());
            List<EnergyStatisticalReport> reportList = this.energyStatisticalReportList(selectMap);
            BigDecimal expendSum = new BigDecimal(0);
            for (EnergyStatisticalReport report : reportList) {
                typeResultMap.put("expend" + report.getBeginTime(), report.getExpend());
                expendSum = expendSum.add(report.getExpend());
            }
            typeResultMap.put("expendSum", expendSum.toPlainString());
            resultList.add(typeResultMap);
        }

        return resultList;
    }

}
