package com.gree.mall.miniapp.logic.promotion;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.PromotionFullPiece;
import com.gree.mall.miniapp.plus.entity.PromotionFullPieceItem;
import com.gree.mall.miniapp.plus.service.PromotionFullPieceGoodsService;
import com.gree.mall.miniapp.plus.service.PromotionFullPieceItemService;
import com.gree.mall.miniapp.plus.service.PromotionFullPieceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PromotionFullPieceLogic {


    @Autowired
    PromotionFullPieceService promotionFullPieceService;
    @Autowired
    PromotionFullPieceItemService promotionFullPieceItemService;
    @Autowired
    PromotionFullPieceGoodsService promotionFullPieceGoodsService;
    @Autowired
    CommonLogic commonLogic;

    public List<PromotionFullPieceItem> getItems() {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat();
        //当前有效的满件打折活动
        PromotionFullPiece one = promotionFullPieceService.lambdaQuery()
                .le(PromotionFullPiece::getStartTime, new Date())
                .ge(PromotionFullPiece::getEndTime, new Date())
                .eq(PromotionFullPiece::getCompanyWechatId,currentCompanyWechat.getCompanyWechatId())
                .last("limit 1").one();
        if(one == null){
            return new ArrayList<>();
        }
        //不能改变这里的排序规则，因为会1个个试探满足的条件
        List<PromotionFullPieceItem> items = promotionFullPieceItemService.lambdaQuery()
                .eq(PromotionFullPieceItem::getPromotionFullPieceId, one.getId())
                .orderByDesc(PromotionFullPieceItem::getSatisfyNum).list();
        return items;
        //shoppingCartDetail.setPromotionFullPieceItems(items);
    }




}
