<?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="cn.com.victorysoft.business.cyyzs.dao.ProductionDataDao">


    <select id="selectList" resultType="cn.com.victorysoft.business.cyyzs.entity.ProductionData">
        SELECT
            B.GYLX, COUNT(1) ZJS,
            SUM(CASE WHEN KCS >= 1 THEN 1 ELSE 0 END) KJS,
            SUM(ZCS) ZCS, SUM(KCS) KCS,
            SUM(CASE WHEN IS_JCJ >= 1 THEN 1 ELSE 0 END) JCJS,
            SUM(JCCS) JCCS, SUM(HGC) HGC, SUM(CZC) CZC, SUM(QZC) QZC, SUM(BQC) BQC
        FROM
            (
            SELECT
                A.WELLNAME, COUNT(1) ZCS,
                SUM(CASE WHEN IS_JCJ = '检查井' THEN 1 ELSE 0 END) IS_JCJ,
                SUM(CASE WHEN IS_JCC = '检查层' THEN 1 ELSE 0 END) JCCS,
                SUM(CASE WHEN WELL_WATER > 0.5 AND JC_ZKSJ > 24 THEN 1 ELSE 0 END) KCS,
                SUM(CASE WHEN IS_JCC = '检查层' AND SPB >= 0.8 AND SPB <![CDATA[ <= ]]> 1.2 THEN 1 ELSE 0 END) HGC,
                SUM(CASE WHEN IS_JCC = '检查层' AND SPB > 1.2 THEN 1 ELSE 0 END) CZC,
                SUM(CASE WHEN IS_JCC = '检查层' AND SPB <![CDATA[ < ]]> 0.8 THEN 1 ELSE 0 END) QZC,
                SUM(CASE WHEN IS_JCC = '检查层' AND SPB IS NULL THEN 1 ELSE 0 END) BQC
            FROM
                (
                SELECT
                    A.*, DECODE(JC_ZPZL, 0, null, JC_ZZSL / JC_ZPZL) SPB
                FROM
                    (
                    SELECT
                        A.*, CASE WHEN IS_JCJ = '检查井' THEN (CASE WHEN JC_FT_COUNT >= 1 AND JC_ZZSL = 0 THEN '不检查层' ELSE '检查层' END) ELSE '不检查层' END IS_JCC
                    FROM
                        (
                        SELECT
                            A.*, CASE WHEN A.JC_ZKSJ <![CDATA[ < ]]> (7 * 24) OR J_BZYY_COUNT >= 1 OR (JC_ZKSJ <![CDATA[ < ]]> 24 OR J_ZZSL <![CDATA[ <= ]]> 0.5) THEN '不检查井' ELSE '检查井' END  IS_JCJ
                        FROM
                            (
                            SELECT
                                A.*,
                                SUM(JC_ZZSL) OVER(PARTITION BY WELLNAME) J_ZZSL,
                                SUM(JC_ZPZL) OVER(PARTITION BY WELLNAME) J_ZPZL,
                                SUM(JC_DTTZ_COUNT) OVER(PARTITION BY WELLNAME) J_DTTZ_COUNT,
                                SUM(JC_FT_COUNT) OVER(PARTITION BY WELLNAME) J_FT_COUNT,
                                SUM(JC_BZYY_COUNT) OVER(PARTITION BY WELLNAME) J_BZYY_COUNT
                            FROM
                                (
                                SELECT
                                    A.WELLNAME, A.SECTION,
                                    SUM(WELL_WATER) WELL_WATER,
                                    SUM(NVL(A.OPEN_TIME, 0)) JC_ZKSJ,
                                    SUM(NVL(A.DAILY_WATER, 0)) JC_ZZSL,
                                    SUM(NVL(A.PLAN_DAILY_WATER,0)) JC_ZPZL,
                                    SUM(CASE WHEN A.REMARK LIKE '%动态停注%' THEN 1 ELSE 0 END) JC_DTTZ_COUNT,
                                    SUM(CASE WHEN A.NOZZLE LIKE '%方停%' THEN 1 ELSE 0 END) JC_FT_COUNT,
                                    SUM(CASE WHEN A.REMARK LIKE '%大修%' THEN 1 WHEN REMARK LIKE '%调剖%' THEN 1
                                             WHEN A.REMARK LIKE '%挤素%' THEN 1
                                             WHEN A.REMARK LIKE '%化堵%' THEN 1
                                             WHEN A.REMARK LIKE '%酸化%' THEN 1
                                             WHEN A.REMARK LIKE '%示踪剂%' THEN 1 ELSE 0 END) JC_BZYY_COUNT
                                FROM
                                    WELL_SECTION_DAILY A
                                LEFT JOIN WATER_WELL_BASIC B ON A.WELLNAME = B.JH
                                WHERE
                                    1 = 1
                                <if test="ksrq != null and ksrq != ''"> AND A.RECORD_DATE >= TO_DATE(#{ksrq}, 'yyyy-mm-dd')</if>
                                <if test="jsrq != null and jsrq != ''"> AND A.RECORD_DATE <![CDATA[ <= ]]> TO_DATE(#{jsrq}, 'yyyy-mm-dd')</if>
                                <if test="dydm != null and dydm != ''"> AND A.QKDM = #{dydm}</if>
                                <if test="dwdm != null and dwdm != ''"> AND A.DWDM IN (SELECT UNITCODE FROM SYS_UNITINFO START WITH UNITCODE = #{dwdm} CONNECT BY PRIOR UNITCODE = P_UNITCODE)</if>
                                <if test="yclx != null and yclx != ''"> AND B.YCLX = #{yclx}</if>
                                GROUP BY A.WELLNAME, A.SECTION
                                ) A
                            ) A
                        ) A
                    ) A
                ) A
                GROUP BY WELLNAME
            ) A
        LEFT JOIN WELL_PROCESS_TYPE B ON A.WELLNAME = B.JH
        GROUP BY B.GYLX
    </select>
</mapper>