<?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.assess.AssessMapper">
    <!--评价列表-->
    <select id="listAll" resultType="java.util.Map">
        select a.assessid,a.assessname,a.comm,a.createtime,u.username createname
        from t_assess a left join t_user u on a.createid=u.userid
        where a.schoolid=#{assess.schoolid} and a.templateid=#{assess.templateid}
        <if test="assess.usertype==1">
            and a.createid=#{assess.createid}
        </if>
        order by a.assessid desc
    </select>

    <!--评价详情-->
    <resultMap id="assessResult" type="java.util.Map">
        <result property="quotaid" column="quotapid"></result>
        <result property="quotaname" column="quotapname"></result>
        <result property="quotacode" column="quotapcode"></result>
        <collection property="children" ofType="java.util.Map" javaType="java.util.List">
            <result property="quotaid" column="quotaid"></result>
            <result property="quotaname" column="quotaname"></result>
            <result property="quotacode" column="quotacode"></result>
            <collection property="users" ofType="java.util.Map" javaType="java.util.List">
                <result property="auid" column="auid"></result>
                <result property="userid" column="userid"></result>
                <result property="username" column="username"></result>
                <result property="usertype" column="usertype"></result>
                <result property="autype" column="autype"></result>
                <result property="aunum" column="aunum"></result>
                <result property="autime" column="autime"></result>
                <result property="comm" column="comm"></result>
                <result property="stutype" column="stutype"></result>
                <result property="gradeid" column="gradeid"></result>
                <result property="snum" column="snum"></result>
                <result property="createtime" column="createtime"></result>
                <result property="createname" column="createname"></result>
                <result property="classname" column="classname"></result>
                <result property="subjectname" column="subjectname"></result>
                <collection property="files" ofType="java.util.Map" javaType="java.util.List"
                            select="listFile" column="auid">
                </collection>
            </collection>
        </collection>
    </resultMap>
    <select id="listFile" resultType="java.util.Map">
        select af.filename,af.fileclass,af.fileurl,af.filecover,af.converted,af.pdfurl,af.suffix,af.filesize,af.duration
        from t_assess_file af where af.auid=#{auid}
    </select>
    <select id="listByAssess" resultMap="assessResult">
        select aq.quotapid,aq1.quotaname quotapname,aq1.quotacode quotapcode
        ,aq.quotaid,aq.quotaname,aq.quotaorder,aq.quotacode
        ,au.userid,u.username,au.usertype,au.autype,au.aunum,au.autime,au.comm,au.auid
        ,au.stutype,au.gradeid,au.snum,au.createtime
        ,(select u.username from t_user u where u.userid=au.createid)createname
        ,(select c.classname from t_class c where c.classid=au.classid)classname
        ,(select s.subjectname from t_subject s where s.subjectid=au.subjectid)subjectname
        from t_assess_user au left join t_assess_quota aq on aq.quotaid=au.quotaid
        left join t_assess_quota aq1 on aq.quotapid = aq1.quotaid
        left join t_user u on au.userid=u.userid
        where au.templateid=#{assess.templateid}
        <if test="assess.assessid!=null and assess.assessid!=0">
            and au.assessid=#{assess.assessid}
        </if>
        union select aq.quotapid,aq1.quotaname quotapname,aq1.quotacode quotapcode
        ,aq.quotaid,aq.quotaname,aq.quotaorder,aq.quotacode
        ,null userid,null username,null usertype,null autype,null aunum,null autime,null comm,null auid
        ,null stutype,null gradeid,null snum,null createtime,null createname,null classname,null subjectname
        from t_assess_template_quota atq left join t_assess_quota aq on atq.quotaid=aq.quotaid
        left join t_assess_quota aq1 on aq.quotapid = aq1.quotaid where atq.templateid=#{assess.templateid}
        order by quotaorder,quotaid,autime
    </select>

    <!--评价统计-->
    <resultMap id="analyzeResult" type="java.util.Map">
        <result property="quotaid" column="quotapid"></result>
        <result property="quotaname" column="quotapname"></result>
        <collection property="children" ofType="java.util.Map" javaType="java.util.List">
            <result property="quotaid" column="quotaid"></result>
            <result property="quotaname" column="quotaname"></result>
            <collection property="users" ofType="java.util.Map" javaType="java.util.List">
                <result property="userid" column="userid"></result>
                <result property="username" column="username"></result>
                <result property="aunum" column="aunum"></result>
                <result property="usertype" column="usertype"></result>
                <result property="autype" column="autype"></result>
            </collection>
        </collection>
    </resultMap>
    <select id="listAnalyze" resultMap="analyzeResult">
        select aq.quotapid,aq1.quotaname quotapname,aq.quotaid,aq.quotaname,aq.quotaorder
        ,au.userid,u.username,au.usertype,au.autype,sum(au.aunum)aunum
        from t_assess_user au left join t_assess_quota aq on aq.quotaid=au.quotaid
        left join t_assess_quota aq1 on aq.quotapid = aq1.quotaid
        left join t_user u on au.userid=u.userid
        where au.templateid=#{assess.templateid}
        <if test="assess.starttime!=null and assess.starttime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(au.autime,'%Y%m%d'))>=#{assess.starttime}
        </if>
        <if test="assess.stoptime!=null and assess.stoptime!=0">
            and UNIX_TIMESTAMP(FROM_UNIXTIME(au.autime,'%Y%m%d'))&lt;=#{assess.stoptime}
        </if>
        <if test="assess.gradeid!=null and assess.gradeid!=0">
            and au.gradeid=#{assess.gradeid}
        </if>
        <if test="assess.classid!=null and assess.classid!=0">
            and au.classid=#{assess.classid}
        </if>
        <if test="assess.subjectid!=null and assess.subjectid!='0'.toString() and assess.subjectid!=''">
            and au.subjectid=#{assess.subjectid}
        </if>
        group by au.userid,au.autype,au.quotaid
        union select aq.quotapid,aq1.quotaname quotapname,aq.quotaid,aq.quotaname,aq.quotaorder
        ,null userid,null username,null usertype,null autype,null aunum
        from t_assess_template_quota atq left join t_assess_quota aq on atq.quotaid=aq.quotaid
        left join t_assess_quota aq1 on aq.quotapid = aq1.quotaid where atq.templateid=#{assess.templateid}
        order by quotaorder,quotaid
    </select>
</mapper>
