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 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,ldc.coupon_type couponType,sum(ldcsf.single_qty) couponNum,COALESCE(ldcsf.sender_user_id, 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 COALESCE(ldcsf.sender_user_id, ldcsf.friend_store_user_id) = su.id left join store_info si on si.id = COALESCE(ldcsf.sender_store_id, su.store_id) and si.delete_flag = 0 ${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendCouponList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper);

    /** 查询收到的代金券列表（life_coupon，voucher_id 不为空） */
    @Select("select si.store_name storeName,lc.name couponName,lc.id voucherId,sum(ldcsf.single_qty) couponNum,COALESCE(ldcsf.sender_user_id, ldcsf.friend_store_user_id) friendStoreUserId from life_discount_coupon_store_friend ldcsf left join life_coupon lc on ldcsf.voucher_id = lc.id left join store_user su on COALESCE(ldcsf.sender_user_id, ldcsf.friend_store_user_id) = su.id left join store_info si on su.store_id = si.id ${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleDetailVo> getReceivedFriendVoucherList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleDetailVo> queryWrapper);

    /** 一条 SQL 同时查规则下的优惠券(type=1)与代金券(type=4)，按 type 区分 */
    @Select("SELECT a.id, a.store_id, a.ac_name, a.money_low, a.money_high, a.delete_flag, " +
            "CASE WHEN d.voucher_id IS NOT NULL THEN 4 ELSE 1 END AS type, " +
            "COALESCE(c.end_get_date, v.end_date) AS endDate, " +
            "COALESCE(c.name, v.name) AS couponName, " +
            "c.id AS couponId, " +
            "d.voucher_id AS voucherId, " +
            "COALESCE(a.coupon_type, c.coupon_type) AS couponType, " +
            "c.discount_rate AS discountRate " +
            "FROM life_discount_coupon_friend_rule a " +
            "INNER JOIN life_discount_coupon_friend_rule_detail d ON d.rule_id = a.id " +
            "LEFT JOIN life_discount_coupon c ON d.coupon_id = c.id " +
            "LEFT JOIN life_coupon v ON d.voucher_id = v.id " +
            "WHERE a.store_id = #{storeId} AND a.delete_flag = 0 " +
            "ORDER BY endDate DESC")
    List<LifeDiscountCouponFriendRuleVo> getRuleList(@Param("storeId") String storeId);

    @Select("SELECT a.*, c.name as couponName, c.id as couponId, " +
            "c.coupon_type as couponType, " +
            "si_friend.store_name AS storeName, " +
            "(SELECT sum(single_qty) FROM life_discount_coupon_store_friend b " +
            "WHERE b.friend_store_user_id = a.friend_store_user_id AND b.coupon_id = a.coupon_id AND 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_user su ON a.friend_store_user_id = su.store_id "+
            "LEFT JOIN store_info si_friend ON su.store_id = si_friend.id " +
            "WHERE a.rule_id = #{ruleId}")
    List<LifeDiscountCouponFriendRuleDetailVo> getDetailList(@Param("ruleId")String ruleId);
}




