<?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.WxCUserMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCUser">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="tenant_id" jdbcType="VARCHAR" property="tenantId"/>
        <result column="open_id" jdbcType="VARCHAR" property="openId"/>
        <result column="union_id" jdbcType="VARCHAR" property="unionId"/>
        <result column="nick_name" jdbcType="VARCHAR" property="nickName"/>
        <result column="gender" jdbcType="INTEGER" property="gender"/>
        <result column="avatar_url" jdbcType="VARCHAR" property="avatarUrl"/>
        <result column="phone" jdbcType="VARCHAR" property="phone"/>
        <result column="pure_phone" jdbcType="VARCHAR" property="purePhone"/>
        <result column="city" jdbcType="VARCHAR" property="city"/>
        <result column="province" jdbcType="VARCHAR" property="province"/>
        <result column="language" jdbcType="VARCHAR" property="language"/>
        <result column="country_code" jdbcType="VARCHAR" property="countryCode"/>
        <result column="register_ip" jdbcType="VARCHAR" property="registerIp"/>
        <result column="verify_code_phone" jdbcType="VARCHAR" property="verifyCodePhone"/>
        <result column="qrcode_source" jdbcType="VARCHAR" property="qrcodeSource"/>
        <result column="scene" jdbcType="VARCHAR" property="scene"/>
        <result column="scene_address" jdbcType="VARCHAR" property="sceneAddress"/>
        <result column="session_key" jdbcType="VARCHAR" property="sessionKey"/>
        <result column="score" jdbcType="INTEGER" property="score"/>
        <result column="update_date" jdbcType="TIMESTAMP" property="updateDate"/>
        <result column="create_date" jdbcType="TIMESTAMP" property="createDate"/>
        <result column="app_id" jdbcType="VARCHAR" property="appId"/>
        <result column="token" jdbcType="VARCHAR" property="token"/>
        <result column="expire_time" jdbcType="TIMESTAMP" property="expireTime"/>
        <result column="latitude" jdbcType="DECIMAL" property="latitude"/>
        <result column="longitude" jdbcType="DECIMAL" property="longitude"/>
        <result column="login_count" jdbcType="INTEGER" property="loginCount"/>
        <result column="extra_info" jdbcType="VARCHAR" property="extraInfo"/>
        <result column="is_ubscribe" jdbcType="TINYINT" property="isSubscribe"/>
    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`open_id`,`union_id`,`nick_name`,`gender`,`avatar_url`,`phone`,`pure_phone`,
	    `city`,`province`,`language`,`country_code`,`register_ip`,`verify_code_phone`,
	    `qrcode_source`,`scene`,`scene_address`,`session_key`,`score`,
	    `update_date`,`create_date`,`app_id`,`token`,`expire_time`,`latitude`, `longitude`,
	    `login_count`, `extra_info`, `is_subscribe`
	</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 != openId ">
            and `open_id` like concat('%', #{openId},'%')
        </if>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        <if test=" null != isSubscribe ">
            and `is_subscribe` = #{isSubscribe}
        </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.WxCUser" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_c_user
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="findByOpenId" parameterType="com.iformall.domain.po.WxCUser" resultMap="BaseResultMap">
		select * from wx_c_user
		where `app_id` = #{appId} and `open_id` =  #{openId}
	</select>

    <select id="findByToken" resultMap="BaseResultMap">
		select * from wx_c_user
		where `token` = #{token}
	</select>

    <select id="findCount" parameterType="com.iformall.domain.dto.WxCUserBasicInfoDto" resultType="java.lang.Long">
        select count(id) from wx_c_user where 1=1
        <if test=" null != sex ">
            and gender =#{sex}
        </if>
        <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="listByChannel" resultMap="BaseResultMap" >
        select id,nick_name,phone,create_date,scene_address from wx_c_user where 1=1
        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>
        <if test=" null != startDate ">
            and create_date &gt;= #{startDate}
        </if>
        <if test=" null != endDate">
            and create_date &lt; #{endDate}
        </if>
        <if test="  sceneList!= null ">
            and scene_address in
            <foreach collection="sceneList" index="index" item="scene" open="(" separator="," close=")">
                #{scene}
            </foreach>
        </if>
        order by create_date desc
    </select>

    <select id="countByChannel" resultType="java.lang.Long" >
        select count(id) from wx_c_user where 1=1
        <if test="null != tenantId">
            and tenant_id =#{tenantId}
        </if>
        <if test=" null != startDate ">
            and create_date &gt;= #{startDate}
        </if>
        <if test=" null != endDate">
            and create_date &lt; #{endDate}
        </if>
        <if test="  sceneList!= null ">
            and scene_address in
            <foreach collection="sceneList" index="index" item="scene" open="(" separator="," close=")">
                #{scene}
            </foreach>
        </if>
    </select>

</mapper>
