<?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.CdPcDeviceDao">
  <resultMap id="DetailMap" type="net.northking.cctp.device.dto.pcDevice.CdPcDeviceDetailDto">
    <!-- 主键id -->
    <id column="id" jdbcType="VARCHAR" property="id"/>
    <!-- 设备名称 -->
    <result column="device_name" jdbcType="VARCHAR" property="deviceName"/>
    <!-- 设备域名 -->
    <result column="domain_name" jdbcType="VARCHAR" property="domainName"/>
    <!-- ip地址 -->
    <result column="ip_addr" jdbcType="VARCHAR" property="ipAddr"/>
    <!-- mac地址 -->
    <result column="mac_addr" jdbcType="VARCHAR" property="macAddr"/>
    <!-- 操作系统架构 -->
    <result column="os_arch" jdbcType="VARCHAR" property="osArch"/>
    <!-- 操作系统 -->
    <result column="os_name" jdbcType="VARCHAR" property="osName"/>
    <!-- 内存容量 -->
    <result column="ram_size" jdbcType="VARCHAR" property="ramSize"/>
    <!-- 分辨率-宽 -->
    <result column="width" jdbcType="INTEGER" property="width"/>
    <!-- 分辨率-高 -->
    <result column="height" jdbcType="INTEGER" property="height"/>
    <!-- 执行机位置 -->
    <result column="location" jdbcType="VARCHAR" property="location"/>
    <!-- 远程协议 -->
    <result column="remote_protocol" jdbcType="VARCHAR" property="remoteProtocol"/>
    <!-- 注册时间 -->
    <result column="registed_time" jdbcType="TIMESTAMP" property="registedTime"/>
    <!-- 创建人 -->
    <result column="created_by" jdbcType="VARCHAR" property="createdBy"/>
    <!-- 创建时间 -->
    <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
    <!-- 更新人 -->
    <result column="updated_by" jdbcType="VARCHAR" property="updatedBy"/>
    <!-- 更新时间 -->
    <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
  </resultMap>

  <select id="pagesQuery" resultMap="DetailMap" parameterType="net.northking.cctp.device.dto.pcDevice.CdPcDeviceDto">
    SELECT
    cpd.id,
    cpd.device_name,
    cpd.domain_name,
    cpd.ip_addr,
    cpd.mac_addr,
    cpd.os_arch,
    cpd.os_name,
    cpd.ram_size,
    cpd.width,
    cpd.height,
    cpd.location,
    cpd.remote_protocol,
    cpd.registed_time,
    cpd.created_by,
    cpd.created_time,
    cpd.updated_by,
    cpd.updated_time,
    cds.device_status AS status,
    cds.usage_status AS usageStatus,
    cdp.project_id AS projectId,
    cdt.expired_time AS tenantExpiredTime,
    cdp.expired_time AS projectExpiredTime,
    cdt.tenant_id AS tenantId,
    cdt.recycle_type AS tenantRecycleType,
    cdp.recycle_type AS proRecycleType
    FROM cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    LEFT JOIN cd_device_project cdp ON cpd.id = cdp.id
    LEFT JOIN cd_device_tenant cdt ON cpd.id = cdt.id
    <trim prefix="WHERE" prefixOverrides="AND" >
      cds.device_status != '3'
      <if test="deviceName != null">
        AND LOCATE(#{deviceName,jdbcType=VARCHAR},cpd.device_name) > 0
      </if>
      <if test="domainName != null">
        AND LOCATE(#{domainName,jdbcType=VARCHAR},cpd.domain_name) > 0
      </if>
      <if test="osArch != null">
        AND LOCATE(#{osArch,jdbcType=VARCHAR}, cpd.os_arch) > 0
      </if>
      <if test="osName != null and osName.size > 0">
        AND cpd.os_name in
        <foreach collection="osName" item="item" open="(" separator="," close=")">
          #{item,jdbcType=VARCHAR}
        </foreach>
      </if>
      <if test="status != null">
        AND cds.device_status = #{status,jdbcType=VARCHAR}
      </if>
      <if test="usageStatus != null">
        AND cds.usage_status = #{usageStatus,jdbcType=VARCHAR}
      </if>
      <if test="projectId != null">
        AND cdp.project_id = #{projectId,jdbcType=VARCHAR}
      </if>
      <if test="tenantId != null">
        AND cdt.tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="projectExpiredTimeStart != null">
        AND cdp.expired_time >= #{projectExpiredTimeStart,jdbcType=TIMESTAMP}
      </if>
      <if test="projectExpiredTimeEnd != null">
        AND cdp.expired_time &lt;= #{projectExpiredTimeEnd,jdbcType=TIMESTAMP}
      </if>
      <if test="tenantExpiredTimeStart != null">
        AND cdt.expired_time >= #{tenantExpiredTimeStart,jdbcType=TIMESTAMP}
      </if>
      <if test="tenantExpiredTimeEnd != null">
        AND cdt.expired_time &lt;= #{tenantExpiredTimeEnd,jdbcType=TIMESTAMP}
      </if>
      <if test="proNull != null">
        AND cdp.project_id is null
      </if>
      <if test="tenantNull != null">
        AND cdt.tenant_id is null
      </if>
      <if test="proNotNull != null">
        AND cdp.project_id is not null
      </if>
      <if test="tenantNotNull != null">
        AND cdt.tenant_id is not null
      </if>
    </trim>
    order by cds.device_status,cpd.created_time desc
  </select>
  <!-- 请在本文件内添加自定义SQL语句 -->


  <select id="queryScriptDevice" resultType="net.northking.cctp.device.dto.pcDevice.ScriptPcResultDto" parameterType="net.northking.cctp.device.dto.pcDevice.CdPcDeviceScriptDto">
    SELECT
    cpd.id AS id,
    cpd.device_name AS productName,
    cpd.ip_addr AS ipAddr,
    cpd.mac_addr AS macAddr,
    cpd.os_arch AS osArch,
    cpd.os_name AS platform,
    cpd.ram_size AS ramSize,
    cds.usage_status AS usageStatus,
    cdp.project_id AS projectId,
    cdt.tenant_id AS tenantId
    FROM cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    LEFT JOIN cd_device_project cdp ON cpd.id = cdp.id
    LEFT JOIN cd_device_tenant cdt ON cpd.id = cdt.id
    WHERE cds.device_status = '1'
    <if test="productName != null">
      AND LOCATE(#{productName,jdbcType=VARCHAR},cpd.device_name) > 0
    </if>
    <if test="platform != null and platform.size > 0">
      AND cpd.os_name in
      <foreach collection="platform" item="item" open="(" separator="," close=")">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="usageStatus != null and usageStatus.size > 0">
      AND cds.usage_status in
      <foreach collection="usageStatus" item="status" open="(" separator="," close=")">
        #{status,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="tenantId != null">
      AND cdt.tenant_id = #{tenantId,jdbcType=VARCHAR}
    </if>
    <if test="proCheck != null">
      AND cdp.project_id = #{projectId,jdbcType=VARCHAR}
    </if>
    <if test="proNull != null">
      AND cdp.project_id is null
    </if>
    order by cds.device_status,cpd.created_time desc
  </select>


  <select id="queryAllPcDevice" resultType="net.northking.cctp.device.dto.pcDevice.ScriptPcResultDto" parameterType="net.northking.cctp.device.dto.pcDevice.CdPcDeviceScriptDto">
    SELECT
    cpd.id AS id,
    cpd.device_name AS productName,
    cpd.ip_addr AS ipAddr,
    cpd.mac_addr AS macAddr,
    cpd.os_arch AS osArch,
    cpd.os_name AS platform,
    cpd.ram_size AS ramSize,
    cds.usage_status AS usageStatus,
    cdp.project_id AS projectId,
    cdt.tenant_id AS tenantId,
    token.user_by AS userBy,
    token.user_id AS userId
    FROM cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    LEFT JOIN cd_device_project cdp ON cpd.id = cdp.id
    LEFT JOIN cd_device_tenant cdt ON cpd.id = cdt.id
    LEFT JOIN cd_device_token token ON token.id = cpd.id
    WHERE cds.device_status = '1'
    and cds.usage_status != '3'
    and (cdt.tenant_id = #{tenantId,jdbcType=VARCHAR} or cdt.tenant_id is null)
    and (cdp.project_id = #{projectId,jdbcType=VARCHAR} or cdp.project_id is null)
    <if test="productName != null">
      AND LOCATE(#{productName,jdbcType=VARCHAR},cpd.device_name) > 0
    </if>
    <if test="osNames != null and osNames.size > 0">
      AND cpd.os_name in
      <foreach collection="osNames" item="item" open="(" separator="," close=")">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="usageStatus != null and usageStatus.size > 0">
      AND cds.usage_status in
      <foreach collection="usageStatus" item="status" open="(" separator="," close=")">
        #{status,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="deviceList != null and deviceList.size > 0">
      AND cpd.id in
      <foreach collection="deviceList" item="deviceId" open="(" separator="," close=")">
        #{deviceId,jdbcType=VARCHAR}
      </foreach>
    </if>
  </select>

  <select id="queryPublicPcDevice" resultType="net.northking.cctp.device.dto.pcDevice.ScriptPcResultDto" parameterType="net.northking.cctp.device.dto.pcDevice.CdPcDeviceScriptDto">
    SELECT
    cpd.id AS id,
    cpd.device_name AS productName,
    cpd.ip_addr AS ipAddr,
    cpd.mac_addr AS macAddr,
    cpd.os_arch AS osArch,
    cpd.os_name AS platform,
    cpd.ram_size AS ramSize,
    cds.usage_status AS usageStatus,
    cdp.project_id AS projectId,
    cdt.tenant_id AS tenantId,
    token.user_by AS userBy,
    token.user_id AS userId
    FROM cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    LEFT JOIN cd_device_project cdp ON cpd.id = cdp.id
    LEFT JOIN cd_device_tenant cdt ON cpd.id = cdt.id
    LEFT JOIN cd_device_token token ON token.id = cpd.id
    WHERE cds.device_status = '1'
    and  cdp.project_id is null
    and (cdt.tenant_id = #{tenantId,jdbcType=VARCHAR} or cdt.tenant_id is null)
    and  cds.usage_status != '3'
    <if test="productName != null">
      AND LOCATE(#{productName,jdbcType=VARCHAR},cpd.device_name) > 0
    </if>
    <if test="osNames != null and osNames.size > 0">
      AND cpd.os_name in
      <foreach collection="osNames" item="item" open="(" separator="," close=")">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="usageStatus != null and usageStatus.size > 0">
      AND cds.usage_status in
      <foreach collection="usageStatus" item="status" open="(" separator="," close=")">
        #{status,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="deviceList != null and deviceList.size > 0">
      AND cpd.id in
      <foreach collection="deviceList" item="deviceId" open="(" separator="," close=")">
        #{deviceId,jdbcType=VARCHAR}
      </foreach>
    </if>
  </select>

  <select id="queryPrivatePcDevice" resultType="net.northking.cctp.device.dto.pcDevice.ScriptPcResultDto" parameterType="net.northking.cctp.device.dto.pcDevice.CdPcDeviceScriptDto">
    SELECT
    cpd.id AS id,
    cpd.device_name AS productName,
    cpd.ip_addr AS ipAddr,
    cpd.mac_addr AS macAddr,
    cpd.os_arch AS osArch,
    cpd.os_name AS platform,
    cpd.ram_size AS ramSize,
    cds.usage_status AS usageStatus,
    cdp.project_id AS projectId,
    cdt.tenant_id AS tenantId,
    token.user_by AS userBy,
    token.user_id AS userId
    FROM cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    LEFT JOIN cd_device_project cdp ON cpd.id = cdp.id
    LEFT JOIN cd_device_tenant cdt ON cpd.id = cdt.id
    LEFT JOIN cd_device_token token ON token.id = cpd.id
    WHERE cds.device_status = '1'
    and cds.usage_status != '3'
    and  cdp.project_id = #{projectId,jdbcType=VARCHAR}
    <if test="productName != null">
      AND LOCATE(#{productName,jdbcType=VARCHAR},cpd.device_name) > 0
    </if>
    <if test="osNames != null and osNames.size > 0">
      AND cpd.os_name in
      <foreach collection="osNames" item="item" open="(" separator="," close=")">
        #{item,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="usageStatus != null and usageStatus.size > 0">
      AND cds.usage_status in
      <foreach collection="usageStatus" item="status" open="(" separator="," close=")">
        #{status,jdbcType=VARCHAR}
      </foreach>
    </if>
    <if test="deviceList != null and deviceList.size > 0">
      AND cpd.id in
      <foreach collection="deviceList" item="deviceId" open="(" separator="," close=")">
        #{deviceId,jdbcType=VARCHAR}
      </foreach>
    </if>
  </select>

  <select id="queryDevice" resultMap="BaseResultMap">
    select
    cpd.*
    from
    cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    WHERE cds.device_status != '3'
  </select>

  <select id="queryDeviceIpAddr" resultType="integer">
    select count(cpd.id)
    from
    cd_pc_device cpd
    LEFT JOIN cd_device_status cds ON cpd.id = cds.id
    WHERE cds.device_status != '3'
    and cpd.ip_addr = #{ipAddr,jdbcType=VARCHAR}
  </select>

</mapper>