package com.bonus.core.device.manage.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.core.device.manage.mapper.DeviceControlRelationMapper;
import com.bonus.core.device.manage.model.DeviceControlRelation;
import com.bonus.core.device.manage.model.DeviceInfo;
import com.bonus.core.device.manage.vo.DeviceControlRelationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DeviceControlRelationService {
   @Autowired
   private DeviceControlRelationMapper deviceControlRelationMapper;
   @Autowired
   @Lazy
   private DeviceInfoService deviceInfoService;

//   private void insert(DeviceControlRelationDTO dto) {
//      DeviceControlRelation entity = dto.convertToEntity();
//      if (entity.getId() == null) {
//         entity.setId(Id.next());
//      }
//
//      this.deviceControlRelationMapper.insert(entity);
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void insert(List<DeviceControlRelationDTO> dtoList) {
//      if (!CollUtil.isEmpty(dtoList)) {
//         Iterator var2 = dtoList.iterator();
//
//         while(var2.hasNext()) {
//            DeviceControlRelationDTO dto = (DeviceControlRelationDTO)var2.next();
//            this.insert(dto);
//         }
//
//      }
//   }
//
//   @Transactional
//   public void update(Long deviceId, List<DeviceControlRelationDTO> dtoList) {
//      if (!CollUtil.isEmpty(dtoList)) {
//         this.deleteByDeviceId(deviceId);
//         this.insert(dtoList);
//      }
//   }
//
//   public void deleteByDeviceId(Long deviceId) {
//      this.deviceControlRelationMapper.delete((Wrapper)Wrappers.lambdaQuery(DeviceControlRelation.class).eq(DeviceControlRelation::getControlDeviceId, deviceId));
//   }

   public List<DeviceControlRelation> listByDeviceIds(List<Long> deviceIds) {
      return (CollUtil.isEmpty(deviceIds) ? CollUtil.newArrayList(new DeviceControlRelation[0]) :
              this.deviceControlRelationMapper.selectList(Wrappers.lambdaQuery(DeviceControlRelation.class)
                      .in(DeviceControlRelation::getControlDeviceId, deviceIds)));
   }

   public List<DeviceControlRelationVO> listVoByDeviceIds(List<Long> deviceIds) {
      if (CollUtil.isEmpty(deviceIds)) {
         return CollUtil.newArrayList(new DeviceControlRelationVO[0]);
      } else {
         List<DeviceControlRelation> relationList = this.listByDeviceIds(deviceIds);
         if (CollUtil.isEmpty(relationList)) {
            return CollUtil.newArrayList(new DeviceControlRelationVO[0]);
         } else {
            List<Long> relateDeviceIds = relationList.stream().map(DeviceControlRelation::getRelateDeviceId).collect(Collectors.toList());
            Map<Long, DeviceInfo> relateDeviceMapById = this.deviceInfoService.listByIds(relateDeviceIds).stream()
                    .collect(Collectors.toMap(DeviceInfo::getDeviceId, Function.identity()));
            return relationList.stream().map((s) -> {
               return DeviceControlRelationVO.of(s, (DeviceInfo)relateDeviceMapById.get(s.getRelateDeviceId()));
            }).collect(Collectors.toList());
         }
      }
   }
}
