<?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.xmomen.module.base.mapper.MemberMapper">

    <resultMap type="com.xmomen.module.base.model.MemberModel" id="Memeber">
        <id column="id" property="id"/>
        <collection property="couponNumbers" ofType="com.xmomen.module.base.model.MemberCouponModel" column="id" select="queryCouponNumberList"></collection>
    </resultMap>

    <!--    查询消息    -->
    <select id="getMemberList" resultMap="Memeber" parameterType="java.util.HashMap">
        SELECT
        cm.*,
        cc.company_name,
        su.realName as managerName
        FROM
        cd_member cm
        left join cd_company cc on cm.cd_company_id = cc.id
        left join sys_users su on cm.cd_user_id = su.id
        <where>
            cm.phone_number is not null
            <if test="keyword">
                AND (cm.name LIKE CONCAT('%', #{keyword}, '%') or cm.phone_number like CONCAT('%', #{keyword}, '%'))
            </if>
            <if test="id">
                AND cm.id = #{id}
            </if>
            <if test="phoneNumber">
                AND cm.phone_number = #{phoneNumber}
            </if>
            <if test="managerId">
                AND cm.cd_user_id = #{managerId}
            </if>
            <if test="couponNumber">
                AND cm.id in (SELECT CD_MEMBER_ID from cd_member_coupon_relation cmcr where cmcr.coupon_number like CONCAT('%', #{couponNumber}, '%') )
            </if>
        </where>
    </select>

    <select id="queryCouponNumberList" resultType="com.xmomen.module.base.model.MemberCouponModel" parameterType="Integer">
		select  
			cmcr.COUPON_NUMBER,
			cc.user_price
		from 
			cd_member_coupon_relation cmcr
		left join 
			cd_coupon cc on cmcr.coupon_number = cc.coupon_number 
		where 
			cmcr.cd_member_id = #{id}
	</select>

</mapper>