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

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.allocation.canteen.mapper.AllocMealLineMapper;
import com.bonus.canteen.core.allocation.canteen.model.AllocMealLine;
import com.bonus.canteen.core.allocation.canteen.service.AllocMealLineService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.util.Iterator;
import java.util.List;

@Service
public class AllocMealLineServiceImpl extends ServiceImpl<AllocMealLineMapper, AllocMealLine> implements AllocMealLineService {
//   private static final Logger log = LoggerFactory.getLogger(AllocMealLineServiceImpl.class);
//   @Autowired
//   @Lazy
//   private AllocAreaService allocAreaService;
//   @Resource
//   private AesEncryptUtil aesEncryptUtil;
//   @Resource
//   private AllocStallMapper allocStallMapper;
//   @Autowired
//   @Lazy
//   private DeviceApi deviceApi;
//   private static final String CACHE_PREFIX = "yst:";
//   private static final String CACHE_KEY = "alloc-mealline";
//   private static final long CACHE_SECONDS = 3600L;
//
//   public String getLatestStallNum(Long superId) {
//      String superNum = this.allocStallMapper.getStallNumById(superId);
//      if (StrUtil.isBlank(superNum)) {
//         superNum = "01";
//      }
//
//      String stallNum = ((AllocMealLineMapper)this.baseMapper).getLatestMealLineNum(superId);
//      return LeNumUtil.getCanteenNum(superNum, stallNum, true);
//   }
//
//   public List<AllocMealLineVO> listMultipleMealLine(AllocCanteenStallParam param) {
//      return ((AllocMealLineMapper)this.baseMapper).listMultipleMealLine(param.getStallIdList());
//   }
//
//   public Page<AllocMealLineVO> pageMealLine(AllocCanteenStallPageParam param) {
//      param.setMealLineName(LeBeanUtil.fieldLikeHandle(param.getMealLineName()));
//      param.setIfDel(LeConstants.COMMON_NO);
//      Page<AllocMealLineVO> page = new Page(param.getCurrent(), param.getSize());
//      if (ObjectUtil.isNotNull(param.getAreaId())) {
//         param.setAreaIdList(this.allocAreaService.getAllChildrenId(param.getAreaId()));
//      }
//
//      page = ((AllocMealLineMapper)this.baseMapper).pageMealLine(page, param);
//      Iterator var3 = page.getRecords().iterator();
//
//      while(var3.hasNext()) {
//         AllocMealLineVO mealLine = (AllocMealLineVO)var3.next();
//         mealLine.setContactTel(this.aesEncryptUtil.aesEncrypt(mealLine.getContactTel()));
//      }
//
//      return page;
//   }
//
//   public void addMealLine(AllocMealLineAddDTO addDTO) {
//      this.checkMealLineNameExist(addDTO.getMealLineName(), (Long)null);
//      AllocStall allocStall = (AllocStall)this.allocStallMapper.selectOne((Wrapper)Wrappers.lambdaQuery(AllocStall.class).eq(AllocStall::getStallId, addDTO.getStallId()));
//      AllocMealLine mealLine = new AllocMealLine(Id.next());
//      BeanUtils.copyProperties(addDTO, mealLine);
//      mealLine.setAreaId(allocStall.getAreaId());
//      mealLine.setContactTel(this.aesEncryptUtil.aesDecode(mealLine.getContactTel()));
//      this.save(mealLine);
//      this.clearCache();
//   }
//
//   public void modifyMealLine(AllocMealLineModifyDTO modifyDTO) {
//      this.checkMealLineNameExist(modifyDTO.getMealLineName(), modifyDTO.getMealLineId());
//      modifyDTO.setContactTel(this.aesEncryptUtil.aesDecode(modifyDTO.getContactTel()));
//      AllocStall allocStall = (AllocStall)this.allocStallMapper.selectOne((Wrapper)Wrappers.lambdaQuery(AllocStall.class).eq(AllocStall::getStallId, modifyDTO.getStallId()));
//      this.update((Wrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(AllocMealLine.class).set(AllocMealLine::getMealLineName, modifyDTO.getMealLineName())).set(AllocMealLine::getMealLineType, modifyDTO.getMealLineType())).set(AllocMealLine::getCanteenId, allocStall.getCanteenId())).set(AllocMealLine::getStallId, allocStall.getStallId())).set(AllocMealLine::getAreaId, allocStall.getAreaId())).set(AllocMealLine::getCustId, modifyDTO.getCustId())).set(AllocMealLine::getContactTel, modifyDTO.getContactTel())).set(AllocMealLine::getImgUrl, modifyDTO.getImgUrl())).eq(AllocMealLine::getMealLineId, modifyDTO.getMealLineId()));
//      this.clearCache();
//   }
//
//   public void removeMealLine(Long mealLineId) {
//      if (this.deviceApi.checkCanteenStallRelation((Long)null, (Long)null, mealLineId)) {
//         throw new LeException(I18n.getMessage("alloc_meal_line_remove_check_device", new Object[0]));
//      } else {
//         this.update((Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(AllocMealLine.class).set(AllocMealLine::getIfDel, LeConstants.COMMON_YES)).eq(AllocMealLine::getMealLineId, mealLineId));
//         this.clearCache();
//      }
//   }
//
//   private void checkMealLineNameExist(String mealLineName, Long mealLineId) {
//      boolean flag = this.count((Wrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery().eq(AllocMealLine::getMealLineName, mealLineName)).ne(ObjectUtil.isNotNull(mealLineId), AllocMealLine::getMealLineId, mealLineId)).eq(AllocMealLine::getIfDel, LeConstants.COMMON_NO)) > 0L;
//      if (flag) {
//         throw new LeException(I18n.getMessage("alloc_meal_line_name_exist", new Object[0]));
//      }
//   }
//
//   private String getCacheKey(Long mealLineId) {
//      Long var10000 = TenantContextHolder.getTenantId();
//      return "yst:" + var10000 + ":alloc-mealline:" + mealLineId;
//   }
//
//   private void clearCache() {
//      RedisUtil.deleteByPattern("yst:" + TenantContextHolder.getTenantId() + ":alloc-mealline:*");
//   }
}
