/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.modular.energy.energyElectricity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.modular.base.baseConfig.entity.BaseConfig;
import vip.xiaonuo.modular.base.baseConfig.mapper.BaseConfigMapper;
import vip.xiaonuo.modular.base.pigpen.mapper.BasePigpenMapper;
import vip.xiaonuo.modular.energy.energyElectricity.entity.EnergyElectricity;
import vip.xiaonuo.modular.energy.energyElectricity.entity.vo.*;
import vip.xiaonuo.modular.energy.energyElectricity.mapper.EnergyElectricityMapper;
import vip.xiaonuo.modular.energy.energyElectricity.param.EnergyElectricityListParm;
import vip.xiaonuo.modular.energy.energyElectricity.param.EnergyElectricityTopParm;
import vip.xiaonuo.modular.energy.energyElectricity.service.EnergyElectricityService;

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

/**
 * 能耗电表Service接口实现类
 *
 * @author newspaper
 * @date  2023/12/21 09:15
 **/
@Service
public class EnergyElectricityServiceImpl extends ServiceImpl<EnergyElectricityMapper, EnergyElectricity> implements EnergyElectricityService {

    @Autowired
    private EnergyElectricityMapper energyElectricityMapper;

    @Autowired
    private BaseConfigMapper baseConfigMapper;

    @Autowired
    private BasePigpenMapper basePigpenMapper;

    @Override
    public EnergyElectricityProportionVo getProportion(String orgId, String type) {
        EnergyElectricityProportionVo electricityProportion = new EnergyElectricityProportionVo();
        BigDecimal thisTotal,lastTotal;
        BigDecimal qoq = BigDecimal.valueOf(100);
        String parentId = baseConfigMapper.selectOne(new QueryWrapper<BaseConfig>().eq("config_name", "栋舍阶段").eq("parent_id", 0)).getId();
        List<BaseConfig> buildStage = baseConfigMapper.selectList(new QueryWrapper<BaseConfig>().eq("parent_id", parentId).orderByAsc("sort_code"));
        List<String> stages = buildStage.stream().map(config -> config.getConfigName()).collect(Collectors.toList());
        List<EnergyElectricityRateVo> rateList = null;
        if (type.equals("0")){
            thisTotal = energyElectricityMapper.getWeekTotal(orgId);
            lastTotal = energyElectricityMapper.getLastWeekTotal(orgId);
            for (String stage : stages) {
                EnergyElectricityRateVo rate = new EnergyElectricityRateVo();
                BigDecimal proportion = BigDecimal.ZERO;
                BigDecimal weekTotal = energyElectricityMapper.getWeekTotalByType(stage, orgId);
                rate.setBuildName(stage+"舍");
                rate.setConsumptionQuantity(weekTotal);
                if (!thisTotal.equals(BigDecimal.ZERO)){
                    proportion = weekTotal.divide(thisTotal).multiply(BigDecimal.valueOf(100));
                }
                rate.setProportion(proportion);
                rateList.add(rate);
            }
        }else {
            thisTotal = energyElectricityMapper.getMonthTotal(orgId);
            lastTotal = energyElectricityMapper.getLastMonthTotal(orgId);
            for (String stage : stages) {
                EnergyElectricityRateVo rate = new EnergyElectricityRateVo();
                BigDecimal proportion = BigDecimal.ZERO;
                BigDecimal weekTotal = energyElectricityMapper.getMonthTotalByType(stage, orgId);
                rate.setBuildName(stage+"舍");
                rate.setConsumptionQuantity(weekTotal);
                if (!thisTotal.equals(BigDecimal.ZERO)){
                    proportion = weekTotal.divide(thisTotal).multiply(BigDecimal.valueOf(100));
                }
                rate.setProportion(proportion);
                rateList.add(rate);
            }
        }
        if (!lastTotal.equals(BigDecimal.ZERO)){
            qoq = thisTotal.subtract(lastTotal).divide(lastTotal).multiply(BigDecimal.valueOf(100));
        }
        electricityProportion.setTotal(thisTotal);
        electricityProportion.setQoq(qoq);
        electricityProportion.setRateList(rateList);
        return electricityProportion;
    }

    @Override
    public EnergyElectricityTrendVo listTrend(EnergyElectricityListParm energyElectricityListParm) {
        String type = energyElectricityListParm.getType();
        String pigpenId = energyElectricityListParm.getPigpenId();
        String location = basePigpenMapper.selectById(pigpenId).getFullName();
        EnergyElectricityTrendVo electricityTrend = new EnergyElectricityTrendVo();
        List<EnergyElectricityListVo> energyElectricityList;
        if (type.equals("0")){
            energyElectricityList = energyElectricityMapper.listToday(pigpenId);
        }else if (type.equals("1")){
            energyElectricityList = energyElectricityMapper.listWeek(pigpenId);
        }else if (type.equals("2")){
            energyElectricityList = energyElectricityMapper.listMonth(pigpenId);
        }else {
            String startDate = energyElectricityListParm.getStartDate() + " 00:00:00";
            String endDate = energyElectricityListParm.getEndDate() + " 23:59:59";
            energyElectricityList = energyElectricityMapper.listCustom(pigpenId,startDate,endDate);
        }
        electricityTrend.setLocation(location);
        electricityTrend.setEnergyElectricityList(energyElectricityList);
        return electricityTrend;
    }

    @Override
    public EnergyElectricityTopVo listTop(EnergyElectricityTopParm energyElectricityTopParm, String orgId) {
        String type = energyElectricityTopParm.getType();
        EnergyElectricityTopVo electricityTop;
        if (type.equals("0")){
            electricityTop = energyElectricityMapper.listTodayTop(orgId);
        }else if (type.equals("1")){
            electricityTop = energyElectricityMapper.listWeekTop(orgId);
        }else if (type.equals("2")){
            electricityTop = energyElectricityMapper.listMonthTop(orgId);
        }else {
            String startDate = energyElectricityTopParm.getStartDate() + " 00:00:00";
            String endDate = energyElectricityTopParm.getEndDate() + " 23:59:59";
            electricityTop = energyElectricityMapper.listCustomTop(orgId,startDate,endDate);
        }
        return electricityTop;
    }
}
