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

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.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.mapper.AllocLabelMapper;
import com.bonus.canteen.core.allocation.canteen.model.AllocLabel;
import com.bonus.canteen.core.allocation.canteen.param.AllocOtherPageParam;
import com.bonus.canteen.core.allocation.canteen.service.AllocLabelService;
import com.bonus.canteen.core.allocation.canteen.vo.AllocLabelVO;
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.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AllocLabelServiceImpl extends ServiceImpl<AllocLabelMapper, AllocLabel> implements AllocLabelService {
   private static final Logger log = LoggerFactory.getLogger(AllocLabelServiceImpl.class);

   public Page<AllocLabelVO> pageLabel(AllocOtherPageParam param) {
      Page<AllocLabelVO> page = new Page(param.getCurrent(), param.getSize());
      String labelName = LeBeanUtil.fieldLikeHandle(param.getLabelName());
      page = ((AllocLabelMapper)this.baseMapper).pageLabel(page, labelName);
      if (CollUtil.isNotEmpty(page.getRecords())) {
         Iterator var4 = page.getRecords().iterator();

         while(var4.hasNext()) {
            AllocLabelVO allocLabelVO = (AllocLabelVO)var4.next();
            AllocLabel allocLabel = ((AllocLabelMapper)this.baseMapper).getTimeByName(allocLabelVO.getLabelName());
            allocLabelVO.setTime(allocLabel);
         }
      }

      return page;
   }

//   public List<String> listLabelName() {
//      return ((AllocLabelMapper)this.baseMapper).listLabelName();
//   }
//
//   public List<String> listRelationLabelName(AllocCanteenStallParam param) {
//      return (List)(ObjectUtil.isNull(param.getCanteenId()) && ObjectUtil.isNull(param.getStallId()) ? new ArrayList() : ((AllocLabelMapper)this.baseMapper).listRelationLabelName(param.getCanteenId(), param.getStallId()));
//   }
//
//   public void addLabel(String labelName) {
//      this.checkLabelName(labelName);
//      this.save(new AllocLabel(Id.next(), labelName));
//   }

   public void saveRelationLabel(Long canteenId, Long stallId, List<String> labelNameList) {
      if (ObjectUtil.isNull(canteenId) && ObjectUtil.isNull(stallId)) {
         throw new ServiceException(I18n.getMessage("alloc_label_relate_check", new Object[0]));
      } else {
         this.remove(Wrappers.lambdaQuery(AllocLabel.class)
                 .eq(ObjectUtil.isNotNull(canteenId), AllocLabel::getCanteenId, canteenId)
                 .eq(ObjectUtil.isNotNull(stallId), AllocLabel::getStallId, stallId));
         if (!CollUtil.isEmpty(labelNameList)) {
            List<AllocLabel> allocLabelList = labelNameList.stream().map((item) -> {
               return new AllocLabel(Id.next(), item, canteenId, stallId);
            }).collect(Collectors.toList());
            this.saveBatch(allocLabelList);
         }
      }
   }
//
//   private void checkLabelName(String labelName) {
//      Long repeatNum = ((AllocLabelMapper)this.baseMapper).countForRepeatCheck(labelName);
//      if (repeatNum > 0L) {
//         throw new LeException(I18n.getMessage("alloc_label_name_repeat", new Object[0]));
//      }
//   }
//
//   public void updateLabel(AllocLabelDTO labelDTO) {
//      if (!labelDTO.getLabelName().equals(labelDTO.getOldLabelName())) {
//         this.checkLabelName(labelDTO.getLabelName());
//      }
//
//      this.update((Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(AllocLabel.class).set(AllocLabel::getLabelName, labelDTO.getLabelName())).eq(AllocLabel::getLabelName, labelDTO.getOldLabelName()));
//   }
//
//   public void removeLabel(String labelName) {
//      Long canteenNum = this.count((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocLabel.class).eq(AllocLabel::getLabelName, labelName)).isNotNull(AllocLabel::getCanteenId));
//      if (canteenNum > 0L) {
//         throw new LeException(I18n.getMessage("alloc_label_remove_check_canteen", new Object[0]));
//      } else {
//         Long stallNum = this.count((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(AllocLabel.class).eq(AllocLabel::getLabelName, labelName)).isNotNull(AllocLabel::getStallId));
//         if (stallNum > 0L) {
//            throw new LeException(I18n.getMessage("alloc_label_remove_check_stall", new Object[0]));
//         } else {
//            this.remove((Wrapper)Wrappers.lambdaQuery(AllocLabel.class).eq(AllocLabel::getLabelName, labelName));
//         }
//      }
//   }

   public void removeLabelRelation(Long canteenId, Long stallId) {
      this.remove(Wrappers.lambdaQuery(AllocLabel.class).eq(ObjectUtil.isNotNull(canteenId),
              AllocLabel::getCanteenId, canteenId).eq(ObjectUtil.isNotNull(stallId), AllocLabel::getStallId, stallId));
   }
}
