<?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.KwsDeviceModelPartMapper">
  <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.mysql.KwsDeviceModelPart">
    <!--@mbg.generated-->
    <!--@Table kws_device_model_part-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="device_model_id" jdbcType="BIGINT" property="deviceModelId" />
    <result column="part_name" jdbcType="VARCHAR" property="partName" />
    <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" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, device_model_id, part_name, `status`, create_by, create_time, update_by, update_time, 
    del_flag
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select
    <include refid="Base_Column_List" />
    from kws_device_model_part
    where id = #{id,jdbcType=BIGINT}
  </select>
    <select id="selectByModelId" parameterType="java.lang.Long" resultType="com.sckw.slope.detection.model.vo.DevicePartModelVo">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List" />
        from kws_device_model_part
        where del_flag=0
        <if test="deviceModelId != null">
            and device_model_id = #{deviceModelId,jdbcType=BIGINT}
        </if>
        order by part_name
    </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    <!--@mbg.generated-->
    delete from kws_device_model_part
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insert" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDeviceModelPart">
    <!--@mbg.generated-->
    insert into kws_device_model_part (id, device_model_id, part_name, 
      `status`, create_by, create_time, 
      update_by, update_time, del_flag
      )
    values (#{id,jdbcType=BIGINT}, #{deviceModelId,jdbcType=BIGINT}, #{partName,jdbcType=VARCHAR}, 
      #{status,jdbcType=TINYINT}, #{createBy,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP}, 
      #{updateBy,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP}, #{delFlag,jdbcType=TINYINT}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDeviceModelPart">
    <!--@mbg.generated-->
    insert into kws_device_model_part
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="deviceModelId != null">
        device_model_id,
      </if>
      <if test="partName != null">
        part_name,
      </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="deviceModelId != null">
        #{deviceModelId,jdbcType=BIGINT},
      </if>
      <if test="partName != null">
        #{partName,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.KwsDeviceModelPart">
    <!--@mbg.generated-->
    update kws_device_model_part
    <set>
      <if test="deviceModelId != null">
        device_model_id = #{deviceModelId,jdbcType=BIGINT},
      </if>
      <if test="partName != null">
        part_name = #{partName,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.KwsDeviceModelPart">
    <!--@mbg.generated-->
    update kws_device_model_part
    set device_model_id = #{deviceModelId,jdbcType=BIGINT},
      part_name = #{partName,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="selectElementList" resultType="com.sckw.slope.detection.model.dto.DeviceElementDTO">
      SELECT b.id              as deviceId,
             b.name,
             b.alias,
             d.device_model_id as deviceModelId,
             d.part_name       as element
      FROM kws_project_device a
               LEFT JOIN kws_device b ON a.device_id = b.id
               LEFT JOIN kws_device_model c ON b.model_id = c.id
               LEFT JOIN kws_device_model_part d ON c.id = d.device_model_id
      WHERE a.del_flag = 0
        and b.del_flag = 0
        and c.del_flag = 0
        and d.del_flag = 0
      <if test="status != null and status.size() > 0">
          and b.status in
          <foreach collection="status" separator="," open="(" close=")" item="status">
              #{status}
          </foreach>
      </if>
      <if test="mountainId != null and mountainId != ''">
          and a.mountain_id = #{mountainId}
      </if>
      <if test="projectId != null and projectId != ''">
          and a.project_id = #{projectId}
      </if>
      <if test="deviceId != null and deviceId != ''">
          and b.id = #{deviceId}
      </if>
  </select>

  <select id="selectElementAllList" resultType="com.sckw.slope.detection.model.dto.DeviceElementDTO">
      SELECT a.id              as deviceId,
      b.name,
      b.alias,
      e.phones as phones,
      d.device_model_id as deviceModelId,
      d.part_name       as element,
      e.integration_id       as integrationElement,
      e.update_name       as updateName,
      e.update_time       as updatTime,
      FROM kws_project_device a
      LEFT JOIN kws_device b ON a.device_id = b.id
      LEFT JOIN kws_device_model c ON b.model_id = c.id
      LEFT JOIN kws_device_model_part d ON c.id = d.device_model_id
      LEFT JOIN kws_threshold e ON b.id = e.device_id
      WHERE a.del_flag = 0
      and b.del_flag = 0
      and c.del_flag = 0
      and d.del_flag = 0
      and e.del_flag = 0
      <if test="status != null and status.size() > 0">
          and b.status in
          <foreach collection="status" separator="," open="(" close=")" item="status">
              #{status}
          </foreach>
      </if>
      and a.mountain_id = #{mountainId}
      and a.project_id = #{projectId}
    </select>

</mapper>