<?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.ReadRateTodayMapper">

  <sql id="Base_Column_List">
    stat_day, tenant_id, category_id, company_org_id, dept_org_id, device_count,
    `read_times`, real_read_times, un_read_times, read_rate, date_create, date_update
  </sql>

  <sql id="permissionCondition">
    <if test="userCondition.tenantId != null">and rd.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 ( rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        rd.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 rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="userCondition.permissionType == 1">
        and rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (rd.dept_org_id is null or rd.dept_org_id =0)
      </if>
    </if>
  </sql>

  <select id="statReadRateGroupCategory" resultType="com.zcxk.rmcp.api.vo.StatisticsReadRateVo">
    SELECT
    rc.name, sum(rd.read_times) read_times, sum(rd.real_read_times) real_read_times,
    SUM(rd.un_read_times) un_read_times, ROUND(sum(rd.real_read_times)/sum(rd.read_times),4) readRate
    from rmcp_stat_meter_read_rate_by_community_today rd
    LEFT JOIN rmcp_product_category rc on rd.category_id = rc.id
    where
    1 = 1
    <include refid="permissionCondition"></include>
    GROUP BY rd.category_id
  </select>


  <select id="countReadRate" resultType="com.zcxk.rmcp.api.vo.StatisticsReadRateVo">
    SELECT sum(rd.read_times) read_times, sum(rd.real_read_times) real_read_times,
    SUM(rd.un_read_times) un_read_times, ROUND(sum(rd.real_read_times)/sum(rd.read_times),4) readRate
    from rmcp_stat_meter_read_rate_by_community_today rd
    LEFT JOIN rmcp_product_category rc on rd.category_id = rc.id
    where
    1 = 1
    <if test="dto.categoryId != null and dto.categoryId != ''">
      and rd.category_id = #{dto.categoryId}
    </if>
    <if test="dto.readRateBegin != null and dto.readRateEnd == null">
      and rd.read_rate >= #{dto.readRateBegin}
    </if>
    <if test="dto.readRateBegin == null and dto.readRateEnd != null">
      and #{dto.readRateEnd} >= rd.read_rate
    </if>
    <if test="dto.readRateBegin != null and dto.readRateEnd != null">
      and rd.read_rate BETWEEN #{dto.readRateBegin} and  #{dto.readRateEnd}
    </if>
    <include refid="permissionCondition"></include>
  </select>

  <select id="listReadRate" resultType="com.zcxk.rmcp.api.vo.ReadRateVo">
    SELECT
    rc.name categoryName, rd.stat_day, rm.`name` communityName, rd.read_times, rd.real_read_times,
    rd.un_read_times,rd.read_rate,
    org1.org_name as companyOrgName,
    org2.org_name as deptOrgName
    from rmcp_stat_meter_read_rate_by_community_today rd
    LEFT JOIN rmcp_product_category rc on rd.category_id = rc.id
    left join rmcp_org org1 on org1.id = rd.company_org_id
    left join rmcp_org org2 on org2.id = rd.dept_org_id
    left join rmcp_community rm on rm.id = rd.community_id
    where
    1=1
    and stat_day = #{statDay}
    <if test="dto.categoryId != null and dto.categoryId != ''">
      and rd.category_id = #{dto.categoryId}
    </if>
    <if test="dto.readRateBegin != null and dto.readRateEnd == null">
      and rd.read_rate >= #{dto.readRateBegin}
    </if>
    <if test="dto.readRateBegin == null and dto.readRateEnd != null">
      and #{dto.readRateEnd} >= rd.read_rate
    </if>
    <if test="dto.readRateBegin != null and dto.readRateEnd != null">
      and rd.read_rate BETWEEN #{dto.readRateBegin} and  #{dto.readRateEnd}
    </if>
    <include refid="permissionCondition"></include>
    <if test="dto.sort == null or dto.sort == 1">
      order by rd.read_rate desc
    </if>
    <if test="dto.sort != null and dto.sort == 0">
      order by rd.read_rate asc
    </if>
  </select>
</mapper>