<?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.sapi.mapper.system.SchoolMapper">
    <!--删除-->
    <update id="deleteById">
        update t_school set schoolstate=10 where schoolid=#{schoolid}
    </update>

    <select id="listSchool" resultType="java.util.Map">
        select f.fschoolid as schoolid,s2.schoolname
        from t_school_friend f left join t_school s2 on f.fschoolid=s2.schoolid
        where f.schoolid=#{schoolid}
    </select>
    <!--加入学校列表-->
    <select id="listJoinSchool" resultType="java.util.Map">
        select f.schoolid,s2.schoolname
        from t_school_friend f left join t_school s2 on f.schoolid=s2.schoolid
        where f.fschoolid=#{schoolid}
    </select>
    <select id="findAll" resultType="java.util.Map">
        select s.schoolid,s.schoolname,s.shortname,s.schoollevel,s.errorrate,s.`year`,s.docklc
        ,(select u.loginname from t_user u where u.schoolid=s.schoolid and u.usertype=0 and u.userstate=1
        ORDER BY u.userid LIMIT 1)loginname
        ,(select count(sf.schoolid) from t_school_friend sf where sf.fschoolid=s.schoolid)joinnum
        ,(select count(sf.fschoolid) from t_school_friend sf where sf.schoolid=s.schoolid)friendnum
        from t_school s
        where s.schoolstate=1 order by s.createtime desc
    </select>
    <!--学年升级-->
    <update id="upgrade">
        update t_school set year=year+1 where schoolid=#{schoolid};
    </update>
    <!--学年降级-->
    <update id="downgrade">
        delete gs.*,g.*,cs.*,ct.*,c.*
        from t_class c left join t_class_student cs on cs.classid=c.classid
        left join t_class_teacher ct on ct.classid=c.classid
        left join t_group g on c.classid=g.classid
        left join t_group_student gs on gs.groupid=g.groupid
        where c.schoolid=#{schoolid} and c.year=#{year};
        update t_school set year=year-1 where schoolid=#{schoolid}
    </update>
    <!--学校选择列表-->
    <select id="listForSel" resultType="java.util.Map">
        select schoolid,schoolname,schoollevel from t_school where schoolstate=1
        <if test="schoolname!=null and schoolname!=''">
            and schoolname like concat('%', #{schoolname}, '%')
        </if>
        order by createtime desc
    </select>

    <!--获取区域码-->
    <select id="getSchoolDetailById" resultType="com.xhkjedu.sapi.model.system.TSchool">
      select * ,(select cloudcode from t_cloud limit 1) cloudcode
      from t_school where schoolid=#{schoolid}
    </select>
    <update id="updateSchoolDock">
        update t_school set docklc=#{docklc} where schoolid=#{schoolid}
    </update>
    <!--根据学校名称查找学校数量-->
    <select id="countBySchoolName" resultType="java.lang.Integer">
        select count(*) from t_school where schoolstate=1 and schoolname=#{schoolname}
    </select>
    <!--获取学校当前年份-->
    <select id="getYearById" resultType="java.lang.Integer">
        select year from t_school where schoolid=#{schoolid}
    </select>
    <!--获取用户当前年份-->
    <select id="getYearByUserId" resultType="java.lang.Integer">
        select s.year from t_user u left join t_school s on u.schoolid=s.schoolid
        where u.userid=#{userid}
    </select>
    <!--获取学校账号数量-->
    <select id="getUserNumById" resultType="java.lang.Integer">
        select usernum from t_school where schoolid=#{schoolid}
    </select>
    <!--修改-->
    <update id="updateById">
        update t_school set schoolname=#{school.schoolname},shortname=#{school.shortname},errorrate=#{school.errorrate}
        ,schoolbindmac=#{school.schoolbindmac},docklc=#{school.docklc},usernum=#{school.usernum},livenum=#{school.livenum}
        ,livetime=#{school.livetime},coordinate=#{school.coordinate},limitenolen=#{school.limitenolen}
        where schoolid=#{school.schoolid}
    </update>
    <!--根据域名获取学校id-->
    <select id="getSchoolIdByDomain" resultType="java.lang.Integer">
        select schoolid from t_school where schoolid=#{schoolid} and schooldomain=#{schooldomain}
    </select>
    <!--获取准考证号长度限制-->
    <select id="getLimiteNoLen" resultType="java.lang.Integer">
        select limitenolen from t_school where schoolid=#{schoolid}
    </select>
    <!--获取学年列表-->
    <select id="listYear" resultType="java.lang.Integer">
        select c.year from t_class c where c.schoolid=#{schoolid} and c.classtype=1 and c.classstate=1 group by c.year
    </select>
    <!--获取区域学年列表-->
    <select id="listCloudYear" resultType="java.lang.Integer">
        select c.year from t_class c where c.classtype=1 and c.classstate=1 group by c.year
    </select>
    <!--详情-->
    <select id="findById" resultType="java.util.Map">
        select s.*,tu.username createname
        ,(select u.loginname from t_user u where u.schoolid=s.schoolid and u.usertype=0 and u.userstate=1
        order by u.userid limit 1) as loginname
        from t_school s left join t_user tu on s.createid = tu.userid
        where s.schoolid=#{schoolid}
    </select>
    <!--伙伴学校选择列表-->
    <select id="listFriendForSel" resultType="java.util.Map">
        select s2.schoolid,s2.schoolname,s2.schoollogo
        from t_school s left join t_school s2 on s.schoollevel=s2.schoollevel and s.year=s2.year
        where s.schoolid=#{school.schoolid} and s2.schoolstate=1 and s2.schoolid!=#{school.schoolid}
        <if test="school.schoolname!=null and school.schoolname!=''">
            and s2.schoolname like concat('%', #{school.schoolname}, '%')
        </if>
        order by s2.createtime desc
    </select>
    <!--学校升级记录-->
    <resultMap id="upResult" type="java.util.Map">
        <result property="suid" column="suid"/>
        <result property="uptype" column="uptype"/>
        <result property="oldyear" column="oldyear"/>
        <result property="newyear" column="newyear"/>
        <result property="uptime" column="uptime"/>
        <result property="upresult" column="upresult"/>
        <result property="upcomm" column="upcomm"/>
        <result property="username" column="username"/>
        <collection property="histories" javaType="java.util.List" select="listHistory" column="suid">
        </collection>
    </resultMap>
    <select id="listHistory" resultType="java.util.Map">
        select gradeid,classyxznum,classsxznum,classyxnnum,classsxnnum,stuyupnum,stusupnum
        from t_school_upgrade_history where suid=#{suid} order by gradeid
    </select>
    <select id="listUpgrade" resultMap="upResult">
        select su.suid,uptype,oldyear,newyear,uptime,upresult,upcomm,u.username
        from t_school_upgrade su left join t_user u on su.upuserid=u.userid
        where su.schoolid=#{schoolid} order by su.suid desc
    </select>
</mapper>
