<?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.zcxk.rmcp.core.dao.MeasurementRecordMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.rmcp.core.entity.MeasurementRecord" id="MeasurementRecordMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="meterNo" column="meter_no" jdbcType="VARCHAR"/>
        <result property="fileMeterNo" column="file_meter_no" jdbcType="VARCHAR"/>
        <result property="approvalStatus" column="approval_status" jdbcType="VARCHAR"/>
        <result property="lastReading" column="last_reading" jdbcType="VARCHAR"/>
        <result property="currentReading" column="current_reading" jdbcType="VARCHAR"/>
        <result property="location" column="location" jdbcType="VARCHAR"/>
        <result property="planId" column="plan_id" jdbcType="INTEGER"/>
        <result property="waterConsumption" column="water_consumption" jdbcType="NUMERIC"/>
        <result property="createDate" column="create_date" jdbcType="TIMESTAMP"/>
        <result property="updateDate" column="update_date" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        id, meter_no, file_meter_no, approval_status, last_reading, current_reading, location, plan_id, water_consumption, create_date, update_date, create_by, update_by, status    </sql>

    <!-- 查询单个 -->
    <select id="selectById" resultMap="MeasurementRecordMap">
        select
        <include refid="Base_Column_List"/>
        from rmcp_measurement_record
        where id = #{id}
    </select>


    <!-- 查询全部 -->
    <select id="selectAll" resultMap="MeasurementRecordMap">
        select
        <include refid="Base_Column_List"/>
        from rmcp_measurement_record
    </select>

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.rmcp.api.dto.measurementsettlement.MeasurementRecordDto">
        select
        <include refid="Base_Column_List"/>
        from rmcp_measurement_record
        <where>
            status=1
            <if test="startingDegree != null">
                and water_consumption <![CDATA[  >= ]]> #{startingDegree}
            </if>
            <if test="stop != null">
                and water_consumption  <![CDATA[  < ]]> #{stop}
            </if>
            <if test="status != null">
                and approval_status = #{status}
            </if>
            <if test="planId != null">
                and plan_id = #{planId}
            </if>
        </where>
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into rmcp_measurement_record(id, meter_no, file_meter_no, approval_status, last_reading, current_reading,
                                            location, plan_id, water_consumption, create_date, update_date, create_by,
                                            update_by, status)
        values (#{id}, #{meterNo}, #{fileMeterNo}, #{approvalStatus}, #{lastReading}, #{currentReading}, #{location},
                #{planId}, #{waterConsumption}, #{createDate}, #{updateDate}, #{createBy}, #{updateBy}, #{status})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into rmcp_measurement_record(id, meter_no, file_meter_no, approval_status, last_reading, current_reading,
        location, plan_id, water_consumption, create_date, update_date, create_by, update_by, status,device_id,
                                            last_period_date,current_period_date)
        values
        <foreach collection="measurementRecords" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.meterNo}, #{item.fileMeterNo}, #{item.approvalStatus}, #{item.lastReading},
            #{item.currentReading}, #{item.location}, #{item.planId}, #{item.waterConsumption}, #{item.createDate},
            #{item.updateDate}, #{item.createBy}, #{item.updateBy}, #{item.status}, #{item.deviceId}
            , #{item.lastPeriodDate}  , #{item.currentPeriodDate}  )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update rmcp_measurement_record
        <set>
            <if test="meterNo != null and meterNo != ''">
                meter_no = #{meterNo},
            </if>
            <if test="fileMeterNo != null and fileMeterNo != ''">
                file_meter_no = #{fileMeterNo},
            </if>
            <if test="approvalStatus != null and approvalStatus != ''">
                approval_status = #{approvalStatus},
            </if>
            <if test="lastReading != null and lastReading != ''">
                last_reading = #{lastReading},
            </if>
            <if test="currentReading != null and currentReading != ''">
                current_reading = #{currentReading},
            </if>
            <if test="location != null and location != ''">
                location = #{location},
            </if>
            <if test="planId != null">
                plan_id = #{planId},
            </if>
            <if test="waterConsumption != null">
                water_consumption = #{waterConsumption},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate},
            </if>
            <if test="createBy != null and createBy != ''">
                create_by = #{createBy},
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by = #{updateBy},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="auditWater">
        update rmcp_measurement_record set approval_status=1
        where id  in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>
    <update id="auditAllWater">
        update rmcp_measurement_record set approval_status=1
        where plan_id =#{planId}

    </update>
    <update id="modifyTheAmountOfWater">
        update rmcp_measurement_record set water_consumption=#{waterConsumption}
        where id=#{recordId}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from rmcp_measurement_record
        where id = #{id}
    </delete>

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*)
        from rmcp_measurement_record
    </select>
    <select id="findClearingRecordItem" resultType="com.zcxk.rmcp.core.entity.MeasurementRecord">
        select * from rmcp_measurement_record where device_id=#{deviceId} and planId=#{planId}
    </select>

</mapper>