<?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.KwsProjectDeviceMapper">
  <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.mysql.KwsProjectDevice">
    <!--@mbg.generated-->
    <!--@Table kws_project_device-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="project_id" jdbcType="BIGINT" property="projectId" />
    <result column="device_id" jdbcType="BIGINT" property="deviceId" />
    <result column="mountain_id" jdbcType="BIGINT" property="mountainId" />
    <result column="status" jdbcType="TINYINT" property="status" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
      <result column="company_id" jdbcType="VARCHAR" property="companyId" />
    <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, project_id, device_id, `status`, create_by, create_time, update_by, update_time, 
    del_flag,company_id,mountain_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from kws_project_device
    where id = #{id,jdbcType=BIGINT}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
    <!--@mbg.generated-->
    delete from kws_project_device
    where id = #{id,jdbcType=BIGINT}
  </delete>
  <insert id="insertSelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsProjectDevice">
    <!--@mbg.generated-->
    insert into kws_project_device
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="projectId != null">
        project_id,
      </if>
      <if test="deviceId != null">
        device_id,
      </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="projectId != null">
        #{projectId,jdbcType=BIGINT},
      </if>
      <if test="deviceId != null">
        #{deviceId,jdbcType=BIGINT},
      </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.KwsProjectDevice">
    <!--@mbg.generated-->
    update kws_project_device
    <set>
      <if test="projectId != null">
        project_id = #{projectId,jdbcType=BIGINT},
      </if>
      <if test="deviceId != null">
        device_id = #{deviceId,jdbcType=BIGINT},
      </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.KwsProjectDevice">
    <!--@mbg.generated-->
    update kws_project_device
    set project_id = #{projectId,jdbcType=BIGINT},
      device_id = #{deviceId,jdbcType=BIGINT},
      `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="selectDataByProjectId" resultType="com.sckw.slope.detection.model.dto.BaseItemDTO">
<!--      SELECT
	*
FROM
	kws_device_model_part
WHERE
	device_model_id IN (
	SELECT DISTINCT
		id
	FROM
		kws_device_model
	WHERE
		id IN (
		SELECT
			model_id
		FROM
			kws_device
		WHERE
			id IN (
			SELECT
				device_id
			FROM
				kws_project_device
			WHERE
			project_id IN ())));-->
      SELECT
          c.`name` AS NAME,
          d.part_name AS partName
      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
        and a.project_id = #{id}
    </select>

  <select id="selectIntegrationItemByDeviceId" resultType="com.sckw.slope.detection.model.vo.IntegrationItemVO">
      SELECT c.integration_id   AS integrationId,
             d.integration_name AS integrationName
      FROM kws_project_device a
               LEFT JOIN kws_device b ON a.device_id = b.id
               LEFT JOIN kws_device_integration c ON b.id = c.device_id
               LEFT JOIN kws_integration d ON c.integration_id = d.id
      WHERE a.del_flag = 0
        AND b.del_flag = 0
        and c.del_flag = 0
        and d.del_flag = 0
        and b.id = #{id}
  </select>

    <select id="selectIntegrationItemByProjectId" resultType="com.sckw.slope.detection.model.vo.IntegrationItemVO">
        SELECT c.integration_id   AS integrationId,
        d.integration_name AS integrationName
        FROM kws_project_device a
        LEFT JOIN kws_device b ON a.device_id = b.id
        LEFT JOIN kws_device_integration c ON b.id = c.integration_id
        LEFT JOIN kws_integration d ON c.integration_id = d.id
        WHERE a.del_flag = 0
        AND b.del_flag = 0
        and c.del_flag = 0
        and d.del_flag
        and b.id = #{id}
    </select>

  <select id="selectDataByDeviceId" resultType="com.sckw.slope.detection.model.dto.BaseItemDTO">
      SELECT
      c.`name` AS NAME,
      d.part_name AS partName
      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
      and b.id = #{id}
    </select>

    <select id="selectProjectByDeviceId" resultType="com.sckw.slope.detection.model.vo.ProjectVo">
        SELECT
            a.id AS projectId,
            a.`name` AS projectName
        FROM
            kws_project a
                LEFT JOIN kws_project_device b ON a.id = b.project_id
        WHERE
            b.device_id = #{deviceId}
        order by  b.create_time desc  limit 1
    </select>

    <select id="selectNotDeleteProjectByDeviceId" resultType="com.sckw.slope.detection.model.vo.ProjectVo">
        SELECT a.id     AS projectId,
               a.`name` AS projectName
        FROM kws_project a
                 LEFT JOIN kws_project_device b ON a.id = b.project_id
        WHERE b.device_id = #{deviceId}
          and a.del_flag = 0
        order by b.create_time desc limit 1
    </select>

  <select id="selectProjectDeviceByDeviceId" resultMap="BaseResultMap">
      SELECT b.*
      FROM kws_project a
               LEFT JOIN kws_project_device b ON a.id = b.project_id
      WHERE b.device_id = #{deviceId}
      order by b.create_time desc limit 1
  </select>
</mapper>