<?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.AssessQuotaMapper">
    <select id="listAllQuotas" resultType="com.xhkjedu.sapi.vo.assess.AssessQuotaVo">
        select q.quotaid,q.quotaname,q.quotaorder,q.quotapid,q.quotastate,q.quotacode,
        q.comm,q.createid,q.createtime,u.username as createname
        from t_assess_quota q left join t_user u on q.createid=u.userid
        where q.schoolid=#{schoolid} and q.quotastate=1 order by q.quotaorder,q.quotaid
    </select>

    <!--指标包含子指标数量-->
    <select id="selectQuotaCount" resultType="java.lang.Integer">
        select count(*) from t_assess_quota where quotastate=1 and quotapid=#{quotaid}
    </select>

    <!--修改指标状态-->
    <update id="updateQuotaState">
        update t_assess_quota set quotastate=#{quotastate} where quotaid=#{quotaid}
    </update>

    <!--删除模版指标关联关系-->
    <delete id="deleteTemplateQuota">
        delete from t_assess_template_quota where quotaid=#{quotaid}
    </delete>

    <!--获取模版下指标（树形）-->
    <resultMap id="quotaMap" 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="quotapid" column="quotapid"></result>
            <result property="quotacode" column="quotacode"></result>
        </collection>
    </resultMap>
    <select id="listQuotaByTemplateid" resultMap="quotaMap">
        select q1.quotaid as quotapid,q1.quotaname as quotapname,q1.quotacode quotapcode,
        q2.quotaid,q2.quotaname,q2.quotapid,q2.quotacode
        from t_assess_quota q1
        left join t_assess_quota q2 on q1.quotaid=q2.quotapid
        left join t_assess_template_quota tq on q2.quotaid=tq.quotaid
        where tq.templateid=#{templateid} order by q1.quotaorder,q2.quotaorder
    </select>
    <!--根据指标名称或编码获取指标id-->
    <select id="getQuotaIdByName" resultType="java.lang.Integer">
        select quotaid from t_assess_quota where schoolid=#{schoolid} and quotastate=1
        and (quotaname=#{quotaname} or quotacode=#{quotaname})
    </select>
    <!--获取模板下的子指标-->
    <select id="listChildQuotaByTemplateId" resultType="java.util.Map">
        select atq.quotaid,aq.quotaname,aq.quotacode
        from t_assess_template_quota atq left join t_assess_quota aq on atq.quotaid=aq.quotaid
        where atq.templateid=#{templateid} and quotastate=1 and aq.quotapid is not null
    </select>
</mapper>
