package cn.iocoder.yudao.module.shop.mapper;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cn.iocoder.yudao.module.shop.entity.ShopCoupon;
import cn.iocoder.yudao.module.shop.entity.ShopCouponTemplate;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 优惠券Mapper接口
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Mapper
public interface ShopCouponMapper extends BaseMapper<ShopCoupon>
{
    /**
     * 查询优惠券
     *
     * @param couponId 优惠券主键
     * @return 优惠券
     */
    public ShopCoupon selectShopCouponByCouponId(Long couponId);

    /**
     * 查询优惠券列表
     *
     * @param shopCoupon 优惠券
     * @return 优惠券集合
     */
    public IPage<ShopCoupon> selectShopCouponList(Page<ShopCoupon> page, @Param("vo") ShopCoupon shopCoupon);

    /**
     * 新增优惠券
     *
     * @param shopCoupon 优惠券
     * @return 结果
     */
    public int insertShopCoupon(ShopCoupon shopCoupon);

    /**
     * 修改优惠券
     *
     * @param shopCoupon 优惠券
     * @return 结果
     */
    public int updateShopCoupon(ShopCoupon shopCoupon);
    int hfStatus(ShopCoupon shopCoupon);

    /**
     * 删除优惠券
     *
     * @param couponId 优惠券主键
     * @return 结果
     */
    public int deleteShopCouponByCouponId(Long couponId);

    /**
     * 批量删除优惠券
     *
     * @param couponIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteShopCouponByCouponIds(Long[] couponIds);

    IPage<ShopCoupon> selectShopCouponByUserId(Page<ShopCoupon> page, @Param("vo") ShopCoupon shopCoupon);

    IPage<ShopCoupon> availableCouponsListByUserId(Page<ShopCoupon> page, @Param("vo") ShopCoupon shopCoupon);

    IPage<ShopCoupon> unAvailableCouponsListByUserId(Page<ShopCoupon> page, @Param("vo") ShopCoupon shopCoupon);

    List<ShopCoupon> canUseCoupon(@Param("userId") Long userId, @Param("partnerId")Integer partnerId,@Param("goodsIdStr") String goodsIdStr,@Param("bindMoudle") String bindMoudle);
    Integer canUseCouponCount(@Param("userId") Long userId, @Param("partnerId")Integer partnerId,@Param("goodsIdStr") String goodsIdStr,@Param("bindMoudle") String bindMoudle);


    @Select("SELECT count(0) " +
            "FROM shop_coupon " +
            "WHERE user_id = #{userId} AND FIND_IN_SET(#{goodId}, bind_goods_ids) > 0 AND coupon_status = 0 AND now() BETWEEN start_time AND expiration_time")
    int getOffsetCouponCount(@Param("userId") Long userId, @Param("goodId") Long goodId);


    @Select("SELECT * FROM shop_coupon " +
            "WHERE user_id = #{userId} AND " +
            "(FIND_IN_SET(#{goodId}, bind_goods_ids) > 0 OR bind_moudle = '维修项目现金券') AND coupon_status = 0 AND now() BETWEEN start_time AND expiration_time " +
            "ORDER BY coupon_id DESC")
    List<ShopCoupon> getQxCouponList(@Param("userId") Long userId, @Param("goodId") Long goodId);

    @Select("SELECT * FROM shop_coupon " +
            "WHERE user_id = #{userId} AND coupon_status = 0 AND " +
            "(FIND_IN_SET(#{goodId}, bind_goods_ids) <= 0 and bind_moudle != '维修项目现金券') or NOT (now() BETWEEN start_time AND expiration_time) " +
            "ORDER BY coupon_id DESC")
    List<ShopCoupon> unGetQxCouponList(@Param("userId") Long userId, @Param("goodId") Long goodId);

    List<ShopCouponTemplate> canLedCoupon(@Param("partnerId") Long partnerId, @Param("userId") Long userId,@Param("roleKey") String roleKey);
    List<ShopCouponTemplate> canLedCouponWtr(@Param("partnerId") Long partnerId, @Param("userId") Long userId,@Param("roleKey") String roleKey);
    List<ShopCouponTemplate> canLedCouponResult(@Param("partnerId") Long partnerId, @Param("userId") Long userId,@Param("roleKey") String roleKey);

    List<ShopCouponTemplate> canLedCouponWtrResult(@Param("partnerId") Long partnerId, @Param("userId") Long userId,@Param("roleKey") String roleKey);
    List<ShopCouponTemplate> canLedCouponPlatform(@Param("userId") Long userId,@Param("roleKey") String roleKey);


}
