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

import cn.hutool.core.bean.BeanUtil;
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.service.impl.ServiceImpl;
import com.bonus.canteen.core.supply.domain.DrpSupplierScoreRule;
import com.bonus.canteen.core.supply.domain.DrpSupplierScoreRuleSection;
import com.bonus.canteen.core.supply.dto.DrpSupplierScoreHistPageDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierScorePageDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierScoreRuleAddDTO;
import com.bonus.canteen.core.supply.dto.DrpSupplierScoreRuleSectionAddDTO;
import com.bonus.canteen.core.supply.mapper.DrpSupplierMapper;
import com.bonus.canteen.core.supply.mapper.DrpSupplierScoreHistMapper;
import com.bonus.canteen.core.supply.mapper.DrpSupplierScoreRuleMapper;
import com.bonus.canteen.core.supply.mapper.DrpSupplierScoreRuleSectionMapper;
import com.bonus.canteen.core.supply.service.DrpSupplierScoreRuleService;
import com.bonus.canteen.core.supply.vo.DrpSupplierScoreHistPageVO;
import com.bonus.canteen.core.supply.vo.DrpSupplierScorePageVO;
import com.bonus.canteen.core.supply.vo.DrpSupplierScoreRuleListVO;
import com.bonus.canteen.core.supply.vo.DrpSupplierScoreRuleSectionVO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.DelFlagEnum;
import com.bonus.common.houqin.utils.id.Id;
import com.github.pagehelper.page.PageMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class DrpSupplierScoreRuleServiceImpl extends ServiceImpl<DrpSupplierScoreRuleMapper, DrpSupplierScoreRule> implements DrpSupplierScoreRuleService {
   @Autowired
   @Lazy
   private DrpSupplierScoreRuleSectionMapper drpSupplierScoreRuleSectionMapper;
   @Autowired
   @Lazy
   private DrpSupplierMapper drpSupplierMapper;
   @Autowired
   @Lazy
   private DrpSupplierScoreHistMapper drpSupplierScoreHistMapper;

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void addOrEditSupplierScoreRule(List<DrpSupplierScoreRuleAddDTO> content) {
      BigDecimal totalScore = (BigDecimal)content.stream().map(DrpSupplierScoreRuleAddDTO::getSetScore).reduce(BigDecimal.ZERO, BigDecimal::add);
      if (totalScore.compareTo(new BigDecimal(100)) != 0) {
         throw new ServiceException("drp.supplier-score-need-100");
      } else {
         Long areaId = ((DrpSupplierScoreRuleAddDTO)content.get(0)).getAreaId();
         List<DrpSupplierScoreRule> drpSupplierScoreRuleList = this.baseMapper.selectList(Wrappers.lambdaQuery(DrpSupplierScoreRule.class).eq(DrpSupplierScoreRule::getDelFlag, DelFlagEnum.DEL_FALSE.key()).eq(DrpSupplierScoreRule::getAreaId, areaId));
         Iterator var5;
         Iterator var9;
         if (CollUtil.isEmpty(drpSupplierScoreRuleList)) {
            var5 = content.iterator();

            while(true) {
               DrpSupplierScoreRuleAddDTO drpSupplierScoreRuleAddDTO;
               Long ruleId;
               do {
                  if (!var5.hasNext()) {
                     return;
                  }

                  drpSupplierScoreRuleAddDTO = (DrpSupplierScoreRuleAddDTO)var5.next();
                  DrpSupplierScoreRule drpSupplierScoreRule = new DrpSupplierScoreRule();
                  BeanUtil.copyProperties(drpSupplierScoreRuleAddDTO, drpSupplierScoreRule, new String[0]);
                  ruleId = Id.next();
                  drpSupplierScoreRule.setRuleId(ruleId);
                  ((DrpSupplierScoreRuleMapper)this.baseMapper).insert(drpSupplierScoreRule);
               } while(CollUtil.isEmpty(drpSupplierScoreRuleAddDTO.getSectionListDTO()));

               var9 = drpSupplierScoreRuleAddDTO.getSectionListDTO().iterator();

               while(var9.hasNext()) {
                  DrpSupplierScoreRuleSectionAddDTO drpSupplierScoreRuleSectionAddDTO = (DrpSupplierScoreRuleSectionAddDTO)var9.next();
                  DrpSupplierScoreRuleSection drpSupplierScoreRuleSection = new DrpSupplierScoreRuleSection();
                  BeanUtil.copyProperties(drpSupplierScoreRuleSectionAddDTO, drpSupplierScoreRuleSection, new String[0]);
                  drpSupplierScoreRuleSection.setRuleId(ruleId);
                  this.drpSupplierScoreRuleSectionMapper.insert(drpSupplierScoreRuleSection);
               }
            }
         } else {
            var5 = drpSupplierScoreRuleList.iterator();

            label55:
            while(var5.hasNext()) {
               DrpSupplierScoreRule drpSupplierScoreRule = (DrpSupplierScoreRule)var5.next();
               Long ruleId = drpSupplierScoreRule.getRuleId();
               boolean ifNeedDeal = true;
               var9 = content.iterator();

               while(true) {
                  DrpSupplierScoreRuleAddDTO drpSupplierScoreRuleAddDTO;
                  do {
                     do {
                        if (!var9.hasNext()) {
                           if (ifNeedDeal) {
                              this.drpSupplierScoreRuleSectionMapper.delete((Wrapper)Wrappers.lambdaQuery(DrpSupplierScoreRuleSection.class).eq(DrpSupplierScoreRuleSection::getRuleId, ruleId));
                              this.baseMapper.update(null, Wrappers.lambdaUpdate(DrpSupplierScoreRule.class).eq(DrpSupplierScoreRule::getRuleId, ruleId).set(DrpSupplierScoreRule::getDelFlag, DelFlagEnum.DEL_TRUE.key()));
                           }
                           continue label55;
                        }

                        drpSupplierScoreRuleAddDTO = (DrpSupplierScoreRuleAddDTO)var9.next();
                     } while(!ObjectUtil.equal(drpSupplierScoreRule.getRuleType(), drpSupplierScoreRuleAddDTO.getRuleType()));

                     ifNeedDeal = false;
                     DrpSupplierScoreRule updateRule = new DrpSupplierScoreRule();
                     BeanUtil.copyProperties(drpSupplierScoreRuleAddDTO, updateRule, new String[0]);
                     ((DrpSupplierScoreRuleMapper)this.baseMapper).update(updateRule, (Wrapper)Wrappers.lambdaQuery(DrpSupplierScoreRule.class).eq(DrpSupplierScoreRule::getRuleId, ruleId));
                  } while(CollUtil.isEmpty(drpSupplierScoreRuleAddDTO.getSectionListDTO()));

                  this.drpSupplierScoreRuleSectionMapper.delete((Wrapper)Wrappers.lambdaQuery(DrpSupplierScoreRuleSection.class).eq(DrpSupplierScoreRuleSection::getRuleId, ruleId));
                  Iterator var12 = drpSupplierScoreRuleAddDTO.getSectionListDTO().iterator();

                  while(var12.hasNext()) {
                     DrpSupplierScoreRuleSectionAddDTO drpSupplierScoreRuleSectionAddDTO = (DrpSupplierScoreRuleSectionAddDTO)var12.next();
                     DrpSupplierScoreRuleSection drpSupplierScoreRuleSection = new DrpSupplierScoreRuleSection();
                     BeanUtil.copyProperties(drpSupplierScoreRuleSectionAddDTO, drpSupplierScoreRuleSection, new String[0]);
                     drpSupplierScoreRuleSection.setRuleId(ruleId);
                     this.drpSupplierScoreRuleSectionMapper.insert(drpSupplierScoreRuleSection);
                  }
               }
            }

         }
      }
   }

   public List<DrpSupplierScoreRuleListVO> listSupplierScoreRule(Long areaId) {
      List<DrpSupplierScoreRule> drpSupplierScoreRuleList = this.baseMapper.selectList(Wrappers.lambdaQuery(DrpSupplierScoreRule.class).eq(DrpSupplierScoreRule::getDelFlag, DelFlagEnum.DEL_FALSE.key()).eq(DrpSupplierScoreRule::getAreaId, areaId));
      List<DrpSupplierScoreRuleListVO> resultList = BeanUtil.copyToList(drpSupplierScoreRuleList, DrpSupplierScoreRuleListVO.class);
      Iterator var4 = resultList.iterator();

      while(var4.hasNext()) {
         DrpSupplierScoreRuleListVO drpSupplierScoreRuleListVO = (DrpSupplierScoreRuleListVO)var4.next();
         List<DrpSupplierScoreRuleSection> drpSupplierScoreRuleSectionList = this.drpSupplierScoreRuleSectionMapper.selectList((Wrapper)Wrappers.lambdaQuery(DrpSupplierScoreRuleSection.class).eq(DrpSupplierScoreRuleSection::getRuleId, drpSupplierScoreRuleListVO.getRuleId()));
         List<DrpSupplierScoreRuleSectionVO> sectionVOList = BeanUtil.copyToList(drpSupplierScoreRuleSectionList, DrpSupplierScoreRuleSectionVO.class);
         drpSupplierScoreRuleListVO.setSectionVOList(sectionVOList);
      }

      return resultList;
   }

   public List<DrpSupplierScorePageVO> pageSupplierScoreRule(DrpSupplierScorePageDTO content) {
      List<DrpSupplierScorePageVO> resultList = this.drpSupplierMapper.pageSupplier(content, DelFlagEnum.DEL_FALSE.key());
      Iterator var3 = resultList.iterator();
      while(var3.hasNext()) {
         DrpSupplierScorePageVO drpSupplierScorePageVO = (DrpSupplierScorePageVO)var3.next();
         DrpSupplierScorePageVO leastScore = this.drpSupplierScoreHistMapper.getLeastScore(drpSupplierScorePageVO.getSupplierId());
         BeanUtil.copyProperties(leastScore, drpSupplierScorePageVO, new String[0]);
      }

      return resultList;
   }

   public List<DrpSupplierScoreHistPageVO> pageDrpSupplierScoreHist(DrpSupplierScoreHistPageDTO content) {
      return this.drpSupplierScoreHistMapper.pageDrpSupplierScoreHist(content);
   }

   // $FF: synthetic method
}
