<?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.nbclass.activity.mapper.WXMapper">

    <select id="getOpenId" resultType="int">
        select count(*) from weixi_user where openid = #{openid}
    </select>

    <insert id="saveWxUser">
        insert into weixi_user
        (
        openid,
        nickname,
        sex,
        province,
        city,
        headimgurl,
        unionid,
        create_time,
        update_time
        )
        values
        (
        #{openid},
        #{nickname},
        #{sex},
        #{province},
        #{city},
        #{headimgurl},
        #{unionid},
        now(),
        now()
        )
    </insert>

    <select id="getWxUser" resultType="com.nbclass.activity.model.WeiXiUser">
        select * from weixi_user where openid = #{openid}
    </select>

    <select id="getUser" resultType="com.nbclass.activity.model.WeiXiUser">
        select * from weixi_user where id = #{userId}
    </select>

    <select id="getUserBasicData" resultType="com.nbclass.activity.model.WeiXiUser">
        SELECT
            nickname,
            sex,
            birthday,
            occupation_id,
            company,
            mailbox,
            brief_introduction
        FROM
            weixi_user
        WHERE
            id = #{userId}
    </select>

    <update id="updateBasicData">
        update weixi_user
        <set>
            <if test="birthday!=null">
                birthday = #{birthday},
            </if>
            <if test="occupationId!=null">
                occupation_id = #{occupationId},
            </if>
            <if test="company!=null and company!=''">
                company = #{company},
            </if>
            <if test="mailbox!=null and mailbox!=''">
                mailbox = #{mailbox},
            </if>
            <if test="briefIntroduction!=null and briefIntroduction!=''">
                brief_introduction = #{briefIntroduction},
            </if>
            <if test="updateTime!=null">
                update_time = #{updateTime}
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="getQyWxUser" resultType="com.nbclass.activity.model.WxUser">
        select * from wx_user where userid = #{username}
    </select>

    <select id="getUserLeader" resultType="com.nbclass.activity.model.Check">
        SELECT
            u.id as checkId
        FROM
            wx_user wx
                LEFT JOIN user_role ur ON wx.NAME = ur.user_id
                LEFT JOIN role r ON r.id = ur.role_id
                LEFT JOIN `user` u ON u.username = wx.userid
        WHERE
            ( locate( CONCAT(',',#{department}), wx.department ) OR wx.department = #{department} OR wx.department = 1)
        AND r.role_id = 2
    </select>

</mapper>