<?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.charge.core.dao.HomePageReportMapper">

    <select id="getDeviceWaterForSixMonth" resultType="com.zcxk.charge.api.vo.DeviceWaterSupplyVo">
        select
        r.year,r.month,
        sum(r.SUM_VALUE)/10000 as "amount"
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.id
        left join sms_device_parm dp on s.id=dp.PARENT_SCENE_ID
        left join sms_year_report r on r.DEVICE_ID= dp.DEVICE_ID and r.ATTRIBUTE_ID = dp.ATTRIBUTE_ID

        where  dp.`STATUS`=1  and s.ENABLE_STATE = 1 and dp.TENANT_ID=#{user.tenantId}
        <if test="dto.companyOrgId != null">
            and s.COMPANY_ORG_ID = #{dto.companyOrgId}
        </if>
        and dp.PARM_TYPE = #{dto.parmType} and st.SCENE_TYPE_NAME = #{dto.sceneType}
        and r.year*12 + r.month >=  year(CURDATE())*12 + month(CURDATE()) - 5
        <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
        group by r.year,r.month
        order by r.year desc,r.month desc
    </select>

    <select id="countSceneByType" resultType="com.zcxk.charge.api.vo.CompanyCountVo">

        select
            sum(if(SCENE_TYPE_NAME = "水源",1,0)) as "waterSource"
            ,sum(if(SCENE_TYPE_NAME = "水厂",1,0)) as "waterFactory"
            ,sum(if(SCENE_TYPE_NAME = "管网",1,0)) as "pipeNetwork"
            ,sum(if(SCENE_TYPE_NAME = "泵站",1,0)) as "pumpingStation"
            ,0 as "waterMeter"
        from  sms_scene s
        left join sms_scene_type st on s.SCENE_TYPE_ID =st.ID
        where s.PARENT_SCENE_ID= 0 and s.`STATUS` = 1 and st.`STATUS` = 1
         and st.SCENE_TYPE_NAME in ('水源','水厂','管网','泵站')
         <if test="dto.companyOrgId != null">
            and s.COMPANY_ORG_ID = #{dto.companyOrgId}
         </if>
        and s.TENANT_ID=#{user.tenantId}
        <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
    </select>

    <select id="getWaterSalesDetails" resultType="com.zcxk.charge.api.vo.CompanyProduceVo">
        select
            sum(RECEIVABLE_TOTAL_AMOUNT) AS "receivableAmount",
            sum(RECEIVED_TOTAL_AMOUNT) AS "receivedAmount",
            sum(METER_READING_USAGE) AS "waterSale"
        from sms_month_revenue r
        where r.TENANT_ID = #{user.tenantId}
        <if test="dto.companyOrgId != null">
            and r.COMPANY_ORG_ID = #{dto.companyOrgId}
        </if>
        and year(r.COLLECT_DATE) =year(CURDATE()) and month(r.COLLECT_DATE) = month(CURDATE())
        <if test="user.userType!=null and user.userType!=-999 and user.userType != -9999 and  user.programItems != null and user.programItems.size() > 0">
            and r.COMPANY_ORG_ID in
            <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                #{item.orgId}
            </foreach>
        </if>
    </select>

    <select id="getPipeNetworkQualification" resultType="com.zcxk.charge.api.vo.WaterQualityRateVo">
        select
            sum(alarmNumber ) as  "abnormal",
            ROUND(sum(if(alarmNumber > 0 ,0,1))/if(count(1) != 0, count(1),1) * 100,2) as "averageQuality"
        from
        (
            select
                s.id as "sceneId"
                ,dp.DEVICE_ID as "deviceId"
                ,if(count(ad.id)>0,1,0) as  "alarmNumber"
            from sms_scene_type pst
            left join sms_scene ps on ps.SCENE_TYPE_ID =pst.ID
            left join sms_scene s on s.PARENT_SCENE_ID =ps.ID
            left join sms_scene_type st on s.SCENE_TYPE_ID =st.ID

            left join sms_device_parm dp on dp.PARENT_SCENE_ID =ps.ID and dp.SCENE_ID =s.ID
            left join sms_alarm_details ad on dp.SCENE_ID =ad.SCENE_ID and dp.DEVICE_ID =ad.DEVICE_ID and dp.attribute_id = ad.attribute_id
            and ad.STATE = 1  and ad.`STATUS` =1 and ad.ALARM_TYPE = '参数报警'
            where pst.`STATUS` = 1 and st.`STATUS` = 1 and ps.`STATUS` = 1 and s.`STATUS` = 1 and dp.`STATUS` = 1
            and pst.SCENE_TYPE_NAME ='管网' and st.SCENE_TYPE_NAME ='水质'
            <if test="dto.companyOrgId != null">
            and ps.COMPANY_ORG_ID = #{dto.companyOrgId}
            </if>
            and ps.TENANT_ID=#{user.tenantId}
            <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>

        group by s.id,dp.DEVICE_ID
        ) a
    </select>

    <select id="getWaterQualification" resultType="com.zcxk.charge.api.vo.WaterQualityRateVo">
        select
        sum(alarmNumber ) as  "abnormal",
        ROUND(sum(if(alarmNumber > 0 ,0,1))/if(count(1) != 0, count(1),1) * 100,2) as "averageQuality"
        from (
            select
            s.id as "sceneId"
            ,if(sum( if(dp.PARM_TYPE IN
                <foreach collection="dto.parmTypes" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                and ad.id is not null,1,0) )>0,1,0 ) AS "alarmNumber"
            from sms_scene_type st
            left join sms_scene s on s.SCENE_TYPE_ID =st.id and PARENT_SCENE_ID ='0'
            left join sms_device_parm dp on dp.PARENT_SCENE_ID =s.ID
            left join sms_alarm_details ad on ad.STATE = 1 and ad.PARENT_SCENE_ID=dp.PARENT_SCENE_ID and ad.DEVICE_ID =dp.DEVICE_ID and ad.ATTRIBUTE_ID =dp.ATTRIBUTE_ID

            where s.`STATUS`=1  and s.ENABLE_STATE = 1 and dp.`STATUS` = 1 and st.SCENE_TYPE_NAME = #{dto.sceneType}
            <if test="dto.companyOrgId != null">
                and s.COMPANY_ORG_ID = #{dto.companyOrgId}
            </if>
            and s.TENANT_ID =#{user.tenantId}
            <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
                <if test="user.permissonType == 5 or user.permissonType == 2">
                    and ( s.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    or
                    s.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    )
                </if>
                <if test="user.permissonType == 4 or user.permissonType == 3">
                    and s.DEPT_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                </if>
                <if test="user.permissonType == 1">
                    and s.COMPANY_ORG_ID in
                    <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                        #{item.orgId}
                    </foreach>
                    and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
                </if>
            </if>
            group by s.id
        ) a

    </select>

    <select id="getMonitorDataFifteenDays" resultType="com.zcxk.charge.api.vo.DeviceWaterSupplyVo">
        select
        r.year,r.month,r.day,
        sum(r.SUM_VALUE) as "amount"
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.id
        left join sms_device_parm dp on s.id=dp.PARENT_SCENE_ID
        left join sms_month_report r on r.DEVICE_ID= dp.DEVICE_ID and r.ATTRIBUTE_ID = dp.ATTRIBUTE_ID
        where  st.`STATUS`=1 and dp.`STATUS`=1  and s.ENABLE_STATE = 1
        and s.id = #{dto.sceneId}
        and st.SCENE_TYPE_NAME = #{dto.sceneType}
        and dp.PARM_TYPE = #{dto.parmType}
        and dp.TENANT_ID=#{user.tenantId}
        and r.COLLECT_DATE >= DATE_SUB(CURDATE(),INTERVAL 14 day)
        <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
        group by r.year,r.month,r.day
        order by r.year desc,r.month desc,r.day desc
    </select>

    <select id="getWaterQualificationForScene" resultType="java.math.BigDecimal">
        select
            ROUND(sum(if(alarmNumber > 0 ,0,1))/if(count(1) != 0, count(1),1) * 100,2)
        from
        (
        select
            s.id as "sceneId"
            ,d.id as "deviceId"
            ,count(ad.id) as  "alarmNumber"
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.ID
        left join sms_device_scene ds on ds.PARENT_SCENE_ID = s.ID
        left join sms_device d on d.id=ds.DEVICE_ID
        left join sms_device_parm dp on dp.SCENE_ID =ds.SCENE_ID and dp.DEVICE_ID =ds.DEVICE_ID and dp.PARM_TYPE =#{dto.parmType}
        left join sms_alarm_details ad on dp.SCENE_ID =ad.SCENE_ID and dp.DEVICE_ID =ad.DEVICE_ID and dp.ATTRIBUTE_ID =ad.ATTRIBUTE_ID
        and ad.STATE = 1  and ad.`STATUS` =1
        where st.`STATUS` = 1 and s.`STATUS` = 1 and ds.`STATUS` = 1 and d.`STATUS` = 1 and dp.`STATUS` = 1
        and s.id= #{dto.sceneId}
        and st.SCENE_TYPE_NAME = #{dto.sceneType}
        and s.TENANT_ID=#{user.tenantId}
        <if test="user.userType != null and user.userType!=-999 and user.userType != -9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>

        group by s.id,d.id
        ) a

    </select>

    <select id="getWaterQualificationByMonth" resultType="java.math.BigDecimal">
        select
        ROUND(if(count(1) >0 ,1-((sum(alarmNumber) /count(1))/timestampdiff(MINUTE,#{dto.monthDate},now())),1)* 100,2) as amount
        from
        (
        select
            ifnull(sum(
                if(ad.state = 1,
                timestampdiff(MINUTE,if(ad.ALARM_START_TIME > #{dto.monthDate},ad.ALARM_START_TIME ,#{dto.monthDate}),now())
                ,if(ad.ALARM_START_TIME > #{dto.monthDate} ,timestampdiff(MINUTE,ad.ALARM_START_TIME,ad.alarm_end_time), timestampdiff(MINUTE,#{dto.monthDate},ad.alarm_end_time))
                )),0) alarmNumber
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.ID
        left join sms_device_scene ds on ds.PARENT_SCENE_ID = s.ID
        left join sms_device d on d.id=ds.DEVICE_ID
        left join sms_device_parm dp on dp.SCENE_ID =ds.SCENE_ID and dp.DEVICE_ID =ds.DEVICE_ID and dp.PARM_TYPE =#{dto.parmType}
        left join sms_alarm_details ad on dp.SCENE_ID =ad.SCENE_ID and dp.DEVICE_ID =ad.DEVICE_ID and dp.ATTRIBUTE_ID =ad.ATTRIBUTE_ID
        and ad.`STATUS` =1 and (ad.alarm_end_time > #{dto.monthDate} or ad.state = 1)
        where st.`STATUS` = 1 and s.`STATUS` = 1 and ds.`STATUS` = 1 and d.`STATUS` = 1 and dp.`STATUS` = 1
        and s.id= #{dto.sceneId}
        and st.SCENE_TYPE_NAME = #{dto.sceneType}
        and s.TENANT_ID=#{user.tenantId}
        <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    <if test="item.orgId != null">
                        #{item.orgId}
                    </if>
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    <if test="item.orgId != null">
                        #{item.orgId}
                    </if>
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    <if test="item.orgId != null">
                        #{item.orgId}
                    </if>
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    <if test="item.orgId != null">
                        #{item.orgId}
                    </if>
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>

        group by s.id,d.id,dp.ATTRIBUTE_ID
        ) a
    </select>

    <select id="getWaterSupplyData" resultType="com.zcxk.charge.api.vo.WaterSupplyDataVo">
        select
        r.year
        ,r.month
        ,r.day
        ,dp.PARM_TYPE as "parmType"
        ,sum(r.SUM_VALUE) as "amount"
        ,da.UNIT as "unit"
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.id
        left join sms_device_parm dp on s.id=dp.PARENT_SCENE_ID
        left join sms_device_attribute da on dp.ATTRIBUTE_ID =da.ID
        left join sms_month_report r on r.DEVICE_ID =dp.DEVICE_ID and r.ATTRIBUTE_ID = dp.ATTRIBUTE_ID
        where  dp.`STATUS`=1  and st.`STATUS`=1 and s.`STATUS`=1 and s.ENABLE_STATE = 1
        and r.COLLECT_DATE >= date_add(curdate(), interval - day(curdate()) + 1 day)
        and dp.PARM_TYPE in (3,4,5,6,12)
        and s.id= #{dto.sceneId}
        and st.SCENE_TYPE_NAME = #{dto.sceneType}
        and dp.TENANT_ID=#{user.tenantId}
        <if test="user.userType!=null and user.userType!=-999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>

        group by r.year,r.month,r.day,dp.PARM_TYPE,da.UNIT
    </select>

    <!--查询前N个月的取水量供水量-->
    <select id="getDeviceWaterForMonth" resultType="com.zcxk.charge.api.vo.DeviceWaterSupplyVo">
        select
        r.year,r.month,
        sum(r.SUM_VALUE)/10000 as "amount"
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.id
        left join sms_device_parm dp on s.id=dp.PARENT_SCENE_ID
        left join sms_year_report r on r.DEVICE_ID= dp.DEVICE_ID and r.ATTRIBUTE_ID = dp.ATTRIBUTE_ID
        where  dp.`STATUS`=1  and s.ENABLE_STATE = 1 and dp.TENANT_ID=#{user.tenantId}
        and dp.PARM_TYPE = #{dto.parmType} and st.SCENE_TYPE_NAME = #{dto.sceneType}
        and r.year*12 + r.month >=  year(CURDATE())*12 + month(CURDATE()) + 1 - #{dto.month}
        and r.year*12 + r.month &lt; year(CURDATE())*12 + month(CURDATE()) + 1
        <if test="dto.companyOrgId != null">
            and s.COMPANY_ORG_ID = #{dto.companyOrgId}
        </if>
        <if test="user.userType != null and user.userType != -999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
        group by r.year,r.month
        order by r.year desc,r.month desc
    </select>


    <!--查询时间段内的取水量供水量 按月查询-->
<!--    <select id="getDeviceWaterForMonth" resultType="com.zcxkdto.generalview.DeviceWaterSupply">-->
<!--        select-->
<!--        r.year,r.month,r.day,-->
<!--        sum(r.SUM_VALUE)/10000 as "amount"-->
<!--        from sms_scene_type st-->
<!--        left join sms_scene s on s.SCENE_TYPE_ID =st.id-->
<!--        left join sms_device_parm dp on s.id=dp.PARENT_SCENE_ID-->
<!--        left join sms_year_report r on r.DEVICE_ID= dp.DEVICE_ID and r.ATTRIBUTE_ID = dp.ATTRIBUTE_ID-->
<!--        where  dp.`STATUS`=1  and s.ENABLE_STATE = 1 and dp.TENANT_ID=#{tenantId}-->
<!--        and dp.PARM_TYPE = #{parmType} and st.SCENE_TYPE_NAME = #{sceneType}-->
<!--        and r.year*12 + r.month >= #{startYear}*12 + #{startMonth}-->
<!--        and r.year*12 + r.month &lt;= #{endYear}*12 + #{endMonth}-->
<!--        <if test="companyOrgId != null">-->
<!--            and s.COMPANY_ORG_ID = #{companyOrgId}-->
<!--        </if>-->
<!--        <if test="userType!=null and userType!=-999 and userType!=-9999 and  programItems != null and programItems.size() > 0">-->
<!--            <if test="permissonType == 5 or permissonType == 2">-->
<!--                and ( s.DEPT_ORG_ID in-->
<!--                <foreach collection="programItems" item="item" open="(" close=")" separator=",">-->
<!--                    #{item.orgId}-->
<!--                </foreach>-->
<!--                or-->
<!--                s.COMPANY_ORG_ID in-->
<!--                <foreach collection="programItems" item="item" open="(" close=")" separator=",">-->
<!--                    #{item.orgId}-->
<!--                </foreach>-->
<!--                )-->
<!--            </if>-->
<!--            <if test="permissonType == 4 or permissonType == 3">-->
<!--                and s.DEPT_ORG_ID in-->
<!--                <foreach collection="programItems" item="item" open="(" close=")" separator=",">-->
<!--                    #{item.orgId}-->
<!--                </foreach>-->
<!--            </if>-->
<!--            <if test="permissonType == 1">-->
<!--                and s.COMPANY_ORG_ID in-->
<!--                <foreach collection="programItems" item="item" open="(" close=")" separator=",">-->
<!--                    #{item.orgId}-->
<!--                </foreach>-->
<!--                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)-->
<!--            </if>-->
<!--        </if>-->
<!--        group by r.year,r.month,r.day-->
<!--        order by r.year desc,r.month desc,r.day desc-->
<!--    </select>-->

    <select id="getDeviceWaterForDay" resultType="com.zcxk.charge.api.vo.DeviceWaterSupplyVo">
        select
        r.year,r.month,r.day,
        sum(r.SUM_VALUE)/10000 as "amount"
        from sms_scene_type st
        left join sms_scene s on s.SCENE_TYPE_ID =st.id
        left join sms_device_parm dp on s.id=dp.PARENT_SCENE_ID
        left join sms_month_report r on r.DEVICE_ID= dp.DEVICE_ID and r.ATTRIBUTE_ID = dp.ATTRIBUTE_ID
        where  st.`STATUS`=1 and dp.`STATUS`=1  and s.ENABLE_STATE = 1 and dp.TENANT_ID=#{user.tenantId}
        and r.COLLECT_DATE >= DATE_SUB(CURDATE(),INTERVAL #{dto.day}-1 day)
        and dp.PARM_TYPE = #{dto.parmType} and st.SCENE_TYPE_NAME = #{dto.sceneType}
        <if test="dto.companyOrgId != null">
            and s.COMPANY_ORG_ID = #{dto.companyOrgId}
        </if>
        <if test="user.userType != null and user.userType != -999 and user.userType!=-9999 and  user.programItems != null and user.programItems.size() > 0">
            <if test="user.permissonType == 5 or user.permissonType == 2">
                and ( s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                or
                s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                )
            </if>
            <if test="user.permissonType == 4 or user.permissonType == 3">
                and s.DEPT_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
            </if>
            <if test="user.permissonType == 1">
                and s.COMPANY_ORG_ID in
                <foreach collection="user.programItems" item="item" open="(" close=")" separator=",">
                    #{item.orgId}
                </foreach>
                and (s.DEPT_ORG_ID is null or s.DEPT_ORG_ID =0)
            </if>
        </if>
        group by r.year,r.month,r.day
        order by r.year desc,r.month desc,r.day desc
    </select>
</mapper>