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

import cn.hutool.core.util.ObjectUtil;
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.pigpen.entity.BasePigpen;
import vip.xiaonuo.modular.base.pigpen.mapper.BasePigpenMapper;
import vip.xiaonuo.modular.env.entity.EnvDeviceEquipment;
import vip.xiaonuo.modular.env.envdata.entity.EnvData;
import vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataCarouselVo;
import vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataListVo;
import vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataRecentVo;
import vip.xiaonuo.modular.env.envdata.entity.vo.EnvDataTrendVo;
import vip.xiaonuo.modular.env.envdata.mapper.EnvDataMapper;
import vip.xiaonuo.modular.env.envdata.param.EnvDataBuildNameParm;
import vip.xiaonuo.modular.env.envdata.param.EnvDataListParm;
import vip.xiaonuo.modular.env.envdata.param.EnvDataPigpenIdParm;
import vip.xiaonuo.modular.env.envdata.service.EnvDataService;
import vip.xiaonuo.modular.env.envdevice.entity.EnvDevice;
import vip.xiaonuo.modular.env.envdevice.mapper.EnvDeviceMapper;
import vip.xiaonuo.modular.env.mapper.EnvDeviceEquipmentMapper;

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

/**
 * 环控数据Service接口实现类
 *
 * @author newspaper
 * @date  2023/12/20 14:38
 **/
@Service
public class EnvDataServiceImpl extends ServiceImpl<EnvDataMapper, EnvData> implements EnvDataService {

    @Autowired
    private BasePigpenMapper basePigpenMapper;
    @Autowired
    private EnvDataMapper envDataMapper;
    @Autowired
    private EnvDeviceEquipmentMapper envDeviceEquipmentMapper;
    @Autowired
    private EnvDeviceMapper envDeviceMapper;

    @Override
    public List<EnvDataRecentVo> getLastDataByPigpenId(EnvDataPigpenIdParm envDataPigpenIdParm) {
        String pigpenId = envDataPigpenIdParm.getPigpenId();
        BasePigpen basePigpen1 = basePigpenMapper.selectById(pigpenId);
        List<EnvDataRecentVo> resultList = new ArrayList();
        List<BasePigpen> basePigpens = new ArrayList<>();
        if (basePigpen1.getType() == 0){
            List<BasePigpen> pigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().eq("parent_id", pigpenId));
            List<String> idList = pigpens.stream().map(basePigpen -> basePigpen.getId()).collect(Collectors.toList());
            if (ObjectUtil.isNotEmpty(idList)){
                basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().in("parent_id", idList).orderByAsc("id"));
            }else {
                return resultList;
            }

        }else if (basePigpen1.getType() == 1){
            basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().eq("parent_id", pigpenId));
        }else {
            basePigpens = basePigpenMapper.selectList(new QueryWrapper<BasePigpen>().eq("id", pigpenId));
        }
        for (BasePigpen basePigpen : basePigpens) {
            EnvDataRecentVo envDataRecent = new EnvDataRecentVo();
            EnvData envData = this.getOne(new QueryWrapper<EnvData>().lambda()
                    .eq(EnvData::getPigpenId, basePigpen.getId())
                    .eq(EnvData::getIsShow,0)
                    .orderByDesc(EnvData::getUpdateTime)
                    .last("limit 1"));
            EnvDevice envDevice = envDeviceMapper.selectOne(new QueryWrapper<EnvDevice>().lambda().eq(EnvDevice::getInstallPosition, basePigpen.getId()));
            if (ObjectUtil.isNotEmpty(envDevice)){
                List<EnvDeviceEquipment> envDeviceEquipments = envDeviceEquipmentMapper.selectList(new QueryWrapper<EnvDeviceEquipment>().lambda()
                        .eq(EnvDeviceEquipment::getDeviceCode, envDevice.getDeviceCode()).orderByAsc(EnvDeviceEquipment::getChipCode));
                if (ObjectUtil.isNotEmpty(envDeviceEquipments)){
                    envDataRecent.setList(envDeviceEquipments);
                }
            }
            if (ObjectUtil.isEmpty(envData)){
                envDataRecent.setTemp(BigDecimal.ZERO);
                envDataRecent.setHum(BigDecimal.ZERO);
                envDataRecent.setLocation(basePigpen.getFullName());
                envDataRecent.setPigpenId(basePigpen.getId());
            }else {
                envDataRecent.setTemp(envData.getTemHandleValue());
                envDataRecent.setHum(envData.getHumiHandleValue());
                envDataRecent.setLocation(basePigpen.getFullName());
                envDataRecent.setPigpenId(basePigpen.getId());
            }
            resultList.add(envDataRecent);
        }
        return resultList;
    }

    @Override
    public EnvDataTrendVo listDataByPigpenId(EnvDataListParm envDataListParm) {
        String type = envDataListParm.getType();
        String pigpenId = envDataListParm.getPigpenId();
        String location = basePigpenMapper.selectById(pigpenId).getFullName();
        EnvDataTrendVo envDataTrend = new EnvDataTrendVo();
        List<EnvDataListVo> envDataList;
        if (type.equals("0")){
            envDataList = envDataMapper.listToday(pigpenId);
        }else if (type.equals("1")){
            envDataList = envDataMapper.listWeek(pigpenId);
        }else if (type.equals("2")){
            envDataList = envDataMapper.listMonth(pigpenId);
        }else {
            String startDate = envDataListParm.getStartDate() + " 00:00:00";
            String endDate = envDataListParm.getEndDate() + " 23:59:59";
            envDataList = envDataMapper.listCustom(pigpenId,startDate,endDate);
        }
        envDataTrend.setLocation(location);
        envDataTrend.setList(envDataList);
        return envDataTrend;
    }

    @Override
    public List<EnvDataCarouselVo> listData(EnvDataBuildNameParm envDataBuildNameParm) {
        String buildName = envDataBuildNameParm.getBuildName();
        String orgId = envDataBuildNameParm.getOrgId();
        List<EnvDataCarouselVo> envDatas = envDataMapper.listData(buildName,orgId);
        return envDatas;
    }

}
