<?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.hw.ems.report.mapper.ReportPointDnbMapper">

    <resultMap type="ReportPointDnb" id="ReportPointDnbResult">
        <result property="objid"    column="objid"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="pMonitorId"    column="p_monitor_id"    />
        <result property="address"    column="address"    />
        <result property="expend"    column="expend"    />
        <result property="price"    column="price"    />
        <result property="pointTime"    column="point_time"    />
        <result property="beginTime"    column="begin_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="recordTime"    column="record_time"    />
        <result property="updateFlag"    column="update_flag"    />
        <result property="meterValue"    column="meter_value"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="collectType"    column="collect_type"    />
        <result property="userId"    column="user_id"    />
    </resultMap>

    <resultMap type="ReportPointDnbDTO" id="ReportPointDnbDTOResult">
        <result property="objid"    column="objid"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="pMonitorId"    column="p_monitor_id"    />
        <result property="address"    column="address"    />
        <result property="expend"    column="expend"    />
        <result property="price"    column="price"    />
        <result property="pointTime"    column="point_time"    />
        <result property="beginTime"    column="begin_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="recordTime"    column="record_time"    />
        <result property="updateFlag"    column="update_flag"    />
        <result property="meterValue"    column="meter_value"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="collectType"    column="collect_type"    />
        <result property="userId"    column="user_id"    />
    </resultMap>

    <sql id="selectReportPointDnbVo">
        select report_point_dnb.objid, report_point_dnb.dept_id, dept_name, report_point_dnb.monitor_id,m.monitor_name,
               p_monitor_id, m.monitor_addr address, expend, price,
               point_time, begin_time, end_time, record_time, update_flag, meter_value,
               report_point_dnb.update_by, report_point_dnb.update_time, collect_type,report_point_dnb. user_id from report_point_dnb
               left join base_monitor_info m on m.monitor_id = report_point_dnb.monitor_id
    </sql>

    <select id="selectReportPointDnbList" parameterType="ReportPointDnb" resultMap="ReportPointDnbResult">
        <include refid="selectReportPointDnbVo"/>
        <where>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="monitorId != null  and monitorId != ''"> and report_point_dnb.monitor_id = #{monitorId}</if>
            <if test="monitorName != null  and monitorName != ''"> and report_point_dnb.monitor_name like concat('%', #{monitorName}, '%')</if>
            <if test="pMonitorId != null  and pMonitorId != ''"> and p_monitor_id = #{pMonitorId}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="expend != null "> and expend = #{expend}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="pointTime != null  and pointTime != ''"> and point_time = #{pointTime}</if>
            <if test="monitorSubset != null  and monitorSubset != ''">
                and (report_point_dnb.monitor_id = #{monitorSubset} OR report_point_dnb.monitor_id IN
                (SELECT t.monitor_id FROM base_monitor_info t WHERE FIND_IN_SET(#{monitorSubset}, ancestors)))
            </if>
            <if test="params.beginBeginTime != null and params.beginBeginTime != '' and params.endBeginTime != null and params.endBeginTime != ''">
             and begin_time between #{params.beginBeginTime} and #{params.endBeginTime}</if>
            <if test="params.startTime != null and params.startTime != '' and params.endTime != null and params.endTime != ''">
                and begin_time between #{params.startTime} and #{params.endTime}</if>
            <if test="params.beginCollectTime != null and params.beginCollectTime != '' and params.endCollectTime != null and params.endCollectTime != ''">
                and begin_time between #{params.beginCollectTime} and #{params.endCollectTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
            <if test="recordTime != null "> and record_time = #{recordTime}</if>
            <if test="updateFlag != null "> and update_flag = #{updateFlag}</if>
            <if test="meterValue != null "> and meter_value = #{meterValue}</if>
            <if test="collectType != null "> and collect_type = #{collectType}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="params.monitorIdList != null  and params.monitorIdList.size > 0">
                and report_point_dnb.monitor_id IN
                <foreach item="monitorId" collection="params.monitorIdList" open="(" separator="," close=")">
                    #{monitorId}
                </foreach>
            </if>
            <if test="publicShareType != null  and publicShareType != ''"> and m.public_share_type = #{publicShareType}</if>
        </where>
        order by report_point_dnb.monitor_id,report_point_dnb.begin_time desc
    </select>

    <select id="selectReportPointDnbByObjid" parameterType="Long" resultMap="ReportPointDnbResult">
        <include refid="selectReportPointDnbVo"/>
        where report_point_dnb.objid = #{objid}
    </select>
    <select id="energyConsumptionReportByTime" resultType="java.util.Map">
        select ert.pointTime, sum(if(ert.expend is null, 0, ert.expend)) expend
        from (
                 select left(rpd.begin_time, #{timeSub}) pointTime, rpd.expend
                 from report_point_dnb rpd
                 left join base_monitor_info m on m.monitor_id = rpd.monitor_id
        <where>
            <if test="publicShareType != null  and publicShareType != ''"> and m.public_share_type = #{publicShareType}</if>
            <if test="monitorIdList != null  and monitorIdList.size > 0">
                and rpd.monitor_id IN
                <foreach item="monitorId" collection="monitorIdList" open="(" separator="," close=")">
                    #{monitorId}
                </foreach>
            </if>
            <if test="monitorId != null  and monitorId != ''"> and rpd.monitor_id = #{monitorId}</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rpd.begin_time between #{startTime} and #{endTime}
            </if>
            <if test="startDay != null and startDay != '' and endDay != null and endDay != ''">
                and left(rpd.begin_time, 10) between #{startDay} and #{endDay}
            </if>
        </where>
             ) ert
        group by ert.pointTime
        order by ert.pointTime
    </select>

    <select id="energyConsumptionAnalysisDetails" resultType="java.util.Map">
        select ert.monitor_id, ert.monitor_name, ert.pointTime, sum(if(ert.expend is null, 0, ert.expend)) expend
        from (
                 select rpd.monitor_id, m.monitor_name, left(rpd.begin_time, 10) pointTime, rpd.expend
                 from report_point_dnb rpd
                     left join base_monitor_info m on m.monitor_id = rpd.monitor_id
        <where>
            <if test="monitorIdList != null  and monitorIdList.size > 0">
                and rpd.monitor_id IN
                <foreach item="monitorId" collection="monitorIdList" open="(" separator="," close=")">
                    #{monitorId}
                </foreach>
            </if>
            <if test="monitorId != null  and monitorId != ''"> and rpd.monitor_id = #{monitorId}</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                and rpd.begin_time between #{startTime} and #{endTime}
            </if>
<!--            <if test="params.beginCollectTime != null and params.beginCollectTime != '' and params.endCollectTime != null and params.endCollectTime != ''">-->
<!--                and rpd.begin_time between #{params.beginCollectTime} and #{params.endCollectTime}-->
<!--            </if>-->
            <if test="startDay != null and startDay != '' and endDay != null and endDay != ''">
                and left(rpd.begin_time, 10) between #{startDay} and #{endDay}
            </if>
        </where>
             ) ert
        group by ert.monitor_id, ert.monitor_name, ert.pointTime
        order by ert.monitor_id, ert.monitor_name, ert.pointTime
    </select>

    <select id="datePointDnbList" parameterType="ReportPointDnb" resultMap="ReportPointDnbDTOResult">
        select ert.monitor_id,
               ert.monitor_name,
        CASE
        WHEN #{params.timeSub} = 10 THEN ert.pointTime
        WHEN #{params.timeSub} = 7 THEN concat(ert.pointTime, '-01')
        WHEN #{params.timeSub} = 4 THEN concat(ert.pointTime, '-01-01')
        END AS                                 begin_time,
        CASE
        WHEN #{params.timeSub} = 10 THEN ert.pointTime
        WHEN #{params.timeSub} = 7 THEN LAST_DAY(concat(ert.pointTime, '-01'))
        WHEN #{params.timeSub} = 4 THEN LAST_DAY(concat(ert.pointTime, '-12-01'))
        END AS                                 end_time,
               sum(if(ert.expend is null, 0, ert.expend)) expend,
               max(ert.meter_value)                       meter_value,
        max(ert.monitor_addr)                       address
        from (
                 select rpd.monitor_id, m.monitor_name, left(rpd.begin_time, #{params.timeSub}) pointTime, rpd.expend, rpd.meter_value,m.monitor_addr
                 from report_point_dnb rpd
                     left join base_monitor_info m on m.monitor_id = rpd.monitor_id
        <where>
            <if test="monitorId != null  and monitorId != ''"> and rpd.monitor_id = #{monitorId}</if>
            <if test="params.beginCollectTime != null and params.beginCollectTime != '' and params.endCollectTime != null and params.endCollectTime != ''">
                and rpd.begin_time between #{params.beginCollectTime} and #{params.endCollectTime}
            </if>
            <if test="monitorSubset != null  and monitorSubset != ''">
                and (rpd.monitor_id = #{monitorSubset} OR rpd.monitor_id IN
                (SELECT t.monitor_id FROM base_monitor_info t WHERE FIND_IN_SET(#{monitorSubset}, ancestors)))
            </if>
            <if test="publicShareType != null  and publicShareType != ''"> and m.public_share_type = #{publicShareType}</if>
        </where>
             ) ert
        group by ert.monitor_id, ert.monitor_name, ert.pointTime
        order by ert.monitor_id, ert.monitor_name, ert.pointTime desc
    </select>

<!--    <select id="energyStatisticsData" resultType="java.util.Map">-->
<!--        select m.parentMonitorId,-->
<!--               m.parentName,-->
<!--               m.is_ammeter,-->
<!--               rpd.monitor_id monitorId,-->
<!--               m.monitor_name monitorName,-->
<!--               m.monitor_addr  address,-->
<!--               sum(if(rpd.expend is null, 0, rpd.expend)) expend,-->
<!--               max(rpd.meter_value)                       meterValue,-->
<!--               GROUP_CONCAT(DISTINCT bpu.user_name SEPARATOR ',')  owningUser,-->
<!--               concat(#{beginCollectTime},' 至 ',#{endCollectTime})   timeRange,-->
<!--               m.monitor_hierarchy monitorHierarchy,-->
<!--               m.grade-->
<!--        from report_point_dnb rpd-->
<!--        left join (SELECT blm.monitor_id   parentMonitorId,-->
<!--        blm.monitor_name parentName,-->
<!--        bmi.monitor_id,-->
<!--        bmi.monitor_name,-->
<!--        bmi.monitor_addr,-->
<!--        bmi.monitor_hierarchy,-->
<!--        bl.grade,-->
<!--        CASE WHEN bmi.is_ammeter = 'true' THEN  '是' ELSE '否' END is_ammeter-->
<!--        FROM base_monitor_info bmi-->
<!--        LEFT JOIN base_line_monitor bl ON bl.monitor_id = bmi.monitor_id-->
<!--        LEFT JOIN base_line_monitor blm ON blm.objid = bl.parent_id) m-->
<!--        on m.monitor_id = rpd.monitor_id-->
<!--                 left join base_monitor_public bmp on m.monitor_id = bmp.monitor_id-->
<!--                 left join base_public_user bpu on bmp.public_share_id = bpu.id-->
<!--        <where>-->
<!--            <if test="beginCollectTime != null and beginCollectTime != '' and endCollectTime != null and endCollectTime != ''">-->
<!--                and left(rpd.begin_time,10) between #{beginCollectTime} and #{endCollectTime}-->
<!--            </if>-->
<!--            <if test="monitorIdList != null  and monitorIdList.size > 0">-->
<!--                and rpd.monitor_id IN-->
<!--                <foreach item="monitorId" collection="monitorIdList" open="(" separator="," close=")">-->
<!--                    #{monitorId}-->
<!--                </foreach>-->
<!--            </if>-->
<!--            <if test="publicShareType != null  and publicShareType != ''"> and m.public_share_type = #{publicShareType}</if>-->
<!--            <if test="monitorHierarchy != null  and monitorHierarchy != ''"> and m.monitor_hierarchy = #{monitorHierarchy}</if>-->
<!--        </where>-->
<!--        group by m.parentMonitorId, m.parentName, m.is_ammeter,-->
<!--        rpd.monitor_id, m.monitor_name, m.monitor_addr, m.monitor_hierarchy, m.grade-->
<!--        order by m.parentName, m.monitor_name-->
<!--    </select>-->
<!--    todo 因为缺少公摊表，所以替换sql为如下-->
    <select id="energyStatisticsData" resultType="java.util.Map">
        select m.parentMonitorId,
        m.parentName,
        m.is_ammeter,
        rpd.monitor_id monitorId,
        m.monitor_name monitorName,
        m.monitor_addr  address,
        sum(if(rpd.expend is null, 0, rpd.expend)) expend,
        max(rpd.meter_value)                       meterValue,
        concat(#{beginCollectTime},' 至 ',#{endCollectTime})  as timeRange,
        m.monitor_hierarchy monitorHierarchy,
        m.grade
        from report_point_dnb rpd
        left join (SELECT blm.monitor_id   parentMonitorId,
        blm.monitor_name parentName,
        bmi.monitor_id,
        bmi.monitor_name,
        bmi.monitor_addr,
        bmi.monitor_hierarchy,
        bl.grade,
        CASE WHEN bmi.is_ammeter = 'true' THEN  '是' ELSE '否' END is_ammeter
        FROM base_monitor_info bmi
        LEFT JOIN base_line_monitor bl ON bl.monitor_id = bmi.monitor_id
        LEFT JOIN base_line_monitor blm ON blm.objid = bl.parent_id) m
        on m.monitor_id = rpd.monitor_id

        <where>
            <if test="beginCollectTime != null and beginCollectTime != '' and endCollectTime != null and endCollectTime != ''">
                and left(rpd.begin_time,10) between #{beginCollectTime} and #{endCollectTime}
            </if>
            <if test="monitorIdList != null  and monitorIdList.size > 0">
                and rpd.monitor_id IN
                <foreach item="monitorId" collection="monitorIdList" open="(" separator="," close=")">
                    #{monitorId}
                </foreach>
            </if>
            <if test="publicShareType != null  and publicShareType != ''"> and m.public_share_type = #{publicShareType}</if>
            <if test="monitorHierarchy != null  and monitorHierarchy != ''"> and m.monitor_hierarchy = #{monitorHierarchy}</if>
        </where>
        group by m.parentMonitorId, m.parentName, m.is_ammeter,
        rpd.monitor_id, m.monitor_name, m.monitor_addr, m.monitor_hierarchy, m.grade
        order by m.parentName, m.monitor_name
    </select>

    <insert id="insertReportPointDnb" parameterType="ReportPointDnb">
        insert into report_point_dnb
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objid != null">objid,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null">dept_name,</if>
            <if test="monitorId != null">monitor_id,</if>
            <if test="monitorName != null">monitor_name,</if>
            <if test="pMonitorId != null">p_monitor_id,</if>
            <if test="address != null">address,</if>
            <if test="expend != null">expend,</if>
            <if test="price != null">price,</if>
            <if test="pointTime != null">point_time,</if>
            <if test="beginTime != null">begin_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="recordTime != null">record_time,</if>
            <if test="updateFlag != null">update_flag,</if>
            <if test="meterValue != null">meter_value,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="collectType != null">collect_type,</if>
            <if test="userId != null">user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objid != null">#{objid},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null">#{deptName},</if>
            <if test="monitorId != null">#{monitorId},</if>
            <if test="monitorName != null">#{monitorName},</if>
            <if test="pMonitorId != null">#{pMonitorId},</if>
            <if test="address != null">#{address},</if>
            <if test="expend != null">#{expend},</if>
            <if test="price != null">#{price},</if>
            <if test="pointTime != null">#{pointTime},</if>
            <if test="beginTime != null">#{beginTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="recordTime != null">#{recordTime},</if>
            <if test="updateFlag != null">#{updateFlag},</if>
            <if test="meterValue != null">#{meterValue},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="collectType != null">#{collectType},</if>
            <if test="userId != null">#{userId},</if>
         </trim>
    </insert>

    <update id="updateReportPointDnb" parameterType="ReportPointDnb">
        update report_point_dnb
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null">dept_name = #{deptName},</if>
            <if test="monitorId != null">monitor_id = #{monitorId},</if>
            <if test="monitorName != null">monitor_name = #{monitorName},</if>
            <if test="pMonitorId != null">p_monitor_id = #{pMonitorId},</if>
            <if test="address != null">address = #{address},</if>
            <if test="expend != null">expend = #{expend},</if>
            <if test="price != null">price = #{price},</if>
            <if test="pointTime != null">point_time = #{pointTime},</if>
            <if test="beginTime != null">begin_time = #{beginTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="recordTime != null">record_time = #{recordTime},</if>
            <if test="updateFlag != null">update_flag = #{updateFlag},</if>
            <if test="meterValue != null">meter_value = #{meterValue},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="collectType != null">collect_type = #{collectType},</if>
            <if test="userId != null">user_id = #{userId},</if>
        </trim>
        where objid = #{objid}
    </update>

    <delete id="deleteReportPointDnbByObjid" parameterType="Long">
        delete from report_point_dnb where objid = #{objid}
    </delete>

    <delete id="deleteReportPointDnbByObjids" parameterType="String">
        delete from report_point_dnb where objid in
        <foreach item="objid" collection="array" open="(" separator="," close=")">
            #{objid}
        </foreach>
    </delete>

</mapper>
