package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.business.domain.VO.EleMonitorDeviceSumVo;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.HwElectronicFenceMapper;
import com.ruoyi.business.domain.HwElectronicFence;
import com.ruoyi.business.service.IHwElectronicFenceService;

/**
 * 电子围栏Service业务层处理
 *
 * @author xins
 * @date 2023-09-04
 */
@Service
public class HwElectronicFenceServiceImpl implements IHwElectronicFenceService
{
    @Autowired
    private HwElectronicFenceMapper hwElectronicFenceMapper;

    /**
     * 查询电子围栏
     *
     * @param electronicFenceId 电子围栏主键
     * @return 电子围栏
     */
    @Override
    public HwElectronicFence selectHwElectronicFenceByElectronicFenceId(Long electronicFenceId)
    {
        return hwElectronicFenceMapper.selectHwElectronicFenceByElectronicFenceId(electronicFenceId);
    }

    @Override
    public List<HwElectronicFence> selectElectronicByScene(Long sceneId) {
        return hwElectronicFenceMapper.selectElectronicByScene(sceneId);
    }

    @Override
    public List<EleMonitorDeviceSumVo> selectEleTypeNums(Long sceneId) {
        List<Integer> list = hwElectronicFenceMapper.selectEleTypeBySceneId(sceneId);
        list.stream().forEach(v->{});
        List<EleMonitorDeviceSumVo> voList = new ArrayList<EleMonitorDeviceSumVo>();
       if (list.size()==1){
           if (list.get(0)==1){
               //电子围栏下只有设备
               voList = hwElectronicFenceMapper.selectSumsFromMonitorByElectronicFence(sceneId);

               return voList;
           }
           else if (list.get(0)==2){
               //电子围栏下只有监控单元
               voList = hwElectronicFenceMapper.selectEleOnlyMonitorBySceneId(sceneId);
               return voList;
           }
           else if (list.get(0)==3){
               //电子围栏下只有场景
               Integer integer = hwElectronicFenceMapper.selectEleOnlyScene(sceneId);
               EleMonitorDeviceSumVo eleMonitorDeviceSumVo = new EleMonitorDeviceSumVo();
               eleMonitorDeviceSumVo.setSceneId(sceneId);
               eleMonitorDeviceSumVo.setSum(integer);
               voList.add(eleMonitorDeviceSumVo);
               return voList;
           }
       }
       else if (list.size()==2){
           if (list.get(0)+list.get(1)==3){
               //设备与监控单元
               List<EleMonitorDeviceSumVo> devices = new ArrayList<>();
               List<EleMonitorDeviceSumVo> monitor = new ArrayList<>();
               //查询场景下的所有设备根据电子围栏分组，在查询场景下的所有监控单元以及下面的设备按电子围栏分组
               //shebei
               devices = hwElectronicFenceMapper.selectSumsFromMonitorByElectronicFence(sceneId);
               //jiankongdanyuan
               monitor = hwElectronicFenceMapper.selectEleOnlyMonitorBySceneId(sceneId);
               for (int i=0; i<devices.size(); i++){
                   for (int j = 0;j<monitor.size();j++){
                      if(devices.get(i).getElectronicFenceId()==monitor.get(j).getElectronicFenceId()){
                          monitor.get(j).setSum(monitor.get(j).getSum()-devices.get(i).getSum());
                          voList.addAll(devices);
                          voList.addAll(monitor);
                      }
                   }
               }
               return voList;
           }
           else if (list.get(0)+list.get(1)==4){
               //设备与场景
               List<EleMonitorDeviceSumVo> devices = new ArrayList<>();

               devices = hwElectronicFenceMapper.selectSumsFromMonitorByElectronicFence(sceneId);
               Integer aLong = hwElectronicFenceMapper.selectEleOnlyScene(sceneId);
               for (int i = 0; i < devices.size();i++){
                   aLong = aLong-devices.get(i).getSum();
               }
               EleMonitorDeviceSumVo eleMonitorDeviceSumVo = new EleMonitorDeviceSumVo();
               eleMonitorDeviceSumVo.setSceneId(sceneId);
               eleMonitorDeviceSumVo.setSum(aLong);
               voList.add(eleMonitorDeviceSumVo);
               voList.addAll(devices);
               return  voList;
           }
           else if (list.get(0)+list.get(1)==5){
               //场景与监控单元
               List<EleMonitorDeviceSumVo> monitor = new ArrayList<>();
               monitor=hwElectronicFenceMapper.selectEleOnlyMonitorBySceneId(sceneId);
               Integer aLong = hwElectronicFenceMapper.selectEleOnlyScene(sceneId);
                for (int i = 0;i<monitor.size();i++){
                    aLong = aLong-monitor.get(i).getSum();
                }
               EleMonitorDeviceSumVo eleMonitorDeviceSumVo = new EleMonitorDeviceSumVo();
               eleMonitorDeviceSumVo.setSceneId(sceneId);
               eleMonitorDeviceSumVo.setSum(aLong);
               voList.add(eleMonitorDeviceSumVo);
               voList.addAll(monitor);
                return voList;
           }

       }else if (list.size()==3){
           List<EleMonitorDeviceSumVo> devices = new ArrayList<>();
           List<EleMonitorDeviceSumVo> monitor = new ArrayList<>();
           //shebei
           devices = hwElectronicFenceMapper.selectSumsFromMonitorByElectronicFence(sceneId);
           //jiankongdanyuan
           monitor = hwElectronicFenceMapper.selectEleOnlyMonitorBySceneId(sceneId);
           Integer aLong = hwElectronicFenceMapper.selectEleOnlyScene(sceneId);
           for (int i=0; i<devices.size(); i++){
               for (int j = 0;j<monitor.size();j++){
                   if(devices.get(i).getElectronicFenceId()==monitor.get(j).getElectronicFenceId()){
                       monitor.get(j).setSum(monitor.get(j).getSum()-devices.get(i).getSum());
                   }
               }
           }
           for (int i=0; i<monitor.size(); i++){
               aLong = aLong - monitor.get(i).getSum();
           }
           for (int i = 0;i<devices.size();i++){
               aLong = aLong-devices.get(i).getSum();
           }
           EleMonitorDeviceSumVo eleMonitorDeviceSumVo = new EleMonitorDeviceSumVo();
           eleMonitorDeviceSumVo.setSceneId(sceneId);
           eleMonitorDeviceSumVo.setSum(aLong);
           voList.add(eleMonitorDeviceSumVo);
           voList.addAll(devices);
           voList.addAll(monitor);
       }
        return voList;
    }

    /**
     * 查询电子围栏列表
     *
     * @param hwElectronicFence 电子围栏
     * @return 电子围栏
     */
    @Override
    public List<HwElectronicFence> selectHwElectronicFenceList(HwElectronicFence hwElectronicFence)
    {
        return hwElectronicFenceMapper.selectHwElectronicFenceList(hwElectronicFence);
    }

    /**
     * 新增电子围栏
     *
     * @param hwElectronicFence 电子围栏
     * @return 结果
     */
    @Override
    public int insertHwElectronicFence(HwElectronicFence hwElectronicFence)
    {
        hwElectronicFence.setCreateTime(DateUtils.getNowDate());
        return hwElectronicFenceMapper.insertHwElectronicFence(hwElectronicFence);
    }

    /**
     * 修改电子围栏
     *
     * @param hwElectronicFence 电子围栏
     * @return 结果
     */
    @Override
    public int updateHwElectronicFence(HwElectronicFence hwElectronicFence)
    {
        hwElectronicFence.setUpdateTime(DateUtils.getNowDate());
        return hwElectronicFenceMapper.updateHwElectronicFence(hwElectronicFence);
    }

    /**
     * 批量删除电子围栏
     *
     * @param electronicFenceIds 需要删除的电子围栏主键
     * @return 结果
     */
    @Override
    public int deleteHwElectronicFenceByElectronicFenceIds(Long[] electronicFenceIds)
    {
        return hwElectronicFenceMapper.deleteHwElectronicFenceByElectronicFenceIds(electronicFenceIds);
    }

    /**
     * 删除电子围栏信息
     *
     * @param electronicFenceId 电子围栏主键
     * @return 结果
     */
    @Override
    public int deleteHwElectronicFenceByElectronicFenceId(Long electronicFenceId)
    {
        return hwElectronicFenceMapper.deleteHwElectronicFenceByElectronicFenceId(electronicFenceId);
    }
}
