<?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.LayeredWaterDao">
    <sql id="Base_Column_List">
        RECORD_DATE
        , DWDM, su.UNITNAME DWMC, QKDM, sb.BLOCKNAME QKMC,
        YCLX, (select flag_name from sys_flag where category_code = 'YCLX' and flag_code = yclx) YCLXMC,
        GYLX, (select flag_name from sys_flag where category_code = 'GYLX' and flag_code = gylx) GYLXMC,
        WELLNAME, LAYER, PLAN_DAILY_WATER, PP, PC, PT, DAILY_WATER
    </sql>
    <select id="selectList" parameterType="cn.com.victorysoft.business.cyyzs.param.LayeredWaterParam"
            resultType="cn.com.victorysoft.business.cyyzs.entity.LayeredWater">
        select * from (
        SELECT RECORD_DATE, DWDM, su.UNITNAME DWMC, QKDM, sb.BLOCKNAME QKMC, YCLX,
        (SELECT flag_name FROM sys_flag WHERE category_code = 'YCLX' AND flag_code = yclx) YCLXMC,
        GYLX,
        (SELECT flag_name FROM sys_flag WHERE category_code = 'GYLX' AND flag_code = gylx) GYLXMC,
        WELLNAME, LAYER, PLAN_DAILY_WATER, PP, PC, PT, DAILY_WATER, SECTION
        FROM WELL_SECTION_DAILY wsd
        LEFT JOIN WATER_WELL_BASIC web ON wsd.WELLNAME = web.JH
        LEFT JOIN WELL_PROCESS_TYPE wpt ON wsd.WELLNAME = wpt.JH and wpt.fwsj &lt;= #{enddate,jdbcType=VARCHAR} and rownum = '1'
        LEFT JOIN sys_unitinfo su ON wsd.UNITCODE = su.UNITCODE
        LEFT JOIN sys_blockinfo sb ON web.QKDM = sb.BLOCKCODE
        WHERE RECORD_DATE = #{enddate,jdbcType=VARCHAR}
        AND wsd.UNITCODE IN (SELECT unitcode
        FROM SYS_UNITINFO
        START WITH UNITCODE = '30200000'
        CONNECT BY PRIOR UNITCODE = P_UNITCODE)) a
        inner join
        (select SECTION,WELLNAME,
        ROUND(sum(DAILY_WATER), 2) ljzsl,
        ROUND(sum(OPEN_TIME), 2) ljzssj,
        case when sum(OPEN_TIME) = '0' then 0 when sum(OPEN_TIME) = null then 0 else
        ROUND(sum(DAILY_WATER)/sum(OPEN_TIME), 2) end rznl,
        case when sum(PLAN_DAILY_WATER) = '0' then 0 when sum(PLAN_DAILY_WATER) = null then 0 else
        ROUND((sum(DAILY_WATER)/sum(PLAN_DAILY_WATER)), 2) end cz,
        case
        when sum(OPEN_TIME) = '0' then 0 when sum(OPEN_TIME) = null then 0
        when sum(PLAN_DAILY_WATER) = '0' then 0 when sum(PLAN_DAILY_WATER) = null then 0
        else ROUND((sum(DAILY_WATER)/sum(OPEN_TIME))/sum(PLAN_DAILY_WATER), 2) end fxpj
        from WELL_SECTION_DAILY
        WHERE RECORD_DATE >= #{startdate,jdbcType=VARCHAR} AND RECORD_DATE &lt;= #{enddate,jdbcType=VARCHAR}
        group by SECTION,WELLNAME) b on a.SECTION = b.SECTION and a.WELLNAME = b.WELLNAME
        where
            a.DWDM in (select unitcode
            from SYS_UNITINFO
            start with UNITCODE = #{unitcode,jdbcType=VARCHAR}
            connect by prior UNITCODE = P_UNITCODE)
        <if test="blockcode != null and blockcode != ''">
            and a.QKDM = #{blockcode,jdbcType=VARCHAR}
        </if>
        <if test="oiltype != null and oiltype != ''">
            and a.YCLX = #{oiltype,jdbcType=VARCHAR}
        </if>
        <if test="crafttype != null and crafttype != ''">
            and a.GYLX = #{crafttype,jdbcType=VARCHAR}
        </if>
        <if test="wellname != null and wellname != ''">
            and a.WELLNAME = #{wellname,jdbcType=VARCHAR}
        </if>
        <if test="evaluation != null and evaluation != ''">

        </if>
        order by a.WELLNAME
    </select>

</mapper>