package shop.alien.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.LifeDiscountCouponStoreFriend;
import shop.alien.entity.store.vo.LifeDiscountCouponFriendRuleVo;
import shop.alien.entity.store.vo.LifeDiscountCouponStoreFriendVo;
import shop.alien.entity.store.vo.LifeDiscountCouponVo;

import java.util.List;

/**
 * <p>
 * 优惠券商户发放好友优惠券关系表	 Mapper 接口
 * </p>
 *
 * @author ssk
 * @since 2025-02-19
 */
public interface LifeDiscountCouponStoreFriendMapper extends BaseMapper<LifeDiscountCouponStoreFriend> {

    @Select("select ldc.store_id,ldc.start_date,ldc.end_date,\n" +
            "ldc.begin_get_date,ldc.end_get_date,ldc.coupon_status,\n" +
            "ldc.long_term_valid,ldc.unlimited_qty,\n" +
            "si.store_name,\n" +
            "ldc.name,ldc.get_status,\n" +
            "ldcsf.single_qty,ldcsf.coupon_id,ldcsf.release_type,ldcsf.id give_coupon_id\n" +
            "from  life_discount_coupon_store_friend ldcsf\n" +
            "left join life_discount_coupon ldc\n" +
            "on ldc.id = ldcsf.coupon_id\n" +
            "left join store_info si\n" +
            "on si.id = ldc.store_id and si.delete_flag = 0\n" +
            "${ew.customSqlSegment}")
    IPage<LifeDiscountCouponVo> selectPage(IPage<LifeDiscountCouponStoreFriendVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponStoreFriendVo> friendLifeDiscountCouponQueryWrapper);

    /** 好友赠我 - 优惠券 */
    @Select("select ldc.valid_date endDate,ldc.valid_date validDate,ldc.nominal_value nominalValue,ldc.minimum_spending_amount minimumSpendingAmount,\n" +
            "si.store_name storeName,\n" +
            "si.id storeId,\n" +
            "ldc.name couponName,\n" +
            "ldc.id couponId,\n" +
            "ldcsf.single_qty couponNum,\n" +
            "1 as type,\n" +
            "null as voucherId,\n" +
            "ldc.coupon_type couponType,\n" +
            "ldc.discount_rate discountRate,\n" +
            "ldc.begin_get_date beginGetDate,\n" +
            "ldc.end_get_date endGetDate,\n" +
            "ldc.expiration_date expirationDate,\n" +
            "COALESCE(ldc.long_term_valid, ldcsf.long_term_valid) longTermValid,\n" +
            "COALESCE(ldcsf.sender_user_id, ldcsf.friend_store_user_id) counterpartyStoreUserId\n" +
            "from  life_discount_coupon_store_friend ldcsf\n" +
            "left join life_discount_coupon ldc\n" +
            "on ldc.id = ldcsf.coupon_id\n" +
            "left join store_user su_cp on su_cp.id = COALESCE(ldcsf.sender_user_id, ldcsf.friend_store_user_id) and su_cp.delete_flag = 0\n" +
            "left join store_info si\n" +
            "on si.id = COALESCE(ldcsf.sender_store_id, su_cp.store_id, CAST(ldc.store_id AS UNSIGNED)) and si.delete_flag = 0\n" +
            "${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponList(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);

    /** 好友赠我 - 代金券(type=4) */
    @Select("select lc.end_date endDate,lc.end_date validDate,CAST(lc.price AS DECIMAL(10,2)) nominalValue,null minimumSpendingAmount,\n" +
            "si.store_name storeName,\n" +
            "si.id storeId,\n" +
            "lc.name couponName,\n" +
            "null couponId,\n" +
            "ldcsf.single_qty couponNum,\n" +
            "4 as type,\n" +
            "lc.id voucherId,\n" +
            "null as beginGetDate,\n" +
            "null as endGetDate\n" +
            "from life_discount_coupon_store_friend ldcsf\n" +
            "left join life_coupon lc on lc.id = ldcsf.voucher_id and lc.delete_flag = 0\n" +
            "left join store_info si on CAST(si.id AS CHAR) COLLATE utf8mb4_unicode_ci = lc.store_id and si.delete_flag = 0\n" +
            "${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponListVoucher(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);

    /** 我赠好友 - 优惠券 */
    @Select("select ldc.valid_date endDate,ldc.valid_date validDate,ldc.nominal_value nominalValue,ldc.minimum_spending_amount minimumSpendingAmount,\n" +
            "si.store_name storeName,\n" +
            "si.id storeId,\n" +
            "ldc.name couponName,\n" +
            "ldc.id couponId,\n" +
            "ldcsf.single_qty couponNum,\n" +
            "1 as type,\n" +
            "null as voucherId,\n" +
            "ldc.coupon_type couponType,\n" +
            "ldc.discount_rate discountRate,\n" +
            "ldc.begin_get_date beginGetDate,\n" +
            "ldc.end_get_date endGetDate,\n" +
            "ldc.expiration_date expirationDate,\n" +
            "COALESCE(ldc.long_term_valid, ldcsf.long_term_valid) longTermValid,\n" +
            "ldcsf.receiver_user_id counterpartyStoreUserId\n" +
            "from  life_discount_coupon_store_friend ldcsf\n" +
            "left join life_discount_coupon ldc\n" +
            "on ldc.id = ldcsf.coupon_id\n" +
            "left join store_user su_cp on su_cp.id = ldcsf.receiver_user_id and su_cp.delete_flag = 0\n" +
            "left join store_info si\n" +
            "on si.id = COALESCE(ldcsf.receiver_store_id, su_cp.store_id, ldcsf.store_user_id) and si.delete_flag = 0\n" +
            "${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponListwzhy(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);

    /** 我赠好友 - 代金券(type=4) */
    @Select("select lc.end_date endDate,lc.end_date validDate,CAST(lc.price AS DECIMAL(10,2)) nominalValue,null minimumSpendingAmount,\n" +
            "si.store_name storeName,\n" +
            "si.id storeId,\n" +
            "lc.name couponName,\n" +
            "null couponId,\n" +
            "ldcsf.single_qty couponNum,\n" +
            "4 as type,\n" +
            "lc.id voucherId,\n" +
            "null as beginGetDate,\n" +
            "null as endGetDate\n" +
            "from life_discount_coupon_store_friend ldcsf\n" +
            "left join life_coupon lc on lc.id = ldcsf.voucher_id and lc.delete_flag = 0\n" +
            "left join store_user su\n" +
            "on COALESCE(ldcsf.sender_user_id, ldcsf.friend_store_user_id) = su.id and su.delete_flag = 0\n" +
            "left join store_info si\n" +
            "on si.id = su.store_id and si.delete_flag = 0\n" +
            "${ew.customSqlSegment}")
    List<LifeDiscountCouponFriendRuleVo> getReceivedSendFriendCouponListwzhyVoucher(@Param(Constants.WRAPPER) QueryWrapper<LifeDiscountCouponFriendRuleVo> lifeDiscountCouponFriendRuleVoQueryWrapper);
}
