package com.bonus.canteen.core.marketing.coupon.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;

import com.bonus.canteen.core.allocation.api.AllocCanteenApi;
import com.bonus.canteen.core.allocation.api.AllocMealtimeApi;
import com.bonus.canteen.core.allocation.api.AllocStallApi;
import com.bonus.canteen.core.marketing.coupon.constants.MktCouponTypeEnum;
import com.bonus.canteen.core.marketing.coupon.mapper.MktCouponTmplBackMapper;
import com.bonus.canteen.core.marketing.coupon.model.MktCouponTmplBack;
import com.bonus.canteen.core.marketing.coupon.service.MktCouponTmplBackService;
import com.bonus.canteen.core.marketing.coupon.vo.MktCouponTmplBackVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service
public class MktCouponTmplBackServiceImpl extends ServiceImpl<MktCouponTmplBackMapper, MktCouponTmplBack> implements MktCouponTmplBackService {
   @Resource
   private AllocCanteenApi canteenAccessApi;
   @Resource
   private AllocStallApi shopstallAccessApi;
   @Resource
   private AllocMealtimeApi mealtimeAccessApi;

   public MktCouponTmplBackVO getBackRelationName(Long backId, MktCouponTypeEnum couponTypeEnum) {
      List<MktCouponTmplBackVO> tmplBackVOList = ((MktCouponTmplBackMapper)this.baseMapper).listBackVO(backId, couponTypeEnum.getKey());
      MktCouponTmplBackVO resultVO = new MktCouponTmplBackVO();
      Set<Long> canteenIdSet = new HashSet();
      Set<Long> shopstallIdSet = new HashSet();
      Set<Long> intervalIdSet = new HashSet();
      BeanUtils.copyProperties(tmplBackVOList.get(0), resultVO);
      Iterator var8 = tmplBackVOList.iterator();

      while(var8.hasNext()) {
         MktCouponTmplBackVO tmplBackVO = (MktCouponTmplBackVO)var8.next();
         if (ObjectUtil.isNotNull(tmplBackVO.getCanteenId())) {
            canteenIdSet.add(tmplBackVO.getCanteenId());
         }

         if (ObjectUtil.isNotNull(tmplBackVO.getShopstallId())) {
            shopstallIdSet.add(tmplBackVO.getShopstallId());
         }

         if (ObjectUtil.isNotNull(tmplBackVO.getIntervalId())) {
            intervalIdSet.add(tmplBackVO.getIntervalId());
         }
      }

      if (CollUtil.isNotEmpty(canteenIdSet)) {
         resultVO.setCanteenNameStr(this.canteenAccessApi.getCanteenNameByIdSet(canteenIdSet));
      }

      if (CollUtil.isNotEmpty(shopstallIdSet)) {
         resultVO.setShopstallNameStr(this.shopstallAccessApi.getStallNameByIdSet(shopstallIdSet));
      }

      if (CollUtil.isNotEmpty(intervalIdSet)) {
         resultVO.setIntervalNameStr(this.mealtimeAccessApi.getMealtimeNameByTypeSet(intervalIdSet));
      }

      return resultVO;
   }

   public List<Long> getBackIdsByTmplId(Long tmplId) {
      return ((MktCouponTmplBackMapper)this.baseMapper).getBackIdsByTmplId(tmplId);
   }
}
