<?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.EmsObjAttrMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmsObjAttr" id="ObjAttrResult">
        <result property="id"    column="id"    />
        <result property="modelCode"    column="model_code"    />
        <result property="attrType"    column="attr_type"    />
        <result property="attrKey"    column="attr_key"    />
        <result property="attrName"    column="attr_name"    />
        <result property="attrUnit"    column="attr_unit"    />
    </resultMap>

    <sql id="selectAdmEmsObjAttrVo">
        select id, model_code, attr_type, attr_key, attr_name, attr_unit from adm_ems_obj_attr
    </sql>

    <select id="selectObjAttrList" parameterType="com.ruoyi.ems.domain.EmsObjAttr" resultMap="ObjAttrResult">
        <include refid="selectAdmEmsObjAttrVo"/>
        <where>  
            <if test="modelCode != null  and modelCode != ''"> and model_code = #{modelCode}</if>
            <if test="attrType != null"> and attr_type = #{attrType}</if>
            <if test="attrKey != null  and attrKey != ''"> and attr_key = #{attrKey}</if>
            <if test="attrName != null  and attrName != ''"> and attr_name like concat('%', #{attrName}, '%')</if>
        </where>
    </select>

    <select id="selectByModelCode" parameterType="java.lang.String" resultMap="ObjAttrResult">
        <include refid="selectAdmEmsObjAttrVo"/>
        where model_code = #{modelCode}
    </select>
    
    <select id="selectObjAttrById" parameterType="Long" resultMap="ObjAttrResult">
        <include refid="selectAdmEmsObjAttrVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertObjAttr" parameterType="com.ruoyi.ems.domain.EmsObjAttr" useGeneratedKeys="true" keyProperty="id">
        insert into adm_ems_obj_attr
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="modelCode != null and modelCode != ''">model_code,</if>
            <if test="attrType != null and attrType != ''">attr_type,</if>
            <if test="attrKey != null and attrKey != ''">attr_key,</if>
            <if test="attrName != null and attrName != ''">attr_name,</if>
            <if test="attrUnit != null">attr_unit,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="modelCode != null and modelCode != ''">#{modelCode},</if>
            <if test="attrType != null and attrType != ''">#{attrType},</if>
            <if test="attrKey != null and attrKey != ''">#{attrKey},</if>
            <if test="attrName != null and attrName != ''">#{attrName},</if>
            <if test="attrUnit != null">#{attrUnit},</if>
         </trim>
    </insert>

    <insert id="insertBatch" parameterType="java.util.List">
        insert into adm_ems_obj_attr (model_code, attr_type, attr_key, attr_name, attr_unit)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.modelCode}, #{item.attrType}, #{item.attrKey}, #{item.attrName}, #{item.attrUnit})
        </foreach>
    </insert>

    <update id="updateObjAttr" parameterType="com.ruoyi.ems.domain.EmsObjAttr">
        update adm_ems_obj_attr
        <trim prefix="SET" suffixOverrides=",">
            <if test="modelCode != null and modelCode != ''">model_code = #{modelCode},</if>
            <if test="attrType != null">attr_type = #{attrType},</if>
            <if test="attrKey != null and attrKey != ''">attr_key = #{attrKey},</if>
            <if test="attrName != null and attrName != ''">attr_name = #{attrName},</if>
            <if test="attrUnit != null">attr_unit = #{attrUnit},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteObjAttrById" parameterType="Long">
        delete from adm_ems_obj_attr where id = #{id}
    </delete>

    <delete id="deleteByModelCode" parameterType="String">
        delete from adm_ems_obj_attr where model_code = #{modelCode}
    </delete>

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