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

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.service.impl.ServiceImpl;
import com.bonus.canteen.core.allocation.advise.constants.AllocFunctionTypeEnum;
import com.bonus.canteen.core.allocation.advise.mapper.AllocPageDecorationMapper;
import com.bonus.canteen.core.allocation.advise.model.AllocPageDecoration;
import com.bonus.canteen.core.allocation.advise.model.AllocPageDecorationModel;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

@Service
public class AllocPageDecorationService extends ServiceImpl<AllocPageDecorationMapper, AllocPageDecoration> {
   private static final Logger log = LoggerFactory.getLogger(AllocPageDecorationService.class);

   public List<AllocPageDecorationModel> queryPageDecoration(Integer applyPage) {
      List<AllocPageDecorationModel> decorationModelList = ((AllocPageDecorationMapper)this.baseMapper)
              .queryPageDecoration(applyPage, AllocFunctionTypeEnum.CUSTOM.getKey());
      return CollUtil.isNotEmpty(decorationModelList) ? decorationModelList : this.getInitModelList(applyPage, true);
   }

//   public List<AllocPageDecorationModel> queryFirstPageDecoration(Integer applyPage) {
//      return this.getInitModelList(applyPage, false);
//   }

   private List<AllocPageDecorationModel> getInitModelList(Integer applyPage, boolean filterFlag) {
      List<AllocPageDecorationModel> initModelList = ((AllocPageDecorationMapper)this.baseMapper)
              .queryPageDecoration(applyPage, AllocFunctionTypeEnum.FIRST_EDITION.getKey());
      if (!filterFlag) {
         return initModelList;
      } else {
         initModelList.removeIf((decorationModel) -> {
            return decorationModel.getFunctionName().contains("自定义");
         });
         return initModelList;
      }
   }
//
//   public void savePageDecoration(List<AllocPageDecorationModel> decorationModelList) {
//      if (CollUtil.isEmpty(decorationModelList)) {
//         throw new LeException(I18n.getMessage("alloc_page_decoration_no_content", new Object[0]));
//      } else {
//         ((AllocPageDecorationMapper)this.baseMapper).removeByApplyPage(CanteenConstants.PAGE_CANTEEN_EVALUATE, AllocFunctionTypeEnum.CUSTOM.getKey());
//         List<AllocPageDecoration> addList = Lists.newArrayList();
//         Iterator var3 = decorationModelList.iterator();
//
//         while(var3.hasNext()) {
//            AllocPageDecorationModel decorationModel = (AllocPageDecorationModel)var3.next();
//            AllocPageDecoration addDecoration = new AllocPageDecoration();
//            LeBeanUtil.copyProperties(decorationModel, addDecoration);
//            addDecoration.setDecorationId(Id.next());
//            addDecoration.setFunctionType(AllocFunctionTypeEnum.CUSTOM.getKey());
//            addList.add(addDecoration);
//            this.update((Wrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(AllocPageDecoration.class).set(AllocPageDecoration::getFunctionName, decorationModel.getFunctionName())).eq(AllocPageDecoration::getApplyPage, CanteenConstants.PAGE_CANTEEN_EVALUATE)).eq(AllocPageDecoration::getFunctionType, AllocFunctionTypeEnum.FIRST_EDITION.getKey())).eq(AllocPageDecoration::getFunctionId, decorationModel.getFunctionId()));
//         }
//
//         this.saveBatch(addList);
//      }
//   }
}
