<?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.scada.mapper.TfScadaReportListMapper">

    <resultMap type="com.tofly.scada.entity.TfScadaReportList" id="TfScadaReportListMap">
        <result property="id" column="ID" jdbcType="INTEGER"/>
        <result property="code" column="CODE" jdbcType="VARCHAR"/>
        <result property="value" column="VALUE" jdbcType="VARCHAR"/>
        <result property="scadaTime" column="SCADA_TIME" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="UPDATE_TIME" jdbcType="TIMESTAMP"/>
        <result property="reportLevel" column="REPORT_LEVEL" jdbcType="VARCHAR"/>
        <result property="reportUpper" column="REPORT_UPPER" jdbcType="VARCHAR"/>
        <result property="reportLower" column="REPORT_LOWER" jdbcType="VARCHAR"/>
        <result property="status" column="STATUS" jdbcType="VARCHAR"/>
        <result property="reportContent" column="REPORT_CONTENT" />
        <result property="dealDate" column="DEAL_DATE" />
        <result property="dealUserId" column="DEAL_USER_ID" />
        <result property="dealUserName" column="DEAL_USER_NAME" />
        <result property="dealRemark" column="DEAL_REMARK" />
     <association property="reportUnit" column="code" select="getUnit" />

    </resultMap>

    <select id="getUnit" resultType="string">
        select unit  from TF_INDEX_ALLOCATION
        where variable_code=#{code} and rownum=1
    </select>
    <select id="getReportLevel" resultType="java.util.Map">
       select report_level,count(1) as total from TF_SCADA_REPORT_LIST t1
        where 1=1
        <if test="query.start != null and query.end != null">
            and t1.scada_time  between #{query.start} and #{query.end}
        </if>
          and code in(
        select ia.VARIABLE_CODE FROM TF_DEVICE_BUSINESS d
        inner JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
        inner JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
        inner JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
        union all
        SELECT  ia.VARIABLE_CODE
        FROM TF_INDEX_ALLOCATION ia
        INNER JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON ia.DEVICE_ID = dm.ID
        INNER JOIN TF_WATER_INFO w ON dm.WATER_ID = w.ID

        )

        <if test="query.codes != null and query.codes != ''">
           and code in
            <foreach item="nodeCodestr" collection="query.codes.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>
        </if>
        group by report_level

    </select>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into TF_SCADA_REPORT_LIST(CODE, VALUE, SCADA_TIME, UPDATE_TIME, REPORT_LEVEL, REPORT_UPPER,
        REPORT_LOWER, STATUS, DISPLAY_NAME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.code}, #{entity.value}, #{entity.scadaTime}, #{entity.updateTime}, #{entity.reportLevel},
            #{entity.reportUpper}, #{entity.reportLower}, #{entity.status}, #{entity.displayName})
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into TF_SCADA_REPORT_LIST(CODE, VALUE, SCADA_TIME, UPDATE_TIME, REPORT_LEVEL, REPORT_UPPER,
        REPORT_LOWER, STATUS, DISPLAY_NAME)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.code}, #{entity.value}, #{entity.scadaTime}, #{entity.updateTime}, #{entity.reportLevel},
            #{entity.reportUpper}, #{entity.reportLower}, #{entity.status}, #{entity.displayName})
        </foreach>
        on duplicate key update
        CODE = values(CODE) , VALUE = values(VALUE) , SCADA_TIME = values(SCADA_TIME) , UPDATE_TIME =
        values(UPDATE_TIME) , REPORT_LEVEL = values(REPORT_LEVEL) , REPORT_UPPER = values(REPORT_UPPER) , REPORT_LOWER =
        values(REPORT_LOWER) , STATUS = values(STATUS) , DISPLAY_NAME = values(DISPLAY_NAME)
    </insert>

    <select id="pageList" resultMap="TfScadaReportListMap">
        select * from TF_SCADA_REPORT_LIST t1
        where 1=1
        <if test="dto.codes!=null and dto.codes.trim()!=''">
            and t1.code in
            <foreach item="nodeCodestr" collection="dto.codes.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>

        </if>
        <if test="dto.code != null and dto.code.trim()!=''">
            and t1.code=#{dto.code}
        </if>
        <if test="dto.dealStatus!=null and dto.dealStatus.trim()!=''">
            and t1.status in
            <foreach item="nodeCodestr" collection="dto.dealStatus.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>

        </if>
        <if test="dto.status!=null and dto.status.trim()!=''">
            and t1.status in
            <foreach item="nodeCodestr" collection="dto.status.split(',')" open="(" separator="," close=")">
                #{nodeCodestr}
            </foreach>

        </if>
        <if test="dto.reportLevel != null and dto.reportLevel.trim()!=''">
            and t1.report_Level=#{dto.reportLevel}
        </if>
        <if test="dto.displayName != null and dto.displayName.trim()!=''">
           and t1.display_Name like '%'||#{dto.displayName}||'%'
        </if>

        <if test="dto.start != null ">
            and t1.SCADA_TIME >= #{dto.start}
        </if>
        <if test="dto.end != null ">
            <![CDATA[ and t1.SCADA_TIME <= #{dto.end}]]>
        </if>

        <if test="dto.dealUserId != null ">
            and t1.deal_user_id = #{dto.dealUserId}
        </if>

        <if test="dto.dealUserName != null and dto.dealUserName.trim()!=''">
          and t1.deal_user_name like '%'||#{dto.dealUserName}||'%'
        </if>
        <if test="dto.dealRemark != null and dto.dealRemark.trim()!=''">
            and t1.DEAL_REMARK like '%'||#{dto.dealRemark}||'%'
        </if>
        <if test="dto.searchOwn != null and dto.searchOwn==1">
        and  t1.id in(
              select report_Id from TF_SCADA_REPORT_DEAL where deal_User_Id=#{dealUserId}

            )
        </if>
order by t1.SCADA_TIME desc
    </select>

</mapper>

