<?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.traceability.mapper.BaseDeviceInfoMapper">

    <resultMap type="BaseDeviceInfo" id="BaseDeviceInfoResult">
        <result property="deviceId"    column="device_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="orderNum"    column="order_num"    />
        <result property="resource"    column="resource"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="materialType"    column="material_type"    />
        <result property="parentName" column="parent_name" />
    </resultMap>

    <sql id="selectBaseDeviceInfoVo">
        select device_id, parent_id, device_name, order_num, resource, status, del_flag, create_by, create_time, update_by, update_time, material_type from base_deviceinfo
    </sql>

    <select id="selectBaseDeviceInfoList" parameterType="BaseDeviceInfo" resultMap="BaseDeviceInfoResult">
        <include refid="selectBaseDeviceInfoVo"/>
        <where>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="resource != null  and resource != ''"> and resource = #{resource}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="materialType != null "> and material_type = #{materialType}</if>
        </where>
        order by parent_id
    </select>

    <select id="selectBaseDeviceInfoByDeviceId" parameterType="Long" resultMap="BaseDeviceInfoResult">
        select t.device_id, t.parent_id, t.device_name, t.order_num, t.resource, t.status, t.del_flag, t.create_by, t.create_time, t.update_by, t.update_time, t.material_type, p.device_name as parent_name
        from base_deviceinfo t
                 left join base_deviceinfo p on p.device_id = t.parent_id
        where t.device_id = #{deviceId}
    </select>

    <insert id="insertBaseDeviceInfo" parameterType="BaseDeviceInfo" useGeneratedKeys="true" keyProperty="deviceId">
        insert into base_deviceinfo
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="resource != null">resource,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</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="materialType != null">material_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="resource != null">#{resource},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="materialType != null">#{materialType},</if>
        </trim>
    </insert>

    <update id="updateBaseDeviceInfo" parameterType="BaseDeviceInfo">
        update base_deviceinfo
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="resource != null">resource = #{resource},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <delete id="deleteBaseDeviceInfoByDeviceId" parameterType="Long">
        delete from base_deviceinfo where device_id = #{deviceId}
    </delete>

    <delete id="deleteBaseDeviceInfoByDeviceIds" parameterType="String">
        delete from base_deviceinfo where device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>

</mapper>