<?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.base.mapper.CbsMonitorReportMapper">

    <resultMap type="CbsMonitorReport" id="CbsMonitorReportResult">
        <result property="objId"    column="obj_id"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="monitorType"    column="monitor_type"    />
        <result property="publicShareType"    column="public_share_type"    />
        <result property="roomCode"    column="room_code"    />
        <result property="roomName"    column="room_name"    />
        <result property="roomName"    column="room_name"    />
        <result property="buildCode"    column="build_code"    />
        <result property="buildName"    column="build_name"    />
        <result property="isBind"    column="is_bind"    />
        <result property="lastMonitorTime"    column="last_monitor_time"    />
        <result property="lastMonitorExpend"    column="last_monitor_expend"    />
        <result property="thisMonitorTime"    column="this_monitor_time"    />
        <result property="thisMonitorExpend"    column="this_monitor_expend"    />
        <result property="lastFlag"    column="last_flag"    />
        <result property="thisFlag"    column="this_flag"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="physicalCode"    column="physical_code"    />
        <result property="loadingPosition"    column="loadingPosition"    />
    </resultMap>

    <sql id="selectCbsMonitorReportVo">
        select obj_id, monitor_id, monitor_name, monitor_type, public_share_type,
               room_code, room_name,physical_code, build_code, build_name, is_bind, last_monitor_time, last_monitor_expend, this_monitor_time, this_monitor_expend, last_flag, this_flag, remark, create_by, create_time, update_by, update_time from cbs_monitor_report
    </sql>

    <select id="selectCbsMonitorReportList" parameterType="CbsMonitorReport" resultMap="CbsMonitorReportResult">
        <include refid="selectCbsMonitorReportVo"/>
        <where>
            <if test="monitorId != null  and monitorId != ''"> and monitor_id = #{monitorId}</if>
            <if test="monitorName != null  and monitorName != ''"> and monitor_name like concat('%', #{monitorName}, '%')</if>
            <if test="monitorType != null  and monitorType != ''"> and monitor_type = #{monitorType}</if>
            <if test="publicShareType != null  and publicShareType != ''"> and public_share_type = #{publicShareType}</if>
            <if test="roomName != null  and roomName != ''"> and room_name like concat('%', #{roomName}, '%')</if>
            <if test="buildCode != null  and buildCode != ''"> and build_code = #{buildCode}</if>
            <if test="buildName != null  and buildName != ''"> and build_name like concat('%', #{buildName}, '%')</if>
            <if test="isBind != null  and isBind != ''"> and is_bind = #{isBind}</if>
            <if test="lastMonitorTime != null "> and last_monitor_time = #{lastMonitorTime}</if>
            <if test="lastMonitorExpend != null "> and last_monitor_expend = #{lastMonitorExpend}</if>
            <if test="thisMonitorTime != null "> and this_monitor_time = #{thisMonitorTime}</if>
            <if test="thisMonitorExpend != null "> and this_monitor_expend = #{thisMonitorExpend}</if>
            <if test="lastFlag != null  and lastFlag != ''"> and last_flag = #{lastFlag}</if>
            <if test="thisFlag != null  and thisFlag != ''"> and this_flag = #{thisFlag}</if>
            <if test="roomCode != null and roomCode != '' ">
                and room_code IN
                <foreach collection="roomCode.split(',')" index="index" item="item" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectCbsMonitorReportByObjId" parameterType="Long" resultMap="CbsMonitorReportResult">
        <include refid="selectCbsMonitorReportVo"/>
        where obj_id = #{objId}
    </select>

<!--    <select id="selectRealTimeMonitorList" parameterType="EmsApiVo" resultMap="CbsMonitorReportResult">-->
<!--        SELECT bmi.monitor_id,-->
<!--               bmi.monitor_name,-->
<!--               bmi.monitor_type,-->
<!--               bmi.public_share_type,-->
<!--               bmi.room_code,-->
<!--               bmi.room_name,-->
<!--               bmi.physical_code,-->
<!--               bmi.monitor_addr loadingPosition,-->
<!--               '0'                                               AS is_bind,-->
<!--               sysdate()                                         AS this_monitor_time,-->
<!--               rdi.this_monitor_expend * IF(ISNULL(bmi.ct), 1, bmi.ct) * IF(ISNULL(bmi.pt), 1, bmi.pt)    AS this_monitor_expend,-->
<!--               IF(rdi.this_monitor_expend IS NOT NULL, '0', '1') AS this_flag-->
<!--        FROM base_monitor_info bmi-->
<!--                 LEFT JOIN (-->
<!--            SELECT t1.monitor_id, t1.water_flow AS this_monitor_expend-->
<!--            FROM record_water_instant t1-->
<!--                     JOIN (-->
<!--                SELECT monitor_id, MAX(collect_time) AS max_time-->
<!--                FROM record_water_instant-->
<!--        <if test="meterReadingTime != null  and meterReadingTime != ''">-->
<!--            where collect_time &lt; #{meterReadingTime}-->
<!--        </if>-->
<!--                GROUP BY monitor_id-->
<!--            ) t2 ON t1.monitor_id = t2.monitor_id AND t1.collect_time = t2.max_time-->
<!--            UNION ALL-->
<!--            SELECT t1.monitor_id, t1.zxyg AS this_monitor_expend-->
<!--            FROM record_dnb_instant t1-->
<!--                     JOIN (-->
<!--                SELECT monitor_id, MAX(collect_time) AS max_time-->
<!--                FROM record_dnb_instant-->
<!--        <if test="meterReadingTime != null  and meterReadingTime != ''">-->
<!--            where collect_time &lt; #{meterReadingTime}-->
<!--        </if>-->
<!--                GROUP BY monitor_id-->
<!--            ) t2 ON t1.monitor_id = t2.monitor_id AND t1.collect_time = t2.max_time-->
<!--        ) AS rdi ON rdi.monitor_id = bmi.monitor_id-->
<!--        WHERE bmi.room_code IS NOT NULL-->
<!--        <if test="monitorIdList != null  and monitorIdList.size > 0">-->
<!--            and bmi.monitor_id IN-->
<!--            <foreach item="monitorId" collection="monitorIdList" open="(" separator="," close=")">-->
<!--                #{monitorId}-->
<!--            </foreach>-->
<!--        </if>-->
<!--    </select>-->
<!--    <select id="selectLastTimeCbsMonitorReport" parameterType="CbsMonitorReport" resultMap="CbsMonitorReportResult">-->
<!--        <include refid="selectCbsMonitorReportVo"/>-->
<!--        <if test="monitorId != null  and monitorId != ''"> where monitor_id = #{monitorId}</if>-->
<!--        order by this_monitor_time desc-->
<!--        limit 1-->
<!--    </select>-->

    <insert id="insertCbsMonitorReport" parameterType="CbsMonitorReport" useGeneratedKeys="true" keyProperty="objId">
        insert into cbs_monitor_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monitorId != null">monitor_id,</if>
            <if test="monitorName != null">monitor_name,</if>
            <if test="monitorType != null">monitor_type,</if>
            <if test="publicShareType != null">public_share_type,</if>
            <if test="roomCode != null">room_code,</if>
            <if test="roomName != null">room_name,</if>
            <if test="buildCode != null">build_code,</if>
            <if test="buildName != null">build_name,</if>
            <if test="isBind != null">is_bind,</if>
            <if test="lastMonitorTime != null">last_monitor_time,</if>
            <if test="lastMonitorExpend != null">last_monitor_expend,</if>
            <if test="thisMonitorTime != null">this_monitor_time,</if>
            <if test="thisMonitorExpend != null">this_monitor_expend,</if>
            <if test="lastFlag != null">last_flag,</if>
            <if test="thisFlag != null">this_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="physicalCode != null">physical_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monitorId != null">#{monitorId},</if>
            <if test="monitorName != null">#{monitorName},</if>
            <if test="monitorType != null">#{monitorType},</if>
            <if test="publicShareType != null">#{publicShareType},</if>
            <if test="roomCode != null">#{roomCode},</if>
            <if test="roomName != null">#{roomName},</if>
            <if test="buildCode != null">#{buildCode},</if>
            <if test="buildName != null">#{buildName},</if>
            <if test="isBind != null">#{isBind},</if>
            <if test="lastMonitorTime != null">#{lastMonitorTime},</if>
            <if test="lastMonitorExpend != null">#{lastMonitorExpend},</if>
            <if test="thisMonitorTime != null">#{thisMonitorTime},</if>
            <if test="thisMonitorExpend != null">#{thisMonitorExpend},</if>
            <if test="lastFlag != null">#{lastFlag},</if>
            <if test="thisFlag != null">#{thisFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="physicalCode != null">#{physicalCode},</if>
         </trim>
    </insert>

    <update id="updateCbsMonitorReport" parameterType="CbsMonitorReport">
        update cbs_monitor_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorId != null">monitor_id = #{monitorId},</if>
            <if test="monitorName != null">monitor_name = #{monitorName},</if>
            <if test="monitorType != null">monitor_type = #{monitorType},</if>
            <if test="publicShareType != null">public_share_type = #{publicShareType},</if>
            <if test="roomCode != null">room_code = #{roomCode},</if>
            <if test="roomName != null">room_name = #{roomName},</if>
            <if test="buildCode != null">build_code = #{buildCode},</if>
            <if test="buildName != null">build_name = #{buildName},</if>
            <if test="isBind != null">is_bind = #{isBind},</if>
            <if test="lastMonitorTime != null">last_monitor_time = #{lastMonitorTime},</if>
            <if test="lastMonitorExpend != null">last_monitor_expend = #{lastMonitorExpend},</if>
            <if test="thisMonitorTime != null">this_monitor_time = #{thisMonitorTime},</if>
            <if test="thisMonitorExpend != null">this_monitor_expend = #{thisMonitorExpend},</if>
            <if test="lastFlag != null">last_flag = #{lastFlag},</if>
            <if test="thisFlag != null">this_flag = #{thisFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="physicalCode != null">physical_code = #{physicalCode},</if>
        </trim>
        where obj_id = #{objId}
    </update>

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

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