<?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.xhkjedu.suser.mapper.system.UserMapper">

    <!-- 登录 -->
    <select id="getUserByLoginname" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.userid,u.username,u.loginname,u.loginpwd,u.userphone,u.userbirthday,u.cardid,u.headpic,u.usertype,u.usersex,
        u.userstate,u.opepassword,u.createid,u.createtime,u.deleteid,u.deletetime,u.schoolid,u.bindmac,u.mac,u.usertime,u.userlock
        ,s.year,s.schoolname,s.schoollevel,s.schoolstate,s.bindmac sbindmac,s.errorrate,s.schooldomain
        ,c.cloudcode,c.stuweb
        from t_cloud c,t_user u left join t_school s on u.schoolid = s.schoolid
        where binary u.loginname=#{loginname} limit 1
    </select>
    <!-- 手机号登录 -->
    <select id="getUserByPhone" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.userid,u.username,u.loginname,u.loginpwd,u.userphone,u.userbirthday,u.cardid,u.headpic,u.usertype,u.usersex,
            u.userstate,u.opepassword,u.createid,u.createtime,u.deleteid,u.deletetime,u.schoolid,u.bindmac,u.mac,u.usertime,u.userlock
             ,s.year,s.schoolname,s.schoollevel,s.schoolstate,s.bindmac sbindmac,s.errorrate,s.schooldomain
             ,c.cloudcode,c.stuweb
        from t_cloud c,t_user u left join t_school s on u.schoolid = s.schoolid
        where u.bindphoned=1 and u.userphone=#{userphone} limit 1
    </select>
    <!--学生andorid登录，20211221添加GROUP_CONCAT，因为不绑定的情况下一个mac可以多个帐号登录-->
    <select id="getUserByLoginnameMac" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.userid,u.username,u.loginname,u.loginpwd,u.userbirthday,u.cardid,u.headpic,u.usertype,u.usersex,u.userphone,
        u.userstate,u.opepassword,u.createid,u.createtime,u.deleteid,u.deletetime,u.schoolid,u.bindmac,u.mac,u.usertime,u.userlock
        ,s.year,s.schoolname,s.schoollevel,s.schoolstate,s.bindmac sbindmac,s.errorrate,s.schooldomain
        ,(case when u.userphone is null or u.userphone='' then 0 else 1 end)hasphone
        ,(select GROUP_CONCAT(u1.loginname) from t_user u1 where u1.mac=#{mac} and u1.schoolid=u.schoolid)macname
        ,c.cloudcode,c.bindphone
        ,(select count(cs.csid) from t_class_student cs left join t_class c on cs.classid=c.classid
        where cs.studentid=u.userid and c.classstate=1 and c.year=s.year and c.classtype=1)xznum
        from t_user u,t_school s,t_cloud c
        where binary u.loginname=#{loginname} and u.schoolid = s.schoolid limit 1
    </select>
    <select id="getStudentByuserIdMac" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.userid
             ,(case when u.userphone is null or u.userphone='' then 0 else 1 end)hasphone
             ,(select GROUP_CONCAT(u1.loginname) from t_user u1 where u1.mac=#{mac} and u1.schoolid=u.schoolid)macname
             ,c.cloudcode,c.bindphone
             ,(select count(cs.csid) from t_class_student cs left join t_class c on cs.classid=c.classid
               where cs.studentid=u.userid and c.classstate=1 and c.year=s.year and c.classtype=1)xznum
        from t_user u,t_school s,t_cloud c
        where binary u.userid=#{userid} and u.schoolid = s.schoolid limit 1
    </select>
    <!--校验手机号-->
    <select id="checkUserPhone" resultType="java.lang.Boolean">
        select (case when c.bindphone=1 and (u.userphone is null or u.userphone='') then true else false end)
        from t_user u,t_cloud c where u.userid=#{userid}
    </select>
    <!--删除用户-->
    <update id="deleteById">
        update t_user set userstate=2,deleteid=#{user.deleteid},deletetime=#{user.deletetime}
        where userid=#{user.userid}
    </update>
    <!--获取用户列表-->
    <select id="findAll" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.*,u2.username createname from t_user u
        left join t_user u2 on u2.userid=u.createid
        where u.schoolid=#{user.schoolid}
        <if test="user.loginname!=null and user.loginname!=''">
            and u.loginname like '%${user.loginname}%'
            or u.username like '%${user.loginname}%'
        </if>
        order by u.createtime desc
    </select>
    <!--获取用户详情-->
    <select id="findById" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.*,u2.username createname,s.year,s.schoolname,s.schoollevel,s.schoolstate,s.bindmac sbindmac,s.errorrate
        ,(select cloudcode from t_cloud limit 1)cloudcode
        from t_user u
        left join t_user u2 on u2.userid=u.createid
        left join t_school s on u.schoolid = s.schoolid
        where u.userid=#{userid} limit 1
    </select>
    <!--修改用户密码-->
    <update id="updatePwd">
        update t_user set loginpwd=#{user.loginpwd} where userid=#{user.userid}
    </update>
    <!--修改用户头像-->
    <update id="updateHeadPic">
        update t_user set headpic=#{user.headpic},headtime=UNIX_TIMESTAMP() where userid=#{user.userid}
    </update>
    <!--检查登录账号是否存在-->
    <select id="checkLoginName" resultType="java.util.Map">
        select u.userid,u.userstate,u.usertype,u.schoolid from t_user u where binary u.loginname=#{loginname}
    </select>
    <!--根据登录账号获取用户详情-->
    <select id="getObjByLoginName" resultType="com.xhkjedu.suser.model.system.TUser">
        select u.userid,u.username,u.loginname,u.examno,u.usersex,u.usertype,u.userstate,u.schoolid
        from t_user u where binary u.loginname=#{loginname} limit 1
    </select>
    <!--修改用户状态-->
    <update id="updateState">
        update t_user set userstate=#{userstate},createtime=UNIX_TIMESTAMP() where userid=#{userid}
    </update>

    <!-- 用户设置操作密码 -->
    <update id="updateOpepasswordByUserId">
        update t_user set opepassword=#{opepassword} where userid=#{userid}
    </update>
    <!-- 验证用户操作密码 -->
    <select id="getUserIdByOpepassword" resultType="java.lang.Integer">
        select count(*) from t_user where userid=#{userid} and opepassword=#{opepassword}
    </select>

    <resultMap id="teacherInfoMap" type="java.util.Map">
        <result property="userid" column="userid"></result>
        <result property="username" column="username"></result>
        <result property="headpic" column="headpic"></result>
        <result property="subjectname" column="subjectname"></result>
        <result property="loginname" column="loginname"></result>
        <result property="usersex" column="usersex"></result>
        <result property="userphone" column="userphone"></result>
        <result property="usertime" column="usertime"></result>
        <result property="createtime" column="createtime"></result>
        <result property="rolename" column="rolename"></result>
        <collection property="lsblist" ofType="java.util.Map" javaType="java.util.List">
            <result property="subjectname" column="subjectname"></result>
            <result property="versionname" column="versionname"></result>
            <result property="lsbname" column="lsbname"></result>
        </collection>
        <collection property="classlist" ofType="java.util.Map"
                    javaType="java.util.List" select="listTeacherClass" column="{userid=userid,classtype=classtype,year=year}">
        </collection>
    </resultMap>

    <!--教师任教班级-->
    <select id="listTeacherClass" resultType="java.util.Map">
        select c.classid,c.classname,c.gradeid from t_class c
        left join t_class_teacher ct on c.classid=ct.classid
        where ct.teacherid=#{userid} and c.classstate=1 and c.year=#{year}
        <if test="classtype!=null and classtype!=0">
            <if test="classtype!=11">and c.classtype=#{classtype}</if>
            <if test="classtype==11">and c.classtype in(1,2)</if>
        </if>
        <if test="classtype==null or classtype==0">
            and c.classtype!=99
        </if>
        order by c.gradeid,c.classtype,c.classorder
    </select>
    <!--教师基本信息-->
    <select id="getTeacherInfo" resultMap="teacherInfoMap">
        select u.userid,u.username,u.headpic,u.loginname,u.usersex,u.userphone,u.usertime,u.createtime
        ,s.subjectname,v.versionname,sb.lsbname
        ,(select group_concat(r.rolename separator '  ')
        from t_user_role ur left join t_role r on ur.roleid=r.roleid where ur.userid=#{userid})rolename
        ,#{classtype} classtype,#{year} year
        from t_user u
        left join t_teacher_powersb ts on u.userid = ts.teacherid
        left join t_subject_book sb on ts.lsbid=sb.lsbid
        left join t_subject s on sb.subjectid=s.subjectid
        LEFT JOIN t_version v ON sb.versionid=v.versionid
        where u.userid=#{userid}
        order by s.subjectorder,v.versionorder,sb.lsborder
    </select>
    <!--更新学生硬件绑定信息-->
    <update id="updateBindMac">
        update t_user set bindmac=#{user.bindmac},mac=#{user.mac} where userid=#{user.userid}
    </update>
    <!--通过短信修改密码-->
    <update id="updatePwdByCode">
        update t_user set loginpwd=#{user.loginpwd} where userphone=#{user.userphone}
    </update>
    <!--批量修改用户密码-->
    <update id="batchUpdatePwd">
        update t_user set loginpwd=#{user.loginpwd} where schoolid=#{user.schoolid}
        and usertype in (
        <foreach collection="user.usertypes" item="usertype" index="index" separator=",">
            ${usertype}
        </foreach>
        )
    </update>

    <!--获取学校账号数量-->
    <select id="getUserNumBySchoolId" resultType="java.lang.Integer">
        select count(*) from t_user where schoolid=#{schoolid} and userstate=1
    </select>
    <!--用户选择列表-->
    <select id="listForSel" resultType="java.util.Map">
        select u.userid,u.username,u.loginname
        from t_user u left join t_class_teacher ct on ct.teacherid=u.userid
        left join t_class c on c.classid=ct.classid
        left join t_teacher_powersb tp on tp.teacherid=u.userid
        left join t_subject_book sb on sb.lsbid=tp.lsbid
        where u.schoolid=#{user.schoolid} and u.userstate=1 and u.usertype=1
        and c.gradeid=#{user.gradeid} and c.year=#{year} and c.classid=#{user.classid}
        and sb.subjectid=#{user.subjectid}
        <if test="user.username!=null and user.username!=''">
            and u.username like '%${user.username}%'
        </if>
        group by u.userid
        order by convert(u.username using gbk)
    </select>
    <!--获取头像是否可以修改-->
    <select id="getHeadTime" resultType="java.lang.Integer">
        select (case when headtime is null then 1 when headtime+6*30*24*60*60 &lt; unix_timestamp() then 1 else 0 end)
        from t_user where userid=#{userid}
    </select>
    <!--绑定密保-->
    <update id="bindUseCert">
        update t_user set bindsecret=1 where userid=#{userid}
    </update>
    <!--绑定手机-->
    <update id="bindUserPhone">
        update t_user set bindphoned=1,userphone=#{userphone} where userid=#{userid}
    </update>
    <!--根据账号获取绑定方式-->
    <select id="getBindByName" resultType="java.util.Map">
        select userid,username,userphone,bindphoned,bindsecret
        from t_user where binary loginname=#{loginname} limit 1
    </select>
    <!--根据用户id获取绑定方式-->
    <select id="getBindById" resultType="java.util.Map">
        select userphone,bindphoned,bindsecret
        from t_user where userid=#{userid} limit 1
    </select>
    <!--获取手机号绑定的其他用户数量-->
    <select id="getNumByUserPhone" resultType="java.lang.Integer">
        select count(*) from t_user where userid!=#{userid} and userphone=#{userphone}
    </select>
    <!--根据用户id获取密码-->
    <select id="getPwdByUserId" resultType="java.lang.String">
        select loginpwd from t_user where userid=#{userid}
    </select>
    <!--修改用户锁定状态-->
    <update id="updateUserLock">
        update t_user set userlock=#{userlock} where userid=#{userid}
    </update>
    <!--获取用户类型和修改头像时间-->
    <select id="getUserTypeAndHeadTime" resultType="com.xhkjedu.suser.model.system.TUser">
        select usertype,(case when headtime is null then 1 when headtime+6*30*24*60*60 &lt; unix_timestamp() then 1 else 0 end)headtime
        from t_user where userid=#{userid}
    </select>
    <!--绑定手机号数量-->
    <select id="getBindNumByUserPhone" resultType="java.lang.Integer">
        select count(*) from t_user where bindphoned=1 and userphone=#{userphone}
    </select>
    <!--绑定手机号用户列表-->
    <select id="listBindPhoneUser" resultType="java.util.Map">
        select loginname,userphone from t_user where userphone!=''
    </select>
    <!--批量解绑-->
    <update id="batchRemoveBind">
        update t_user set bindmac=0,mac=NULL where userid in
        <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
            ${userid}
        </foreach>
    </update>
    <!--批量修改密码-->
    <update id="batchUpdatePwd2">
        update t_user set loginpwd=#{loginpwd} where userid in
        <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
            ${userid}
        </foreach>
    </update>
    <!--批量修有效期-->
    <update id="batchUpdateTime">
        update t_user set usertime=#{usertime} where userid in
        <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
            ${userid}
        </foreach>
    </update>
    <!--批量更新用户状态-->
    <update id="batchUpdateUserState">
        update t_user set userstate=#{userstate},createtime=UNIX_TIMESTAMP() where userid in
        <foreach collection="userids" item="userid" index="index" open="(" separator="," close=")">
            ${userid}
        </foreach>
    </update>

    <!--根据用户id获取用户详细信息，学校基本信息-->
    <select id="getUserSchoolByUserid" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.userid,u.username,u.loginname,u.loginpwd,u.userphone,u.userbirthday,u.cardid,u.headpic,u.usertype,
            u.userstate,u.opepassword,u.createid,u.createtime,u.deleteid,u.deletetime,u.schoolid,u.bindmac,u.mac,u.usertime,
            u.userlock,u.bindphoned,s.year,s.schoolname,s.schoollevel,s.schoolstate,s.bindmac sbindmac,s.errorrate
        from t_user u left join t_school s on u.schoolid = s.schoolid
        where u.userid=#{userid}
    </select>
    <!--根据手机号获取用户基本信息-->
    <select id="getUserByUserPhone" resultType="com.xhkjedu.suser.vo.system.UserVo">
        select u.usertype,u.userstate,u.usertime,u.userlock,u.bindphoned,s.schoolstate,c.stuweb
        from t_cloud c,t_user u left join t_school s on u.schoolid = s.schoolid
        where u.userphone=#{userphone} limit 1
    </select>
    <!--根据登录名查找用户集合-->
    <select id="listByLoginNames" resultType="com.xhkjedu.suser.model.system.TUser">
        select u.userid,u.username,u.loginname,u.examno,u.usersex,u.usertype,u.userstate,u.schoolid
        from t_user u where
        <foreach collection="loginnames" item="loginname" separator="or">
            binary u.loginname=#{loginname}
        </foreach>
    </select>
    <!--根据准考证号查找用户集合-->
    <select id="listByExamnos" resultType="com.xhkjedu.suser.model.system.TUser">
        select u.userid,u.username,u.loginname,u.examno,u.usersex,u.usertype,u.userstate,u.schoolid
        from t_user u where
        <foreach collection="examnos" item="examno" separator="or">
            binary u.examno=#{examno}
        </foreach>
    </select>
    <!--批量更新用户信息-->
    <update id="batchUpdateUser">
        <foreach collection="users" item="u" separator="or">
            update t_user set username=#{u.username},examno=#{u.examno},usersex=#{u.usersex}
            where userid=#{u.userid}
        </foreach>
    </update>
    <!--更新学生信息-->
    <update id="updateStudent">
        update t_user set username=#{user.username},loginname=#{user.loginname},studentno=#{user.studentno},examno=#{user.examno}
        ,usertime =#{user.usertime},createtime=#{user.createtime},usersex=#{user.usersex}
        <if test="user.userphone!=null and user.userphone!=''">,userphone =#{user.userphone}</if>
        <if test="user.bindphoned!=null">,bindphoned =#{user.bindphoned}</if>
        where userid=#{user.userid}
    </update>
</mapper>
