<?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.flossom.common.core.mapper.WxUserMemberMapper">
    
    <resultMap type="WxUserMember" id="WxUserMemberResult">
        <result property="id"    column="id"    />
        <result property="nickname"    column="nickname"    />
        <result property="headimg"    column="headimg"    />
        <result property="username"    column="username"    />
        <result property="credit"    column="credit"    />
        <result property="expireCredit"    column="expire_credit"    />
        <result property="openid"    column="openid"    />
        <result property="unionid"    column="unionid"    />
        <result property="userType"    column="user_type"    />
        <result property="level"    column="level"    />
        <result property="mobile"    column="mobile"    />
        <result property="provinceId"    column="province_id"    />
        <result property="cityId"    column="city_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="area"    column="area"    />
        <result property="birthday"    column="birthday"    />
        <result property="clock"    column="clock"    />
        <result property="activity"    column="activity"    />
        <result property="wechat"    column="wechat"    />
        <result property="isAbutment"    column="is_abutment"    />
        <result property="isCompleteInformation"    column="is_complete_information"    />
        <result property="devicesNum"    column="devices_num"    />
        <result property="loginTime"    column="login_time"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap type="WxUserMemberRet" id="WxUserMemberRetResult">
        <result property="id"    column="id"    />
        <result property="nickname"    column="nickname"    />
        <result property="headimg"    column="headimg"    />
        <result property="username"    column="username"    />
        <result property="credit"    column="credit"    />
        <result property="expireCredit"    column="expire_credit"    />
        <result property="openid"    column="openid"    />
        <result property="unionid"    column="unionid"    />
        <result property="userType"    column="user_type"    />
        <result property="level"    column="level"    />
        <result property="mobile"    column="mobile"    />
        <result property="provinceId"    column="province_id"    />
        <result property="cityId"    column="city_id"    />
        <result property="areaId"    column="area_id"    />
        <result property="province"    column="province"    />
        <result property="city"    column="city"    />
        <result property="area"    column="area"    />
        <result property="birthday"    column="birthday"    />
        <result property="clock"    column="clock"    />
        <result property="activity"    column="activity"    />
        <result property="wechat"    column="wechat"    />
        <result property="isAbutment"    column="is_abutment"    />
        <result property="isCompleteInformation"    column="is_complete_information"    />
        <result property="devicesNum"    column="devices_num"    />
        <result property="loginTime"    column="login_time"    />
        <result property="status"    column="status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectWxUserMemberVo">
        select id, nickname, headimg, username, credit, expire_credit, openid, unionid, user_type, level, mobile, province_id, city_id, area_id, province, city, area, birthday, clock, activity, wechat, is_abutment, login_time, is_complete_information, devices_num, status, create_by, create_time, update_by, update_time, remark from wx_user_member
    </sql>

    <sql id="fieldList">
        member.id, member.nickname, member.headimg, member.username, member.credit, member.expire_credit, member.openid, member.unionid, member.user_type, member.level, member.mobile,
            member.province_id, member.city_id, member.area_id, member.province, member.city, member.area, member.birthday, member.clock, member.activity,
            member.wechat, member.is_abutment, member.login_time, member.is_complete_information, member.devices_num, member.status, member.create_by,
            member.create_time, member.update_by, member.update_time, member.remark
            FROM wx_user_member as member
    </sql>

    <select id="selectWxUserMemberList" parameterType="WxUserMember" resultMap="WxUserMemberResult">
        <include refid="selectWxUserMemberVo"/>
        <where>  
            <if test="nickname != null  and nickname != ''"> and nickname like concat('%', #{nickname}, '%')</if>
            <if test="headimg != null  and headimg != ''"> and headimg = #{headimg}</if>
            <if test="username != null  and username != ''"> and username like concat('%', #{username}, '%')</if>
            <if test="credit != null "> and credit = #{credit}</if>
            <if test="expireCredit != null "> and expire_credit = #{expireCredit}</if>
            <if test="openid != null  and openid != ''"> and openid = #{openid}</if>
            <if test="unionid != null  and unionid != ''"> and unionid = #{unionid}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="level != null "> and level = #{level}</if>
            <if test="mobile != null  and mobile != ''"> and mobile = #{mobile}</if>
            <if test="provinceId != null  and provinceId != ''"> and province = #{provinceId}</if>
            <if test="cityId != null  and cityId != ''"> and city = #{cityId}</if>
            <if test="areaId != null  and areaId != ''"> and area = #{areaId}</if>
            <if test="province != null  and province != ''"> and province = #{province}</if>
            <if test="city != null  and city != ''"> and city = #{city}</if>
            <if test="area != null  and area != ''"> and area = #{area}</if>
            <if test="birthday != null "> and birthday = #{birthday}</if>
            <if test="clock != null "> and clock = #{clock}</if>
            <if test="activity != null "> and activity = #{activity}</if>
            <if test="wechat != null  and wechat != ''"> and wechat = #{wechat}</if>
            <if test="isAbutment != null "> and is_abutment = #{isAbutment}</if>
            <if test="loginTime != null "> and login_time = #{loginTime}</if>
            <if test="isCompleteInformation != null "> and is_complete_information = #{isCompleteInformation}</if>
            <if test="devicesNum != null "> and devices_num = #{devicesNum}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectWxUserMemberById" parameterType="Long" resultMap="WxUserMemberResult">
        <include refid="selectWxUserMemberVo"/>
        where id = #{id}
    </select>

    <select id="selectWxUserMemberByOpenId" parameterType="String" resultMap="WxUserMemberResult">
        <include refid="selectWxUserMemberVo"/>
        WHERE openid = #{openid}
    </select>

    <select id="selectWxUserMemberListByVm" parameterType="WxUserMemberVm" resultMap="WxUserMemberResult">
        SELECT  <include refid="fieldList" />
        <where>
            <if test="nickname != null  and nickname != ''"> and member.nickname like concat('%', #{nickname}, '%')</if>
            <if test="creditStart != null"> and member.credit &gt;= #{creditStart} </if>
            <if test="creditEnd != null"> and member.credit &lt;= #{creditEnd}</if>
            <if test="userType != null "> and member.user_type = #{userType}</if>
            <if test="provinceId != null  and provinceId != ''"> and member.province_id = #{provinceId}</if>
            <if test="cityId != null  and cityId != ''"> and member.city_id = #{cityId}</if>
            <if test="areaId != null  and areaId != ''"> and member.area_id = #{areaId}</if>
            <if test="birthday != null "> and member.birthday = #{birthday}</if>
            <if test="id!=null and id !=''">
                and
                <foreach collection="id.split(',')" index="index" item="item" open="(" separator="OR"
                         close=")">
                    member.id LIKE concat('%', #{item}, '%')
                </foreach>
            </if>
            <if test="mobile!=null and mobile !=''">
                OR
                <foreach collection="mobile.split(',')" index="index" item="item" open="(" separator="OR"
                         close=")">
                    member.mobile like concat('%', #{item}, '%')
                </foreach>
            </if>
            <if test="params != null and params.beginTime != null">
                and member.create_time &gt;= #{params.beginTime}
            </if>
            <if test="params != null and params.endTime != null">
                and member.create_time &lt;= #{params.endTime}
            </if>
            <if test="deviceNumStart != null"> and member.devices_num &gt;= #{deviceNumStart}</if>
            <if test="deviceNumEnd != null"> and member.devices_num &lt;= #{deviceNumEnd}</if>
            <if test="wecomTags != null and wecomTags.size > 0" >
                and id in (
                    SELECT
                        user_id
                    FROM
                        wx_user_tag
                    WHERE
                        type  = 2
                        AND tag_id IN
                        <foreach item="wecomTag" collection="wecomTags" open="(" separator="," close=")">
                            #{wecomTag}
                        </foreach>
                    GROUP BY
                        user_id
                    HAVING
                        COUNT( DISTINCT tag_id ) = #{wecomTagNum}
                )
            </if>
            <if test="miniProgramTags != null and miniProgramTags.size > 0" >
                and id in (
                    SELECT
                        DISTINCT user_id
                    FROM
                        wx_user_tag
                    WHERE
                        type  = 1
                        AND tag_id IN
                    <foreach item="miniProgramTag" collection="miniProgramTags" open="(" separator="," close=")">
                        #{miniProgramTag}
                    </foreach>
                    GROUP BY
                        user_id
                    HAVING
                        COUNT( DISTINCT tag_id ) = #{miniProgramTagNum}
                )
            </if>
            and member.status = 0
        </where>
    </select>

    <insert id="insertWxUserMember" parameterType="WxUserMember" useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" resultType="Long" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into wx_user_member
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="nickname != null">nickname,</if>
            <if test="headimg != null">headimg,</if>
            <if test="username != null">username,</if>
            <if test="credit != null">credit,</if>
            <if test="expireCredit != null">expire_credit,</if>
            <if test="openid != null">openid,</if>
            <if test="unionid != null">unionid,</if>
            <if test="userType != null">user_type,</if>
            <if test="level != null">level,</if>
            <if test="mobile != null">mobile,</if>
            <if test="provinceId != null">province_id,</if>
            <if test="cityId != null">city_id,</if>
            <if test="areaId != null">area_id,</if>
            <if test="province != null">province,</if>
            <if test="city != null">city,</if>
            <if test="area != null">area,</if>
            <if test="birthday != null">birthday,</if>
            <if test="clock != null">clock,</if>
            <if test="activity != null">activity,</if>
            <if test="wechat != null">wechat,</if>
            <if test="isAbutment != null">is_abutment,</if>
            <if test="isCompleteInformation != null">is_complete_information,</if>
            <if test="devicesNum != null">devices_num,</if>
            <if test="loginTime != null">login_time,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="nickname != null">#{nickname},</if>
            <if test="headimg != null">#{headimg},</if>
            <if test="username != null">#{username},</if>
            <if test="credit != null">#{credit},</if>
            <if test="expireCredit != null">#{expireCredit},</if>
            <if test="openid != null">#{openid},</if>
            <if test="unionid != null">#{unionid},</if>
            <if test="userType != null">#{userType},</if>
            <if test="level != null">#{level},</if>
            <if test="mobile != null">#{mobile},</if>
            <if test="provinceId != null">#{provinceId},</if>
            <if test="cityId != null">#{cityId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="province != null">#{province},</if>
            <if test="city != null">#{city},</if>
            <if test="area != null">#{area},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="clock != null">#{clock},</if>
            <if test="activity != null">#{activity},</if>
            <if test="wechat != null">#{wechat},</if>
            <if test="isAbutment != null">#{isAbutment},</if>
            <if test="isCompleteInformation != null">#{isCompleteInformation},</if>
            <if test="devicesNum != null">#{devicesNum},</if>
            <if test="loginTime != null">#{loginTime},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateWxUserMember" parameterType="WxUserMember">
        update wx_user_member
        <trim prefix="SET" suffixOverrides=",">
            <if test="nickname != null">nickname = #{nickname},</if>
            <if test="headimg != null">headimg = #{headimg},</if>
            <if test="username != null">username = #{username},</if>
            <if test="credit != null">credit = #{credit},</if>
            <if test="expireCredit != null">expire_credit = #{expireCredit},</if>
            <if test="openid != null">openid = #{openid},</if>
            <if test="unionid != null">unionid = #{unionid},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="level != null">level = #{level},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="provinceId != null">province_id = #{provinceId},</if>
            <if test="cityId != null">city_id = #{cityId},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="province != null">province = #{province},</if>
            <if test="city != null">city = #{city},</if>
            <if test="area != null">area = #{area},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="clock != null">clock = #{clock},</if>
            <if test="activity != null">activity = #{activity},</if>
            <if test="wechat != null">wechat = #{wechat},</if>
            <if test="isAbutment != null">is_abutment = #{isAbutment},</if>
            <if test="loginTime != null">login_time = #{loginTime},</if>
            <if test="isCompleteInformation != null">is_complete_information = #{isCompleteInformation},</if>
            <if test="devicesNum != null">devices_num = #{devicesNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="upgradeMember">
        UPDATE `wx_user_member`
        SET `user_type` = 1,
            `level` = 1,
            `mobile` = #{phoneNumber},
            `create_time` = sysdate(),
            `login_time` = sysdate(),
            `update_time` = sysdate()
        WHERE
            `id` = #{id};
    </update>

    <delete id="deleteWxUserMemberById" parameterType="Long">
        delete from wx_user_member where id = #{id}
    </delete>

    <delete id="deleteWxUserMemberByIds" parameterType="String">
        delete from wx_user_member where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertTouristUser" parameterType="WxUserMember" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO `wx_user_member` (login_time, create_time) VALUES(sysdate(), sysdate());
    </insert>

    <select id="selectUserCount" resultType="integer">
        SELECT count(1) as count FROM `wx_user_member`
        union ALL
        SELECT count(1) as count FROM `wx_user_member` WHERE mobile is not null
        union ALL
        SELECT count(1) as count FROM `wx_user_member` WHERE devices_num > 0
    </select>

    <select id="selectWxUserMemberIdList" resultType="java.lang.Integer">
        SELECT id FROM `wx_user_member`
    </select>

    <select id="selectWxUserMemberByMobile" resultMap="WxUserMemberResult">
        <include refid="selectWxUserMemberVo"/>
        WHERE
            mobile = #{mobile}
    </select>

    <select id="selectWxUserMemberByIdList" resultType="com.flossom.common.core.domain.entity.WxUserMember">
        <include refid="selectWxUserMemberVo"/>
        where id in
        <foreach item="id" collection="userIdList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectWxUserMemberRetByIdList" resultMap="WxUserMemberRetResult">
        SELECT  <include refid="fieldList" />
        where id in
        <foreach item="id" collection="userIdList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectWxUserMemberRetListByVm" resultMap="WxUserMemberRetResult">
        SELECT  <include refid="fieldList" />
        <where>
            <if test="nickname != null  and nickname != ''"> and member.nickname like concat('%', #{nickname}, '%')</if>
            <if test="creditStart != null"> and member.credit &gt;= #{creditStart} </if>
            <if test="creditEnd != null"> and member.credit &lt;= #{creditEnd}</if>
            <if test="userType != null "> and member.user_type = #{userType}</if>
            <if test="provinceId != null  and provinceId != ''"> and member.province_id = #{provinceId}</if>
            <if test="cityId != null  and cityId != ''"> and member.city_id = #{cityId}</if>
            <if test="areaId != null  and areaId != ''"> and member.area_id = #{areaId}</if>
            <if test="birthday != null "> and member.birthday = #{birthday}</if>
            <if test="id!=null and id !=''">
                and
                <foreach collection="id.split(',')" index="index" item="item" open="(" separator="OR"
                         close=")">
                    member.id LIKE concat('%', #{item}, '%')
                </foreach>
            </if>
            <if test="mobile!=null and mobile !=''">
                OR
                <foreach collection="mobile.split(',')" index="index" item="item" open="(" separator="OR"
                         close=")">
                    member.mobile like concat('%', #{item}, '%')
                </foreach>
            </if>
            <if test="params != null and params.beginTime != null">
                and member.create_time &gt;= #{params.beginTime}
            </if>
            <if test="params != null and params.endTime != null">
                and member.create_time &lt;= #{params.endTime}
            </if>
            <if test="deviceNumStart != null"> and member.devices_num &gt;= #{deviceNumStart}</if>
            <if test="deviceNumEnd != null"> and member.devices_num &lt;= #{deviceNumEnd}</if>
            <if test="wecomTags != null and wecomTags.size > 0" >
            and id in (
                SELECT
                    user_id
                FROM
                    wx_user_tag
                WHERE
                    type  = 2
                    AND tag_id IN
                    <foreach item="wecomTag" collection="wecomTags" open="(" separator="," close=")">
                        #{wecomTag}
                    </foreach>
                GROUP BY
                    user_id
                HAVING
                    COUNT( DISTINCT tag_id ) = #{wecomTagNum}
            )
            </if>
            <if test="miniProgramTags != null and miniProgramTags.size > 0" >
            and id in (
                SELECT
                    DISTINCT user_id
                FROM
                    wx_user_tag
                WHERE
                    type  = 1
                    AND tag_id IN
                    <foreach item="miniProgramTag" collection="miniProgramTags" open="(" separator="," close=")">
                        #{miniProgramTag}
                    </foreach>
                GROUP BY
                    user_id
                HAVING
                    COUNT( DISTINCT tag_id ) = #{miniProgramTagNum}
            )
            </if>
            <if test="devicesIdList != null and devicesIdList.size > 0" >
                and id in (
                    SELECT
                        ui.user_id
                    FROM
                        `wx_user_instrument` ui
                    WHERE
                        ui.binding_status = 0
                        AND ui.instrument_id IN
                        <foreach item="instrumentId" collection="devicesIdList" open="(" separator="," close=")">
                            #{instrumentId}
                        </foreach>
                )
            </if>
            and member.status = 0
        </where>
        order by member.id desc
    </select>


</mapper>