<?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.KwsDeviceIntegrationMapper">
    <resultMap id="BaseResultMap" type="com.sckw.slope.detection.model.dos.mysql.KwsDeviceIntegration">
        <!--@mbg.generated-->
        <!--@Table kws_device_integration-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="device_id" jdbcType="BIGINT" property="deviceId"/>
        <result column="integration_id" jdbcType="BIGINT" property="integrationId"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="mountain_id" jdbcType="VARCHAR" property="mountainId"/>
        <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_id,
        integration_id,
        `status`,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag,mountain_id
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from kws_device_integration
        where id = #{id,jdbcType=BIGINT}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete
        from kws_device_integration
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDeviceIntegration">
        <!--@mbg.generated-->
        insert into kws_device_integration (id, device_id, integration_id,
                                            `status`, create_by, create_time,
                                            update_by, update_time, del_flag,mountain_id)
        values (#{id,jdbcType=BIGINT}, #{deviceId,jdbcType=BIGINT}, #{integrationId,jdbcType=BIGINT},
                #{status,jdbcType=TINYINT}, #{createBy,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP},
                #{updateBy,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP}, #{delFlag,jdbcType=TINYINT}, #{mountainId,jdbcType=TINYINT})
    </insert>
    <insert id="insertSelective" parameterType="com.sckw.slope.detection.model.dos.mysql.KwsDeviceIntegration">
        <!--@mbg.generated-->
        insert into kws_device_integration
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="deviceId != null">
                device_id,
            </if>
            <if test="integrationId != null">
                integration_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="deviceId != null">
                #{deviceId,jdbcType=BIGINT},
            </if>
            <if test="integrationId != null">
                #{integrationId,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.KwsDeviceIntegration">
        <!--@mbg.generated-->
        update kws_device_integration
        <set>
            <if test="deviceId != null">
                device_id = #{deviceId,jdbcType=BIGINT},
            </if>
            <if test="integrationId != null">
                integration_id = #{integrationId,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.KwsDeviceIntegration">
        <!--@mbg.generated-->
        update kws_device_integration
        set device_id      = #{deviceId,jdbcType=BIGINT},
            integration_id = #{integrationId,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="selectIntegratedElement" resultType="com.sckw.slope.detection.model.dto.DeviceElementDTO">
        SELECT a.id               as deviceId,
               c.`name`,
               c.alias,
               e.integration_id   as element,
               f.integration_name as integrationElement
        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
                 LEFT JOIN kws_device_integration e ON c.id = e.device_id
                 LEFT JOIN kws_integration f ON f.id = e.integration_id
        WHERE a.del_flag = 0
          and e.del_flag = 0
          and b.del_flag = 0
          and c.del_flag = 0
          and a.mountain_id = c.mountain_id
        <if test="status != null and status.size() > 0">
            and c.status in
            <foreach collection="status" item="status" close=")" open="(" separator=",">
                #{status}
            </foreach>
        </if>
        <if test="projectId != null and projectId != ''">
            and a.id = #{projectId}
        </if>
        <if test="mountainId != null and mountainId != ''">
            and a.mountain_id = #{mountainId}
        </if>
        <if test="deviceId != null  and deviceId != ''">
            and b.device_id = #{deviceId}
        </if>
    </select>
    <select id="selectListByParms" resultType="com.sckw.slope.detection.model.dos.mysql.KwsDeviceIntegration">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from kws_device_integration
        where d.del_flag = 0
        <if test="vo.id != null and vo.id != ''">
            and device_id = #{vo.id}
        </if>
    </select>

    <select id="selectListByParamsAndInterName" resultType="com.sckw.slope.detection.model.vo.DeviceIntegrationVo">
        <!--@mbg.generated-->
        select
        d.id,d.integration_id,integration_name,part_names,i.unit,formula
        from kws_device_integration as d
        inner join kws_integration as i on d.integration_id=i.id
        where d.del_flag = 0 and i.del_flag = 0
        <if test="vo.id != null and vo.id != ''">
            and device_id = #{vo.id}
        </if>
    </select>

    <select id="selectListParamsAndInterNameByDeviceId"
            resultType="com.sckw.slope.detection.model.vo.DeviceIntegrationVo">
        <!--@mbg.generated-->
        select
        d.id,d.integration_id,integration_name,part_names,i.unit,formula
        from kws_device_integration as d
        inner join kws_integration as i on d.integration_id=i.id
        where d.del_flag = 0 and i.del_flag = 0
        <if test="id != null and id != ''">
            and device_id = #{id}
        </if>
    </select>
</mapper>