/*
 * 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.energyThreshold.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.pigpen.entity.BasePigpen;
import vip.xiaonuo.modular.base.pigpen.mapper.BasePigpenMapper;
import vip.xiaonuo.modular.energy.energyThreshold.entity.EnergyThreshold;
import vip.xiaonuo.modular.energy.energyThreshold.entity.vo.EnergyThresholdListVo;
import vip.xiaonuo.modular.energy.energyThreshold.entity.vo.EnergyThresholdVo;
import vip.xiaonuo.modular.energy.energyThreshold.mapper.EnergyThresholdMapper;
import vip.xiaonuo.modular.energy.energyThreshold.param.*;
import vip.xiaonuo.modular.energy.energyThreshold.service.EnergyThresholdService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 能耗阈值Service接口实现类
 *
 * @author newspaper
 * @date  2023/12/26 09:32
 **/
@Service
public class EnergyThresholdServiceImpl extends ServiceImpl<EnergyThresholdMapper, EnergyThreshold> implements EnergyThresholdService {

    @Autowired
    private BasePigpenMapper basePigpenMapper;

    @Override
    public List<EnergyThresholdListVo> getThreshold(EnergyThresholdOrgIdParam energyThresholdOrgIdParam) {
        String orgId = energyThresholdOrgIdParam.getOrgId();
        List<EnergyThresholdListVo> resultList = new ArrayList<>();
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda().eq(BasePigpen::getOrgId, orgId).eq(BasePigpen::getType,0));
        for (BasePigpen basePigpen : basePigpens) {
            EnergyThresholdListVo energyThresholdListVo = new EnergyThresholdListVo();
            energyThresholdListVo.setBuildingId(basePigpen.getId());
            energyThresholdListVo.setBuildingName(basePigpen.getBuildName());
            List<EnergyThresholdVo> energyThresholdVos = new ArrayList<>();
            List<BasePigpen> floorBasePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda().eq(BasePigpen::getParentId, basePigpen.getId()));
            if (ObjectUtil.isNotEmpty(floorBasePigpens)){
                List<String> floorIds = floorBasePigpens.stream().map(basePigpen1 -> basePigpen1.getId()).collect(Collectors.toList());
                List<BasePigpen> basePigpens1 = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda().in(BasePigpen::getParentId, floorIds));
                for (BasePigpen pigpen : basePigpens1) {
                    String unitId = pigpen.getId();
                    EnergyThresholdVo energyThresholdVo = new EnergyThresholdVo();
                    EnergyThreshold energyThreshold = this.getOne(new QueryWrapper<EnergyThreshold>().lambda().eq(EnergyThreshold::getOrgId, orgId).eq(EnergyThreshold::getUnitId, unitId));
                    if (ObjectUtil.isEmpty(energyThreshold)){
                        energyThreshold = new EnergyThreshold();
                        energyThreshold.setOrgId(orgId);
                        energyThreshold.setUnitId(unitId);
                        this.save(energyThreshold);
                    }
                    BeanUtil.copyProperties(energyThreshold,energyThresholdVo);
                    energyThresholdVos.add(energyThresholdVo);
                }
            }
            energyThresholdListVo.setList(energyThresholdVos);
            resultList.add(energyThresholdListVo);
        }
        return resultList;
    }

    @Override
    public void saveThreshold(EnergyThresholdSaveParam energyThresholdSaveParam) {
        String orgId = energyThresholdSaveParam.getOrgId();
        EnergyThreshold energyThreshold = this.getOne(new QueryWrapper<EnergyThreshold>().lambda().eq(EnergyThreshold::getOrgId, orgId).eq(EnergyThreshold::getUnitId, energyThresholdSaveParam.getUnitId()));
        BeanUtil.copyProperties(energyThresholdSaveParam,energyThreshold);
        this.updateById(energyThreshold);
    }

    @Override
    public void modifyThresholdStatus(EnergyThresholdModifyParam energyThresholdModifyParam) {
        String unitId = energyThresholdModifyParam.getUnitId();
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda().like(BasePigpen::getPids, unitId).eq(BasePigpen::getType, 2));
        List<String> unitIdList = basePigpens.stream().map(basePigpen -> basePigpen.getId()).collect(Collectors.toList());
        this.update(new UpdateWrapper<EnergyThreshold>().lambda().set(EnergyThreshold::getIsEnable,energyThresholdModifyParam.getIsEnable()).in(EnergyThreshold::getUnitId,unitIdList));
    }

    @Override
    public void syncThreshold(EnergyThresholdSyncParam energyThresholdSyncParam) {
        String orgId = energyThresholdSyncParam.getOrgId();
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda()
                .like(BasePigpen::getPids, energyThresholdSyncParam.getUnitId())
                .eq(BasePigpen::getOrgId, orgId)
                .eq(BasePigpen::getType,2));
        List<String> unitList = basePigpens.stream().map(basePigpen -> basePigpen.getId()).collect(Collectors.toList());
        this.remove(new QueryWrapper<EnergyThreshold>().lambda().in(EnergyThreshold::getUnitId,unitList));
        List<EnergyThreshold> energyThresholds = new ArrayList<>();
        for (String s : unitList) {
            EnergyThreshold energyThreshold = new EnergyThreshold();
            BeanUtil.copyProperties(energyThresholdSyncParam,energyThreshold);
            energyThreshold.setUnitId(s);
            energyThreshold.setOrgId(orgId);
            energyThresholds.add(energyThreshold);
        }
        this.saveBatch(energyThresholds);
    }
}
