package com.ruoyi.ems.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import com.ruoyi.common.core.utils.DateTimeUtil;
import com.ruoyi.ems.domain.ElecPgSupplyH;
import com.ruoyi.ems.domain.ElecPvSupplyH;
import com.ruoyi.ems.mapper.ElecPgSupplyHMapper;
import com.ruoyi.ems.mapper.ElecPvSupplyHMapper;
import com.ruoyi.ems.model.QueryMeter;
import com.ruoyi.ems.service.IElecPgSupplyHService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * 电网供应计量-小时Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-09
 */
@Service
@Slf4j
public class ElecPgSupplyHServiceImpl implements IElecPgSupplyHService {
    @Autowired
    private ElecPgSupplyHMapper pgSupplyHMapper;

    @Autowired
    private ElecPvSupplyHMapper pvSupplyHMapper;

    /**
     * 查询电网供应计量-小时
     *
     * @param id 电网供应计量-小时主键
     * @return 电网供应计量-小时
     */
    @Override
    public ElecPgSupplyH selectPgSupplyHById(Long id) {
        return pgSupplyHMapper.selectPgSupplyHById(id);
    }

    /**
     * 查询电网供应计量-小时列表
     *
     * @param elecPgSupplyH 电网供应计量-小时
     * @return 电网供应计量-小时
     */
    @Override
    public List<ElecPgSupplyH> selectPgSupplyHList(ElecPgSupplyH elecPgSupplyH) {
        if (StringUtils.equals("-1", elecPgSupplyH.getAreaCode())) {
            return pgSupplyHMapper.selectPgSupplyHAllArea(elecPgSupplyH);
        } else {
            return pgSupplyHMapper.selectPgSupplyHList(elecPgSupplyH);
        }
    }

    @Override
    public List<ElecPgSupplyH> selectPgSupplyH(QueryMeter queryMeter) {
        return pgSupplyHMapper.selectPgSupplyH(queryMeter);
    }

    @Override
    public Map<String, Object> selectSupplyPvByH() {
        String thisDay = DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd);
        Map<String, Map<String, Object>> supplyByHMap = pgSupplyHMapper.calThisDaySupplyByH(thisDay);
        Map<String, Map<String, Object>> pvByHMap = pvSupplyHMapper.calThisDayPvByH(thisDay);
        return new HashMap<String, Object>() {{
            put("supply", supplyByHMap);
            put("pv", pvByHMap);
        }};
    }

    @Override
    public Map<String, Object> selectSupplyPvByDay() {
        String thisDay = DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd);
        Map<String, Object> thisDaySupply = pgSupplyHMapper.calThisDaySupply(thisDay);
        Map<String, Object> thisDayPv = pvSupplyHMapper.calThisDayPv(thisDay);
        return new HashMap<String, Object>() {{
            put("supply", thisDaySupply);
            put("pv", thisDayPv);
        }};
    }

    /**
     * 新增电网供应计量-小时
     *
     * @param elecPgSupplyH 电网供应计量-小时
     * @return 结果
     */
    @Override
    public int insertPgSupplyH(ElecPgSupplyH elecPgSupplyH) {
        return pgSupplyHMapper.insertPgSupplyH(elecPgSupplyH);
    }

    /**
     * 修改电网供应计量-小时
     *
     * @param elecPgSupplyH 电网供应计量-小时
     * @return 结果
     */
    @Override
    public int updatePgSupplyH(ElecPgSupplyH elecPgSupplyH) {
        return pgSupplyHMapper.updatePgSupplyH(elecPgSupplyH);
    }

    /**
     * 批量删除电网供应计量-小时
     *
     * @param ids 需要删除的电网供应计量-小时主键
     * @return 结果
     */
    @Override
    public int deletePgSupplyHByIds(Long[] ids) {
        return pgSupplyHMapper.deletePgSupplyHByIds(ids);
    }

    /**
     * 删除电网供应计量-小时信息
     *
     * @param id 电网供应计量-小时主键
     * @return 结果
     */
    @Override
    public int deletePgSupplyHById(Long id) {
        return pgSupplyHMapper.deletePgSupplyHById(id);
    }


    @Override
    public Map<String, ElecPvSupplyH> calPvByDateRange(QueryMeter param) {
        Future<?> thisDay = ThreadUtil.execAsync(() -> pvSupplyHMapper.calPvByDateRange(new QueryMeter() {{
            setDate(DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM_dd));
            setAreaCode(param.getAreaCode());
        }}));
        Future<?> thisMonth = ThreadUtil.execAsync(() -> pvSupplyHMapper.calPvByDateRange(new QueryMeter() {
            {
                setDate(DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy_MM));
                setAreaCode(param.getAreaCode());
            }
        }));
        Future<?> thisYear = ThreadUtil.execAsync(() -> pvSupplyHMapper.calPvByDateRange(new QueryMeter() {{
            setAreaCode(param.getAreaCode());
            setDate(DateTimeUtil.currentDateTime(DateTimeUtil.DateFormatter.yyyy));
        }}));
        Future<?> total = ThreadUtil.execAsync(() -> pvSupplyHMapper.calPvByDateRange(new QueryMeter() {{
            setAreaCode(param.getAreaCode());
        }}));
        Map<String, ElecPvSupplyH> result = new HashMap<>();
        try {
            result.put("thisDay", (ElecPvSupplyH) thisDay.get());
            result.put("thisMonth", (ElecPvSupplyH) thisMonth.get());
            result.put("thisYear", (ElecPvSupplyH) thisYear.get());
            result.put("total", (ElecPvSupplyH) total.get());
        } catch (Exception e) {
            log.error("calPvByDateRange error {}", e.getMessage());
        }
        return result;
    }

    @Override
    public List<ElecPvSupplyH> calDayPvRange(QueryMeter param) {
        return pvSupplyHMapper.calDayPvRange(param);
    }

    @Override
    public List<ElecPvSupplyH> calHourPvRange(QueryMeter param) {
        return pvSupplyHMapper.calHourPvRange(param);
    }

    @Override
    public List<ElecPvSupplyH> calMonthPvByDateRange(QueryMeter param) {
        return pvSupplyHMapper.calMonthPvByDateRange(param);
    }

    @Override
    public List<ElecPgSupplyH> calMonthSupplyByH(ElecPgSupplyH param) {
        return pgSupplyHMapper.calMonthSupplyByH(param);
    }
}
