/*
 * 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.env.envThreshold.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 org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.modular.base.pigpen.entity.BasePigpen;
import vip.xiaonuo.modular.base.pigpen.mapper.BasePigpenMapper;
import vip.xiaonuo.modular.env.envThreshold.entity.EnvThreshold;
import vip.xiaonuo.modular.env.envThreshold.entity.vo.EnvThresholdListVo;
import vip.xiaonuo.modular.env.envThreshold.entity.vo.EnvThresholdVo;
import vip.xiaonuo.modular.env.envThreshold.mapper.EnvThresholdMapper;
import vip.xiaonuo.modular.env.envThreshold.param.*;
import vip.xiaonuo.modular.env.envThreshold.service.EnvThresholdService;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 环控阈值Service接口实现类
 *
 * @author newspaper
 * @date  2023/12/25 15:38
 **/
@Service
public class EnvThresholdServiceImpl extends ServiceImpl<EnvThresholdMapper, EnvThreshold> implements EnvThresholdService {

    @Autowired
    private BasePigpenMapper basePigpenMapper;

    @Override
    public List<EnvThresholdListVo> getThreshold(EnvThresholdOrgIdParam envThresholdOrgIdParam) {
        String orgId = envThresholdOrgIdParam.getOrgId();
        List<EnvThresholdListVo> resultList = new ArrayList<>();
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda().eq(BasePigpen::getOrgId, orgId).eq(BasePigpen::getType,0));
        for (BasePigpen basePigpen : basePigpens) {
            EnvThresholdListVo envThresholdListVo = new EnvThresholdListVo();
            envThresholdListVo.setBuildingId(basePigpen.getId());
            envThresholdListVo.setBuildingName(basePigpen.getBuildName());
            List<EnvThresholdVo> envThresholdVos = 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();
                    EnvThresholdVo envThresholdVo = new EnvThresholdVo();
                    EnvThreshold envThreshold = this.getOne(new QueryWrapper<EnvThreshold>().lambda().eq(EnvThreshold::getOrgId, orgId).eq(EnvThreshold::getUnitId, unitId));
                    if (ObjectUtil.isEmpty(envThreshold)){
                        envThreshold = new EnvThreshold();
                        envThreshold.setOrgId(orgId);
                        envThreshold.setUnitId(unitId);
                        this.save(envThreshold);
                    }
                    BeanUtil.copyProperties(envThreshold,envThresholdVo);
                    envThresholdVos.add(envThresholdVo);
                }
            }
            if (ObjectUtil.isNotEmpty(envThresholdVos)){
                envThresholdListVo.setIsEnable(envThresholdVos.get(0).getIsEnable());
            }else {
                envThresholdListVo.setIsEnable(0);
            }
            envThresholdListVo.setList(envThresholdVos);
            resultList.add(envThresholdListVo);
        }
        return resultList;
    }


    @Override
    public void saveThreshold(EnvThresholdSaveParam envThresholdSaveParam) {
        String orgId = envThresholdSaveParam.getOrgId();
        EnvThreshold envThreshold = this.getOne(new QueryWrapper<EnvThreshold>().lambda().eq(EnvThreshold::getOrgId, orgId).eq(EnvThreshold::getUnitId, envThresholdSaveParam.getUnitId()));
        BeanUtil.copyProperties(envThresholdSaveParam,envThreshold);
        this.updateById(envThreshold);
    }

    @Override
    @Transactional
    public void modifyThresholdStatus(EnvThresholdModifyParam envThresholdModifyParam) {
        String unitId = envThresholdModifyParam.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());
        if (ObjectUtil.isNotEmpty(unitIdList)){
            this.update(new UpdateWrapper<EnvThreshold>().lambda().set(EnvThreshold::getIsEnable,envThresholdModifyParam.getIsEnable()).in(EnvThreshold::getUnitId,unitIdList));
        }
    }

    @Override
    @Transactional
    public void syncThreshold(EnvThresholdSyncParam envThresholdSyncParam) {
        String orgId = StpLoginUserUtil.getLoginUser().getOrgId();
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().lambda()
                .like(BasePigpen::getPids, envThresholdSyncParam.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<EnvThreshold>().lambda().in(EnvThreshold::getUnitId,unitList));
        List<EnvThreshold> envThresholds = new ArrayList<>();
        for (String s : unitList) {
            EnvThreshold envThreshold = new EnvThreshold();
            BeanUtil.copyProperties(envThresholdSyncParam,envThreshold);
            envThreshold.setUnitId(s);
            envThreshold.setOrgId(orgId);
            envThresholds.add(envThreshold);
        }
        this.saveBatch(envThresholds);
    }
}
