<?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.ruoyi.ems.mapper.EmsDeviceComponentMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmsDeviceComponent" id="DeviceComponentResult">
        <result property="id"    column="id"    />
        <result property="deviceCode"      column="device_code"    />
        <result property="compoCode"       column="compo_code"    />
        <result property="compoTag"        column="compo_tag"    />
        <result property="extCompoCode"    column="ext_compo_code"    />
        <result property="psCode"          column="ps_code"    />
        <result property="psName"          column="ps_name"    />
        <result property="compoModel"      column="compo_model"    />
        <result property="compoBrand"      column="compo_brand"    />
        <result property="compoSpec"       column="compo_spec"    />
        <result property="ancestors"       column="ancestors"    />
        <result property="parentCompo"     column="parent_compo"    />
    </resultMap>

    <sql id="selectComponentVo">
        select id, device_code, compo_code, compo_tag, ps_code, ext_compo_code, compo_model, compo_brand, compo_spec, ancestors, parent_compo from adm_ems_device_component
    </sql>

    <select id="selectList" parameterType="com.ruoyi.ems.domain.EmsDeviceComponent" resultMap="DeviceComponentResult">
        <include refid="selectComponentVo"/>
        <where>  
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="compoCode != null  and compoCode != ''"> and compo_code = #{compoCode}</if>
            <if test="psCode != null  and psCode != ''"> and ps_code = #{psCode}</if>
            <if test="extCompoCode != null  and extCompoCode != ''"> and ext_compo_code = #{extCompoCode}</if>
            <if test="compoModel != null  and compoModel != ''"> and compo_model = #{compoModel}</if>
            <if test="parentCompo != null  and parentCompo != ''"> and parent_compo = #{parentCompo}</if>
        </where>
    </select>

    <select id="selectByDeviceCode" parameterType="java.lang.String" resultMap="DeviceComponentResult">
        select
            c.id, c.device_code, c.compo_code, c.compo_tag, c.ps_code, c.ext_compo_code, c.compo_model, c.compo_brand, c.compo_spec, c.ancestors, c.parent_compo,
            p.ps_name
        from adm_ems_device_component c
          left join dim_ems_dev_process p on c.ps_code = p.ps_code
        where device_code = #{deviceCode}
    </select>

    <select id="selectById" parameterType="Long" resultMap="DeviceComponentResult">
        <include refid="selectComponentVo"/>
        where id = #{id}
    </select>

    <select id="selectByCode"  resultMap="DeviceComponentResult">
        <include refid="selectComponentVo"/>
        where compo_code = #{compoCode}
    </select>

    <insert id="insertComponent" parameterType="com.ruoyi.ems.domain.EmsDeviceComponent" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_device_component
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="compoCode != null and compoCode != ''">compo_code,</if>
            <if test="compoTag != null and compoTag != ''">compo_tag,</if>
            <if test="psCode != null and psCode != ''">ps_code,</if>
            <if test="extCompoCode != null">ext_compo_code,</if>
            <if test="compoModel != null">compo_model,</if>
            <if test="compoBrand != null">compo_brand,</if>
            <if test="compoSpec != null">compo_spec,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="parentCompo != null">parent_compo,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="compoCode != null and compoCode != ''">#{compoCode},</if>
            <if test="compoTag != null and compoTag != ''">#{compoTag},</if>
            <if test="psCode != null and psCode != ''">#{psCode},</if>
            <if test="extCompoCode != null">#{extCompoCode},</if>
            <if test="compoModel != null">#{compoModel},</if>
            <if test="compoBrand != null">#{compoBrand},</if>
            <if test="compoSpec != null">#{compoSpec},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="parentCompo != null">#{parentCompo},</if>
         </trim>
    </insert>

    <update id="updateComponent" parameterType="com.ruoyi.ems.domain.EmsDeviceComponent">
        update adm_ems_device_component
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="compoCode != null and compoCode != ''">compo_code = #{compoCode},</if>
            <if test="compoTag != null and compoTag != ''">compo_tag = #{compoTag},</if>
            <if test="psCode != null and psCode != ''">ps_code = #{psCode},</if>
            <if test="extCompoCode != null">ext_compo_code = #{extCompoCode},</if>
            <if test="compoModel != null">compo_model = #{compoModel},</if>
            <if test="compoBrand != null">compo_brand = #{compoBrand},</if>
            <if test="compoSpec != null">compo_spec = #{compoSpec},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="parentCompo != null">parent_compo = #{parentCompo},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteComponentById" parameterType="Long">
        delete from adm_ems_device_component where id = #{id}
    </delete>

    <delete id="deleteComponentByIds" parameterType="String">
        delete from adm_ems_device_component where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>