<?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.water.mapper.ScadaStatusDurationMapper">

    <resultMap type="com.tofly.water.entity.ScadaStatusDuration" id="ScadaStatusDurationMap">
        <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="runTime" column="RUN_TIME" jdbcType="VARCHAR"/>
        <result property="total" column="TOTAL" jdbcType="VARCHAR"/>
    </resultMap>

    <!-- 批量插入 -->
    <insert id="insertBatch" keyProperty="" useGeneratedKeys="true">
        insert into SCADA_STATUS_DURATION(ID, CODE, VALUE, SCADA_TIME, UPDATE_TIME, RUN_TIME, TOTAL)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id}, #{entity.code}, #{entity.value}, #{entity.scadaTime}, #{entity.updateTime},
            #{entity.runTime}, #{entity.total})
        </foreach>
    </insert>
    <!-- 批量插入或按主键更新 -->
    <insert id="insertOrUpdateBatch" keyProperty="" useGeneratedKeys="true">
        insert into SCADA_STATUS_DURATION(ID, CODE, VALUE, SCADA_TIME, UPDATE_TIME, RUN_TIME, TOTAL)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.id}, #{entity.code}, #{entity.value}, #{entity.scadaTime}, #{entity.updateTime},
            #{entity.runTime}, #{entity.total})
        </foreach>
        on duplicate key update
        ID = values(ID) , CODE = values(CODE) , VALUE = values(VALUE) , SCADA_TIME = values(SCADA_TIME) , UPDATE_TIME =
        values(UPDATE_TIME) , RUN_TIME = values(RUN_TIME) , TOTAL = values(TOTAL)
    </insert>

    <select id="statisticsPumpReal" resultType="com.tofly.water.entity.vo.StatisticsPumpRunTimeVo">
        <include refid="statisticsPumpRealSql"></include>
    </select>

    <select id="statisticsPumpRealPage" resultType="com.tofly.water.entity.vo.StatisticsPumpRunTimeVo">
        <include refid="statisticsPumpRealSql"></include>
    </select>

    <sql id="statisticsPumpRealSql">
        SELECT * FROM
        (
        SELECT p.NAME pName,pg.NAME pgName,ia.DISPLAY_NAME iaName,ia.MODEL iaModel,ia.TYPE iaType,dm.NAME dName,
        sm.*,ia.UNIT
        FROM TF_DEVICE_BUSINESS d
        LEFT JOIN TF_BASE_PUMP p ON (p.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP')
        LEFT JOIN TF_BASE_PUMP_GROUP pg ON (pg.ID = d.BUSINESS_ID AND d.OUTSIDE_TABLE = 'TF_BASE_PUMP_GROUP')
        LEFT JOIN TF_DEVICE_ARCHIVE_MANAGE dm ON dm.WATER_ID = d.ID
        LEFT JOIN TF_INDEX_ALLOCATION ia ON ia.DEVICE_ID = dm.ID
        LEFT JOIN SCADA_STATUS_DURATION sm ON sm.code = ia.VARIABLE_CODE
        where 1=1
        <if test="overviewDto!=null">
            <if test="overviewDto.searchStr!=null and overviewDto.searchStr!=''">
                and ia.DISPLAY_NAME like '%' || #{overviewDto.searchStr} || '%'
            </if>
            <choose>
                <when test="overviewDto.pumpId!=null and overviewDto.pumpId > 0">
                    and p.id = #{overviewDto.pumpId}
                </when>
                <otherwise>
                    and d.ID in (SELECT d1.WATER_ID FROM TF_DEVICE_ARCHIVE_MANAGE d1,TF_DEVICE_BUSINESS b1,TF_BASE_PUMP p1
                    WHERE d1.WATER_ID = b1.ID and b1.BUSINESS_ID = p1.ID and b1.OUTSIDE_TABLE = 'TF_BASE_PUMP')
                </otherwise>
            </choose>
            <if test="overviewDto.start!=null and overviewDto.end!=null">
                and sm.SCADA_TIME BETWEEN #{overviewDto.start} AND #{overviewDto.end}
            </if>
            <if test="overviewDto.codeStr!=null and overviewDto.codeStr!=''">
                and ia.VARIABLE_CODE in (#{overviewDto.codeStr})
            </if>
            <if test="overviewDto.statisticsType!=null and overviewDto.statisticsType!=''">
                and sm.STATISTICS_TYPE = #{overviewDto.statisticsType}
            </if>
            <if test="overviewDto.codes!=null and overviewDto.codes.size()!=0">
                and sm.CODE in
                <foreach item="item" collection="overviewDto.codes" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
        </if>
        ) DATA ORDER BY SCADA_TIME ASC
    </sql>

</mapper>

