<?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.zioinfo.esn.mapper.HCoreMapper">

  <resultMap id="hcoreMap" type="com.zioinfo.esn.domain.HCoreVo">
    <id property="id"              column="id"/>
    <result property="tenantCode"      column="tenant_code"/>
    <result property="storeId"         column="store_id"/>
    <result property="storeName"       column="store_name"/>
    <result property="deviceType"      column="device_type"/>
    <result property="deviceId"        column="device_id"/>
    <result property="ipAddress"       column="ip_address"/>
    <result property="macAddress"      column="mac_address"/>
    <result property="firmwareVersion" column="firmware_version"/>
    <result property="status"          column="status"/>
    <result property="batteryLevel"    column="battery_level"/>
    <result property="signalStrength"  column="signal_strength"/>
    <result property="lastSeenAt"      column="last_seen_at"/>
    <result property="createdAt"       column="created_at"/>
  </resultMap>

  <select id="findAll" resultMap="hcoreMap">
    SELECT h.id, h.tenant_code, h.store_id, s.store_name, h.device_type, h.device_id,
           h.ip_address, h.mac_address, h.firmware_version, h.status, h.battery_level,
           h.signal_strength, h.last_seen_at, h.created_at
    FROM esn_hcore_device h
    LEFT JOIN esn_store s ON h.store_id = s.id
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND h.tenant_code = #{tenantCode}</if>
      <if test="storeId != null">AND h.store_id = #{storeId}</if>
      <if test="deviceType != null and deviceType != ''">AND h.device_type = #{deviceType}</if>
      <if test="status != null and status != ''">AND h.status = #{status}</if>
    </where>
    ORDER BY h.device_id
  </select>

  <select id="findById" resultMap="hcoreMap">
    SELECT h.id, h.tenant_code, h.store_id, s.store_name, h.device_type, h.device_id,
           h.ip_address, h.mac_address, h.firmware_version, h.status, h.battery_level,
           h.signal_strength, h.last_seen_at, h.created_at
    FROM esn_hcore_device h
    LEFT JOIN esn_store s ON h.store_id = s.id
    WHERE h.id = #{id}
  </select>

  <select id="findByDeviceId" resultMap="hcoreMap">
    SELECT h.id, h.tenant_code, h.store_id, s.store_name, h.device_type, h.device_id,
           h.ip_address, h.mac_address, h.firmware_version, h.status, h.battery_level,
           h.signal_strength, h.last_seen_at, h.created_at
    FROM esn_hcore_device h
    LEFT JOIN esn_store s ON h.store_id = s.id
    WHERE h.device_id = #{deviceId}
  </select>

  <insert id="insert" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO esn_hcore_device (tenant_code, store_id, device_type, device_id, ip_address,
                                   mac_address, firmware_version, status)
    VALUES (#{tenantCode}, #{storeId}, #{deviceType}, #{deviceId}, #{ipAddress},
            #{macAddress}, #{firmwareVersion}, #{status})
  </insert>

  <update id="update">
    UPDATE esn_hcore_device SET
      ip_address       = #{ipAddress},
      mac_address      = #{macAddress},
      firmware_version = #{firmwareVersion},
      status           = #{status},
      battery_level    = #{batteryLevel},
      signal_strength  = #{signalStrength},
      last_seen_at     = NOW()
    WHERE id = #{id}
  </update>

  <update id="updateStatus">
    UPDATE esn_hcore_device SET status = #{status}, last_seen_at = NOW()
    WHERE id = #{id}
  </update>

  <delete id="delete">DELETE FROM esn_hcore_device WHERE id = #{id}</delete>

  <select id="countOnline" resultType="long">
    SELECT COUNT(*) FROM esn_hcore_device WHERE status = 'ONLINE'
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </select>

  <select id="countOffline" resultType="long">
    SELECT COUNT(*) FROM esn_hcore_device WHERE status = 'OFFLINE'
    <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
  </select>

  <select id="countAll" resultType="long">
    SELECT COUNT(*) FROM esn_hcore_device
    <where>
      <if test="tenantCode != null and tenantCode != ''">AND tenant_code = #{tenantCode}</if>
    </where>
  </select>

</mapper>
