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

  <sql id="Base_Column_List">
    id, stat_day, tenant_id, company_org_id, dept_org_id, category_id, community_id, water_consumption, 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>

  <insert id="insertMeterReadWaterRecord" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.MeterReadWaterRecord" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_meter_read_water_record
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="statDay != null">
      stat_day,
      </if>
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="companyOrgId != null">
        company_org_id,
      </if>
      <if test="deptOrgId != null">
        dept_org_id,
      </if>
      <if test="categoryId != null">
        category_id,
      </if>
      <if test="communityId != null">
        community_id,
      </if>
      <if test="waterConsumption != null">
        water_consumption,
      </if>
      <if test="dateCreate != null">
        create_date,
      </if>
      <if test="dateUpdate != null">
        update_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="statDay != null">
        #{statDay},
      </if>
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="companyOrgId != null">
        #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        #{deptOrgId},
      </if>
      <if test="categoryId != null">
        #{categoryId},
      </if>
      <if test="communityId != null">
        #{communityId},
      </if>
      <if test="waterConsumption != null">
        #{waterConsumption},
      </if>
      <if test="dateCreate != null">
        #{createDate},
      </if>
      <if test="dateUpdate != null">
        #{updateDate},
      </if>
    </trim>
  </insert>

  <insert id="batchInsert">
    insert into rmcp_meter_read_water_record(stat_day, tenant_id, company_org_id, dept_org_id, category_id, community_id, water_consumption, date_create,
    date_update)
    values
    <foreach collection="waterRecordList" item="item" index="index" separator=",">
      (#{item.statDay}, #{item.tenantId}, #{item.companyOrgId}, #{item.deptOrgId}, #{item.categoryId}, #{item.communityId},
      #{item.waterConsumption}, #{item.createDate}, #{item.updateDate})
    </foreach>
  </insert>

  <select id="listWaterRecord" resultType="com.zcxk.rmcp.api.vo.MeterReadWaterRecordVo">
  select sum(rd.water_consumption) waterConsumption, community_id, stat_day
  from rmcp_meter_read_water_record rd
  where
  rd.stat_day BETWEEN #{beginDate} and #{endDate}
  <include refid="permissionCondition"></include>
  group by community_id, stat_day
  </select>

  <select id="totalReadRate" resultType="com.zcxk.rmcp.api.vo.StatisticsReadRateVo">
    select
    ifnull(SUM(IF(rmrr.`read_status` = 1, 1, 0)),0) as un_read_times,
    ifnull(SUM(IF(rmrr.`read_status` = 2, 1, 0)),0) as real_read_times
    from rmcp_install_list ril
    inner join rmcp_meter_read_record rmrr on (ril.device_id = rmrr.device_id and rmrr.read_date = DATE_FORMAT( date_add(NOW(), interval -1 day), '%Y%m%d' ))
    where ril.status = 1 and ril.install_plan_id = #{planId}
  </select>
</mapper>