package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeDiscountCouponFriendRuleDetail;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleDetailVo;
import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleVo;
import shop.alien.entity.store.vo.LifeGroupBuyThaliVo;

import java.util.List;

/**
* @author youch
* @description 针对表【life_discount_coupon_friend_rule_detail(赠券规则明细)】的数据库操作Mapper
* @createDate 2025-09-18 13:32:40
* @Entity shop.alien.entity.store.LifeDiscountCouponFriendRuleDetail
*/
public interface LifeDiscountCouponFriendRuleDetailMapper extends BaseMapper<LifeDiscountCouponFriendRuleDetail> {

    void insertList(List<LifeDiscountCouponFriendRuleDetail> lifeDiscountCouponFriendRuleDetailList);

    @Select("select si.store_name storeName,ldc.name couponName,ldc.id couponId,sum(ldcsf.single_qty) couponNum,ldcsf.friend_store_user_id friendStoreUserId from life_discount_coupon_store_friend ldcsf left join life_discount_coupon ldc on ldcsf.coupon_id = ldc.id left join store_user su on ldcsf.friend_store_user_id = su.id left join store_info si on su.store_id = si.id ${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper);

    @Select("SELECT\n" +
            "\ta.*,\n" +
            "\t(select MAX( end_get_date ) from life_discount_coupon c left join life_discount_coupon_friend_rule_detail b on c.id = b.coupon_id where b.rule_id = a.id) endDate \n" +
            "FROM\n" +
            "\tlife_discount_coupon_friend_rule a\n" +
            "\t where a.store_id = #{storeId} and a.delete_flag = 0 order by endDate")
    List<LifeDiscountCouponFriendRuleVo> getRuleList(@Param("storeId")String storeId);

    @Select("SELECT a.*, c.name as couponName, c.id as couponId,si.store_name storeName, " +
            "(SELECT\n" +
            "\t\tsum( single_qty ) \n" +
            "\tFROM\n" +
            "\t\tlife_discount_coupon_store_friend b\n" +
            "\tWHERE\n" +
            "\t\tb.friend_store_user_id = a.friend_store_user_id \n" +
            "\t\tAND b.coupon_id = a.coupon_id \n" +
            "\t\tAND b.store_user_id = a.store_id) couponNum " +
            "FROM life_discount_coupon_friend_rule_detail a " +
            "LEFT JOIN life_discount_coupon c ON a.coupon_id = c.id LEFT JOIN store_info si on c.store_id = si.id " +
            "WHERE a.rule_id = #{ruleId}")
    List<LifeDiscountCouponFriendRuleDetailVo> getDetailList(@Param("ruleId")String ruleId);
}




