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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.core.device.manage.mapper.DeviceLockerCabinetMapper;
import com.bonus.core.device.manage.model.DeviceLockerCabinet;
import com.bonus.core.device.manage.vo.DeviceLockerCabinetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class DeviceLockerCabinetService {
   @Autowired
   private DeviceLockerCabinetMapper deviceLockerCabinetMapper;
   @Autowired
   private DeviceInfoService deviceInfoService;

//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void insert(List<DeviceLockerCabinetDTO> dtoList) {
//      if (!CollUtil.isEmpty(dtoList)) {
//         Iterator var2 = dtoList.iterator();
//
//         while(var2.hasNext()) {
//            DeviceLockerCabinetDTO dto = (DeviceLockerCabinetDTO)var2.next();
//            this.insert(dto);
//         }
//
//      }
//   }
//
//   public void insert(DeviceLockerCabinetDTO dto) {
//      DeviceLockerCabinet entity = dto.convertToEntity();
//      if (entity.getCabinetId() == null) {
//         entity.setCabinetId(Id.next());
//      }
//
//      this.deviceLockerCabinetMapper.insert(entity);
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void update(Long deviceId, List<DeviceLockerCabinetDTO> dtoList) {
//      this.deleteByDeviceId(deviceId);
//      if (!CollUtil.isEmpty(dtoList)) {
//         Iterator var3 = dtoList.iterator();
//
//         while(var3.hasNext()) {
//            DeviceLockerCabinetDTO dto = (DeviceLockerCabinetDTO)var3.next();
//            this.insert(dto);
//         }
//
//      }
//   }
//
//   public void deleteByDeviceId(Long deviceId) {
//      this.deviceLockerCabinetMapper.delete((Wrapper)Wrappers.lambdaQuery(DeviceLockerCabinet.class).eq(DeviceLockerCabinet::getDeviceId, deviceId));
//   }
//
//   public List<DeviceLockerCabinet> listByCabinetIds(List<Long> cabinetIds) {
//      return CollUtil.isEmpty(cabinetIds) ? Collections.emptyList() : this.deviceLockerCabinetMapper.selectList((Wrapper)Wrappers.lambdaQuery(DeviceLockerCabinet.class).in(DeviceLockerCabinet::getCabinetId, cabinetIds));
//   }

   public List<DeviceLockerCabinet> listByDeviceIds(List<Long> deviceIds) {
      return CollUtil.isEmpty(deviceIds) ? Collections.emptyList() : this.deviceLockerCabinetMapper.selectList(Wrappers
              .lambdaQuery(DeviceLockerCabinet.class).in(DeviceLockerCabinet::getDeviceId, deviceIds));
   }

   public List<DeviceLockerCabinetVO> listVoByDeviceIds(List<Long> deviceIds) {
      return this.listByDeviceIds(deviceIds).stream().map(DeviceLockerCabinetVO::of).collect(Collectors.toList());
   }

//   public Integer getLockerNum(Long deviceId) {
//      List<DeviceLockerNumVo> voList = this.listLockerNumByDeviceIds((List)(deviceId != null ? CollUtil.toList(new Long[]{deviceId}) : Collections.emptyList()));
//      return CollUtil.isNotEmpty(voList) ? ((DeviceLockerNumVo)voList.get(0)).getLockerNum() : 0;
//   }
//
//   public List<DeviceLockerNumVo> listLockerNumByDeviceIds(List<Long> deviceIds) {
//      LambdaQueryWrapper<DeviceLockerCabinet> queryWrapper = Wrappers.lambdaQuery(DeviceLockerCabinet.class);
//      if (CollUtil.isNotEmpty(deviceIds)) {
//         queryWrapper.in(DeviceLockerCabinet::getDeviceId, deviceIds);
//      }
//
//      List<DeviceLockerCabinet> cabinetList = this.deviceLockerCabinetMapper.selectList(queryWrapper);
//      Map<Long, List<DeviceLockerCabinet>> cabinetListGroupByDeviceId = (Map)cabinetList.stream().collect(Collectors.groupingBy(DeviceLockerCabinet::getDeviceId));
//      return (List)deviceIds.stream().map((s) -> {
//         int sum = ((List)cabinetListGroupByDeviceId.getOrDefault(s, Collections.emptyList())).stream().mapToInt(DeviceLockerCabinet::getLockerCount).sum();
//         DeviceLockerNumVo vo = new DeviceLockerNumVo();
//         vo.setDeviceId(s);
//         vo.setLockerNum(sum);
//         return vo;
//      }).collect(Collectors.toList());
//   }
//
//   public List<DeviceLockerCabinetVO> getDeviceLockerCabinetBySn(String deviceSn) {
//      DeviceInfo deviceInfo = this.deviceInfoService.getBySn(deviceSn);
//      LambdaQueryWrapper<DeviceLockerCabinet> queryWrapper = Wrappers.lambdaQuery(DeviceLockerCabinet.class);
//      if (ObjectUtil.isNotEmpty(deviceInfo)) {
//         queryWrapper.eq(DeviceLockerCabinet::getDeviceId, deviceInfo.getDeviceId());
//      }
//
//      List<DeviceLockerCabinet> cabinetList = this.deviceLockerCabinetMapper.selectList(queryWrapper);
//      return (List)cabinetList.stream().map(DeviceLockerCabinetVO::of).collect(Collectors.toList());
//   }
}
