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.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import shop.alien.entity.store.ActivityInviteLog;
import shop.alien.entity.store.vo.ActivityInviteLogVo;

/**
 * 邀请活动记录mapper
 */
@Mapper
public interface ActivityInviteLogMapper extends BaseMapper<ActivityInviteLog> {

    @Select("SELECT " +
            "  ail.*," +
            " lud.real_name as invitedRealName," +
            " lud.user_name as invitedNickName," +
            " lu.user_phone AS invitePhone," +
            " lu.real_name as inviteRealName," +
            " lu.user_name as inviteNickName," +
            " lu1.user_phone AS invitedPhone," +
            " lu1.user_image," +
            " lu1.user_name AS invitedNickName," +
            " CASE " +
            " WHEN ail.invite_reward_type = 1 AND lc_invite.id IS NOT NULL THEN " +
            " CONCAT('优惠券：', lc_invite.name, '（', IFNULL(lc_invite.offprice, lc_invite.price), '元）') " +
            " WHEN ail.invite_reward_type = 2 AND ail.invite_reward_point IS NOT NULL THEN " +
            " CONCAT('积分：', ail.invite_reward_point, '分') " +
            " ELSE '' " +
            " END AS inviteRewardInfo, " +
            " CASE " +
            " WHEN ail.invited_reward_type = 1 AND lc_invited.id IS NOT NULL THEN " +
            " CONCAT('优惠券：', lc_invited.name, '（', IFNULL(lc_invited.offprice, lc_invited.price), '元）') " +
            " WHEN ail.invited_reward_type = 2 AND ail.invited_reward_point IS NOT NULL THEN " +
            " CONCAT('积分：', ail.invited_reward_point, '分') " +
            " ELSE '' " +
            " END AS invitedRewardInfo " +
            " FROM activity_invite_log ail " +
            " LEFT JOIN life_user lu ON lu.id = ail.invite_user_id " +
            " AND lu.delete_flag = 0 " +
            " LEFT JOIN life_user lud ON lud.id = ail.invited_user_id " +
            " AND lud.delete_flag = 0 " +
            " LEFT JOIN life_user lu1 ON lu1.id = ail.invited_user_id AND lu1.delete_flag = 0 " +
            " LEFT JOIN life_coupon lc_invite ON lc_invite.id = ail.invite_reward_coupon AND lc_invite.delete_flag = 0 " +
            " LEFT JOIN life_coupon lc_invited ON lc_invited.id = ail.invited_reward_coupon AND lc_invited.delete_flag = 0 " +
            "${ew.customSqlSegment}")
    IPage<ActivityInviteLogVo> getInviteActivityLogList(IPage<ActivityInviteLogVo> iPage, @Param(Constants.WRAPPER) QueryWrapper<ActivityInviteLogVo> wrapper);
}