<?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>

    <resultMap id="listSchools" type="java.util.Map">
        <result property="schoolid" column="schoolid"></result>
        <result property="schoolname" column="schoolname"></result>
        <result property="shortname" column="shortname"></result>
        <result property="schoollevel" column="schoollevel"></result>
        <result property="bindmac" column="bindmac"></result>
        <result property="schoolstate" column="schoolstate"></result>
        <result property="year" column="year"></result>
        <result property="errorrate" column="errorrate"></result>
        <result property="schooldomain" column="schooldomain"></result>
        <result property="tesupport" column="tesupport"></result>
        <result property="recordno" column="recordno"></result>
        <result property="limitenolen" column="limitenolen"></result>
        <result property="livenum" column="livenum"></result>
        <result property="livetime" column="livetime"></result>
        <result property="coordinate" column="coordinate"></result>
        <result property="docklc" column="docklc"></result>
        <result property="usernum" column="usernum"></result>
        <result property="createtime" column="createtime"></result>
        <result property="createname" column="createname"></result>
        <result property="loginname" column="loginname"></result>
        <collection property="schools" ofType="java.util.Map" javaType="java.util.List" select="listSchool" column="schoolid">
        </collection>
    </resultMap>
    <select id="listSchool" resultType="java.util.Map">
        select f.fschoolid 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="findAll" resultMap="listSchools">
        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 ASC LIMIT 1)loginname
        from t_school s left join t_user tu on s.createid = tu.userid
        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>
    </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}
        ,bindmac=#{school.bindmac},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>
</mapper>
