<?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.dao.ComdisplayMapLocationMapper">


    <resultMap type="com.zcxk.entity.ComdisplayMapLocationEntity" id="ComdisplayMapLocationResult">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="pointLeft" column="point_left"/>
        <result property="pointTop" column="point_top"/>
        <result property="companyOrgId" column="company_org_id"/>
        <result property="status" column="status"/>
        <result property="dateCreate" column="date_create"/>
        <result property="createBy" column="create_by"/>
        <result property="dateUpdate" column="date_update"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <!--  实体栏位  -->
    <sql id="comdisplayMapLocationColumns">
         a.id as "id" ,
         a.tenant_id as "tenantId" ,
         a.point_left as "pointLeft" ,
         a.point_top as "pointTop" ,
         a.company_org_id as "companyOrgId" ,
         a.status as "status" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy" 
     </sql>


    <!--  根据主键获取实体   -->
    <select id="findComdisplayMapLocationById" resultType="com.zcxk.entity.ComdisplayMapLocationEntity">
        SELECT
        <include refid="comdisplayMapLocationColumns"/>
        FROM sms_comdisplay_map_location a
        WHERE a.id = #{id}
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.entity.ComdisplayMapLocationEntity">
        SELECT
        <include refid="comdisplayMapLocationColumns"/>
        FROM sms_comdisplay_map_location a
        <where>
            a.status=1
            <if test="id != null ">and a.id = #{id}</if>
            <if test="tenantId != null  and tenantId != ''">and a.tenant_id = #{tenantId}</if>
        </where>
    </select>

    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.entity.ComdisplayMapLocationEntity">
        SELECT
        <include refid="comdisplayMapLocationColumns"/>
        FROM sms_comdisplay_map_location a
        <where>
            a.status=1
            <if test="comdisplayMapLocation.id != null ">and a.id = #{comdisplayMapLocation.id}</if>
            <if test="comdisplayMapLocation.tenantId != null  and comdisplayMapLocation.tenantId != ''">and a.tenant_id
                = #{comdisplayMapLocation.tenantId}
            </if>
        </where>
    </select>
    <select id="selectMapDataForCompany" resultType="com.zcxk.dto.ComdisplayInfoDto">
    select a.COMPANY_ORG_ID as 'companyOrgId',a.POINT_LEFT as 'pointLeft',
           a.POINT_TOP as 'pointTop',e.meterReadingArrivalRate,e.receivableTotalAmount,
           e.receivedTotalAmount,e.userMeterCount,e.waterUsage
    from  sms_comdisplay_map_location a
    left join (select c.company_org_id,c.USER_METER_COUNT as 'userMeterCount',c.METER_READING_ARRIVAL_RATE  as 'meterReadingArrivalRate',
                      c.RECEIVABLE_TOTAL_AMOUNT as 'receivableTotalAmount',c.RECEIVED_TOTAL_AMOUNT as 'receivedTotalAmount',
                      c.METER_READING_USAGE as 'waterUsage'
    from sms_month_revenue c
    inner join (select b.company_org_id,max(b.collect_date) as  collect_date
                from sms_month_revenue b
                <if test="tenantId != null  and tenantId != ''">where b.TENANT_ID = #{tenantId}</if>
                group by b.company_org_id ) d
          on c.company_org_id=d.company_org_id and c.collect_date=d.collect_date
          <if test="tenantId != null  and tenantId != ''">where c.TENANT_ID = #{tenantId}</if>
       ) e on a.COMPANY_ORG_ID=e.company_org_id
      where a.`STATUS`=1
     <if test="tenantId != null  and tenantId != ''">and a.TENANT_ID = #{tenantId}</if>
    </select>
    <select id="selectMapDataForSingleCompany" resultType="com.zcxk.dto.ComdisplayInfoDto">
        select a.COMPANY_ORG_ID as 'companyOrgId',a.POINT_LEFT as 'pointLeft',
        a.POINT_TOP as 'pointTop',e.meterReadingArrivalRate,e.receivableTotalAmount,
        e.receivedTotalAmount,e.userMeterCount,e.waterUsage
        from  sms_comdisplay_map_location a
        left join (select c.company_org_id,c.USER_METER_COUNT as 'userMeterCount',c.METER_READING_ARRIVAL_RATE  as 'meterReadingArrivalRate',
        c.RECEIVABLE_TOTAL_AMOUNT as 'receivableTotalAmount',c.RECEIVED_TOTAL_AMOUNT as 'receivedTotalAmount',
        c.METER_READING_USAGE as 'waterUsage'
        from sms_month_revenue c
        inner join (select b.company_org_id,max(b.collect_date) as  collect_date
        from sms_month_revenue b
        <if test="tenantId != null  and tenantId != ''">where b.TENANT_ID = #{tenantId}</if>
        group by b.company_org_id ) d
        on c.company_org_id=d.company_org_id and c.collect_date=d.collect_date
        <if test="tenantId != null  and tenantId != ''">where c.TENANT_ID = #{tenantId}</if>
        ) e on a.COMPANY_ORG_ID=e.company_org_id
        where a.`STATUS`=1
        <if test="tenantId != null  and tenantId != ''">and a.TENANT_ID = #{tenantId}</if>
        <if test="companyOrgId != null  and companyOrgId != ''">and a.COMPANY_ORG_ID = #{companyOrgId}</if>
    </select>
</mapper>