package com.bonus.canteen.core.bus.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.bus.constant.BusDeviceSceneEnum;
import com.bonus.canteen.core.bus.dto.BusDeviceInfoDTO;
import com.bonus.canteen.core.bus.dto.BusDeviceInfoEditDTO;
import com.bonus.canteen.core.bus.mapper.BusDeviceInfoMapper;
import com.bonus.canteen.core.bus.model.BusDeviceInfo;
import com.bonus.canteen.core.bus.model.BusLine;
import com.bonus.canteen.core.bus.param.BusDeviceInfoParam;
import com.bonus.canteen.core.bus.service.BusDeviceInfoService;
import com.bonus.canteen.core.bus.service.BusLineService;
import com.bonus.canteen.core.bus.vo.BusDeviceInfoVO;
import com.bonus.canteen.core.common.enums.SourceTypeEnum;
import com.bonus.canteen.core.common.page.PageDTO;
import com.bonus.canteen.core.device.manage.dto.DeviceDTO;
import com.bonus.canteen.core.device.manage.model.DeviceInfo;
import com.bonus.canteen.core.device.manage.service.DeviceInfoService;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.common.houqin.i18n.I18n;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

@Service
public class BusDeviceInfoServiceImpl extends ServiceImpl<BusDeviceInfoMapper, BusDeviceInfo> implements BusDeviceInfoService {
   private static final Logger log = LoggerFactory.getLogger(BusDeviceInfoServiceImpl.class);
   @Autowired
   @Lazy
   private BusDeviceInfoMapper busDeviceInfoMapper;
   @Autowired
   @Lazy
   private BusLineService busLineService;
   @Autowired
   @Lazy
   private DeviceInfoService deviceInfoService;

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void createBusDeviceInfo(BusDeviceInfoDTO content) {
      this.checkSceneLineType(content.getDeviceType(), content.getUseScene(), Long.valueOf(content.getLineId()));
      DeviceDTO deviceDTO = (DeviceDTO)BeanUtil.copyProperties(content, DeviceDTO.class, new String[0]);
      this.deviceInfoService.insert(deviceDTO);
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void cancelBusDeviceInfo(List<Long> content) {
      if (!CollUtil.isEmpty(content)) {
         Iterator var2 = content.iterator();

         while(var2.hasNext()) {
            Long deviceId = (Long)var2.next();
            DeviceInfo deviceInfo = this.deviceInfoService.getById(deviceId);
            if (ObjectUtil.isNotNull(deviceInfo)) {
               this.deviceInfoService.delete(deviceId);
            }
         }

      }
   }

   public void editBusDeviceInfo(BusDeviceInfoEditDTO content) {
      if (!SourceTypeEnum.BUS_DEVICE.getKey().equals(content.getDeviceType())) {
         throw new ServiceException(I18n.getMessage("bus_device_info_type_check", new Object[0]));
      } else {
         DeviceDTO deviceDTO = (DeviceDTO)BeanUtil.copyProperties(content, DeviceDTO.class, new String[0]);
         this.deviceInfoService.update(deviceDTO);
      }
   }

   public Page<BusDeviceInfoVO> pageBusDeviceInfo(BusDeviceInfoParam content) {
      PageDTO page = content.getPage();
      String keyword = content.getKeyword();
      return this.busDeviceInfoMapper.queryPageBusDeviceInfo(Page.of(page.getCurrent(), page.getSize()), keyword);
   }

   public BusDeviceInfoVO queryBusDeviceInfoDetail(Long content) {
      return ObjectUtil.isEmpty(content) ? new BusDeviceInfoVO() : this.busDeviceInfoMapper.queryBusDeviceInfoDetail(content);
   }

   private void checkSceneLineType(Integer deviceType, Integer useScene, Long lineId) {
      if (SourceTypeEnum.BUS_DEVICE.getKey().equals(deviceType) && BusDeviceSceneEnum.getDescKeyMap().containsKey(useScene)) {
         long count = this.busLineService.count((Wrappers.lambdaQuery(BusLine.class)
                 .eq(BusLine::getLineId, lineId)).eq(BusLine::getDelFlag, LeConstants.COMMON_NO));
         if (0L >= count) {
            throw new ServiceException(I18n.getMessage("bus_line_info_not_exist", new Object[0]));
         }
      } else {
         throw new ServiceException(I18n.getMessage("bus_device_info_type_check", new Object[0]));
      }
   }
}
