package com.liquidnet.service.adam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeClaimCountDto;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeClaimUserDto;
import com.liquidnet.service.adam.dto.AdamCaomeiPerformanceIdTitleDto;
import com.liquidnet.service.adam.entity.AdamCaomeiBadge;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

public interface AdamCaomeiBadgeMapper extends BaseMapper<AdamCaomeiBadge> {
    @Select({
            "<script>",
            "select badge_id as badgeId, count(1) as claimedCount",
            "from adam_caomei_user_badge",
            "where badge_id in",
            "<foreach collection='badgeIds' item='badgeId' open='(' separator=',' close=')'>",
            "#{badgeId}",
            "</foreach>",
            "group by badge_id",
            "</script>"
    })
    List<AdamCaomeiBadgeClaimCountDto> selectClaimedCountByBadgeIds(@Param("badgeIds") List<String> badgeIds);

    @Select({
            "select ",
            "ifnull(ui.nickname,'') as nickname,",
            "case ",
            "when u.mobile is null then '' ",
            "when length(u.mobile) >= 11 then concat(substring(u.mobile,1,3),'****',substring(u.mobile,8,4)) ",
            "else u.mobile end as mobile,",
            "ub.created_at as claimedAt,",
            "ub.source as source ",
            "from adam_caomei_user_badge ub ",
            "left join adam_user_info ui on ub.user_id = ui.uid ",
            "left join adam_user u on ub.user_id = u.uid ",
            "where ub.badge_id = #{badgeId} ",
            "order by ub.created_at desc"
    })
    List<AdamCaomeiBadgeClaimUserDto> selectClaimUsersByBadgeId(@Param("badgeId") String badgeId);

    @Delete({
            "delete ub from adam_caomei_user_badge ub ",
            "inner join adam_caomei_badge b on ub.badge_id = b.badge_id ",
            "where ub.user_id = #{uid} and ub.source = 1 and b.type = 1"
    })
    int deletePassportTypeBadgesByUid(@Param("uid") String uid);

    /**
     * 绑定护照成功后，为已上架的「护照纪念徽章」(type=1) 批量发放 source=1（与解绑删除规则对称）
     */
    @Insert({
            "insert ignore into adam_caomei_user_badge (user_id, badge_id, source, created_at) ",
            "select #{userId}, b.badge_id, 1, now() ",
            "from adam_caomei_badge b ",
            "where b.type = 1 and b.display_status = 1"
    })
    int insertPassportBindingBadgesForUser(@Param("userId") String userId);

    @Insert({
            "<script>",
            "insert ignore into adam_caomei_user_badge (user_id, badge_id, source, created_at) values ",
            "<foreach collection='badgeIds' item='badgeId' separator=','>",
            "(#{userId}, #{badgeId}, #{source}, #{createdAt})",
            "</foreach>",
            "</script>"
    })
    int insertUserBadgesBatch(@Param("userId") String userId,
                              @Param("badgeIds") List<String> badgeIds,
                              @Param("source") Integer source,
                              @Param("createdAt") Date createdAt);

    /**
     * 根据身份证号查询用户已支付的演出ID列表
     */
    @Select({
            "select distinct performance_id ",
            "from kylin_order_ticket_entities ",
            "where enter_id_code = #{idCard} and is_payment = 1"
    })
    List<String> selectPaidPerformanceIdsByIdCard(@Param("idCard") String idCard);

    @Select("select count(1) from adam_caomei_user_badge where user_id = #{userId} and badge_id = #{badgeId}")
    int checkUserBadgeExists(@Param("userId") String userId, @Param("badgeId") String badgeId);

    @Select({
            "<script>",
            "select badge_id from adam_caomei_user_badge",
            "where user_id = #{userId} and badge_id in",
            "<foreach collection='badgeIds' item='badgeId' open='(' separator=',' close=')'>",
            "#{badgeId}",
            "</foreach>",
            "</script>"
    })
    List<String> selectClaimedBadgeIdsByUserAndBadgeIds(@Param("userId") String userId,
                                                        @Param("badgeIds") List<String> badgeIds);

    @Insert({
            "insert into adam_caomei_user_badge (user_id, badge_id, source, created_at) ",
            "values (#{userId}, #{badgeId}, #{source}, now())"
    })
    int insertUserBadge(@Param("userId") String userId, @Param("badgeId") String badgeId, @Param("source") Integer source);

    @Select("select title from kylin_performances where performances_id = #{performanceId}")
    String selectKylinPerformanceTitleById(@Param("performanceId") String performanceId);

    @Select({
            "<script>",
            "select performances_id as performanceId, title",
            "from kylin_performances",
            "where performances_id in",
            "<foreach collection='performanceIds' item='performanceId' open='(' separator=',' close=')'>",
            "#{performanceId}",
            "</foreach>",
            "</script>"
    })
    List<AdamCaomeiPerformanceIdTitleDto> selectKylinPerformanceTitlesByIds(@Param("performanceIds") List<String> performanceIds);
}
