<?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.xrtygis.custom.mapper.CosTprStationMapper">

    <select id="isFinance" resultType="long">
        select id
        from ${tableName}
        where MARK = '1'
        and to_char(UP_DATE, 'yyyy-mm') = #{time}
        <if test="preType != null">
            and pre_type = #{preType}
        </if>
    </select>

    <select id="notFinance" resultType="long">
        select id
        from TF_YWPN_COSTPRESTATION_W
        where MARK = '0'
          and to_char(UP_DATE, 'yyyy-mm-dd') = #{time}
          and pre_type = #{preType}
    </select>


    <select id="getRealSum" resultType="map">
        select to_char(UP_DATE, 'yyyy-mm') up_date,
               sum(gsl)                    gsl,
               sum(dl)                     dl,
               sum(dwhd)                   dwhd,
               sum(fdsc)                   fdsc,
               sum(fdyyl)                  fdyyl,
               sum(gscb)                   gscb,
               sum(SJJE)                   SJJE,
               sum(SJDL)                   SJDL,
               sum(SJDH)                   SJDH,
               sum(YGCB)                   YGCB
        from TF_YWPN_COSTPRESTATION_W
        where MARK = #{mark}
          and to_char(up_date, 'yyyy-mm-dd') between to_char(add_months(sysdate, -1), 'yyyy-mm') || '-' ||
                                                     #{start,jdbcType = VARCHAR} and to_char(sysdate, 'yyyy-mm') || '-' || #{end,jdbcType = VARCHAR}
        group by to_char(UP_DATE, 'yyyy-mm')
    </select>

    <select id="getRealSum2" resultType="com.tofly.xrtygis.entity.Costprestation">
        select id,
        PRE_TYPE,
        (select scada.STNM from TF_YWPN_SCADASTATION_B scada where ID = PRE_TYPE) preName,
        UP_DATE,
        round(gsl, 4) gsl,
        round(dl, 4) dl,
        round(dwhd, 4) dwhd,
        round(fdsc, 4) fdsc,
        round(fdyyl, 4) fdyyl,
        round(gscb, 4) gscb,
        round(SJJE, 4) SJJE,
        round(SJDH, 4) SJDH,
        round(SJDL, 4) SJDL,
        round(YGCB, 4) YGCB
        from TF_YWPN_COSTPRESTATION_W
        where MARK = #{mark} and to_char(up_date,'yyyy-mm-dd') between
        to_char(add_months(to_date(#{month,jdbcType=VARCHAR},'yyyy-mm'),-1),'yyyy-mm') || '-' ||
        #{start,jdbcType = VARCHAR} and #{month,jdbcType=VARCHAR}|| '-' || #{end,jdbcType = VARCHAR}
        <if test="preType != null">
            and pre_type = #{preType}
        </if>
    </select>

    <select id="getRealSum3" resultType="com.tofly.xrtygis.entity.Costprestation">
        select round(sum(gsl), 4) gsl,
        round(sum(dl), 4) dl,
        round(sum(dwhd), 4) dwhd,
        round(sum(fdsc), 4) fdsc,
        round(sum(fdyyl), 4) fdyyl,
        round(sum(gscb), 4) gscb,
        round(sum(SJJE), 4) sjje,
        round(sum(SJDL), 4) sjdl,
        round(sum(SJJE) / nullif(sum(GSL), 0) * 1000, 4) ygcb,
        round(sum(SJDL) / nullif(sum(GSL), 0) * 10, 4) sjdh
        from TF_YWPN_COSTPRESTATION_W
        where MARK = #{mark} and to_char(up_date,'yyyy-mm-dd') between
        to_char(add_months(to_date(#{month,jdbcType=VARCHAR},'yyyy-mm'),-1),'yyyy-mm') || '-' ||
        #{start,jdbcType = VARCHAR} and #{month,jdbcType=VARCHAR}|| '-' || #{end,jdbcType = VARCHAR}
        <if test="preType != null">
            and pre_type = #{preType}
        </if>
    </select>

    <select id="getStationInfo" resultType="map">
        select id,
        to_char(up_date, 'yyyy-mm-dd') up_date,
        PRE_TYPE,
        (select scada.STNM from TF_YWPN_SCADASTATION_B scada where ID = PRE_TYPE) preName,
        pre_stcd,
        round(gsl, 4) gsl,
        round(dl, 4) dl,
        round(dwhd, 4) dwhd,
        round(fdsc, 4) fdsc,
        round(fdyyl, 4) fdyyl,
        round(gscb, 4) gscb,
        round(SJJE, 4) SJJE,
        round(SJDL, 4) SJDL,
        round(YGCB, 4) YGCB,
        round(SJDH, 4) SJDH
        from TF_YWPN_COSTPRESTATION_W
        where MARK = '0'
        <if test="preType != null and month != null">
            and PRE_TYPE = #{preType,jdbcType=NUMERIC}
            and to_char(up_date,'yyyy-mm-dd') between
            to_char(add_months(to_date(#{month,jdbcType=VARCHAR},'yyyy-mm'),-1),'yyyy-mm') || '-' ||
            #{start,jdbcType = VARCHAR} and #{month,jdbcType=VARCHAR}|| '-' || #{end,jdbcType = VARCHAR}
        </if>
        <if test="preType != null and month == null">
            and PRE_TYPE = #{preType,jdbcType=NUMERIC}
            and to_char(up_date,'yyyy-mm-dd') between to_char(add_months(sysdate,-1),'yyyy-mm') || '-' ||
            #{start,jdbcType = VARCHAR} and to_char(sysdate,'yyyy-mm') || '-' || #{end,jdbcType = VARCHAR}
        </if>
        <if test="preType == null and month != null">
            and to_char(up_date,'yyyy-mm-dd') between
            to_char(add_months(to_date(#{month,jdbcType=VARCHAR},'yyyy-mm'),-1),'yyyy-mm') || '-' ||
            #{start,jdbcType = VARCHAR} and #{month,jdbcType=VARCHAR}|| '-' || #{end,jdbcType = VARCHAR}
        </if>
        <if test="preType == null and month == null">
            and to_char(up_date,'yyyy-mm-dd') between to_char(add_months(sysdate,-1),'yyyy-mm') || '-' ||
            #{start,jdbcType = VARCHAR} and to_char(sysdate,'yyyy-mm') || '-' || #{end,jdbcType = VARCHAR}
        </if>
        order by up_date,pre_type
    </select>

    <select id="getPreValue" resultType="map">
        select max(GSL) max,
        min(GSL) min,
        round(sum(GSL) / nullif(count(GSL),0),5) avg,
        (select STNM from TF_YWPN_SCADASTATION_B where ID = PRE_TYPE) stnm
        from TF_YWPN_COSTPRESTATION_W
        where MARK = 0
        <if test="pcid != null">
            and PRE_TYPE = #{pcid,jdbcType=NUMERIC}
        </if>
        <if test="month != null">
            and to_char(UP_DATE,'yyyy-mm') = #{month,jdbcType=VARCHAR}
        </if>
        <if test="month == null">
            and to_char(UP_DATE,'yyyy-mm') = to_char(sysdate,'yyyy-mm')
        </if>
        group by to_char(UP_DATE,'yyyy-mm'),PRE_TYPE
    </select>

</mapper>