<?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.yonge.cooleshow.auth.dal.dao.SysUserDao">

    <resultMap type="com.yonge.cooleshow.auth.api.entity.SysUser" id="SysUser">
        <result column="id_" property="id"/>
        <result column="username_" property="username"/>
        <result column="real_name_" property="realName"/>
        <result column="password_" property="password"/>
        <result column="salt_" property="salt"/>
        <result column="phone_" property="phone"/>
        <result column="avatar_" property="avatar"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="lock_flag_" property="lockFlag"/>
        <result column="del_flag_" property="delFlag"/>
        <result column="wx_openid_" property="wxOpenid"/>
        <result column="qq_openid_" property="qqOpenid"/>
        <result column="user_type_" property="userType"/>
        <result column="gender_" property="gender"/>
        <result column="nation_" property="nation"/>
        <result column="birthdate_" property="birthdate"/>
        <result column="email_" property="email"/>
        <result column="im_token_" property="imToken"/>
        <result column="id_card_no_" property="idCardNo"/>
        <result column="wechat_id_" property="wechatId"/>
        <result column="is_super_admin_" property="isSuperAdmin"/>
        <result column="certificate_type_" property="certificateType"/>
        <result column="last_username_time_" property="lastUsernameTime"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

    <!-- 查询条件 -->
    <sql id="queryCondition">
        <where>
            and del_flag_ = 0
            <if test="userStartDate != null">
                and create_time_ &gt;= #{createStartDate}
            </if>
            <if test="userEndDate != null">
                and create_time_ &lt;= #{createEndDate}
            </if>
            <if test="userType != null">
                and user_type_ = #{userType}
            </if>
        </where>
    </sql>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SysUser">
		SELECT su.* FROM sys_user su
        WHERE su.del_flag_ = 0 and su.id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SysUser">
		SELECT * FROM sys_user where del_flag_ = 0 ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.yonge.cooleshow.auth.api.entity.SysUser" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_user
        (is_super_admin_,im_token_,id_,username_,salt_,phone_,avatar_,create_time_,update_time_,wx_openid_,qq_openid_,user_type_,
        gender_,nation_,birthdate_,email_,id_card_no_,wechat_id_,real_name_,certificate_type_)
        VALUES(#{isSuperAdmin},#{imToken},#{id},#{username},#{salt},#{phone},#{avatar},now(),now(),#{wxOpenid},#{qqOpenid},#{userType},
        #{gender},#{nation},#{birthdate},#{email},#{idCardNo},#{wechatId},#{realName},#{certificateType})
    </insert>

    <insert id="saveTeacher">
        INSERT INTO teacher (user_id_,create_time_,update_time_)VALUES(#{userId},NOW(),NOW())
        ON DUPLICATE KEY UPDATE user_id_ = VALUES(user_id_)
    </insert>
    <insert id="saveStudent">
        INSERT INTO student (user_id_,create_time_,update_time_,hide_flag_) VALUES(#{userId},NOW(),NOW(),0)
        ON DUPLICATE KEY UPDATE user_id_ = VALUES(user_id_)
    </insert>

    <select id="queryUserAccount" resultType="java.lang.Boolean">
        select count(1) from user_cash_account where #{userId}
    </select>

    <insert id="createUserAccount">
        INSERT INTO user_cash_account (user_id_,create_time_,update_time_)VALUES(#{userId},NOW(),NOW())
            ON DUPLICATE KEY UPDATE user_id_ = VALUES(user_id_)
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.yonge.cooleshow.auth.api.entity.SysUser">
        UPDATE sys_user
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="wxOpenid != null and wxOpenid != ''">
                wx_openid_ = #{wxOpenid},
            </if>
            <if test="avatar != null and avatar != ''">
                avatar_ = #{avatar},
            </if>
            <if test="email != null and email != ''">
                email_ = #{email},
            </if>
            <if test="gender != null">
                gender_ = #{gender},
            </if>
            <if test="salt != null and salt != ''">
                salt_ = #{salt},
            </if>
            <if test="username != null and username != ''">
                username_ = #{username},
            </if>
            <if test="userType != null and userType != ''">
                user_type_ = #{userType},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="lockFlag != null">
                lock_flag_ = #{lockFlag},
            </if>
            <if test="birthdate != null">
                birthdate_ = #{birthdate},
            </if>
            <if test="phone != null and phone != ''">
                phone_ = #{phone},
            </if>
            <if test="qqOpenid != null and qqOpenid != ''">
                qq_openid_ = #{qqOpenid},
            </if>
            <if test="nation != null and nation != ''">
                nation_ = #{nation},
            </if>
            <if test="imToken != null and imToken != ''">
                im_token_ = #{imToken},
            </if>
            <if test="idCardNo != null and idCardNo != ''">
                id_card_no_ = #{idCardNo},
            </if>
            <if test="password != null and password != ''">
                password_ = #{password},
            </if>
            <if test="wechatId != null and wechatId != ''">
                wechat_id_ = #{wechatId},
            </if>
            <if test="realName != null and realName != ''">
                real_name_ = #{realName},
            </if>
            <if test="certificateType != null">
                certificate_type_ = #{certificateType},
            </if>
            <if test="lastUsernameTime != null">
                last_username_time_ = #{lastUsernameTime},
            </if>
        </set>
        WHERE del_flag_ = 0 and id_ = #{id}
    </update>
    <update id="updatePasswordReq">
        UPDATE sys_user SET password_ = #{password} WHERE del_flag_ = 0 and phone_ = #{mobile}
    </update>
    <update id="refreshImToken">
        UPDATE sys_user SET im_token_ = #{imToken},update_time_ = NOW() WHERE del_flag_ = 0 and id_ = #{userId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		UPDATE sys_user SET del_flag_ = 1 WHERE id_ = #{id}
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysUser" parameterType="map">
        SELECT * FROM sys_user
        <include refid="queryCondition"/>
        <include refid="global.orderby"/>
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM sys_user
        <include refid="queryCondition"/>
    </select>

    <select id="queryByUsername" resultMap="SysUser">
		select * from sys_user where del_flag_ = 0 and username_ = #{username} OR phone_ = #{username} LIMIT 1 FOR UPDATE
	</select>

    <select id="queryByPhone" resultMap="SysUser">
		select * from sys_user where del_flag_ = 0 and phone_ = #{phone} OR username_ = #{phone} LIMIT 1
	</select>

    <select id="queryLockByPhone" resultMap="SysUser">
		select * from sys_user where del_flag_ = 0 and phone_ = #{phone} OR username_ = #{phone} LIMIT 1 FOR UPDATE
	</select>
    <resultMap id="ImUserModel" type="com.yonge.cooleshow.common.entity.ImUserModel">
        <result column="id_" property="id"/>
        <result property="name" column="username_"/>
        <result property="portrait" column="avatar_"/>
    </resultMap>
    <select id="getBasic" resultMap="ImUserModel">
        select CASE WHEN user_type_ LIKE '%TEACHER%' OR user_type_ LIKE '%SYSTEM%' THEN real_name_ ELSE username_ END username_,id_,avatar_
        from sys_user where del_flag_ = 0 and id_ = #{userId} LIMIT 1
    </select>
    
    <select id="queryByStatus" resultMap="SysUser">
        select * from sys_user where del_flag_ = 0 and lock_flag_ = #{status}
    </select>
    <select id="findUserBySalt" resultMap="SysUser">
        SELECT * FROM sys_user WHERE del_flag_ = 0 and salt_ IS NOT NULL AND salt_ = 'replace' AND user_type_ LIKE CONCAT('%',#{userType},'%') LIMIT 1
    </select>
    <select id="getTeacherOrganId" resultType="java.lang.Integer">
        SELECT organ_id_ FROM teacher WHERE id_ = #{userId}
    </select>
    <select id="queryByPhoneAndClient" resultMap="SysUser">
        select * from sys_user where del_flag_ = 0 and (phone_ = #{phone} OR username_ = #{phone}) AND user_type_ LIKE CONCAT('%',#{client},'%') LIMIT 1 FOR UPDATE
    </select>

    <update id="updateUserCard">
        UPDATE sys_user
        set
            <if test="param.realName != null and param.realName != ''">
                real_name_ = #{param.realName},
            </if>
            <if test="param.idCardNo != null and param.idCardNo != ''">
                id_card_no_ = #{param.idCardNo},
            </if>
            <if test="param.gender != null">
                gender_ = #{param.gender},
            </if>
            <if test="param.birthday != null">
                birthdate_ = #{param.birthday},
            </if>
        update_time_ = now()
        WHERE del_flag_ = 0 and id_ = #{param.userId}
    </update>

    <select id="getSetDetail" resultType="com.yonge.cooleshow.auth.api.vo.UserSetVo">
        SELECT
            u.avatar_ as avatar,
            u.username_ as username,
            u.gender_ as gender,
            u.phone_ as phone,
            u.birthdate_ as birthdate,
            (case when isnull(u.id_card_no_) then 0 else 1 end) as isReal
        FROM sys_user u
        where u.del_flag_ = 0 and u.id_ = #{id}
    </select>

    <update id="updatetSetDetail">
        UPDATE sys_user
        set
        <if test="param.avatar != null and param.avatar != ''">
            avatar_ = #{param.avatar},
        </if>
        <if test="param.username != null and param.username != ''">
            username_ = #{param.username},
            <if test="param.clientId != null and param.clientId == 'TEACHER'">
                last_username_time_ = now(),
            </if>
        </if>
        <if test="param.gender != null">
            gender_ = #{param.gender},
        </if>
        <if test="param.birthdate != null">
            birthdate_ = #{param.birthdate},
        </if>
        update_time_ = now()
        WHERE del_flag_ = 0 and id_ = #{id}
    </update>
    <update id="updateImGroupMember">
        UPDATE im_group_member SET avatar_ = #{avatar},nickname_ = #{username} WHERE user_id_ = #{userId}
    </update>
    <update id="updateImUserFriend">
        UPDATE im_user_friend SET friend_avatar_ = #{avatar},friend_nickname_ = #{username} WHERE friend_id_ = #{userId}
    </update>
    <update id="logoffById">
        update sys_user set del_flag_ = 1, phone_ = concat('D', ${num}, '+', phone_) where id_ = ${userId}
    </update>
    <select id="queryEmployeeList" resultMap="SysUser">
        select  su.*
        from employee e
        left join sys_user su on su.id_ = e.user_id_
        <where>
            su.del_flag_ = 0
            <if test="createStartDate != null and createStartDate != ''">
                and #{createStartDate} &lt;= e.create_time_
            </if>
            <if test="createEndDate != null and createEndDate != ''">
                and #{createEndDate} &gt; e.create_time_
            </if>
        </where>
    </select>

    <select id="queryHistoryUserById" resultMap="SysUser">
        select * from sys_user
        where phone_ like CONCAT('%',(select phone_ from sys_user where id_ = #{id} and del_flag_ = 0),'%')
    </select>

    <select id="getUnCompCourseNum" resultType="java.lang.Integer">
        select count(1) from course_schedule cs
        where cs.teacher_id_ = #{userId}
          and status_ in ('NOT_START','ING')
    </select>
    <select id="getTenantByClient" resultType="java.lang.Long">
        select tenant_id_ from
        <choose>
            <when test="clientId == 'STUDENT'">
                student
            </when>
            <when test="clientId == 'TEACHER'">
                teacher
            </when>
            <when test="clientId == 'ORGANIZATION'">
                tenant_staff
            </when>
            <otherwise>
                employee
            </otherwise>
        </choose>
        where user_id_ = #{userId}
    </select>

    <update id="updateLockStatus">
        <if test="sysUserType == 'STUDENT'">
            update student set lock_flag_ = #{lockFlag} where user_id_ = #{userId}
        </if>
        <if test="sysUserType == 'TEACHER'">
            update teacher set lock_flag_ = #{lockFlag} where user_id_ = #{userId}
        </if>
        <if test="sysUserType == 'SYSTEM'">
            update employee set lock_flag_ = #{lockFlag} where user_id_ = #{userId}
        </if>
    </update>
    <update id="logoffByPhone">
        update sys_user set del_flag_ = 1, phone_ = concat(#{num},'D', phone_),update_time_ = now() where phone_ = #{phone}
    </update>
    <update id="updateLockStatusByPhone">
        update student set lock_flag_ = 1 where user_id_ = (select id_ from sys_user where phone_ = #{phone});
        update teacher set lock_flag_ = 1 where user_id_ = (select id_ from sys_user where phone_ = #{phone});
        update employee set lock_flag_ = 1 where user_id_ = (select id_ from sys_user where phone_ = #{phone});
    </update>
    <select id="countByPhone" resultType="java.lang.Integer">
        SELECT count(1) FROM sys_user WHERE phone_ LIKE CONCAT('%',#{phone},'%')
    </select>

    <select id="getSysUserByOpenId" resultType="com.yonge.cooleshow.auth.api.entity.SysUser">
        SELECT t1.*
        FROM sys_user t1 LEFT JOIN
        <choose>
            <when test="clientId == 'STUDENT'">
                student
            </when>
            <when test="clientId == 'TEACHER'">
                teacher
            </when>
            <when test="clientId == 'ORGANIZATION'">
                tenant_staff
            </when>
            <otherwise>
                employee
            </otherwise>
        </choose> t2 ON (t1.id_ = t2.user_id_)
        WHERE t1.del_flag_ = 0 and t2.wx_openid_ = #{openId} LIMIT 1
    </select>

    <update id="updateStudentHideFlag">
        update student set hide_flag_ = #{hideFlag} where user_id_ = #{userId}
    </update>

    <update id="updateAvatar">
        <if test="clientId == 'STUDENT'">
            update student set avatar_ = #{avatar} where user_id_ = #{id}
        </if>
        <if test="clientId == 'TEACHER'">
            update teacher set avatar_ = #{avatar} where user_id_ = #{id}
        </if>
        <if test="clientId == 'SYSTEM'">
            update sys_user set avatar_ = #{avatar} where id_ = #{id}
        </if>
    </update>
</mapper>
