/*
 * 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.energyWater.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.vo.*;
import vip.xiaonuo.modular.energy.energyWater.entity.EnergyWater;
import vip.xiaonuo.modular.energy.energyWater.entity.vo.*;
import vip.xiaonuo.modular.energy.energyWater.mapper.EnergyWaterMapper;
import vip.xiaonuo.modular.energy.energyWater.param.EnergyWaterListParm;
import vip.xiaonuo.modular.energy.energyWater.param.EnergyWaterTopParm;
import vip.xiaonuo.modular.energy.energyWater.service.EnergyWaterService;

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 EnergyWaterServiceImpl extends ServiceImpl<EnergyWaterMapper, EnergyWater> implements EnergyWaterService {

    @Autowired
    private BaseConfigMapper baseConfigMapper;

    @Autowired
    private EnergyWaterMapper energyWaterMapper;

    @Autowired
    private BasePigpenMapper basePigpenMapper;

    @Override
    public EnergyWaterProportionVo getProportion(String orgId, String type) {
        EnergyWaterProportionVo energyWaterProportion = new EnergyWaterProportionVo();
        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<EnergyWaterRateVo> rateList = null;
        if (type.equals("0")){
            thisTotal = energyWaterMapper.getWeekTotal(orgId);
            lastTotal = energyWaterMapper.getLastWeekTotal(orgId);
            for (String stage : stages) {
                EnergyWaterRateVo rate = new EnergyWaterRateVo();
                BigDecimal proportion = BigDecimal.ZERO;
                BigDecimal weekTotal = energyWaterMapper.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 = energyWaterMapper.getMonthTotal(orgId);
            lastTotal = energyWaterMapper.getLastMonthTotal(orgId);
            for (String stage : stages) {
                EnergyWaterRateVo rate = new EnergyWaterRateVo();
                BigDecimal proportion = BigDecimal.ZERO;
                BigDecimal weekTotal = energyWaterMapper.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));
        }
        energyWaterProportion.setTotal(thisTotal);
        energyWaterProportion.setQoq(qoq);
        energyWaterProportion.setRateList(rateList);
        return energyWaterProportion;
    }

    @Override
    public EnergyWaterTrendVo listTrend(EnergyWaterListParm energyWaterListParm) {
        String type = energyWaterListParm.getType();
        String pigpenId = energyWaterListParm.getPigpenId();
        String location = basePigpenMapper.selectById(pigpenId).getFullName();
        EnergyWaterTrendVo waterTrend = new EnergyWaterTrendVo();
        List<EnergyWaterListVo> energyWaterList;
        if (type.equals("0")){
            energyWaterList = energyWaterMapper.listToday(pigpenId);
        }else if (type.equals("1")){
            energyWaterList = energyWaterMapper.listWeek(pigpenId);
        }else if (type.equals("2")){
            energyWaterList = energyWaterMapper.listMonth(pigpenId);
        }else {
            String startDate = energyWaterListParm.getStartDate() + " 00:00:00";
            String endDate = energyWaterListParm.getEndDate() + " 23:59:59";
            energyWaterList = energyWaterMapper.listCustom(pigpenId,startDate,endDate);
        }
        waterTrend.setLocation(location);
        waterTrend.setEnergyWaterList(energyWaterList);
        return waterTrend;
    }

    @Override
    public EnergyWaterTopVo listTop(EnergyWaterTopParm energyWaterTopParm, String orgId) {
        String type = energyWaterTopParm.getType();
        EnergyWaterTopVo waterTop;
        if (type.equals("0")){
            waterTop = energyWaterMapper.listTodayTop(orgId);
        }else if (type.equals("1")){
            waterTop = energyWaterMapper.listWeekTop(orgId);
        }else if (type.equals("2")){
            waterTop = energyWaterMapper.listMonthTop(orgId);
        }else {
            String startDate = energyWaterTopParm.getStartDate() + " 00:00:00";
            String endDate = energyWaterTopParm.getEndDate() + " 23:59:59";
            waterTop = energyWaterMapper.listCustomTop(orgId,startDate,endDate);
        }
        return waterTop;
    }
}
