<?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.pqs9300.mapper.formdata.FormDataMapper'>
    <resultMap id="limitRateDataDto" type="com.pqs9300.pojo.formdata.LimitRateDto" autoMapping="true">
        <result property="lineIndex" column="lineIndex"></result>
        <result property="bdName" column="bdName"/>
        <result property="lineName" column="lineName"/>
        <result property="times" column="times"/>
        <association property="limitRate" javaType="com.njcn.pojo.data.LimitRate" autoMapping="true">
            <id property="myIndex" column="myIndex"></id>
        </association>
    </resultMap>

    <select id="getListDepts" resultType="com.pqs9300.pojo.formdata.FormData">
        select distinct (a.depts_index) deptsIndex, b.deptsname deptsName, b.custom_dept customDept
        from pqs_deptsline a, pqs_depts b
        where a.systype = #{sysType}
        and a.depts_index = b.depts_index
    </select>

    <select id="getListDeptsByIndex" resultType="com.pqs9300.pojo.formdata.FormData">
        select depts_index deptsIndex, deptsname deptsName, custom_dept customDept
        from pqs_depts
        where depts_index in
        <foreach collection="deptsIndex" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getListLineByDepts" resultType="java.lang.Long">
        select a.line_index
        from pqs_deptsline a, pq_line b
        where a.depts_index = #{deptsIndex}
        and a.systype = #{sysType}
        and a.line_index = b.line_index
        <foreach collection="level" index="index" item="item" separator="," open="and b.scale in ("
                 close=")">
            #{item}
        </foreach>
    </select>

    <select id="getOptionList" resultType="java.lang.String">
        select dic_name from pqs_dicdata where dic_index in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <insert id="insertSuperData" parameterType="com.pqs9300.pojo.formdata.FormData">
        insert into ths_supervise
        (sup_index, name, depts, create_user, create_time, type, progress,modify_time,eventDate)
        values
        (#{formData.supIndex},#{formData.supName},#{formData.deptsIndex},#{formData.createUser},#{formData.createTime},#{type},0,#{formData.createTime},#{formData.eventDate})
    </insert>

    <insert id="insertOverData">
        insert into ths_overrunlog (line_index,sup_index,updatetime,overrun_describe,overruntimes,name)
        values (#{lineId},#{supIndex},#{time},#{des},#{times},#{name})
    </insert>
    
    <delete id="deleteBySupIndex">
    	delete from ths_overrunlog where sup_Index = #{supIndex}
    </delete>

    <select id="getLimitRate" resultMap="limitRateDataDto">
        select ${conditionData.conditionResult},b.timeid times, c.line_index lineIndex,c.line_name lineName,d.name
        bdName,e.name gdName,c.line_index myIndex,b.timeid timeID
        from pqs_deptsline a, limit_rate b, pq_linedetail c, pq_substation d,PQ_GDINFORMATION e
        where (1=1
        <if test="conditionData.listExitLoadType!=null and conditionData.listExitLoadType.size() &gt; 0">
        and c.loadtype in
        <foreach collection="conditionData.listExitLoadType" index="index" item="item" separator="," open="("
                 close=")">
            #{item}
        </foreach>
        </if>
        <if test="conditionData.listExitLoadType!=null and conditionData.listExitLoadType.size() &gt; 0 and conditionData.listExitsLine!=null and conditionData.listExitsLine.size()&gt;0">
            <foreach collection="conditionData.listExitsLine" index="index" item="lineId" separator=","
                     open="or c.line_index in("
                     close=")">
                #{lineId}
            </foreach>
        </if>

        <if test="conditionData.listExitLoadType!=null and conditionData.listExitLoadType.size() == 0 and conditionData.listExitsLine!=null and conditionData.listExitsLine.size()&gt;0">
           and c.line_index in
            <foreach collection="conditionData.listExitsLine" index="index" item="lineId" separator=","
                     open="("
                     close=")">
                #{lineId}
            </foreach>
        </if>
        )
        and a.depts_index = #{conditionData.deptsIndex}
        and a.systype = #{conditionData.sysType}
        and b.phasic_type = 'T'
        and c.line_index = a.line_index
        and c.line_index = b.myindex
        and b.timeid between #{conditionData.startTime} and #{conditionData.endTime}
        and (${conditionData.condition})
        and c.sub_index = d.sub_index
        and c.gd_index = e.gd_index
    </select>

    <select id="getUserIndexByDeptsIndex" resultType="java.lang.String">
        select b.user_index
        from pqs_userset b
        where b.depts_index in
        (select a.depts_index
        from pqs_depts a
        start with a.depts_index = #{deptsIndex}
        connect by nocycle prior a.parentnodeid = a.depts_index)
    </select>

    <select id="getLevel" resultType="com.njcn.pojo.configuration.DicData">
        select b.dic_name dicName, b.dic_index dicIndex
        from pqs_dictype a, pqs_dicdata b
        where a.dictype_name = #{name}
        and a.dictype_index = b.dic_type
    </select>
    
    <select id="getExistLog" resultType="com.pqs9300.pojo.transaction.OverRunLog">
    	select *
		  from ths_overrunlog
		 where updatetime = #{start}
		   and sup_index in (select sup_index from ths_supervise where type = #{type})
    </select>
    
        
    <select id="getLogBySupIndex" resultType="com.pqs9300.pojo.transaction.OverRunLog">
    	select a.*, b.name as gdName, c.name as subName, d.name as lineName
		  from ths_overrunlog a, pq_gdinformation b, pq_substation c, pq_line d
		  where d.line_index = a.line_index
		  and b.gd_index = d.gd_index
		  and c.sub_index = d.sub_index
		  and sup_index = #{supIndex}
    </select>

    <select id="getStrategyLineByDept" resultType="java.lang.Long">
        select b.wfass_index from ths_warnform a
        left join ths_warnformass b on a.WF_INDEX = b.WF_INDEX
        left join pqs_userset c on a.user_index=c.user_index
        where a.state = 1
        and a.grade=#{grade}
        and b.type = 2
        and c.depts_index in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>

    <select id="getAlarmStrategyLineByDept" resultType="java.lang.Long">
        select b.alarmass_index from ths_alarm a
        left join ths_alarmass b on a.alarm_index = b.alarm_index
        left join pqs_userset c on a.user_index=c.user_index
        where a.state = 1
        and a.grade=#{grade}
        and b.type = 2
        and c.depts_index in
        <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>
</mapper>