<?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="net.northking.cctp.device.db.dao.CdEngineInfoDao">
  <resultMap id="DetailMap" type="net.northking.cctp.device.dto.engineInfo.CdEngineInfoDetailDto">
    <!-- 主键id -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
    <!-- 引擎名称 -->
    <result column="engine_name" jdbcType="VARCHAR" property="engineName"/>
    <!-- pc设备id -->
    <result column="device_id" jdbcType="VARCHAR" property="deviceId"/>
    <!-- ip地址 -->
    <result column="ip_addr" jdbcType="VARCHAR" property="ipAddr"/>
    <!-- 引擎版本 -->
    <result column="running_release" jdbcType="VARCHAR" property="runningRelease"/>
    <!-- 注册时间 -->
    <result column="registed_time" jdbcType="TIMESTAMP" property="registedTime"/>
    <!-- 启用/禁用 -->
    <result column="is_enabled" jdbcType="BIT" property="enabled"/>
    <!-- 引擎状态 -->
    <result column="engine_status" jdbcType="VARCHAR" property="engineStatus"/>
    <!-- 引擎描述 -->
    <result column="engine_desc" jdbcType="VARCHAR" property="engineDesc"/>
  </resultMap>


  <select id="queryPages" resultMap="DetailMap" parameterType="net.northking.cctp.device.dto.engineInfo.CdEngineInfoQueryDto">
    SELECT
    info.id
    ,info.engine_name
    ,info.device_id
    ,pc.device_name
    ,info.ip_addr
    ,info.running_release
    ,info.registed_time
    ,info.is_enabled
    ,info.engine_status
    ,info.engine_desc
    FROM cd_engine_info info
    LEFT JOIN cd_pc_device pc ON info.device_id = pc.id
    <trim prefix="WHERE" prefixOverrides="AND">
      <if test="deviceName != null">
        AND LOCATE(#{deviceName,jdbcType=VARCHAR},pc.device_name) > 0
      </if>
      <if test="engineName != null">
        AND LOCATE(#{engineName,jdbcType=VARCHAR},info.engine_name) > 0
      </if>
      <if test="registedTime != null">
        AND info.registed_time = #{registedTime,jdbcType=TIMESTAMP}
      </if>
      <if test="ipAddr != null">
        AND info.ip_addr = #{ipAddr,jdbcType=VARCHAR}
      </if>
      <if test="runningRelease != null">
        AND info.running_release = #{runningRelease,jdbcType=VARCHAR}
      </if>
      <if test="engineStatus != null">
        AND info.engine_status = #{engineStatus,jdbcType=VARCHAR}
      </if>
    </trim>
    order by info.registed_time desc
  </select>

  <update id="updateVersion" parameterType="net.northking.cctp.device.dto.engineInfo.CdEngineInfoVersionDto">
    update
    <include refid="Table_Name"/>
    <set>
      <if test="version != null">
        running_release = #{version, jdbcType=VARCHAR},
      </if>
    </set>
    where id in
    <foreach collection="ids" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </update>


    <select id="queryEngineInfoNotIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where engine_status = '1'
      <if test="ipAddr!=null">
        and ip_addr != #{ipAddr,jdbcType=VARCHAR}
      </if>
    <if test="ids != null and ids.size()>0">
      and id not in
      <foreach collection="ids" item="idItem" open="(" separator="," close=")">
        #{idItem, jdbcType=VARCHAR}
      </foreach>
    </if>
    order by last_heartbeat_time desc
    limit #{num,jdbcType=INTEGER}
  </select>

  <select id="queryNgInDeviceIds" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where engine_status = '1'
    <if test="pcDeviceIds != null and pcDeviceIds.size()>0">
      and device_id in
      <foreach collection="pcDeviceIds" item="item" open="(" separator="," close=")">
        #{item, jdbcType=VARCHAR}
      </foreach>
    </if>
    order by last_heartbeat_time desc
    limit #{countNeed,jdbcType=INTEGER}
  </select>

  <select id="queryEngineOffLive" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where engine_status = '1'
    and TIMESTAMPDIFF(SECOND,last_heartbeat_time, #{date,jdbcType=TIMESTAMP}) > 300
  </select>

  <select id="queryUsableEngine" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    <include refid="Table_Name"/>
    where is_enabled = 1 and engine_status = '1'
    <if test="deviceIdList != null and deviceIdList.size() > 0">
      and device_id in
      <foreach collection="deviceIdList" item="item" open="(" separator="," close=")">
        #{item, jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="deviceIdList == null or deviceIdList.size() == 0">
      and device_id is null
    </if>
  </select>


  <update id="updateEngineStatus" parameterType="list">
    update
    <include refid="Table_Name"/>
    set engine_status = '2'
    where id in
    <foreach collection="ids" item="idItem" open="(" separator="," close=")">
      #{idItem, jdbcType=VARCHAR}
    </foreach>
  </update>
</mapper>