<?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.tofly.watermeter.mapper.QfcbplandetailsMapper">

    <resultMap id="qfcbplandetailsMap" type="com.tofly.watermeter.entity.Qfcbplandetails">
        <id property="id" column="ID"/>
        <result property="planId" column="PLAN_ID"/>
        <result property="cbqyMc" column="CBQY_MC"/>
        <result property="cbqyBm" column="CBQY_BM"/>
        <result property="cbqydBm" column="CBQYD_BM"/>
        <result property="cbry" column="CBRY"/>
        <result property="cbryDepart" column="CBRY_DEPART"/>
        <result property="sinDouMonth" column="SIN_DOU_MONTH"/>
        <result property="startDay" column="START_DAY"/>
        <result property="endDay" column="END_DAY"/>
        <result property="isState" column="IS_STATE"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="isGeneral" column="IS_GENERAL"/>
        <result property="qydmc" column="QYDMC"/>
        <association property="sinDouMonthName" column="SIN_DOU_MONTH"
                     select="com.tofly.watermeter.mapper.QfcbplandetailsMapper.getSinDouMonthName"/>
        <association property="isStateName" column="IS_STATE"
                     select="getIsStateName"/>
        <association property="cbryName" column="CBRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
    </resultMap>


    <select id="getIsStateName" resultType="string">
        select decode(#{isState}, '0', '禁用', '1', '启用')
        from dual
    </select>

    <select id="getSinDouMonthName" resultType="java.lang.String">
        select decode(#{sinDouMonth}, '0', '双月', '1', '单月', '2', '每月')
        from dual
    </select>

    <select id="getQyCollect" resultType="java.util.Map">
        SELECT CBQY_BM,
               CBQY_MC,
               COUNT(1)                                     ALL_NUM,
               count(DISTINCT CBQYD_BM)                     QYDNUM,
               sum(CASE WHEN XZBS = 1 THEN 1 ELSE 0 END) as DWON_NUM,
               sum(CASE WHEN XZBS = 0 THEN 1 ELSE 0 END) as NO_DWON,
               sum(CASE WHEN CBBZ = 1 THEN 1 ELSE 0 END) as READ_NUM,
               sum(CASE WHEN CBBZ = 0 THEN 1 ELSE 0 END) as NO_READ
        FROM TF_YWPN_READMETERHIS_W
        WHERE CBRY = #{cbryId}
          AND IS_GENERAL = #{isGeneral}
          AND MONTH_PLAN = #{monthPlan}
          AND START_DAY <![CDATA[ <= ]]> #{day}
        GROUP BY CBQY_BM, CBQY_MC
        ORDER BY CBQY_MC
    </select>


    <select id="qydPlanCollect" resultType="java.util.Map">
        SELECT CBQYD_BM,
               QYDMC,
               COUNT(1)                                     ALL_READ,
               sum(CASE WHEN XZBS = 1 THEN 1 ELSE 0 END) as DWON,
               sum(CASE WHEN XZBS = 0 THEN 1 ELSE 0 END) as NO_DWON,
               sum(CASE WHEN CBBZ = 1 THEN 1 ELSE 0 END) as READ,
               sum(CASE WHEN CBBZ = 0 THEN 1 ELSE 0 END) as NO_READ,
               WM_CONCAT(distinct SBLX)                     table_type
        FROM TF_YWPN_READMETERHIS_W
        WHERE CBRY = #{cbryId}
          AND IS_GENERAL = #{isGeneral}
          AND CBQY_BM = #{qybm}
          AND MONTH_PLAN = #{monthPlan}
          AND START_DAY <![CDATA[ <= ]]> #{day}
        GROUP BY CBQYD_BM, QYDMC
        ORDER BY QYDMC
    </select>
    <select id="savePlanBatch">
        INSERT INTO TF_YWPN_QFCBPLANDETAILS_W(id,PLAN_ID,CBQY_MC,CBQY_BM,CBQYD_BM,QYDMC,IS_GENERAL, IS_STATE)
        <foreach collection="list" item="de" separator="union all">
            select
            #{de.id},
            #{de.planId},
            #{de.cbqyMc},
            #{de.cbqyBm},
            #{de.cbqydBm},
            #{de.qydmc},
            #{de.isGeneral},
            #{de.isState}
            from dual
        </foreach>
    </select>

    <select id="savePlanCbryBatch">
        INSERT INTO TF_YWPN_QFCBPLANDETAILS_W(id,PLAN_ID,CBQY_MC,CBQY_BM,CBQYD_BM,QYDMC,IS_GENERAL, IS_STATE , CBRY ,
        CBRY_DEPART,SIN_DOU_MONTH,START_DAY,END_DAY,CREATE_USER,CREATE_TIME)
        <foreach collection="list" item="de" separator="union all">
            select
            #{de.id,jdbcType=NUMERIC},
            #{de.planId,jdbcType=NUMERIC},
            #{de.cbqyMc,jdbcType=VARCHAR},
            #{de.cbqyBm,jdbcType=VARCHAR},
            #{de.cbqydBm,jdbcType=VARCHAR},
            #{de.qydmc,jdbcType=VARCHAR},
            #{de.isGeneral,jdbcType=VARCHAR},
            #{de.isState,jdbcType=VARCHAR},
            #{de.cbry,jdbcType=NUMERIC},
            #{de.cbryDepart,jdbcType=NUMERIC},
            #{de.sinDouMonth,jdbcType=VARCHAR},
            #{de.startDay,jdbcType=NUMERIC},
            #{de.endDay,jdbcType=NUMERIC},
            #{de.createUser,jdbcType=NUMERIC},
            #{de.createTime,jdbcType=TIMESTAMP}
            from dual
        </foreach>
    </select>

    <select id="appCollect" resultType="java.util.Map">
        SELECT temp.qyNum,
               temp.qydNum,
               temp.allNum,
               NVL(temp.readNum, 0)                                 readNum,
               NVL(temp.noReadNum, 0)                               noReadNum,
               NVL(temp.noUploadNum, 0)                             noUploadNum,
               NVL(temp.uploadNum, 0)                               uploadNum,
               NVL(trunc(temp.readNum / temp.allNum, 3) * 100, 0)   FINISH_PERCENT,
               NVL(trunc(temp.uploadNum / temp.allNum, 3) * 100, 0) UPLOAD_PERCENT
        FROM (SELECT COUNT(DISTINCT CBQY_BM)          qyNum,
                     COUNT(DISTINCT CBQYD_BM)         qydNum,
                     COUNT(1)                         allNum,
                     SUM(DECODE(CBBZ, 1, 1, 0))       readNum,
                     SUM(DECODE(CBBZ, 0, 1, 0))       noReadNum,
                     SUM(DECODE(IS_OFFLINE, 0, 0, 1)) noUploadNum,
                     SUM(DECODE(IS_OFFLINE, 0, 1, 0)) uploadNum
              FROM TF_YWPN_READMETERHIS_W
              WHERE MONTH_PLAN = #{yearMonth}
                AND CBRY = #{cbry}
                AND IS_GENERAL = #{isGeneral}) temp
    </select>
    <select id="getQydCbInfo" resultType="java.util.Map">
        SELECT COUNT(1) noFinishQYD
        FROM (
                 SELECT CBQYD_BM
                 FROM TF_YWPN_READMETERHIS_W
                 WHERE MONTH_PLAN = #{yearMonth}
                   AND CBRY = #{cbry}
                   AND IS_GENERAL = #{isGeneral}
                   AND CBBZ = '0'
                 GROUP BY CBQYD_BM
                 HAVING count(1) > 0)
    </select>
    <select id="qyCbInfo" resultType="java.util.Map">
        SELECT COUNT(1) noFinishQy
        FROM (
                 SELECT CBQY_BM
                 FROM TF_YWPN_READMETERHIS_W
                 WHERE MONTH_PLAN = #{yearMonth}
                   AND CBRY = #{cbry}
                   AND IS_GENERAL = #{isGeneral}
                   AND CBBZ = '0'
                 GROUP BY CBQY_BM
                 HAVING count(1) > 0) temp
    </select>

    <update id="initCbslEveyDay">
        UPDATE TF_YWPN_READMETERHIS_W a
        SET SCDS = (SELECT b.SCDS
                    FROM TF_YWPN_QFKHJBXX_W b
                    WHERE a.YHBH = b.YHBH
                      and a.MONTH_PLAN = #{yearMonth}
                      and a.CBBZ = '0'
                      and b.SBLX != '3'),
            ZGL  = (SELECT b.SCDS
                    FROM TF_YWPN_QFKHJBXX_W b
                    WHERE a.YHBH = b.YHBH
                      and a.MONTH_PLAN = #{yearMonth}
                      and a.CBBZ = '0'
                      and b.SBLX = '3')
        WHERE MONTH_PLAN = #{yearMonth}
          and a.CBBZ = '0'
          AND YHBH IN (SELECT b.YHBH
                       FROM TF_YWPN_QFKHJBXX_W b
                       WHERE a.MONTH_PLAN = #{yearMonth}
                         and a.CBBZ = '0'
                         AND a.YHBH = b.YHBH)
    </update>

    <select id="getClientMeterReading" resultType="map">
        select YHBH,
               YHXM,
               MONTH_PLAN,
               BCJJL,
               (select CNAME from TF_YWPN_SYSCHILDCODE_D d where ID = CBFS_CODE) cbfs
        from TF_YWPN_METERREADING_W_VIEW v
        where YHBH = #{client}
          and MONTH_PLAN between #{startDate} and #{endDate}
    </select>

    <select id="getUserZone" resultType="map">
        SELECT CBQY_BM, CBQY_MC, CBQYD_BM, QYDMC,
        LISTAGG(SBLX, ', ') WITHIN GROUP (ORDER BY SBLX) AS SBLX
        FROM (
        SELECT DISTINCT CBQY_BM, CBQY_MC, CBQYD_BM, QYDMC, SBLX
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = TO_CHAR(SYSDATE, 'yyyymm') AND CBRY = #{userId}
        <if test="isGeneral != null and isGeneral != ''">
            and IS_GENERAL = #{isGeneral}
        </if>
        ) subquery
        GROUP BY CBQY_BM, CBQY_MC, CBQYD_BM, QYDMC
    </select>

    <select id="countRepeatReadMater" resultType="integer">
        SELECT count(*)
        FROM TF_YWPN_READMETERHIS_W
        WHERE MONTH_PLAN = to_char(sysdate, 'yyyymm')
        GROUP BY YHBH
        HAVING COUNT(*) > 1
    </select>

    <delete id="deleteRepeatData">
        delete
        from TF_YWPN_READMETERHIS_W
        where MONTH_PLAN = to_char(sysdate, 'yyyymm')
          AND ROWID in (SELECT max(ROWID)
                        FROM TF_YWPN_READMETERHIS_W
                        WHERE MONTH_PLAN = to_char(sysdate, 'yyyymm')
                        GROUP BY YHBH
                        HAVING COUNT(YHBH) > 1)
    </delete>

</mapper>
