package com.ruoyi.ems.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.utils.DateTimeUtil;
import com.ruoyi.common.core.utils.StreamUtils;
import com.ruoyi.ems.domain.ElecUseH;
import com.ruoyi.ems.mapper.ElecUseHMapper;
import com.ruoyi.ems.mapper.PvSupplyHMapper;
import com.ruoyi.ems.service.IElecUseHService;

import cn.hutool.core.map.MapUtil;

/**
 * 用能计量-小时Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-15
 */
@Service
public class ElecUseHServiceImpl implements IElecUseHService {
    @Autowired
    private ElecUseHMapper elecUseHMapper;

    @Autowired
    private PvSupplyHMapper pvSupplyHMapper;

    /**
     * 查询用能计量-小时
     *
     * @param id 用能计量-小时主键
     * @return 用能计量-小时
     */
    @Override
    public ElecUseH selectElecUseHById(Long id) {
        return elecUseHMapper.selectElecUseHById(id);
    }

    /**
     * 查询用能计量-小时列表
     *
     * @param ElecUseH 用能计量-小时
     * @return 用能计量-小时
     */
    @Override
    public List<ElecUseH> selectElecUseHList(ElecUseH ElecUseH) {
        return elecUseHMapper.selectElecUseHList(ElecUseH);
    }

    /**
     * 新增用能计量-小时
     *
     * @param ElecUseH 用能计量-小时
     * @return 结果
     */
    @Override
    public int insertElecUseH(ElecUseH ElecUseH) {
        return elecUseHMapper.insertElecUseH(ElecUseH);
    }

    /**
     * 修改用能计量-小时
     *
     * @param ElecUseH 用能计量-小时
     * @return 结果
     */
    @Override
    public int updateElecUseH(ElecUseH ElecUseH) {
        return elecUseHMapper.updateElecUseH(ElecUseH);
    }

    /**
     * 批量删除用能计量-小时
     *
     * @param ids 需要删除的用能计量-小时主键
     * @return 结果
     */
    @Override
    public int deleteElecUseHByIds(Long[] ids) {
        return elecUseHMapper.deleteElecUseHByIds(ids);
    }

    /**
     * 删除用能计量-小时信息
     *
     * @param id 用能计量-小时主键
     * @return 结果
     */
    @Override
    public int deleteElecUseHById(Long id) {
        return elecUseHMapper.deleteElecUseHById(id);
    }

    @Override
    public List<Map> selectElecAndPower(String areaCode) {
        String firstDayOfRecentYear = DateTimeUtil.getFirstDayOfRecentYear();
        List<Map<String, Object>> monthElecUse = elecUseHMapper.selectElecUseHByMonth(areaCode, firstDayOfRecentYear);
        List<Map<String, Object>> monthPv = pvSupplyHMapper.selectAreaSupplyByMonth(areaCode, firstDayOfRecentYear);
        Map<String, Map<String, Object>> dateElecUseIndex = StreamUtils.toIdentityMap(monthElecUse,
            map -> MapUtil.getStr(map, "dateIndex"));
        Map<String, Map<String, Object>> datePvIndex = StreamUtils.toIdentityMap(monthPv,
            map -> MapUtil.getStr(map, "dateIndex"));
        List<String> dates = DateTimeUtil.monthOfRange(firstDayOfRecentYear,
            DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd), DateTimeUtil.DateFormatter.yyyy_MM_dd,
            DateTimeUtil.DateFormatter.yyyy_MM);
        List<Map> result = new ArrayList<>();
        Map<Object, Object> dateIndex;
        for (String date : dates) {
            dateIndex = MapUtil.builder().put("dateIndex", date).map();
            if (dateElecUseIndex.containsKey(date)) {
                dateIndex.putAll(dateElecUseIndex.get(date));
            }
            if (datePvIndex.containsKey(date)) {
                dateIndex.putAll(datePvIndex.get(date));
            }
            result.add(dateIndex);
        }
        return result;
    }

    @Override
    public Map<String, List<Map<String, Object>>> selectMonthElecUse(String areaCode) {
        String date = DateTimeUtil.getFirstDayOfRecentYear();
        List<Map<String, Object>> areaData = elecUseHMapper.selectMonthElecUse(areaCode, date);
        return StreamUtils.groupByKey(areaData, map -> MapUtil.getStr(map, "areaCode"));
    }

    @Override
    public List<Map<String, Object>> selectThisDayFacsTypeElecUse(String areaCode) {
        String date = DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd);
        return elecUseHMapper.selectThisDayFacsTypeElecUse(areaCode, date);
    }

    @Override
    public Map<String, Object> selectThisDayAreaElecUse(String areaCode) {
        String date = DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd);
        return elecUseHMapper.selectThisDayElecUse(areaCode, date);
    }
}
