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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.allocation.canteen.dto.AllocKindDTO;
import com.bonus.canteen.core.allocation.canteen.dto.AllocKindUpdateDTO;
import com.bonus.canteen.core.allocation.canteen.mapper.AllocKindMapper;
import com.bonus.canteen.core.allocation.canteen.model.AllocKind;
import com.bonus.canteen.core.allocation.canteen.param.AllocOtherPageParam;
import com.bonus.canteen.core.allocation.canteen.service.AllocKindService;
import com.bonus.canteen.core.allocation.canteen.vo.AllocKindVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.i18n.I18n;
import com.bonus.common.houqin.utils.LeBeanUtil;
import com.bonus.common.houqin.utils.id.Id;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.util.*;

@Service
public class AllocKindServiceImpl extends ServiceImpl<AllocKindMapper, AllocKind> implements AllocKindService {
   private static final Logger log = LoggerFactory.getLogger(AllocKindServiceImpl.class);

   public Page<AllocKindVO> pageKind(AllocOtherPageParam param) {
      Page<AllocKindVO> page = new Page(param.getCurrent(), param.getSize());
      String kindName = LeBeanUtil.fieldLikeHandle(param.getKindName());
      page = this.baseMapper.pageKind(page, kindName);
      if (CollUtil.isNotEmpty(page.getRecords())) {
         Iterator var4 = page.getRecords().iterator();

         while(var4.hasNext()) {
            AllocKindVO allocKindVO = (AllocKindVO)var4.next();
            AllocKind allocKind = this.baseMapper.getTimeByName(allocKindVO.getKindName());
            allocKindVO.setUrlAndTime(allocKind);
         }
      }

      return page;
   }

   public List<AllocKind> listKindName() {
      return this.baseMapper.listKindName();
   }

   public List<AllocKind> listKindNameImg() {
      return this.baseMapper.listKindNameImg();
   }

   public void addKind(AllocKindDTO kindDTO) {
      this.checkKindName(kindDTO.getKindName());
      AllocKind allocKind = new AllocKind();
      allocKind.setKindId(Id.next());
      allocKind.setKindName(kindDTO.getKindName());
      allocKind.setImgUrl(kindDTO.getImgUrl());
      this.save(allocKind);
   }

   public void saveStallKind(List<String> kindNameList, Long stallId) {
      this.remove(Wrappers.lambdaUpdate(AllocKind.class).eq(AllocKind::getStallId, stallId));
      if (!CollUtil.isEmpty(kindNameList)) {
         List<AllocKind> kindList = ((AllocKindMapper)this.baseMapper).listKindByNameList(kindNameList);
         Map<String, AllocKind> kindMap = new HashMap();
         kindList.forEach((kind) -> {
            kindMap.put(kind.getKindName(), kind);
         });
         List<AllocKind> allocKindList = new ArrayList();
         Iterator var6 = kindMap.entrySet().iterator();

         while(var6.hasNext()) {
            Map.Entry<String, AllocKind> entry = (Map.Entry)var6.next();
            allocKindList.add(new AllocKind(Id.next(), (AllocKind)entry.getValue(), stallId));
         }

         this.saveBatch(allocKindList);
      }
   }

   private void checkKindName(String kindName) {
      Long repeatNum = ((AllocKindMapper)this.baseMapper).countForRepeatCheck(kindName);
      if (repeatNum > 0L) {
         throw new ServiceException(I18n.getMessage("alloc_kind_check_name", new Object[]{kindName}));
      }
   }

   public void modifyKind(AllocKindUpdateDTO kindDTO) {
      if (!kindDTO.getKindName().equals(kindDTO.getOldKindName())) {
         this.checkKindName(kindDTO.getKindName());
      }

      this.update(Wrappers.lambdaUpdate(AllocKind.class).set(AllocKind::getKindName, kindDTO.getKindName()).set(AllocKind::getImgUrl, kindDTO.getImgUrl()).eq(AllocKind::getKindName, kindDTO.getOldKindName()));
   }

   public void removeKind(String kindName) {
      Long stallCount = this.count(Wrappers.lambdaUpdate(AllocKind.class).eq(AllocKind::getKindName, kindName).isNotNull(AllocKind::getStallId));
      if (stallCount > 0L) {
         throw new ServiceException(I18n.getMessage("alloc_kind_remove_check_relate", new Object[0]));
      } else {
         this.remove(Wrappers.lambdaUpdate(AllocKind.class).eq(AllocKind::getKindName, kindName));
      }
   }

   public void removeStallKind(Long stallId) {
      this.remove(Wrappers.lambdaUpdate(AllocKind.class).eq(AllocKind::getStallId, stallId));
   }
}
