<?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.casic.missiles.modular.system.dao.MetaModuleMapper">

    <sql id="keywords_sql" databaseId="oracle">
                CONCAT('%',#{keywords},'%')
    </sql>
    <sql id="keywords_sql" databaseId="mysql">
        CONCAT('%',#{keywords},'%')
    </sql>
    <sql id="keywords_sql" databaseId="pgsql">
        CONCAT('%',#{keywords},'%')
    </sql>

    <sql id="char_toDate" databaseId="mysql">
       ${paramStr}
    </sql>

    <sql id="char_toDate" databaseId="pgsql">
      TO_TIMESTAMP(${paramStr},'yyyy-MM-dd hh24:mi:ss')::timestamp without time zone
    </sql>

    <sql id="char_toDate" databaseId="oracle">
        TO_DATE(${paramStr},'yyyy-mm-dd hh24:mi')
    </sql>

    <sql id="date_toChar" databaseId="mysql">
       DATE_FORMAT(${paramTime},'%Y-%m-%d %H:%i:%s')
    </sql>

    <sql id="date_toChar" databaseId="pgsql">
       TO_CHAR(${paramTime},'yyyy-MM-dd HH24:mi:ss')
    </sql>

    <sql id="date_toChar" databaseId="oracle">
       TO_CHAR(${paramTime},'yyyy-MM-dd HH24:mi:ss')
    </sql>

    <sql id="data_diff" databaseId="mysql">
         and datediff(now(),dg.UPTIME) <![CDATA[< ]]>  ${paramDay}
    </sql>

    <sql id="data_diff" databaseId="pgsql">
        AND date_part('day',now()-dg.UPTIME)   <![CDATA[< ]]>  ${paramDay}
    </sql>

    <sql id="data_diff" databaseId="oracle">
         and now()-dg.UPTIME <![CDATA[< ]]>  ${paramDay}
    </sql>

    <select id="getDeviceDataList" resultType="map">
        SELECT bd.devcode AS "devcode",
        <include refid="date_toChar">
            <property name="paramTime" value="tb.LOGTIME"/>
        </include>
        AS "logtime",
        ${fields},
        bd.device_Type as "deviceType",
        deptid AS "deptid",
        bwi.well_code as "wellCode",
        type_name as "deviceTypeName"
        FROM ${tableName} tb
        JOIN bus_device bd ON bd.DEVCODE=tb.DEVCODE
        JOIN bus_well_info bwi ON bwi.WELL_CODE = tb.WELL_CODE
        JOIN base_device_type bdt ON bd.DEVICE_TYPE = bdt.ID
        where bd.valid='1' AND bwi.valid='1'
        <if test="deptid != null and deptid != -1 ">
            and deptid = #{deptid}
        </if>
        <if test="beginTime != null and beginTime != '' ">
            and tb.logtime <![CDATA[ >= ]]>
            <include refid="char_toDate">
                <property name="paramStr" value="#{beginTime}"/>
            </include>
        </if>
        <if test="endTime != null and endTime != ''">
            and tb.logtime <![CDATA[ <= ]]>
            <include refid="char_toDate">
                <property name="paramStr" value="#{endTime}"/>
            </include>
        </if>
        <if test="keywords != null and keywords !=''">
            and (bd.devcode like
            <include refid="keywords_sql"/>
            OR bwi.well_code LIKE
            <include refid="keywords_sql"/>
            )
        </if>
        ORDER BY tb.logtime DESC
    </select>

</mapper>
