<?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.sckw.slope.detection.dao.mysql.KwsDeviceMapper">
  <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.mysql.KwsDevice">
    <!--@mbg.generated-->
    <!--@Table kws_device-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="attribute" jdbcType="INTEGER" property="attribute" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="alias" jdbcType="VARCHAR" property="alias" />
    <result column="sn_code" jdbcType="VARCHAR" property="snCode" />
    <result column="sn_code_full_name" jdbcType="VARCHAR" property="snCodeFullName" />
    <result column="model_id" jdbcType="BIGINT" property="modelId" />
    <result column="mountain_id" jdbcType="VARCHAR" property="mountainId" />
    <result column="valid_time" jdbcType="TIMESTAMP" property="validTime" />
    <result column="install_time" jdbcType="TIMESTAMP" property="installTime" />
    <result column="secret_key" jdbcType="VARCHAR" property="secretKey" />
    <result column="relevance_level" jdbcType="VARCHAR" property="relevanceLevel" />
    <result column="inter_face" jdbcType="VARCHAR" property="interFace" />
    <result column="related" jdbcType="VARCHAR" property="related" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="BIGINT" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="del_flag" jdbcType="TINYINT" property="delFlag" />
    <result column="online" jdbcType="TINYINT" property="online" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, `attribute`, `name`, `alias`, sn_code, model_id, valid_time, secret_key, inter_face,relevance_level,sn_code_full_name,
    remark, `status`, create_by, create_time, update_by, update_time, del_flag,mountain_id,related,check_time,online,install_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
      <!--@mbg.generated-->
      select
      <include refid="Base_Column_List"/>
      from kws_device
      where del_flag = 0
        and id = #{id,jdbcType=BIGINT}
  </select>

    <select id="selectProjectByDeviceId" parameterType="java.lang.Long" resultMap="BaseResultMap">
        <!--@mbg.generated-->
        select * from kws_project as p inner join kws_project_device as d on p.id=d.project_id WHERE
        p.del_flag=0 and d.del_flag=0 and d.device_id=#{deviceId,jdbcType=BIGINT} order by d.create_time desc limit 1
    </select>

  <select id="selectByDeviceParam" resultType="com.sckw.slope.detection.model.vo.DeviceVo">
      select DISTINCT d.id,
             d.name,
             manufacturer,
             m.name   as m_name,
             m.device_type,
             d.remark,
             d.update_by,
             d.update_time,
             p.`name` as project_name,
             d.status,
            m.`name` as mName,
            d.sn_code
      from kws_device as d
      left join kws_device_model as m on d.model_id = m.id
               left join kws_project_device as pd on d.id = pd.device_id
               left join kws_project as p on pd.project_id = p.id
      WHERE d.del_flag = 0


      <if test="deviceQuery.mountainId != null and deviceQuery.mountainId != ''">
          and d.mountain_id = #{deviceQuery.mountainId,jdbcType=BIGINT}
      </if>
          <if test="deviceQuery.deviceName != null and deviceQuery.deviceName != ''">
              and d.name like concat('%', #{deviceQuery.deviceName}
                   , '%')
          </if>
      <if test="deviceQuery.projectName != null and deviceQuery.projectName != ''">
          and p.name like concat('%', #{deviceQuery.projectName}
          , '%')
      </if>
          <if test="deviceQuery.id != null and deviceQuery.id != ''">
              and id = #{deviceQuery.id,jdbcType=BIGINT}
          </if>
          <if test="deviceQuery.deviceType != null and deviceQuery.deviceType != ''">
              and m.device_type = #{deviceQuery.deviceType,jdbcType=BIGINT}
          </if>
          <if test="deviceQuery.status != null and deviceQuery.status != ''">
              and d.status = #{deviceQuery.status,jdbcType=TINYINT}
          </if>
          <if test="deviceQuery.projectId != null and deviceQuery.projectId != ''">
              and  p.id= #{deviceQuery.projectId,jdbcType=TINYINT}
          </if>
          <if test="deviceQuery.attribute != null and deviceQuery.attribute != ''">
              and attribute = #{deviceQuery.attribute,jdbcType=TINYINT}
          </if>
      order by d.create_time desc
  </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete
        from kws_device
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <insert id="insertSelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDevice">
        <!--@mbg.generated-->
        insert into kws_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="attribute != null">
                `attribute`,
            </if>
            <if test="name != null">
                `name`,
            </if>
            <if test="alias != null">
                `alias`,
            </if>
            <if test="snCode != null">
                sn_code,
            </if>
            <if test="modelId != null">
                model_id,
            </if>
            <if test="validTime != null">
                valid_time,
            </if>
            <if test="secretKey != null">
                secret_key,
            </if>
            <if test="interFace != null">
                interface,
            </if>
            <if test="remark != null">
                remark,
            </if>
            <if test="status != null">
                `status`,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="delFlag != null">
                del_flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=BIGINT},
            </if>
            <if test="attribute != null">
                #{attribute,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="alias != null">
                #{alias,jdbcType=VARCHAR},
            </if>
            <if test="snCode != null">
                #{snCode,jdbcType=VARCHAR},
            </if>
            <if test="modelId != null">
                #{modelId,jdbcType=BIGINT},
            </if>
            <if test="validTime != null">
                #{validTime,jdbcType=TIMESTAMP},
            </if>
            <if test="secretKey != null">
                #{secretKey,jdbcType=VARCHAR},
            </if>
            <if test="interFace != null">
                #{interFace,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status,jdbcType=TINYINT},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=BIGINT},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="delFlag != null">
                #{delFlag,jdbcType=TINYINT},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDevice">
        <!--@mbg.generated-->
        update kws_device
        <set>
            <if test="attribute != null">
                `attribute` = #{attribute,jdbcType=INTEGER},
            </if>
            <if test="name != null">
                `name` = #{name,jdbcType=VARCHAR},
            </if>
            <if test="alias != null">
                `alias` = #{alias,jdbcType=VARCHAR},
            </if>
            <if test="snCode != null">
                sn_code = #{snCode,jdbcType=VARCHAR},
            </if>
            <if test="modelId != null">
                model_id = #{modelId,jdbcType=BIGINT},
            </if>
            <if test="validTime != null">
                valid_time = #{validTime,jdbcType=TIMESTAMP},
            </if>
            <if test="secretKey != null">
                secret_key = #{secretKey,jdbcType=VARCHAR},
            </if>
            <if test="interFace != null">
                inter_face = #{interFace,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                `status` = #{status,jdbcType=TINYINT},
            </if>
            <if test="createBy != null">
                create_by = #{createBy,jdbcType=BIGINT},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=BIGINT},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag,jdbcType=TINYINT},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDevice">
        <!--@mbg.generated-->
        update kws_device
        set `attribute` = #{attribute,jdbcType=INTEGER},
            `name`      = #{name,jdbcType=VARCHAR},
            `alias`     = #{alias,jdbcType=VARCHAR},
            sn_code     = #{snCode,jdbcType=VARCHAR},
            model_id    = #{modelId,jdbcType=BIGINT},
            valid_time  = #{validTime,jdbcType=TIMESTAMP},
            secret_key  = #{secretKey,jdbcType=VARCHAR},
            inter_face  = #{interFace,jdbcType=VARCHAR},
            remark      = #{remark,jdbcType=VARCHAR},
            `status`    = #{status,jdbcType=TINYINT},
            create_by   = #{createBy,jdbcType=BIGINT},
            create_time = #{createTime,jdbcType=TIMESTAMP},
            update_by   = #{updateBy,jdbcType=BIGINT},
            update_time = #{updateTime,jdbcType=TIMESTAMP},
            del_flag    = #{delFlag,jdbcType=TINYINT}
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="selectDeviceAllNotDeviceRelation" resultType="com.sckw.slope.detection.model.dto.DeviceDataDTO">
        SELECT c.id     AS deviceId,
               c.alias  as deviceAlias,
               c.`name` AS deviceName
        FROM kws_project a
                 LEFT JOIN kws_device c ON a.mountain_id = c.mountain_id
                 LEFT JOIN kws_device_model d ON c.model_id = d.id
        WHERE a.del_flag = 0
          AND c.del_flag = 0
          AND d.del_flag = 0
          AND c.relevance_level in ('1','0')
          AND c.id NOT in (SELECT DISTINCT a.relate_device_id
                           FROM kws_device_relation a
                                    LEFT JOIN kws_project b ON a.mountain_id = b.mountain_id where a.del_flag = 0)
          AND c.id NOT in (SELECT DISTINCT a.device_id
                           FROM kws_device_relation a
                                    LEFT JOIN kws_project b ON a.mountain_id = b.mountain_id where a.del_flag = 0)
        <if test="projectId != null and projectId != ''">
            AND a.id = #{projectId}
        </if>
    </select>

    <select id="selectDeviceAllAndProjectData"
            resultType="com.sckw.slope.detection.model.vo.AffiliationDeviceVO">
        SELECT a.id        as deviceId,
               a.`name`    as name,
               a.attribute as attribute,
               a.alias     as alias,
               a.logic_alt as logicAlt,
               a.logic_lat as logicLat,
               a.logic_lng as logicLng,
               a.sn_code
        FROM kws_device a
                 LEFT JOIN kws_project_device b ON a.id = b.device_id
                 LEFT JOIN kws_project c ON b.project_id = c.id
        WHERE
         a.del_flag=0
        and b.del_flag=0
        and c.del_flag=0
        and c.id = #{projectId}
    </select>

    <select id="selectAllDeviceByProject"
            resultType="com.sckw.slope.detection.model.dto.DeviceDataDTO">
        SELECT a.id     AS deviceId,
               a.alias  as deviceAlias,
               a.`name` AS deviceName
        FROM kws_device a
                 LEFT JOIN kws_project_device b ON a.id = b.device_id
                 LEFT JOIN kws_project c ON b.project_id = c.id
        WHERE
            a.del_flag=0
          and b.del_flag=0
          and c.del_flag=0
          and c.id = #{projectId}
    </select>

    <select id="selectDeviceAllByProjectAndMountainId" resultType="com.sckw.slope.detection.model.dto.ThresholdSelectDTO">
        SELECT b.id     AS deviceId,
               b.alias  AS deviceAlias,
               b.`name` AS deviceName
        FROM kws_project_device a
                 LEFT JOIN kws_device b ON a.device_id = b.id
        WHERE a.del_flag = 0
          AND b.del_flag = 0
          AND a.mountain_id = b.mountain_id
        <if test="projectId != null and projectId != ''">
            and a.project_id = #{projectId}
        </if>
        <if test="mountainId != null and mountainId != ''">
            and a.mountain_id = #{mountainId}
        </if>
        <if test="status != null and status.size() > 0">
            and b.status in
            <foreach collection="status" separator="," open="(" close=")" item="status">
                #{status}
            </foreach>
        </if>
    </select>

  <select id="selectCountByProject" resultType="int">
      SELECT count(distinct c.id)
      FROM kws_project a
               LEFT JOIN kws_project_device b
                         ON a.id = b.project_id
               LEFT JOIN kws_device c ON b.device_id = c.id
      WHERE a.del_flag = 0
        AND b.del_flag = 0
        AND c.del_flag = 0
        and a.mountain_id = b.mountain_id
        and b.mountain_id = c.mountain_id
      <if test="projectId != null">
          and a.id = #{projectId}
      </if>
      <if test="companyId != null">
          and a.company_id = #{companyId}
      </if>
  </select>

  <select id="selectDeviceOnlineRateCountByProject" resultType="int">
      SELECT count(distinct c.id)
      FROM kws_project a
      LEFT JOIN kws_project_device b
      ON a.id = b.project_id
      LEFT JOIN kws_device c ON b.device_id = c.id
      WHERE a.del_flag = 0
      AND b.del_flag = 0
      AND c.del_flag = 0
      and a.mountain_id = b.mountain_id
      and b.mountain_id = c.mountain_id
      and c.online = 0
      <if test="projectId != null">
          and a.id=#{projectId}
      </if>
      <if test="companyId != null">
          and a.company_id=#{companyId}
      </if>
    </select>

  <select id="selectListByModelType" resultType="java.util.Map">
      SELECT b.id          as deviceId,
             b.name        as deviceName,
             c.id          as deviceModelId,
             c.`name`      as deviceModelName,
             c.device_type as deviceType
      FROM kws_device b
               LEFT JOIN kws_device_model c ON b.model_id = c.id
      WHERE b.del_flag = 0
        and c.del_flag = 0
      <if test="modelType != null and modelType != ''">
          and c.device_type = #{modelType}
      </if>

      <if test="companyId != null and companyId != ''">
          and b.mountain_id = #{companyId}
      </if>
  </select>

  <select id="getDataByDeviceIdAndProjectId" resultType="com.sckw.slope.detection.model.vo.excel.KwsAlarmExportVO">

    </select>

  <select id="selectAlarmAndDeviceAll" resultType="com.sckw.slope.detection.model.dto.ThresholdSelectDTO">
      SELECT b.id     AS deviceId,
             b.alias  AS deviceAlias,
             b.`name` AS deviceName
          a.id as ,
  </select>
</mapper>