<?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.iformall.mapper.WxCUserBasicInfoMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCUserBasicInfo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="birthdate" jdbcType="TIMESTAMP" property="birthdate"/>
        <result column="education" jdbcType="VARCHAR" property="education"/>
        <result column="sex" jdbcType="INTEGER" property="sex"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="poins" jdbcType="INTEGER" property="poins"/>
        <result column="tag_id" jdbcType="BIGINT" property="tagId"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="level" jdbcType="VARCHAR" property="level"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="credit" jdbcType="INTEGER" property="credit"/>
        <result column="active_time" jdbcType="TIMESTAMP" property="activeTime"/>
        <result column="act_record" jdbcType="INTEGER" property="actRecord"/>
        <result column="score_date" jdbcType="TIMESTAMP" property="scoreDate"/>
        <result column="status" jdbcType="INTEGER" property="status"/>

    </resultMap>

    <sql id="allColumns">
	    `id`,`phone`,`birthdate`,`education`,`sex`,`email`,`address`,`poins`,`tag_id`,
	    `create_date`,`update_date`,`tenant_id`,`name`,`nick_name`,`credit`,`active_time`,`act_record`
	    ,(select level from wx_level_config
	       where  poins >= points and tenant_id=#{tenantId}
	       order by points desc limit 1 ) level,status
	</sql>

    <sql id="dynamicWhereConditions">
        where 1 = 1

        <if test=" null != id ">
            and `id` = #{id}
        </if>

        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>

        <if test=" null != phone ">
            and `phone` like concat('%', #{phone},'%')
        </if>

        <if test=" null != birthdate ">
            and `birthdate` = #{birthdate}
        </if>

        <if test=" null != education ">
            and `education` like concat('%', #{education},'%')
        </if>

        <if test=" null != sex ">
            and `sex` = #{sex}
        </if>

        <if test=" null != email ">
            and `email` like concat('%', #{email},'%')
        </if>

        <if test=" null != address ">
            and `address` like concat('%', #{address},'%')
        </if>

        <if test=" null != poins ">
            and `poins` = #{poins}
        </if>

        <if test=" null != credit ">
            and `credit` = #{credit}
        </if>

        <if test=" null != tagId ">
            and `tag_id` = #{tagId}
        </if>

        <if test=" null != createDate ">
            and `create_date` = #{createDate}
        </if>

        <if test=" null != updateDate ">
            and `update_date` = #{updateDate}
        </if>

        <if test=" null != actRecord ">
            and `act_record` = #{actRecord}
        </if>
    
        <if test=" null != name ">
            and `name` like concat('%', #{name},'%')
        </if>
        <if test=" null != startTime and  null!=endTime">
            and `create_date` between #{startTime} and #{endTime}
        </if>
    
        <if test=" null != status ">
            and `status` = #{status}
        </if>
    
        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findList" parameterType="com.iformall.domain.po.WxCUserBasicInfo" resultMap="BaseResultMap">
        select r.* from (
            select
            <include refid="allColumns"/>
            from wx_c_user_basic_info
            <include refid="dynamicWhereConditions"/>
        ) r where 1=1

        <if test="null != level and '' != level">
          and r.level =#{level}
        </if>
    </select>

    <sql id="allUserColumns">
	    c.`id`,c.`phone`,cb.`birthdate`,cb.`education`,cb.`sex`,cb.`email`,cb.`address`,cb.`poins`,cb.`tag_id`,
	    cb.`create_date`,cb.`update_date`,c.`tenant_id`,cb.`name`,cb.level,cb.nick_name,`cb.act_record`
	</sql>

    <update id="updateScore" parameterType="com.iformall.domain.po.WxCUserBasicInfo">
		update wx_c_user_basic_info set poins=#{poins}
		where phone=#{phone} and tenant_id=#{tenantId}
		 and id=#{id}
	</update>

    <update id="updateNewId" parameterType="com.iformall.domain.vo.CUserBaseVo">
		update wx_c_user_basic_info set id=#{newId} , credit=#{credit} , poins=#{poins}
		 where tenant_id=#{tenantId}
		 and id = #{id}
	</update>

    <select id="findCountBySex" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        select count(id) from wx_c_user_basic_info
        where sex =#{sex}
        <if test=" null != startTime ">
            and create_date &gt;= #{startTime}
        </if>
        <if test=" null != endTime">
            and create_date &lt;= #{endTime}
        </if>
        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>
    </select>


    <select id="findCountByAge" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        select count(id) from wx_c_user_basic_info where 1=1
        <if test=" null != startTime ">
            and create_date &gt;= #{startTime}
        </if>
        <if test=" null != endTime">
            and create_date &lt;= #{endTime}
        </if>

        <if test=" null != birthStartTime ">
            and birthdate &gt;= #{birthStartTime}
        </if>

        <if test=" null != birthEndTime">
            and birthdate &lt;= #{birthEndTime}
        </if>

        <if test=" null == birthStartTime and null == birthEndTime">
            and birthdate is null
        </if>

        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>

    </select>

    <select id="findCountByScore" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        select count(id) from wx_c_user_basic_info where 1=1
        <if test=" null != startTime ">
            and create_date &gt;= #{startTime}
        </if>
        <if test=" null != endTime">
            and create_date &lt;= #{endTime}
        </if>

        <if test=" null != levelStartScore ">
            and poins &gt;= #{levelStartScore}
        </if>

        <if test=" null != levelEndScore">
            and poins &lt; #{levelEndScore}
        </if>

        <if test=" null == levelStartScore and null == levelEndScore">
            and poins is null
        </if>

        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>

    </select>

    <sql id="allColumnsOfScoreList">
	    cub.id,cub.phone,cub.birthdate,cub.education,cub.sex,cub.email,cub.address,cub.poins,cub.tag_id,
	    cub.create_date,cub.update_date,cub.tenant_id,cub.name,cub.level,cub.nick_name,cub.score_date
	</sql>

    <select id="findListByScore" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultMap="BaseResultMap">
        select <include refid="allColumnsOfScoreList"/>
        from wx_c_user_basic_info cub
        inner join wx_c_user cu on cu.id = cub.id
	
	    where 1=1 and cub.status=0

        <if test=" null != levelStartScore ">
            and cub.poins &gt;= #{levelStartScore}
        </if>

        <if test=" null != levelEndScore">
            and cub.poins &lt;= #{levelEndScore}
        </if>

        <if test=" null == levelStartScore and null == levelEndScore">
            and cub.poins is null
        </if>

        <if test="null != tenantId">
            and cub.tenant_id =#{tenantId}
        </if>

    </select>

    <select id="findBirthdayList" resultMap="BaseResultMap">
        select <include refid="allColumnsOfScoreList"/>
        from wx_c_user_basic_info cub
        inner join wx_c_user cu on cu.id = cub.id
	    where cub.status = 0 and cu.phone is not null
        AND DATE_FORMAT(cub.birthdate,'%m-%d') &gt;= #{begin}
        AND DATE_FORMAT(cub.birthdate,'%m-%d') &lt;= #{end}
        <if test="null != tenantId">
            and cub.tenant_id = #{tenantId}
        </if>
    </select>

    <select id="findCount" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        select count(id) from wx_c_user_basic_info where 1=1
        <if test=" null != startTime ">
            and create_date &gt;= #{startTime}
        </if>
        <if test=" null != endTime">
            and create_date &lt; #{endTime}
        </if>
        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>
        <if test="1 == queryImport">
            and create_date like '%00:00:00'
        </if>
    </select>

    <select id="findCountHistory" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="com.iformall.domain.vo.UserCountVo">
        select count(id) as incCount,
        <if test="1 == reportType ">
            date_format(create_date, '%Y-%m-%d') as reportTime,
        </if>
        <if test="2 == reportType ">
            date_format(create_date, '%Y-%m') as reportTime,
        </if>
        <if test="3 == reportType ">
            date_format(create_date, '%Y') as reportTime,
        </if>

        <if test="1 == reportType or 2 == reportType or 3 == reportType">
            (select count(id) from wx_c_user_basic_info
              where
            <if test="1 == reportType ">
                date_format(create_date, '%Y-%m-%d') &lt;= reportTime
            </if>
            <if test="2 == reportType ">
                date_format(create_date, '%Y-%m') &lt;= reportTime
            </if>
            <if test="3 == reportType ">
                date_format(create_date, '%Y') &lt;= reportTime
            </if>
            <if test="null != tenantId">
                and tenant_id =#{tenantId}
            </if>
            ) as totalCount
        </if>

        from wx_c_user_basic_info where 1=1
        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>
        <if test=" null != startTime ">
            and create_date &gt;= #{startTime}
        </if>
        <if test=" null != endTime">
            and create_date &lt; #{endTime}
        </if>
        <if test="1 == reportType or 2 == reportType or 3 == reportType">
            group by reportTime
        </if>
    </select>

    <resultMap id="VoBaseResultMap" type="com.iformall.domain.vo.WxCUserBasicInfoVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="birthdate" jdbcType="TIMESTAMP" property="birthdate"/>
        <result column="education" jdbcType="VARCHAR" property="education"/>
        <result column="sex" jdbcType="INTEGER" property="sex"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="poins" jdbcType="INTEGER" property="poins"/>
        <result column="tag_id" jdbcType="BIGINT" property="tagId"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="level" jdbcType="VARCHAR" property="level"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="credit" jdbcType="INTEGER" property="credit"/>
        <result column="tags" jdbcType="VARCHAR" property="tags"/>
        <result column="active_time" jdbcType="TIMESTAMP" property="activeTime"/>
    </resultMap>

    <sql id="allVoColumns">
	    cu.`id`,cu.`phone`,cu.`birthdate`,cu.`education`,cu.`sex`,cu.`email`,cu.`address`,cu.`poins`,cu.`tag_id`,
	    cu.`create_date`,cu.`update_date`,cu.`tenant_id`,cu.`name`,cu.`level`,cu.`nick_name`,
	    cut.`tags`,cu.`credit`,cu.`active_time`,cu.`act_record`
	</sql>

    <sql id="dynamicVoWhereConditions">
        where 1 = 1

        <if test=" null != id ">
            and cu.`id` = #{id}
        </if>

        <if test=" null != tenantId ">
            and cu.`tenant_id` = #{tenantId}
        </if>

        <if test=" null != phone ">
            and cu.`phone` like concat('%', #{phone},'%')
        </if>

        <if test=" null != birthdate ">
            and cu.`birthdate` = #{birthdate}
        </if>

        <if test=" null != education ">
            and cu.`education` like concat('%', #{education},'%')
        </if>

        <if test=" null != sex ">
            and cu.`sex` = #{sex}
        </if>

        <if test=" null != email ">
            and cu.`email` like concat('%', #{email},'%')
        </if>

        <if test=" null != address ">
            and cu.`address` like concat('%', #{address},'%')
        </if>

        <if test=" null != poins ">
            and cu.`poins` = #{poins}
        </if>

        <if test=" null != tagId ">
            and cu.`tag_id` = #{tagId}
        </if>

        <if test=" null != createDate ">
            and cu.`create_date` = #{createDate}
        </if>

        <if test=" null != updateDate ">
            and cu.`update_date` = #{updateDate}
        </if>

        <if test=" null != actRecord ">
            and cu.`act_record` = #{actRecord}
        </if>

        <if test=" null != name ">
            and `name` like concat('%', #{name},'%')
        </if>
        <if test=" null != startTime and  null!=endTime">
            and cu.`create_date` between #{startTime} and #{endTime}
        </if>
        <if test=" null != ids ">
            and id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findVoList" parameterType="com.iformall.domain.po.WxCUserBasicInfo" resultMap="VoBaseResultMap">
        select
        <include refid="allVoColumns"/>
        from wx_c_user_basic_info cu
        left join wx_c_user_tags cut on cut.`id` = cu.`tag_id`
        <include refid="dynamicVoWhereConditions"/>
    </select>

    <select id="countVoList" parameterType="com.iformall.domain.po.WxCUserBasicInfo" resultType="java.lang.Integer">
        select
        count(*)
        from wx_c_user_basic_info cu
        left join wx_c_user_tags cut on cut.`id` = cu.`tag_id`
        <include refid="dynamicVoWhereConditions"/>
    </select>


    <sql id="msgColumns">
	    cu.`id`,cu.`phone`,cu.`email`
	</sql>

    <sql id="filterWhereConditions">
        where 1=1
        <if test="null != tenantId">
            and cu.tenant_id =#{tenantId}
        </if>

        <if test=" null != startTime ">
            and cu.create_date &gt;= #{startTime}
        </if>

        <if test=" null != endTime">
            and cu.create_date &lt;= #{endTime}
        </if>

        <if test=" null != birthStartTime ">
            and cu.birthdate &gt;= #{birthStartTime}
        </if>

        <if test=" null != birthEndTime">
            and cu.birthdate &lt; #{birthEndTime}
        </if>

        <if test=" null != loginStartTime ">
            and cu.active_time &gt;= #{loginStartTime}
        </if>

        <if test=" null != loginEndTime">
            and cu.active_time &lt;= #{loginEndTime}
        </if>

        <if test=" null != verifiedStartTime and null != verifiedEndTime ">
            and 0&lt;
            (select count(*) from wx_coupon_order co
                where cu.id = co.c_user_id
                and co.coupon_order_status = 1
                and co.update_date &gt;= #{verifiedStartTime}
                and co.update_date &lt;= #{verifiedEndTime})
        </if>

        <if test=" null != phones">
            and cu.phone in (${phones})
        </if>

        <if test=" null != levelStartScore and (0 != levelStartScore and '' != levelStartScore)">
            and cu.poins &gt;= #{levelStartScore}
            <if test=" null != levelEndScore">
                and cu.poins &lt; #{levelEndScore}
            </if>
        </if>

        <if test=" null != levelStartScore and (0 == levelStartScore or '' == levelStartScore)">
            and (cu.poins is null or
            (cu.poins &gt;= #{levelStartScore}
            <if test=" null != levelEndScore">
                and cu.poins &lt; #{levelEndScore}
            </if>
            ))
        </if>

        <if test=" null != tagIds ">
            and JSON_CONTAINS(JSON_ARRAY(cut.tags),JSON_ARRAY
            <foreach collection="tagIds" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
            )
        </if>
        <if test=" null != sortColumns">order by ${sortColumns}</if>
    </sql>

    <select id="findCountByFilter" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        select count(*) from wx_c_user_basic_info cu
        <if test=" null != tagIds ">
          join wx_c_user_tags cut on cut.`id` = cu.`tag_id`
        </if>
        <include refid="filterWhereConditions"/>
    </select>

    <select id="findListByFilter" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultMap="BaseResultMap">
        select <include refid="msgColumns"/>
        from wx_c_user_basic_info cu
        <if test=" null != tagIds ">
          join wx_c_user_tags cut on cut.`id` = cu.`tag_id`
        </if>
        <include refid="filterWhereConditions"/>
    </select>


    <select id="findImportUserCount" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        SELECT count(*) FROM wx_c_user_basic_info cubi left join wx_c_user cu on cubi.id=cu.id where cu.id is null
        <if test="null != tenantId">
            and cubi.tenant_id =#{tenantId}
        </if>
    </select>

    <select id="findGrowUserCount" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        SELECT count(*) FROM wx_c_user_basic_info cubi left join wx_c_user cu on cubi.id=cu.id where cu.id is not null
        <if test="null != tenantId">
            and cubi.tenant_id =#{tenantId}
        </if>
    </select>

    <update id="incActRecordById" parameterType="java.lang.Long">
        UPDATE wx_c_user_basic_info set act_record = act_record +1 where id = #{id}
    </update>

    <select id="selectNotCUserList" resultMap="BaseResultMap">
        select id,credit from wx_c_user_basic_info cub where not exists (select * from wx_c_user c where c.id=cub.id) and cub.credit is null
    </select>
</mapper>

