<?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="cn.iocoder.yudao.module.custom.mapper.BasePromotionMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.custom.vo.BasePromotionRespVO">
        <id property="id" column="bp_id" jdbcType="VARCHAR"/>
        <result property="oldUserName" column="bp_old_user_name" jdbcType="VARCHAR"/>
        <result property="promotionChannel" column="bp_promotion_channel" jdbcType="VARCHAR"/>
        <result property="newUserName" column="bp_new_user_name" jdbcType="VARCHAR"/>
        <result property="registerTime" column="bp_register_time" jdbcType="TIMESTAMP"/>
        <result property="uniqueCode" column="bp_unique_code" jdbcType="VARCHAR"/>

        <association property="oldUser" javaType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
            <result property="mobile" column="os_mobile"/>
            <result property="userType" column="os_user_type"/>
        </association>

        <association property="newUser" javaType="cn.iocoder.yudao.module.system.api.user.dto.UserDTO">
            <result property="mobile" column="ns_mobile"/>
        </association>
    </resultMap>

    <select id="queryListPage" resultMap="BaseResultMap">
        select bp.id as bp_id,
        bp.promotion_channel as bp_promotion_channel,
        bp.register_time as bp_register_time,
        bp.unique_code as bp_unique_code,
        bp.old_user_name as bp_old_user_name,
        bp.new_user_name as bp_new_user_name,
        os.mobile as os_mobile,
        os.user_type as os_user_type,
        ns.mobile as ns_mobile
        from base_promotion bp
        INNER JOIN system_users os on bp.old_user_id = os.id
        INNER JOIN system_users ns on bp.new_user_id = ns.id
        where bp.deleted = '0'
        <if test="map.oldUserName != null and map.oldUserName != ''">
            and (bp.old_user_name like concat('%', #{map.oldUserName}, '%'))
        </if>
        <if test="map.uniqueCode != null and map.uniqueCode != ''">
            and (bp.unique_code like concat('%', #{map.uniqueCode}, '%'))
        </if>
        <if test="map.newUserName != null and map.newUserName != ''">
            and (bp.new_user_name like concat('%', #{map.newUserName}, '%'))
        </if>
        <if test="map.promotionChannel != null and map.promotionChannel != ''">
            and (bp.promotion_channel = #{map.promotionChannel})
        </if>
        <if test="map.registerTimeArray != null and map.registerTimeArray.length > 0">
            and (bp.register_time between #{map.registerTimeArray[0]} and #{map.registerTimeArray[1]})
        </if>
        <if test="map.userType != null and map.userType != ''">
            and (os.user_type = #{map.userType})
        </if>
        order by bp.create_time desc
    </select>
</mapper>