<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qs.mp.user.mapper.MarketingUserCodeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qs.mp.user.domain.MarketingUserCode">
        <id column="id" property="id" />
        <result column="marketing_id" property="marketingId" />
        <result column="user_id" property="userId" />
        <result column="user_type" property="userType" />
        <result column="help_user_id" property="helpUserId" />
        <result column="code" property="code" />
        <result column="is_hit" property="isHit" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
        <result column="is_deleted" property="isDeleted" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, marketing_id, user_id, user_type, help_user_id, code, is_hit, created_time, updated_time, is_deleted
    </sql>

    <select id="listMarketingUserCodeByQueryWrapper" resultType="com.qs.mp.admin.domain.vo.MarketingUserCodeListVO">
        select t1.user_id,
                t1.code,
                t1.created_time,
                t3.nick_name,
                t4.name awardsName,
                t5.title prizeName,
                t5.prize_type,
                t5.value
        from mp_marketing_user_code t1
                left join mp_marketing_hit_prize t2 on t1.id = t2.marketing_user_code_id
                left join sys_user t3 on t1.user_id = t3.user_id
                left join mp_marketing_awards t4 on t2.awards_id = t4.id
                left join mp_marketing_awards_prize t5 on t2.prize_id = t5.id
        ${ew.customSqlSegment}
    </select>

    <select id="countRealUserNumByMarketingId" resultType="int">
        select count(*)
        from (select user_id
              from mp_marketing_user_code
              where user_type = '00'
              and marketing_id = #{marketingId}
              group by user_id) t1;
    </select>
</mapper>