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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bonus.canteen.core.device.manage.mapper.DeviceDishesMapper;
import com.bonus.canteen.core.device.manage.model.DeviceDishes;
import com.bonus.canteen.core.device.manage.vo.DeviceDishesVO;
import com.bonus.canteen.core.menu.api.MenuDishesApi;
import com.bonus.canteen.core.menu.entity.MenuDishes;
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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class DeviceDishesService {
   @Autowired
   private DeviceDishesMapper deviceDishesMapper;
   @Autowired
   private MenuDishesApi menuDishesApi;

//   private void insert(DeviceDishesDTO dto) {
//      DeviceDishes entity = dto.convertToEntity();
//      if (entity.getId() == null) {
//         entity.setId(Id.next());
//      }
//
//      this.deviceDishesMapper.insert(entity);
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void insert(List<DeviceDishesDTO> dtoList) {
//      if (!CollUtil.isEmpty(dtoList)) {
//         Iterator var2 = dtoList.iterator();
//
//         while(var2.hasNext()) {
//            DeviceDishesDTO dto = (DeviceDishesDTO)var2.next();
//            this.insert(dto);
//         }
//
//      }
//   }
//
//   @Transactional
//   public void update(Long deviceId, List<DeviceDishesDTO> dtoList) {
//      this.deleteByDeviceId(deviceId);
//      if (!CollUtil.isEmpty(dtoList)) {
//         this.insert(dtoList);
//      }
//   }
//
//   @Transactional
//   public void update(List<DeviceDishesDTO> dtoList) {
//      if (!CollUtil.isEmpty(dtoList)) {
//         ((DeviceDishesService)SpringUtil.getBean(DeviceDishesService.class)).deleteByDeviceIds(dtoList.stream().map(DeviceDishesDTO::getDeviceId).toList());
//         ((DeviceDishesService)SpringUtil.getBean(DeviceDishesService.class)).insert(dtoList);
//      }
//   }
//
//   public void deleteByDeviceId(Long deviceId) {
//      this.deviceDishesMapper.delete((Wrapper)Wrappers.lambdaQuery(DeviceDishes.class).eq(DeviceDishes::getDeviceId, deviceId));
//   }
//
//   public void deleteByDeviceIds(List<Long> deviceIds) {
//      if (!CollUtil.isEmpty(deviceIds)) {
//         this.deviceDishesMapper.delete((Wrapper)Wrappers.lambdaQuery(DeviceDishes.class).in(DeviceDishes::getDeviceId, deviceIds));
//      }
//   }

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

   public List<DeviceDishesVO> listVoByDeviceIds(List<Long> deviceIds) {
      List<DeviceDishes> deviceDishesList = this.listByDeviceIds(deviceIds);
      if (CollUtil.isEmpty(deviceDishesList)) {
         return CollUtil.newArrayList(new DeviceDishesVO[0]);
      } else {
         List<Long> dishIds = CollUtil.map(deviceDishesList, DeviceDishes::getDishesId, true);
         Map<Long, MenuDishes> dishesMapById = CollUtil.isEmpty(dishIds) ? MapUtil.empty() :
                 this.menuDishesApi.getMenuDishesList(dishIds).stream().collect(Collectors
                         .toMap(MenuDishes::getDishesId, Function.identity()));
         return deviceDishesList.stream().map((s) -> {
            return DeviceDishesVO.of(s, (MenuDishes)dishesMapById.get(s.getDishesId()));
         }).collect(Collectors.toList());
      }
   }
}
