<?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.goldenwater.yjjt.mapper.YjjtSzDataMapper">

    <resultMap type="com.goldenwater.yjjt.domain.YjjtSzData" id="YjjtSzDataResult">
        <id     property="id"          column="id"           />
        <result property="stcd"        column="stcd"         />
        <result property="stnm"        column="stnm"         />
        <result property="rvnm"        column="rvnm"         />
        <result property="spt"         column="spt"          />
        <result property="addvcd"      column="addvcd"       />
        <result property="wt"          column="wt"           />
        <result property="ph"          column="ph"           />
        <result property="dox"         column="dox"          />
        <result property="codmn"       column="codmn"        />
        <result property="codcr"       column="codcr"        />
        <result property="bod5"        column="bod5"         />
        <result property="nh3n"        column="nh3n"         />
        <result property="tp"          column="tp"           />
        <result property="tn"          column="tn"           />
        <result property="cu"          column="cu"           />
        <result property="zn"          column="zn"           />
        <result property="f"           column="f"            />
        <result property="se"          column="se"           />
        <result property="ars"         column="ars"          />
        <result property="hg"          column="hg"           />
        <result property="cd"          column="cd"           />
        <result property="cr6"         column="cr6"          />
        <result property="pb"          column="pb"           />
        <result property="cn"          column="cn"           />
        <result property="vlph"        column="vlph"         />
        <result property="oil"         column="oil"          />
        <result property="las"         column="las"          />
        <result property="s2"          column="s2"           />
        <result property="fcg"         column="fcg"          />
        <result property="cond"        column="cond"         />
        <result property="no2"         column="no2"          />
        <result property="no3"         column="no3"          />
        <result property="turb"        column="turb"         />
        <result property="ss"          column="ss"           />
        <result property="thrd"        column="thrd"         />
        <result property="watercolor"  column="watercolor"   />
        <result property="smell"       column="smell"        />
        <result property="sb"          column="sb"           />
        <result property="clarity"     column="clarity"      />
        <result property="dtp"         column="dtp"          />
        <result property="chla"        column="chla"         />
        <result property="phyd"        column="phyd"         />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"  column="create_time"  />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"  column="update_time"  />
    </resultMap>

    <sql id="selectSzDataVo">
        select id, stcd, stnm, rvnm, spt, addvcd,
               wt, ph, dox, codmn, codcr, bod5, nh3n, tp, tn,
               cu, zn, f, se, ars, hg, cd, cr6, pb, cn, vlph, oil, las, s2, fcg,
               cond, no2, no3, turb, ss, thrd, watercolor, smell, sb, clarity, dtp, chla, phyd,
               create_by, create_time, update_by, update_time
        from YJJT_SZ_DATA
    </sql>

    <select id="selectYjjtSzDataList" parameterType="com.goldenwater.yjjt.domain.YjjtSzData" resultMap="YjjtSzDataResult">
        <include refid="selectSzDataVo"/>
        <where>
            <if test="stcd != null and stcd != ''">
                AND stcd like '%' || #{stcd} || '%'
            </if>
            <if test="stnm != null and stnm != ''">
                AND stnm like '%' || #{stnm} || '%'
            </if>
            <if test="rvnm != null and rvnm != ''">
                AND rvnm like '%' || #{rvnm} || '%'
            </if>
            <if test="addvcd != null and addvcd != ''">
                AND UPPER(addvcd) = UPPER(#{addvcd})
            </if>
            <if test="params.beginTime != null and params.beginTime != ''">
                AND spt &gt;= TO_DATE(#{params.beginTime}, 'YYYY-MM-DD')
            </if>
            <if test="params.endTime != null and params.endTime != ''">
                AND spt &lt;= TO_DATE(#{params.endTime} || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
            </if>
        </where>
        order by spt desc, rvnm, stnm
    </select>

    <select id="selectYjjtSzDataById" parameterType="Long" resultMap="YjjtSzDataResult">
        <include refid="selectSzDataVo"/>
        where id = #{id}
    </select>

    <select id="selectDistinctRvnmList" resultType="String">
        select distinct rvnm from YJJT_SZ_DATA where rvnm is not null order by rvnm
    </select>

    <select id="countByStcdAndSpt" resultType="int">
        select count(1) from YJJT_SZ_DATA
        where stcd = #{stcd}
        and TO_CHAR(spt, 'YYYY-MM-DD HH24:MI:SS') = #{spt}
        and UPPER(addvcd) = UPPER(#{addvcd})
    </select>

    <select id="selectCountByDateRange" resultType="java.util.Map">
        select TO_CHAR(d.dt, 'YYYY-MM-DD') as dt,
               NVL(s.sz_count, 0) as sz_count
        from (
            select TRUNC(TO_DATE(#{beginDate}, 'YYYY-MM-DD')) + LEVEL - 1 as dt
            from dual
            CONNECT BY LEVEL &lt;= (TO_DATE(#{endDate}, 'YYYY-MM-DD') - TO_DATE(#{beginDate}, 'YYYY-MM-DD') + 1)
        ) d
        left join (
            select TRUNC(spt) as spt_date, count(1) as sz_count
            from YJJT_SZ_DATA
            where spt &gt;= TO_DATE(#{beginDate}, 'YYYY-MM-DD')
            and spt &lt;= TO_DATE(#{endDate} || ' 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
            group by TRUNC(spt)
        ) s on d.dt = s.spt_date
        order by d.dt
    </select>

    <select id="selectTodayWeekMonthCount" resultType="java.util.Map">
        select
            (select count(1) from YJJT_SZ_DATA where TRUNC(spt) = TRUNC(SYSDATE)
             <if test="addvcd != null and addvcd != ''"> and UPPER(addvcd) = UPPER(#{addvcd}) </if>) as today_count,
            (select count(1) from YJJT_SZ_DATA where spt &gt;= TRUNC(SYSDATE, 'IW')
             <if test="addvcd != null and addvcd != ''"> and UPPER(addvcd) = UPPER(#{addvcd}) </if>) as week_count,
            (select count(1) from YJJT_SZ_DATA where spt &gt;= TRUNC(SYSDATE, 'MM')
             <if test="addvcd != null and addvcd != ''"> and UPPER(addvcd) = UPPER(#{addvcd}) </if>) as month_count
        from dual
    </select>

    <insert id="insertYjjtSzData" parameterType="com.goldenwater.yjjt.domain.YjjtSzData" useGeneratedKeys="true" keyProperty="id">
        insert into YJJT_SZ_DATA
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stcd != null and stcd != ''">stcd,</if>
            <if test="stnm != null">stnm,</if>
            <if test="rvnm != null">rvnm,</if>
            <if test="spt != null">spt,</if>
            <if test="addvcd != null">addvcd,</if>
            <if test="wt != null">wt,</if>
            <if test="ph != null">ph,</if>
            <if test="dox != null">dox,</if>
            <if test="codmn != null">codmn,</if>
            <if test="codcr != null">codcr,</if>
            <if test="bod5 != null">bod5,</if>
            <if test="nh3n != null">nh3n,</if>
            <if test="tp != null">tp,</if>
            <if test="tn != null">tn,</if>
            <if test="cu != null">cu,</if>
            <if test="zn != null">zn,</if>
            <if test="f != null">f,</if>
            <if test="se != null">se,</if>
            <if test="ars != null">ars,</if>
            <if test="hg != null">hg,</if>
            <if test="cd != null">cd,</if>
            <if test="cr6 != null">cr6,</if>
            <if test="pb != null">pb,</if>
            <if test="cn != null">cn,</if>
            <if test="vlph != null">vlph,</if>
            <if test="oil != null">oil,</if>
            <if test="las != null">las,</if>
            <if test="s2 != null">s2,</if>
            <if test="fcg != null">fcg,</if>
            <if test="cond != null">cond,</if>
            <if test="no2 != null">no2,</if>
            <if test="no3 != null">no3,</if>
            <if test="turb != null">turb,</if>
            <if test="ss != null">ss,</if>
            <if test="thrd != null">thrd,</if>
            <if test="watercolor != null">watercolor,</if>
            <if test="smell != null">smell,</if>
            <if test="sb != null">sb,</if>
            <if test="clarity != null">clarity,</if>
            <if test="dtp != null">dtp,</if>
            <if test="chla != null">chla,</if>
            <if test="phyd != null">phyd,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stcd != null and stcd != ''">#{stcd},</if>
            <if test="stnm != null">#{stnm},</if>
            <if test="rvnm != null">#{rvnm},</if>
            <if test="spt != null">#{spt},</if>
            <if test="addvcd != null">UPPER(#{addvcd}),</if>
            <if test="wt != null">#{wt},</if>
            <if test="ph != null">#{ph},</if>
            <if test="dox != null">#{dox},</if>
            <if test="codmn != null">#{codmn},</if>
            <if test="codcr != null">#{codcr},</if>
            <if test="bod5 != null">#{bod5},</if>
            <if test="nh3n != null">#{nh3n},</if>
            <if test="tp != null">#{tp},</if>
            <if test="tn != null">#{tn},</if>
            <if test="cu != null">#{cu},</if>
            <if test="zn != null">#{zn},</if>
            <if test="f != null">#{f},</if>
            <if test="se != null">#{se},</if>
            <if test="ars != null">#{ars},</if>
            <if test="hg != null">#{hg},</if>
            <if test="cd != null">#{cd},</if>
            <if test="cr6 != null">#{cr6},</if>
            <if test="pb != null">#{pb},</if>
            <if test="cn != null">#{cn},</if>
            <if test="vlph != null">#{vlph},</if>
            <if test="oil != null">#{oil},</if>
            <if test="las != null">#{las},</if>
            <if test="s2 != null">#{s2},</if>
            <if test="fcg != null">#{fcg},</if>
            <if test="cond != null">#{cond},</if>
            <if test="no2 != null">#{no2},</if>
            <if test="no3 != null">#{no3},</if>
            <if test="turb != null">#{turb},</if>
            <if test="ss != null">#{ss},</if>
            <if test="thrd != null">#{thrd},</if>
            <if test="watercolor != null">#{watercolor},</if>
            <if test="smell != null">#{smell},</if>
            <if test="sb != null">#{sb},</if>
            <if test="clarity != null">#{clarity},</if>
            <if test="dtp != null">#{dtp},</if>
            <if test="chla != null">#{chla},</if>
            <if test="phyd != null">#{phyd},</if>
            <if test="createBy != null">#{createBy},</if>
            SYSDATE
        </trim>
    </insert>

    <update id="updateYjjtSzData" parameterType="com.goldenwater.yjjt.domain.YjjtSzData">
        update YJJT_SZ_DATA
        <set>
            <if test="wt != null">wt = #{wt},</if>
            <if test="ph != null">ph = #{ph},</if>
            <if test="dox != null">dox = #{dox},</if>
            <if test="codmn != null">codmn = #{codmn},</if>
            <if test="codcr != null">codcr = #{codcr},</if>
            <if test="bod5 != null">bod5 = #{bod5},</if>
            <if test="nh3n != null">nh3n = #{nh3n},</if>
            <if test="tp != null">tp = #{tp},</if>
            <if test="tn != null">tn = #{tn},</if>
            <if test="cu != null">cu = #{cu},</if>
            <if test="zn != null">zn = #{zn},</if>
            <if test="f != null">f = #{f},</if>
            <if test="se != null">se = #{se},</if>
            <if test="ars != null">ars = #{ars},</if>
            <if test="hg != null">hg = #{hg},</if>
            <if test="cd != null">cd = #{cd},</if>
            <if test="cr6 != null">cr6 = #{cr6},</if>
            <if test="pb != null">pb = #{pb},</if>
            <if test="cn != null">cn = #{cn},</if>
            <if test="vlph != null">vlph = #{vlph},</if>
            <if test="oil != null">oil = #{oil},</if>
            <if test="las != null">las = #{las},</if>
            <if test="s2 != null">s2 = #{s2},</if>
            <if test="fcg != null">fcg = #{fcg},</if>
            <if test="cond != null">cond = #{cond},</if>
            <if test="no2 != null">no2 = #{no2},</if>
            <if test="no3 != null">no3 = #{no3},</if>
            <if test="turb != null">turb = #{turb},</if>
            <if test="ss != null">ss = #{ss},</if>
            <if test="thrd != null">thrd = #{thrd},</if>
            <if test="watercolor != null">watercolor = #{watercolor},</if>
            <if test="smell != null">smell = #{smell},</if>
            <if test="sb != null">sb = #{sb},</if>
            <if test="clarity != null">clarity = #{clarity},</if>
            <if test="dtp != null">dtp = #{dtp},</if>
            <if test="chla != null">chla = #{chla},</if>
            <if test="phyd != null">phyd = #{phyd},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = SYSDATE
        </set>
        where stcd = #{stcd} and spt = #{spt} and UPPER(addvcd) = UPPER(#{addvcd})
    </update>

    <delete id="deleteYjjtSzDataById" parameterType="Long">
        delete from YJJT_SZ_DATA where id = #{id}
    </delete>

    <delete id="deleteYjjtSzDataByIds" parameterType="Long">
        delete from YJJT_SZ_DATA where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
