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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.canteen.core.customer.utils.DelFlagEnum;
import com.bonus.canteen.core.drp.api.DrpAuthorityApi;
import com.bonus.canteen.core.drp.dto.DrpWarehousePageDTO;
import com.bonus.canteen.core.drp.mapper.DrpWarehouseFetchUserMapper;
import com.bonus.canteen.core.drp.mapper.DrpWarehouseMapper;
import com.bonus.canteen.core.drp.model.DrpWarehouse;
import com.bonus.canteen.core.drp.service.DrpWarehouseService;
import com.bonus.canteen.core.drp.vo.DrpWarehouseFetchUserVO;
import com.bonus.canteen.core.drp.vo.DrpWarehousePageVO;
import com.bonus.common.houqin.utils.AesEncryptUtil;
import com.bonus.common.houqin.utils.LeBeanUtil;
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 java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DrpWarehouseServiceImpl extends ServiceImpl<DrpWarehouseMapper, DrpWarehouse> implements DrpWarehouseService {
   private static final Logger log = LoggerFactory.getLogger(DrpWarehouseServiceImpl.class);
   @Autowired
   @Lazy
   private DrpAuthorityApi drpAuthorityApi;
//   @Autowired
//   @Lazy
//   private DrpInventoryMapper drpInventoryMapper;
   @Autowired
   @Lazy
   private DrpWarehouseFetchUserMapper drpWarehouseFetchUserMapper;

//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void addDrpWarehouse(DrpWarehouseAddDTO content) {
//      List<DrpWarehouse> check = ((DrpWarehouseMapper)this.baseMapper).selectList(((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseName, content.getWarehouseName())).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key())).select(new SFunction[]{DrpWarehouse::getWarehouseId}));
//      if (CollUtil.isNotEmpty(check)) {
//         throw new LeException(I18n.getMessage("drp.warehouse-name-have", new Object[0]));
//      } else {
//         if (content.getCanteenId() != null) {
//            List<DrpWarehouse> canteenCheck = ((DrpWarehouseMapper)this.baseMapper).selectList(((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getCanteenId, content.getCanteenId())).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key())).select(new SFunction[]{DrpWarehouse::getWarehouseId, DrpWarehouse::getWarehouseName}));
//            if (CollUtil.isNotEmpty(canteenCheck)) {
//               Stream var10002 = canteenCheck.stream().map(DrpWarehouse::getWarehouseName);
//               throw new LeException("该食堂已被" + String.valueOf(var10002.collect(Collectors.toList())) + "仓库绑定！");
//            }
//         }
//
//         DrpWarehouse warehouse = new DrpWarehouse();
//         BeanUtil.copyProperties(content, warehouse, new String[0]);
//         Long warehouseId = Id.next();
//         warehouse.setWarehouseId(warehouseId);
//         String warehouseNum = this.generateWarehouseNum();
//         warehouse.setWarehouseNum(warehouseNum);
//         ((DrpWarehouseMapper)this.baseMapper).insert(warehouse);
//         if (CollUtil.isNotEmpty(content.getFetchUserList())) {
//            content.getFetchUserList().forEach((fetchUser) -> {
//               DrpWarehouseFetchUser drpWarehouseFetchUser = new DrpWarehouseFetchUser();
//               drpWarehouseFetchUser.setWarehouseId(warehouseId);
//               drpWarehouseFetchUser.setCanteenId(fetchUser.getCanteenId());
//               drpWarehouseFetchUser.setStallId(fetchUser.getStallId());
//               drpWarehouseFetchUser.setFetchUserId(fetchUser.getFetchUserId());
//               this.drpWarehouseFetchUserMapper.insert(drpWarehouseFetchUser);
//            });
//         }
//
//         MqUtil.sendDataChange(warehouse, LeMqConstant.DataChangeType.ADD, LeMqConstant.Topic.DATA_CHANGE_WAREHOUSE);
//      }
//   }

//   protected String generateWarehouseNum() {
//      String warehouseMaxNumStr = ((DrpWarehouseMapper)this.baseMapper).selectMaxNum(DrpBusinessConstants.WAREHOUSE_NUM_LENGTH, DelFlagEnum.DEL_FALSE.key());
//      String warehouseNum;
//      if (CharSequenceUtil.isBlank(warehouseMaxNumStr)) {
//         warehouseNum = "00001";
//      } else {
//         int warehouseMaxNum = Integer.parseInt(warehouseMaxNumStr);
//         warehouseNum = String.format("%05d", warehouseMaxNum + 1);
//      }
//
//      return warehouseNum;
//   }

   @Override
   public Page<DrpWarehousePageVO> getDrpWarehousePage(DrpWarehousePageDTO content) {
      List<Long> warehouseAuthority = this.drpAuthorityApi.listWarehouseAuthority();
      List<Long> areaAuth = this.drpAuthorityApi.listAreaAuth();
      Page<DrpWarehousePageVO> page = new Page(content.getCurrent(), content.getSize());
      if (CharSequenceUtil.isNotEmpty(content.getWarehouseName())) {
         content.setWarehouseName(LeBeanUtil.fieldLikeHandle(content.getWarehouseName()));
      }

      if (CharSequenceUtil.isNotEmpty(content.getRealName())) {
         content.setRealName(content.getRealName());
      }

      Page<DrpWarehousePageVO> recordPage = ((DrpWarehouseMapper)this.baseMapper).selectAllList(page, content, DelFlagEnum.DEL_FALSE.key(), warehouseAuthority, areaAuth);
      if (CollUtil.isEmpty(recordPage.getRecords())) {
         return recordPage;
      } else {
         List<DrpWarehouseFetchUserVO> list = this.drpWarehouseFetchUserMapper.getFetchUserByWareHouseIdList(recordPage.getRecords().stream().map(DrpWarehousePageVO::getWarehouseId).collect(Collectors.toList()));
         Map<Long, List<DrpWarehouseFetchUserVO>> collect = list.stream().collect(Collectors.groupingBy(DrpWarehouseFetchUserVO::getWarehouseId));
         recordPage.getRecords().forEach((drpWarehousePageVO) -> {
            drpWarehousePageVO.setPhone(AesEncryptUtil.aesEncrypt(drpWarehousePageVO.getPhone()));
            if (collect.get(drpWarehousePageVO.getWarehouseId()) != null) {
               drpWarehousePageVO.setFetchUserList(collect.get(drpWarehousePageVO.getWarehouseId()));
            }

         });
         return recordPage;
      }
   }
//
//   public void editDrpWarehouse(DrpWarehouseEditDTO content) {
//      List<DrpWarehouse> check = ((DrpWarehouseMapper)this.baseMapper).selectList(((LambdaQueryWrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseName, content.getWarehouseName())).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key())).ne(DrpWarehouse::getWarehouseId, content.getWarehouseId())).select(new SFunction[]{DrpWarehouse::getWarehouseId}));
//      if (CollUtil.isNotEmpty(check)) {
//         throw new LeException(I18n.getMessage("drp.warehouse-name-have", new Object[0]));
//      } else {
//         if (content.getCanteenId() != null) {
//            List<DrpWarehouse> canteenCheck = ((DrpWarehouseMapper)this.baseMapper).selectList(((LambdaQueryWrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getCanteenId, content.getCanteenId())).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key())).ne(DrpWarehouse::getWarehouseId, content.getWarehouseId())).select(new SFunction[]{DrpWarehouse::getWarehouseId}));
//            if (CollUtil.isNotEmpty(canteenCheck)) {
//               throw new LeException("该食堂已被仓库绑定！");
//            }
//         }
//
//         DrpWarehouse warehouse = new DrpWarehouse();
//         BeanUtil.copyProperties(content, warehouse, new String[0]);
//         ((DrpWarehouseMapper)this.baseMapper).update(warehouse, (Wrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseId, content.getWarehouseId()));
//         if (CollUtil.isNotEmpty(content.getFetchUserList())) {
//            this.drpWarehouseFetchUserMapper.delete((Wrapper)Wrappers.lambdaQuery(DrpWarehouseFetchUser.class).eq(DrpWarehouseFetchUser::getWarehouseId, content.getWarehouseId()));
//            content.getFetchUserList().forEach((fetchUser) -> {
//               DrpWarehouseFetchUser drpWarehouseFetchUser = new DrpWarehouseFetchUser();
//               drpWarehouseFetchUser.setWarehouseId(warehouse.getWarehouseId());
//               drpWarehouseFetchUser.setCanteenId(fetchUser.getCanteenId());
//               drpWarehouseFetchUser.setStallId(fetchUser.getStallId());
//               drpWarehouseFetchUser.setFetchUserId(fetchUser.getFetchUserId());
//               this.drpWarehouseFetchUserMapper.insert(drpWarehouseFetchUser);
//            });
//         } else {
//            this.drpWarehouseFetchUserMapper.delete((Wrapper)Wrappers.lambdaQuery(DrpWarehouseFetchUser.class).eq(DrpWarehouseFetchUser::getWarehouseId, content.getWarehouseId()));
//         }
//
//      }
//   }
//
//   public void removeBywWarehouseId(Long warehouseId) {
//      if (warehouseId == null) {
//         log.info("***[库存中心_仓库管理]_删除仓库信息_传入的仓库id为空************");
//         throw new LeException(I18n.getMessage("drp.select-delete-warehouse", new Object[0]));
//      } else {
//         BigDecimal checkWarehouseInventory = this.drpInventoryMapper.checkInventoryNum(warehouseId);
//         if (ObjectUtil.isNotEmpty(checkWarehouseInventory) && checkWarehouseInventory.compareTo(BigDecimal.ZERO) > 0) {
//            throw new LeException(I18n.getMessage("drp.warehouse-inventory-not-out", new Object[0]));
//         } else {
//            DrpWarehouse drpWarehouse2 = (DrpWarehouse)((DrpWarehouseMapper)this.baseMapper).selectOne((Wrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseId, warehouseId));
//            if (drpWarehouse2 != null && drpWarehouse2.getParentId() == null) {
//               Long count = ((DrpWarehouseMapper)this.baseMapper).selectCount((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key())).eq(DrpWarehouse::getParentId, warehouseId));
//               if (count != null && count > 0L) {
//                  throw new LeException("该仓库存在分仓！");
//               }
//            }
//
//            ((DrpWarehouseMapper)this.baseMapper).update((Object)null, (Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseId, warehouseId)).set(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
//            DrpWarehouse drpWarehouse = new DrpWarehouse();
//            drpWarehouse.setWarehouseId(warehouseId);
//            MqUtil.sendDataChange(drpWarehouse, LeMqConstant.DataChangeType.REMOVE, LeMqConstant.Topic.DATA_CHANGE_WAREHOUSE);
//         }
//      }
//   }
//
//   public List<DrpWarehouseAllVO> getAllDrpWarehouse(String warehouseName, DrpWarehouseAllDTO content) {
//      List<Long> warehouseAuthority = this.drpAuthorityApi.listWarehouseAuthority();
//      List<Long> areaAuth = this.drpAuthorityApi.listAreaAuth();
//      return ((DrpWarehouseMapper)this.baseMapper).selectAllWareHouse(DelFlagEnum.DEL_FALSE.key(), DrpUseStatusEnum.ENABLE.key(), warehouseAuthority, warehouseName, areaAuth, content);
//   }
//
//   public void changeWarehouseStatus(WarehouseStatusChangeDTO content) {
//      this.update((Object)null, (Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(DrpWarehouse.class).eq(DrpWarehouse::getWarehouseId, content.getWarehouseId())).set(DrpWarehouse::getStatus, content.getStatus()));
//   }
//
//   public List<DrpWarehouseAllVO> getAllDrpWarehouseForBackDevice() {
//      return ((DrpWarehouseMapper)this.baseMapper).selectAllWareHouse(DelFlagEnum.DEL_FALSE.key(), DrpUseStatusEnum.ENABLE.key(), (List)null, (String)null, (List)null, new DrpWarehouseAllDTO());
//   }
//
//   public DrpWarehouseAllVO getWarehouseByCanteenId(Long content) {
//      DrpWarehouse drpWarehouse = (DrpWarehouse)((DrpWarehouseMapper)this.baseMapper).selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(DrpWarehouse.class).eq(DrpWarehouse::getCanteenId, content)).eq(DrpWarehouse::getDelFlag, DelFlagEnum.DEL_FALSE.key()));
//      if (drpWarehouse != null) {
//         DrpWarehouseAllVO drpWarehouseAllVO = new DrpWarehouseAllVO();
//         drpWarehouseAllVO.setWarehouseId(drpWarehouse.getWarehouseId());
//         drpWarehouseAllVO.setWarehouseName(drpWarehouse.getWarehouseName());
//         drpWarehouseAllVO.setWarehouseNum(drpWarehouse.getWarehouseNum());
//         String var10001 = drpWarehouse.getRegionProvince();
//         drpWarehouseAllVO.setAddress(var10001 + drpWarehouse.getRegionCity() + drpWarehouse.getRegionDistrict() + (drpWarehouse.getAddress() == null ? "" : drpWarehouse.getAddress()));
//         return drpWarehouseAllVO;
//      } else {
//         return null;
//      }
//   }

}
