<?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.biz.dal.mapper.SysUserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.SysUser">
        <id column="id_" property="id" />
        <result column="username_" property="username" />
        <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="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="real_name_" property="realName" />
        <result column="id_card_no_" property="idCardNo" />
        <result column="certificate_type_" property="certificateType" />
        <result column="is_super_admin_" property="superAdmin" />
        <result column="wechat_id_" property="wechatId" />
        <result column="del_flag_" property="delFlag" />
        <result column="last_username_time_" property="lastUsernameTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id_, username_, password_, salt_, phone_, avatar_, create_time_, update_time_, lock_flag_, wx_openid_, qq_openid_, user_type_, gender_, nation_, birthdate_, email_, im_token_, real_name_, id_card_no_, certificate_type_, is_super_admin_, wechat_id_, del_flag_, last_username_time_
    </sql>

    <select id="queryCouponIssueUser" resultType="com.yonge.cooleshow.biz.dal.vo.CouponIssueUserVo">

        select
            su.id_ as userId,
            su.username_ as username,
            su.real_name_ as realName,
            #{client} as clientType,
            su.phone_ as phone
        from
        <choose>
            <when test="client.code == 'TEACHER'">
                teacher t
            </when>
            <when test="client.code == 'STUDENT'">
                student t
            </when>
        </choose>
        left join sys_user su on su.id_ = t.user_id_
        <where>
            t.lock_flag_ = 0 and su.del_flag_ = 0
            <if test="keyword != null and keyword != ''">
                and (
                su.real_name_ like concat('%',#{keyword},'%')
                or su.username_ like concat('%',#{keyword},'%')
                or su.phone_ like concat('%',#{keyword},'%')
                or su.id_ like concat('%',#{keyword},'%')
                )
            </if>
            <if test="client.code == 'STUDENT'">
                AND t.tenant_id_ = -1
            </if>
        </where>

    </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="selectMessageReceives"
            resultType="com.yonge.cooleshow.biz.dal.wrapper.im.CustomerService$MessageReceives">
        SELECT t1.id_ AS userId, t1.username_ FROM sys_user t1
        <if test="param.clientType != null">
            <if test="param.clientType.code == 'TEACHER'"> LEFT JOIN teacher t2 ON (t1.id_ = t2.user_id_)</if>
            <if test="param.clientType.code == 'STUDENT'"> LEFT JOIN student t2 ON (t1.id_ = t2.user_id_)</if>
        </if>
        <where>
            <if test="param.receives != null">
                AND t1.phone_ IN (<foreach collection="param.receives" separator="," item="item">#{item}</foreach>)
            </if>
            <if test="param.subjectId != null">
                AND FIND_IN_SET(#{param.subjectId}, t2.subject_id_)
            </if>
        </where>
        LIMIT 1500
    </select>

    <select id="findUserByPhone" resultType="com.yonge.cooleshow.auth.api.entity.SysUser">
        SELECT * FROM sys_user WHERE phone_ = #{phone} AND del_flag_ = 0
    </select>

    <select id="getByUserId" resultType="com.yonge.cooleshow.auth.api.entity.SysUser">
        select id_ as id,
               username_ as username,
               password_ as password,
               salt_ as salt,
        del_flag_ as delFlag,
               phone_ as phone,
               avatar_ as avatar,
               lock_flag_ as lockFlag,
               user_type_ as userType,
               gender_ as gender,
               nation_ as nation,
               birthdate_ as birthdate,
               email_ as email,
               im_token_ as imToken,
               real_name_ as realName,
               id_card_no_ as idCardNo,
               certificate_type_ as certificateType
        from sys_user
        where id_ = #{userId}
    </select>
    <!--客服消息接收者-->
</mapper>
