<?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.MeasurementInstanceMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.rmcp.core.entity.MeasurementInstance" id="MeasurementInstanceMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="planid" column="planId" jdbcType="INTEGER"/>
        <result property="totalWaterMeters" column="total_water_meters" jdbcType="INTEGER"/>
        <result property="numberOfWaterMetersReviewed" column="number_of_water_meters_reviewed" jdbcType="INTEGER"/>
        <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"/>
        <result property="billingPeriod" column="billing_period" jdbcType="VARCHAR"/>
        <result property="clearingDay" column="clearing_day" jdbcType="INTEGER"/>
        <result property="clearingEndDate" column="clearing_end_date" jdbcType="TIMESTAMP"/>
        <result property="clearingStartDate" column="clearing_start_date" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        id, plan_id, total_water_meters, number_of_water_meters_reviewed, create_date, update_date, create_by, update_by, status, billing_period, clearing_day, clearing_end_date, clearing_start_date    </sql>

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


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

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.rmcp.api.dto.measurementsettlement.MeasurementSettlementDto">
        select
        mi.id,mi.total_water_meters,mi.number_of_water_meters_reviewed,mi.billing_period
        ,ms.plan_name,mi.clearing_end_date,mi.clearing_start_date,
               company.ORG_NAME companyOrgName,depart.ORG_NAME deptOrgName,
        commuity.name communityName
        from rmcp_measurement_instance mi  join rmcp_measurement_settlement ms on
            mi.plan_id=ms.id
        join rmcp_org company on ms.company_org_id=company.ID
        left join rmcp_org depart on ms.dept_org_id=depart.ID
        left join  rmcp_community commuity on ms.community_id=commuity.id
        <where>
            mi.status=1
            <if test="measurement.companyOrgId != null">
                and ms.company_org_id = #{measurement.companyOrgId}
            </if>
            <if test="measurement.deptOrgId != null">
                and ms.dept_org_id = #{measurement.deptOrgId}
            </if>
            <if test="measurement.planName != null and measurement.planName != ''">
                and ms.plan_name  like concat('%',#{measurement.planName} ,'%')
            </if>
            <if test="measurement.id != null">
                and id = #{measurement.id}
            </if>

            <if test="measurement.totalWaterMeters != null">
                and total_water_meters = #{measurement.totalWaterMeters}
            </if>
            <if test="measurement.numberOfWaterMetersReviewed != null">
                and number_of_water_meters_reviewed = #{measurement.numberOfWaterMetersReviewed}
            </if>
            <if test="measurement.createDate != null">
                and create_date = #{measurement.createDate}
            </if>
            <if test="measurement.updateDate != null">
                and update_date = #{measurement.updateDate}
            </if>
            <if test="measurement.createBy != null and measurement.createBy != ''">
                and create_by = #{measurement.createBy}
            </if>
            <if test="measurement.updateBy != null and measurement.updateBy != ''">
                and update_by = #{measurement.updateBy}
            </if>
            <if test="measurement.status != null">
                and status = #{measurement.status}
            </if>
            <if test="measurement.billingPeriod != null and measurement.billingPeriod != ''">
                and billing_period = #{measurement.billingPeriod}
            </if>

            <include refid="permissionCondition"/>
        </where>
        order by mi.create_date desc
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into rmcp_measurement_instance(id, plan_id, total_water_meters, number_of_water_meters_reviewed,
                                              create_date, update_date, create_by, update_by, status, billing_period,
                                              clearing_day, clearing_end_date, clearing_start_date)
        values (#{id}, #{planid}, #{totalWaterMeters}, #{numberOfWaterMetersReviewed}, #{createDate}, #{updateDate},
                #{createBy}, #{updateBy}, #{status}, #{billingPeriod}, #{clearingDay}, #{clearingEndDate},
                #{clearingStartDate})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into rmcp_measurement_instance(id, plan_Id, total_water_meters, number_of_water_meters_reviewed,
        create_date, update_date, create_by, update_by, status, billing_period, clearing_day, clearing_end_date,
        clearing_start_date)
        values
        <foreach collection="measurementInstances" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.planid}, #{item.totalWaterMeters}, #{item.numberOfWaterMetersReviewed},
            #{item.createDate}, #{item.updateDate}, #{item.createBy}, #{item.updateBy}, #{item.status},
            #{item.billingPeriod}, #{item.clearingDay}, #{item.clearingEndDate}, #{item.clearingStartDate} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update rmcp_measurement_instance
        <set>
            <if test="planid != null">
                plan_Id = #{planid},
            </if>
            <if test="totalWaterMeters != null">
                total_water_meters = #{totalWaterMeters},
            </if>
            <if test="numberOfWaterMetersReviewed != null">
                number_of_water_meters_reviewed = #{numberOfWaterMetersReviewed},
            </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>
            <if test="billingPeriod != null ">
                billing_period = #{billingPeriod},
            </if>
            <if test="clearingDay != null">
                clearing_day = #{clearingDay},
            </if>
            <if test="clearingEndDate != null">
                clearing_end_date = #{clearingEndDate},
            </if>
            <if test="clearingStartDate != null">
                clearing_start_date = #{clearingStartDate},
            </if>
        </set>
        where id = #{id}
    </update>
    <update id="modifyTheNumberOfAudits">
        update rmcp_measurement_instance set number_of_water_meters_reviewed=
                                                 number_of_water_meters_reviewed+#{size}
                                where id=#{id}
    </update>
    <update id="modifyAllTheNumberOfAudits">
        update rmcp_measurement_instance set number_of_water_meters_reviewed=total_water_meters
        where id=#{id}
    </update>

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

    <!-- 总数 -->
    <select id="count" resultType="int">
        select count(*)
        from rmcp_measurement_instance
    </select>

    <select id="findClearingRecord" resultType="com.zcxk.rmcp.core.entity.MeasurementInstance">
        select id from rmcp_measurement_instance where id=#{id} and clearing_day=#{lastClearingDay}
    </select>
    <sql id="permissionCondition">
        <if test="userCondition.tenantId != null">and ms.tenant_id = #{userCondition.tenantId}</if>
        <if test="userCondition.userType!=null and userCondition.userType!=-999 and userCondition.userType!=-9999 and  userCondition.programItems != null and userCondition.programItems.size() > 0">
            <if test="userCondition.permissionType == 5 or userCondition.permissionType == 2">
                and ( ms.dept_org_id in
                <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                ms.company_org_id in
                <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="userCondition.permissionType == 4 or userCondition.permissionType == 3">
                and ms.dept_org_id in
                <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="userCondition.permissionType == 1">
                and ms.company_org_id in
                <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (ms.dept_org_id is null or ms.dept_org_id =0)
            </if>
        </if>
    </sql>
</mapper>
