<?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.njcn.prepare.harmonic.mapper.mysql.dim.RDimVoltageDMapper">

    <select id="getLineData" resultType="com.njcn.harmonic.pojo.dto.LineDataDto">
        select
            A.`time`,
            A.line_id lineId,
            A.rms,
            C.vUnbalance,
            D.pst,
            B.vThd,
            B.v1,B.v2,B.v3,B.v4,B.v5,
            B.v6,B.v7,B.v8,B.v9,B.v10,
            B.v11,B.v12,B.v13,B.v14,B.v15,
            B.v16,B.v17,B.v18,B.v19,B.v20,
            B.v21,B.v22,B.v23,B.v24,B.v25,
            B.v26,B.v27,B.v28,B.v29,B.v30,
            B.v31,B.v32,B.v33,B.v34,B.v35,
         	B.v36,B.v37,B.v38,B.v39,B.v40,
            B.v41,B.v42,B.v43,B.v44,B.v45,
            B.v46,B.v47,B.v48,B.v49,B.v50,
			E.iNeg
        from
            (
                select
                    `time`,
                    line_id,
                    max(rms) rms
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and phasic_type in ('A', 'B', 'C')
                    and value_type in ('AVG')
					<if test="collection != null and collection.size() > 0">
						and line_id in
						<foreach collection="collection" item="item" open="(" close=")" separator=",">
							#{item}
						</foreach>
					</if>
                group by `time`, line_id
            ) A
        inner join
            (
                select
                    `time`,
                    line_id,
                    max(v_thd) vThd,
                    max(v_1) v1,max(v_2) v2,
		            max(v_3) v3,max(v_4) v4,
		            max(v_5) v5,max(v_6) v6,
		            max(v_7) v7,max(v_8) v8,
		            max(v_9) v9,max(v_10) v10,
	             	max(v_11) v11,max(v_12) v12,
	             	max(v_13) v13,max(v_14) v14,
	             	max(v_15) v15,max(v_16) v16,
	             	max(v_17) v17,max(v_18) v18,
	             	max(v_19) v19,max(v_20) v20,
	             	max(v_21) v21,max(v_22) v22,
	             	max(v_23) v23,max(v_24) v24,
	             	max(v_25) v25,max(v_26) v26,
	             	max(v_27) v27,max(v_28) v28,
	             	max(v_29) v29,max(v_30) v30,
	             	max(v_31) v31,max(v_32) v32,
	             	max(v_33) v33,max(v_34) v34,
	             	max(v_35) v35,max(v_36) v36,
	             	max(v_37) v37,max(v_38) v38,
	             	max(v_39) v39,max(v_40) v40,
	             	max(v_41) v41,max(v_42) v42,
	             	max(v_43) v43,max(v_44) v44,
	             	max(v_45) v45,max(v_46) v46,
	             	max(v_47) v47,max(v_48) v48,
	             	max(v_49) v49,max(v_50) v50
                from
                    r_stat_data_v_d
                where
                    `time` between #{startTime} and #{endTime}
                    and phasic_type in ('A', 'B', 'C')
                    and value_type in ('CP95')
					<if test="collection != null and collection.size() > 0">
						and line_id in
						<foreach collection="collection" item="item" open="(" close=")" separator=",">
							#{item}
						</foreach>
					</if>
                group by `time`,line_id
            ) B
        on A.`time` = B.`time` and A.line_id = B.line_id
	  	inner join
		    (
		        select
		            `time`,
		            line_id,
		            max(v_unbalance) vUnbalance
		        from
		            r_stat_data_v_d
		        where
		            `time` between #{startTime} and #{endTime}
		            and phasic_type = 'T'
		            and value_type in ('CP95')
					<if test="collection != null and collection.size() > 0">
						and line_id in
						<foreach collection="collection" item="item" open="(" close=")" separator=",">
							#{item}
						</foreach>
					</if>
		        group by `time`,line_id
		    ) C
        on A.`time` = C.`time` and A.line_id = C.line_id
	  	inner join
		    (
		        select
		            `time`,
		            line_id,
		            max(pst) pst
		        from
		            r_stat_data_flicker_d
		        where
		            `time` between #{startTime} and #{endTime}
		            and phasic_type in ('A', 'B', 'C')
		            and value_type in ('MAX')
					<if test="collection != null and collection.size() > 0">
						and line_id in
						<foreach collection="collection" item="item" open="(" close=")" separator=",">
							#{item}
						</foreach>
					</if>
		        group by `time`,line_id
		    ) D
        on A.`time` = D.`time` and A.line_id = D.line_id
		inner join
			(
				select
					`time`,
					line_id,
					max(i_neg) iNeg
				from
					r_stat_data_i_d
				where
					`time` between #{startTime} and #{endTime}
					and phasic_type = 'T'
					and value_type in ('CP95')
					<if test="collection != null and collection.size() > 0">
						and line_id in
						<foreach collection="collection" item="item" open="(" close=")" separator=",">
							#{item}
						</foreach>
					</if>
				group by `time`,line_id
			) E
		on A.`time` = E.`time` and A.line_id = E.line_id
    </select>
</mapper>
