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.entity.AdamCaomeiBadge;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

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);
}
