<?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.hw.dms.mapper.DmsBaseDeviceBomMapper">

    <resultMap type="DmsBaseDeviceBom" id="DmsBaseDeviceBomResult">
        <result property="deviceBomId"    column="device_bom_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="bomAmount"    column="bom_amount"    />
        <result property="ancestors"    column="ancestors"    />
        <result property="isFlag"    column="is_flag"    />
        <result property="remark"    column="remark"    />
        <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="deviceName"    column="device_name"    />
    </resultMap>

    <sql id="selectDmsBaseDeviceBomVo">
        select device_bom_id, parent_id, device_id, bom_amount, ancestors, is_flag, remark, create_by, create_time, update_by, update_time from dms_base_device_bom
    </sql>

    <select id="selectDmsBaseDeviceBomList" parameterType="DmsBaseDeviceBom" resultMap="DmsBaseDeviceBomResult">
        select a.device_bom_id, a.parent_id, a.device_id, a.bom_amount, a.ancestors, a.is_flag, a.remark, a.create_by, a.create_time, a.update_by, a.update_time
        from dms_base_device_bom a left join dms_base_device_ledger b on a.device_id = b.device_id
        <where>
            <if test="parentId != null "> and a.parent_id = #{parentId}</if>
            <if test="deviceId != null "> and a.device_id = #{deviceId}</if>
            <if test="bomAmount != null "> and a.bom_amount = #{bomAmount}</if>
            <if test="ancestors != null  and ancestors != ''"> and a.ancestors = #{ancestors}</if>
            <if test="isFlag != null "> and a.is_flag = #{isFlag}</if>
            <if test="deviceName != null "> and b.device_name like concat('%', #{deviceName},'%')</if>
        </where>
    </select>

    <select id="selectDmsBaseDeviceBomByDeviceBomId" parameterType="Long" resultMap="DmsBaseDeviceBomResult">
        <include refid="selectDmsBaseDeviceBomVo"/>
        where device_bom_id = #{deviceBomId}
    </select>

    <insert id="insertDmsBaseDeviceBom" parameterType="DmsBaseDeviceBom" useGeneratedKeys="true" keyProperty="deviceBomId">
        insert into dms_base_device_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="bomAmount != null">bom_amount,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="isFlag != null">is_flag,</if>
            <if test="remark != null">remark,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="bomAmount != null">#{bomAmount},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="isFlag != null">#{isFlag},</if>
            <if test="remark != null">#{remark},</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>
         </trim>
    </insert>

    <update id="updateDmsBaseDeviceBom" parameterType="DmsBaseDeviceBom">
        update dms_base_device_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="bomAmount != null">bom_amount = #{bomAmount},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="isFlag != null">is_flag = #{isFlag},</if>
            <if test="remark != null">remark = #{remark},</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>
        </trim>
        where device_bom_id = #{deviceBomId}
    </update>

    <delete id="deleteDmsBaseDeviceBomByDeviceBomId" parameterType="Long">
        delete from dms_base_device_bom where device_bom_id = #{deviceBomId}
    </delete>

    <delete id="deleteDmsBaseDeviceBomByDeviceBomIds" parameterType="String">
        delete from dms_base_device_bom where device_bom_id in
        <foreach item="deviceBomId" collection="array" open="(" separator="," close=")">
            #{deviceBomId}
        </foreach>
    </delete>
    <select id="selectBomByDeviceType" parameterType="String" resultMap="DmsBaseDeviceBomResult">
    select a.device_bom_id,a.parent_id,a.device_id,a.bom_amount,a.ancestors,a.is_flag,a.remark,a.create_by,a.create_time,a.update_by,a.update_time
    from dms_base_device_bom  a
    left join dms_base_device_ledger b on a.device_id = b.device_id
    where b.device_type_id = #{deviceType}
    </select>


</mapper>
