<?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.stdiet.custom.mapper.SysWxUserInfoMapper">

    <resultMap type="SysWxUserInfo" id="SysWxUserInfoResult">
        <result property="openid" column="openid"/>
        <result property="cusId" column="cus_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="appid" column="appid"/>
        <result property="phone" column="phone"/>
        <result property="avatarUrl" column="avatar_url"/>
        <result property="sex" column="sex"/>
        <result property="city" column="city"/>
        <result property="province" column="province"/>
        <result property="country" column="country"/>
        <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"/>
        <result property="language" column="language"/>
    </resultMap>

    <sql id="selectSysWxUserInfoVo">
        select * from sys_wx_user_info
    </sql>

    <select id="selectSysWxUserInfoList" parameterType="SysWxUserInfo" resultMap="SysWxUserInfoResult">
        <include refid="selectSysWxUserInfoVo"/>
        <where>
            <if test="nickName != null  and nickName != ''">and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="appid != null  and appid != ''">and appid = #{appid}</if>
            <if test="cusId != null  and cusId != ''">and cus_id = #{cusId}</if>
            <if test="phone != null  and phone != ''">and phone = #{phone}</if>
            <if test="sex != null  and sex != ''">and sex = #{sex}</if>
        </where>
    </select>

    <select id="selectSysWxUserInfoListNot" parameterType="SysWxUserInfo" resultMap="SysWxUserInfoResult">
        select distinct wxuser.openid, wxuser.nick_name, wxuser.appid, wxuser.phone, wxuser.avatar_url, wxuser.sex, wxuser.update_time from sys_wx_user_info wxuser
        where (SELECT COUNT(wxlog.id) FROM sys_wx_user_log wxlog WHERE wxlog.openid = wxuser.openid) > 0
        <if test="appid != null  and appid != ''">and wxuser.appid = #{appid}</if>
        <if test="openid != null and openid != ''">and wxuser.openid  <![CDATA[ <> ]]> #{openid} </if>
        order by wxuser.update_time desc
    </select>

    <select id="selectSysWxUserInfoByCusId" parameterType="Long" resultMap="SysWxUserInfoResult">
        <include refid="selectSysWxUserInfoVo"/>
        where cus_id = #{cusId}
    </select>

    <select id="selectSysWxUserInfoById" parameterType="String" resultMap="SysWxUserInfoResult">
        <include refid="selectSysWxUserInfoVo"/>
        where openid = #{openid}
    </select>

    <insert id="insertSysWxUserInfo" parameterType="SysWxUserInfo">
        insert into sys_wx_user_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="openid != null">openid,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="appid != null">appid,</if>
            <if test="cusId != null">cus_id,</if>
            <if test="phone != null">phone,</if>
            <if test="avatarUrl != null">avatar_url,</if>
            <if test="sex != null">sex,</if>
            <if test="city != null">city,</if>
            <if test="province != null">province,</if>
            <if test="country != null">country,</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>
            <if test="language != null">language,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="openid != null">#{openid},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="appid != null">#{appid},</if>
            <if test="cusId != null">#{cusId},</if>
            <if test="phone != null">#{phone},</if>
            <if test="avatarUrl != null">#{avatarUrl},</if>
            <if test="sex != null">#{sex},</if>
            <if test="city != null">#{city},</if>
            <if test="province != null">#{province},</if>
            <if test="country != null">#{country},</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>
            <if test="language != null">#{language},</if>
        </trim>
    </insert>

    <update id="updateSysWxUserInfo" parameterType="SysWxUserInfo">
        update sys_wx_user_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="appid != null">appid = #{appid},</if>
            <if test="cusId != null">cus_id = #{cusId},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="avatarUrl != null">avatar_url = #{avatarUrl},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="city != null">city = #{city},</if>
            <if test="province != null">province = #{province},</if>
            <if test="country != null">country = #{country},</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>
            <if test="language != null">language = #{language},</if>
        </trim>
        where openid = #{openid}
    </update>

    <delete id="deleteSysWxUserInfoById" parameterType="String">
        delete from sys_wx_user_info where openid = #{openid}
    </delete>

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

    <!-- 根据手机号查询微信用户 -->
    <select id="getSysWxUserInfoByPhone" parameterType="String" resultMap="SysWxUserInfoResult">
        <include refid="selectSysWxUserInfoVo"/>
        where phone = #{phone} order by create_time desc limit 1
    </select>

</mapper>