<?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.custom.mapper.statisticanalysis.ReportManagementMapper">

    <select id="getEconomicBenefitCountAndAmount" resultType="hashmap">
        select zg.RECTIFY_TYPE,count(1) as count,nvl(sum(zg.INCREASE_PROFIT),0) as amount
        from TF_YWPN_ZLZG_W zg left join TF_YWPN_ZLRZ_W rz on zg.CERTIFICATION_NUMBER = rz.CERTIFICATION_NUMBER
        where to_char(rz.DISPATCH_TIME,'yyyy-mm') between #{startDate} AND #{endDate}
        and VERIFY_STATUS = '4'
        group by zg.RECTIFY_TYPE
    </select>
<!--    select RECTIFY_TYPE,count(1) as count,nvl(sum(INCREASE_PROFIT),0) as amount-->
<!--    from TF_YWPN_ZLZG_W where to_char(RESULT_TIME_LIMIT,'yyyy-mm') between #{startDate} AND #{endDate}-->
<!--                        and VERIFY_STATUS = '4'-->
<!--    group by RECTIFY_TYPE-->

    <select id="getEconomicBenefitDetail" resultType="hashmap" >
        select zg.ID zgid,zg.RECTIFY_NUMBER zgbh,zg.CLIENT_NUMBER khbh, zg.CLIENT_NAME khxm, gl.QYBM qybm,
               gl.QYDBM qydbm,zg.CLIENT_ADDRESS khdz,zg.RECTIFY_TYPE zglb,zg.INCREASE_PROFIT jjxy,
               zg.INCREASE_VOLUME zjsl,zg.RECTIFY_TIME zgzj
        from TF_YWPN_ZLZG_W zg left join TF_YWPN_ZLRZ_W rz on zg.CERTIFICATION_NUMBER = rz.CERTIFICATION_NUMBER
                               left join TF_YWPN_YHBINGQY_W gl on zg.CLIENT_NUMBER = gl.YHBH
        where to_char(rz.DISPATCH_TIME,'yyyy-mm') between #{startDate} AND #{endDate}
                               and VERIFY_STATUS = '4'
          and zg.RECTIFY_TYPE = #{rectifyType}
    </select>
<!--   select * from TF_YWPN_ZLZG_W where to_char(RESULT_TIME_LIMIT,'yyyy-mm') between #{startDate} AND #{endDate}
                          and VERIFY_STATUS = '4'
                          and RECTIFY_TYPE = #{rectifyType}-->
    <select id="getQualityStatisticsByYearMonth" resultType="hashmap">
        select CERTIFICATION_PROJECT,to_char(DISPATCH_TIME,'yyyy-mm') yearMonth,count(ID) sum
        from TF_YWPN_ZLRZ_W
        where to_char(DISPATCH_TIME,'yyyy-mm') = #{yearMonth}
        <if test="queryType != null and queryType != '' ">
            and CERTIFICATION_PROJECT in
            <foreach collection="queryType.split(',')" item="type"  open="(" separator="," close=")" >
                #{type}
            </foreach>
        </if>
        group by CERTIFICATION_PROJECT,to_char(DISPATCH_TIME,'yyyy-mm')
    </select>

    <select id="getQualityStatisticsDetail" resultType="hashmap">
        select zg.*
        from TF_YWPN_ZLRZ_W rz left join TF_YWPN_ZLZG_W zg on rz.CERTIFICATION_NUMBER = zg.CERTIFICATION_NUMBER
        where rz.CERTIFICATION_PROJECT = #{zglx} and to_char(rz.DISPATCH_TIME,'yyyy-mm') = #{yearMonth}
    </select>

    <!-- 这里应该查询的是 TF_YWPN_QFCBQFMXLS_W 表，但因当前LS表设置字段不全，写不了逻辑，暂时用TF_YWPN_QFCBQFMX_W 代替 2021-06-10 -->
    <!--<select id="getUnqualifiedQuantity" resultType="hashmap">
        select to_char(CBRQ,'yyyy-mm') yearMonth,nvl(count(*),0) count
        from TF_YWPN_QFCBQFMX_W
        where AUDIT_RESULT = '0'
          and CBPQ = #{cbpq}
          and to_char(CBRQ,'yyyy-mm') between #{startDate} and #{endDate}
        group by to_char(CBRQ,'yyyy-mm')
    </select>-->

    <!-- 6.15 修改关联查询逻辑 0为合格 1为不合格 -->
    <select id="getUnqualifiedQuantity" resultType="hashmap">
        select to_char(CBRQ,'yyyy-mm') yearMonth,nvl(count(*),0) count from (
            <include refid="getTempView" /> ) temp
        where isPass = '1'
        group by to_char(CBRQ,'yyyy-mm')
    </select>

    <!-- 这里应该查询的是 TF_YWPN_QFCBQFMXLS_W 表，但因当前LS表设置字段不全，写不了逻辑，暂时用TF_YWPN_QFCBQFMX_W 代替 2021-06-10 -->
<!--    <select id="getPassRate" resultType="hashmap" >
        select to_char(CBRQ,'yyyy-mm') yearMonth,
               round(nvl(count(*),0) * 100 / (select count(*) from TF_YWPN_QFCBQFMX_W WHERE CBPQ = #{cbpq}
                         and to_char(CBRQ,'yyyy-mm') between #{startDate} and #{endDate} ),2 ) ratio
        from TF_YWPN_QFCBQFMX_W
        where AUDIT_RESULT = '1'
          and CBPQ = #{cbpq}
          and to_char(CBRQ,'yyyy-mm') between #{startDate} and #{endDate}
        group by to_char(CBRQ,'yyyy-mm')
    </select>-->
    <!-- 6.15 修改关联查询关系  0为合格 1为不合格  -->
    <select id="getMonthlyPassRate" resultType="hashmap" >
        select to_char(temp.CBRQ,'yyyy-mm') yearMonth,
               round(nvl(count(temp.yhbh),0) * 100 / (select count(1) from TF_YWPN_QFCBQFMXLS_W WHERE CBPQ = #{cbpq}
                     and to_char(CBRQ,'yyyy-mm') = #{queryDate} ),2 ) ratio
        from (
                 select cb.YHBH yhbh,cb.CBPQ cbpq,cb.CBRQ cbrq,nvl(rz.IS_QUALIFIED,0) isPass
                 from TF_YWPN_QFCBQFMXLS_W cb left join TF_YWPN_ZLRZ_W rz on rz.CLIENT_NUMBER = cb.YHBH
                     and to_char(rz.DISPATCH_TIME,'yyyy-mm') = to_char(cb.CBRQ,'yyyy-mm')
                 where CBPQ = #{cbpq} and to_char(CBRQ,'yyyy-mm') = #{queryDate} ) temp
        where temp.isPass = '0'
          and temp.CBPQ = #{cbpq}
          and to_char(temp.CBRQ,'yyyy-mm') = #{queryDate}
        group by to_char(temp.CBRQ,'yyyy-mm')
    </select>

    <select id="getQualityStatisticsByCertificationProject" resultType="hashmap" >
        select to_char(DISPATCH_TIME,'yyyy-mm') queryDate,count(ID) count
        from TF_YWPN_ZLRZ_W
        where CERTIFICATION_PROJECT = #{type}
          and to_char(DISPATCH_TIME,'yyyy-mm') between #{startDate} and #{endDate}
        group by to_char(DISPATCH_TIME,'yyyy-mm')
    </select>

    <select id="getUnqualifiedQuantityDetail" resultType="hashmap" >
        select * from TF_YWPN_QFCBQFMX_W
        where AUDIT_RESULT = '0'
          and CBPQ = #{cbpq}
          and to_char(CBRQ,'yyyy-mm') = #{queryDate}
    </select>

    <select id="getPassRateDetailDetail" resultType="hashmap" >
        select cb.YHBH yhbh,cb.AUDIT_RESULT pass,rz.CERTIFICATION_NUMBER rzbh
        from TF_YWPN_QFCBQFMX_W cb left join
             (SELECT CERTIFICATION_NUMBER,CLIENT_NUMBER FROM TF_YWPN_ZLRZ_W
              WHERE TO_CHAR(DISPATCH_TIME,'YYYY-MM') = #{queryDate} ) rz on cb.YHBH = rz.CLIENT_NUMBER
        where to_char(cb.CBRQ,'yyyy-mm') = #{queryDate}
          and cb.CBPQ = #{cbpq}
    </select>

    <sql id="getTempView" >
        select cb.YHBH yhbh,cb.CBPQ cbpq,cb.CBRQ cbrq,nvl(rz.IS_QUALIFIED,0) isPass
            from TF_YWPN_QFCBQFMXLS_W cb left join TF_YWPN_ZLRZ_W rz
                on rz.CLIENT_NUMBER = cb.YHBH and to_char(rz.DISPATCH_TIME,'yyyy-mm') = to_char(cb.CBRQ,'yyyy-mm')
        where CBPQ = #{cbpq}
          and to_char(CBRQ,'yyyy-mm') between #{startDate} and #{endDate}
    </sql>

    <select id="getRectifyByNumber" resultType="hashmap" >
        select * from TF_YWPN_ZLZG_W where RECTIFY_NUMBER = #{rectifyNumber}
    </select>


</mapper>
