<?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.aucma.report.mapper.ReportDayDnbMapper">

    <resultMap type="ReportDayDnb" id="ReportDayDnbResult">
        <result property="objId" column="obj_id"/>
        <result property="monitorCode" column="monitor_code"/>
        <result property="monitorName" column="monitor_name"/>
        <result property="instrumentValue" column="instrument_value"/>
        <result property="expend" column="expend"/>
        <result property="reportDate" column="report_date"/>
        <result property="beginTime" column="begin_time"/>
        <result property="endTime" column="end_time"/>
        <result property="isFlag" column="is_flag"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdTime" column="created_time"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedTime" column="updated_time"/>
    </resultMap>

    <sql id="selectReportDayDnbVo">
        select rpd.monitor_code,
               bm.MONITOR_NAME,
               MAX(rpd.instrument_value)     instrument_value,
               SUM(rpd.expend)               expend,
               SUBSTR(rpd.begin_time, 1, 10) report_date
        from report_point_dnb rpd
                 left join BASE_MONITORINFO bm on bm.MONITOR_CODE = rpd.MONITOR_CODE
    </sql>

    <select id="selectReportDayDnbList" parameterType="ReportDayDnb" resultMap="ReportDayDnbResult">
        <include refid="selectReportDayDnbVo"/>
        <where>
            <if test="monitorCode != null  and monitorCode != ''">and rpd.monitor_code = #{monitorCode}</if>
            <if test="instrumentValue != null ">and instrument_value = #{instrumentValue}</if>
            <if test="expend != null ">and expend = #{expend}</if>
            <if test="reportDate != null  and reportDate != ''">and report_date = #{reportDate}</if>
            <if test="beginTime != null  and beginTime != ''">and begin_time = #{beginTime}</if>
            <if test="endTime != null  and endTime != ''">and end_time = #{endTime}</if>
            <if test="isFlag != null ">and is_flag = #{isFlag}</if>
            <if test="params.beginCollectTime != null and params.beginCollectTime != '' and params.endCollectTime != null and params.endCollectTime != ''">
                and SUBSTR(rpd.begin_time, 1, 10) between  #{params.beginCollectTime} and #{params.endCollectTime}
            </if>
            <if test="createdBy != null  and createdBy != ''">and created_by = #{createdBy}</if>
            <if test="createdTime != null ">and created_time = #{createdTime}</if>
            <if test="updatedBy != null  and updatedBy != ''">and updated_by = #{updatedBy}</if>
            <if test="updatedTime != null ">and updated_time = #{updatedTime}</if>
        </where>
        GROUP BY rpd.monitor_code, bm.MONITOR_NAME, SUBSTR(rpd.begin_time, 1, 10)
        order by report_date,rpd.monitor_code
    </select>

    <select id="selectReportDayDnbByObjId" parameterType="Long" resultMap="ReportDayDnbResult">
        <include refid="selectReportDayDnbVo"/>
        where rpd.obj_id = #{objId}
    </select>

    <insert id="insertReportDayDnb" parameterType="ReportDayDnb">
        <selectKey keyProperty="objId" resultType="long" order="BEFORE">
            SELECT seq_report_day_dnb.NEXTVAL as objId FROM DUAL
        </selectKey>
        insert into report_day_dnb
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="objId != null">obj_id,</if>
            <if test="monitorCode != null">monitor_code,</if>
            <if test="instrumentValue != null">instrument_value,</if>
            <if test="expend != null">expend,</if>
            <if test="reportDate != null">report_date,</if>
            <if test="beginTime != null">begin_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updatedBy != null">updated_by,</if>
            <if test="updatedTime != null">updated_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="objId != null">#{objId},</if>
            <if test="monitorCode != null">#{monitorCode},</if>
            <if test="instrumentValue != null">#{instrumentValue},</if>
            <if test="expend != null">#{expend},</if>
            <if test="reportDate != null">#{reportDate},</if>
            <if test="beginTime != null">#{beginTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updatedBy != null">#{updatedBy},</if>
            <if test="updatedTime != null">#{updatedTime},</if>
        </trim>
    </insert>

    <update id="updateReportDayDnb" parameterType="ReportDayDnb">
        update report_day_dnb
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorCode != null">monitor_code = #{monitorCode},</if>
            <if test="instrumentValue != null">instrument_value = #{instrumentValue},</if>
            <if test="expend != null">expend = #{expend},</if>
            <if test="reportDate != null">report_date = #{reportDate},</if>
            <if test="beginTime != null">begin_time = #{beginTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updatedBy != null">updated_by = #{updatedBy},</if>
            <if test="updatedTime != null">updated_time = #{updatedTime},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteReportDayDnbByObjId" parameterType="Long">
        delete
        from report_day_dnb
        where obj_id = #{objId}
    </delete>

    <delete id="deleteReportDayDnbByObjIds" parameterType="String">
        delete from report_day_dnb where obj_id in
        <foreach item="objId" collection="array" open="(" separator="," close=")">
            #{objId}
        </foreach>
    </delete>

    <select id="energyStatisticalReportList" resultType="com.aucma.report.domain.EnergyStatisticalReport"
            parameterType="java.util.HashMap">
        WITH DT AS (SELECT #{timeSub} TIMESUB FROM DUAL)
        SELECT WU.WORK_UNIT_CODE                     workUnitCode,
        WU.WORK_UNIT_NAME                     workUnitName,
        MAX(RPD.INSTRUMENT_VALUE)             instrumentValue,
        SUM(RPD.EXPEND)                       expend,
        SUBSTR(RPD.BEGIN_TIME, 1, DT.TIMESUB) beginTime,
        SUBSTR(RPD.BEGIN_TIME, 1, DT.TIMESUB) endTime
        FROM BASE_WORK_UNIT WU
                 LEFT JOIN BASE_MONITOR_WORK_UNIT MWU ON MWU.WORK_UNIT_CODE = WU.WORK_UNIT_CODE
                 LEFT JOIN REPORT_POINT_DNB RPD ON RPD.MONITOR_CODE = MWU.MONITOR_CODE
                 CROSS JOIN DT
        WHERE RPD.EXPEND IS NOT NULL
        <if test="beginCollectTime != null and beginCollectTime != '' and endCollectTime != null and endCollectTime != ''">
            and SUBSTR(RPD.begin_time, 1, 10) between  #{beginCollectTime} and #{endCollectTime}
        </if>
        <if test="workUnitCode != null  and workUnitCode != ''">and WU.WORK_UNIT_CODE = #{workUnitCode}</if>
        GROUP BY WU.WORK_UNIT_CODE, WU.WORK_UNIT_NAME, SUBSTR(RPD.BEGIN_TIME, 1, DT.TIMESUB)
        ORDER BY beginTime, workUnitCode
    </select>
</mapper>