package org.dromara.ems.report.service.impl;

import org.dromara.ems.base.domain.EmsBaseWorkUnit;
import org.dromara.ems.base.mapper.EmsBaseMonitorWorkUnitMapper;
import org.dromara.ems.base.mapper.EmsBaseWorkUnitMapper;
import org.dromara.ems.report.domain.EnergyStatisticalReport;
import org.dromara.ems.report.domain.PeaksValleysConsumptionReport;
import org.dromara.ems.report.mapper.EmsReportMapper;
import org.dromara.ems.report.service.IEmsReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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


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

    private final EmsReportMapper emsReportMapper;


    private final EmsBaseMonitorWorkUnitMapper emsBaseMonitorWorkUnitMapper;


    private final 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;
    }

}
