<?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.MeasurementSettlementMapper">
    <!-- 结果集 -->
    <resultMap type="com.zcxk.rmcp.core.entity.MeasurementSettlement" id="MeasurementSettlementMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="planName" column="plan_name" jdbcType="VARCHAR"/>
        <result property="settlementTime" column="settlement_time" jdbcType="TIMESTAMP"/>
        <result property="companyOrgId" column="company_org_id" jdbcType="INTEGER"/>
        <result property="deptOrgId" column="dept_org_id" jdbcType="INTEGER"/>
        <result property="communityId" column="community_id" jdbcType="INTEGER"/>
        <result property="billingCycle" column="billing_cycle" 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="taskId" column="task_id" jdbcType="INTEGER"/>
        <result property="tenantId" column="tenant_id" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 基本字段 -->
    <sql id="Base_Column_List">
        id, plan_name, settlement_time, company_org_id, dept_org_id, community_id, billing_cycle, create_date, update_date, create_by, update_by, status, task_id, tenant_id    </sql>

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


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

    <!--通过实体作为筛选条件查询-->
    <select id="selectList" resultType="com.zcxk.rmcp.api.dto.measurementsettlement.MeasurementSettlementDto">
        select
        ms.id,ms.settlement_time,ms.plan_name,ms.billing_cycle,
               company.ORG_NAME companyOrgName,depart.ORG_NAME deptOrgName,
               commuity.name communityName,task_id
        from rmcp_measurement_settlement ms
        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>
            ms.status=1
            <if test="measurement.id != null">
                and id = #{measurement.id}
            </if>
            <if test="measurement.planName != null and measurement.planName != ''">
                and plan_name  like concat('%',#{measurement.planName} ,'%')
            </if>

            <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.communityId != null">
                and ms.community_id = #{measurement.communityId}
            </if>
            <if test="measurement.totalWaterMeters != null">
                and total_water_meters = #{measurement.totalWaterMeters}
            </if>
            <if test="measurement.billingCycle != null">
                and billing_cycle = #{measurement.billingCycle}
            </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 measurement.status != ''">
                and ms.status = #{measurement.status}
            </if>
            <if test="measurement.taskId != null">
                and task_id = #{measurement.taskId}
            </if>
            <include refid="permissionCondition"/>
        </where>
        order by ms.create_date desc
    </select>

    <!-- 新增所有列 -->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into rmcp_measurement_settlement(id, plan_name, settlement_time, company_org_id, dept_org_id,
                                                community_id, billing_cycle, create_date, update_date, create_by,
                                                update_by, status, task_id, tenant_id,total_water_meters)
        values (#{id}, #{planName}, #{settlementTime}, #{companyOrgId}, #{deptOrgId}, #{communityId}, #{billingCycle},
                #{createDate}, #{updateDate}, #{createBy}, #{updateBy}, #{status}, #{taskId}, #{tenantId},#{totalWaterMeters})
    </insert>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        insert into rmcp_measurement_settlement(id, plan_name, settlement_time, company_org_id, dept_org_id,
        community_id, billing_cycle, create_date, update_date, create_by, update_by, status, task_id, tenant_id)
        values
        <foreach collection="measurementSettlements" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.planName}, #{item.settlementTime}, #{item.companyOrgId}, #{item.deptOrgId},
            #{item.communityId}, #{item.billingCycle}, #{item.createDate}, #{item.updateDate}, #{item.createBy},
            #{item.updateBy}, #{item.status}, #{item.taskId}, #{item.tenantId} )
        </foreach>
    </insert>

    <!-- 通过主键修改数据 -->
    <update id="update">
        update rmcp_measurement_settlement
        <set>
            <if test="planName != null and planName != ''">
                plan_name = #{planName},
            </if>
            <if test="settlementTime != null">
                settlement_time = #{settlementTime},
            </if>
            <if test="companyOrgId != null">
                company_org_id = #{companyOrgId},
            </if>
            <if test="deptOrgId != null">
                dept_org_id = #{deptOrgId},
            </if>
            <if test="communityId != null">
                community_id = #{communityId},
            </if>
            <if test="billingCycle != null">
                billing_cycle = #{billingCycle},
            </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="taskId != null">
                task_id = #{taskId},
            </if>
            <if test="tenantId != null and tenantId != ''">
                tenant_id = #{tenantId},
            </if>
            <if test="totalWaterMeters != null">
                total_water_meters = #{totalWaterMeters},
            </if>
        </set>
        where id = #{id}
    </update>


    <!--通过主键删除-->
    <delete id="deleteById">
        delete
        from rmcp_measurement_settlement
        where id = #{id}
    </delete>
    <update id="deletePlan">
        update rmcp_measurement_settlement set status=0
        where id  in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>
    <update id="deleteAll">
        update rmcp_measurement_settlement ms set status=0
        <where>
            <include refid="permissionCondition"/>
        </where>
    </update>

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

    <select id="selectRecordList"
            resultType="com.zcxk.rmcp.api.dto.measurementsettlement.MeasurementSettlementDto">
        select
        <include refid="Base_Column_List"/>
        from rmcp_measurement_settlement ms
        <where>
        status=1
            <if test="measurement.id != null">
                and id = #{measurement.id}
            </if>
            <if test="measurement.planName != null and measurement.planName != ''">
                and plan_name  like concat('%',#{measurement.planName} ,'%')
            </if>
            <if test="measurement.settlementTime != null">
                and settlement_time = #{measurement.settlementTime}
            </if>
            <if test="measurement.companyOrgId != null">
                and company_org_id = #{measurement.companyOrgId}
            </if>
            <if test="measurement.deptOrgId != null">
                and dept_org_id = #{measurement.deptOrgId}
            </if>
            <if test="measurement.communityId != null">
                and community_id = #{measurement.communityId}
            </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.billingPeriod != null">
                and billing_period = #{measurement.billingPeriod}
            </if>
            <if test="measurement.billingCycle != null">
                and billing_cycle = #{measurement.billingCycle}
            </if>
            <if test="measurement.planStatus ==1">
                and plan_status in(1,2)
            </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 measurement.status != ''">
                and status = #{measurement.status}
            </if>
            <if test="measurement.taskId != null">
                and task_id = #{measurement.taskId}
            </if>
            <include refid="permissionCondition"/>
        </where>
    </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>
