package com.gree.mall.manager.logic.member;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.UserCoupon;
import com.gree.mall.manager.plus.service.UserCouponService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
public class UserCouponLogic {

    @Resource
    UserCouponService userCouponService;
    @Autowired
    CommonLogic commonLogic;

    /**
     * 领取记录列表
     * @param couponName
     * @param couponType
     * @param pageNum
     * @param pageSize
     * @return
     */
    public IPage<UserCoupon> page(HttpServletRequest request, String couponName, String couponType, Integer pageNum, Integer pageSize) {
        //获取当前登录企业微信id
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        List<String> companyWechatIds = adminUser.getAdminCompanyIds();

        return userCouponService.lambdaQuery()
                .in(CollectionUtils.isNotEmpty(companyWechatIds), UserCoupon::getCompanyId , companyWechatIds)
                .like(StringUtils.isNotBlank(couponName), UserCoupon::getCouponName, couponName)
                .eq(StringUtils.isNotBlank(couponName), UserCoupon::getCouponType, couponType)
                .page(new Page<>(pageNum, pageSize));
    }
}
