<?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.oa.mapper.CmcBudgetDeviceMapper">
    
    <resultMap type="CmcBudgetDevice" id="CmcBudgetDeviceResult">
        <result property="budgetDeviceId"    column="budget_device_id"    />
        <result property="budgetId"    column="budget_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="days"    column="days"    />
        <result property="depreciation"    column="depreciation"    />
        <result property="remark"    column="remark"    />
        <association property="device"    javaType="CmcDevice"         resultMap="CmcDeviceResult" />
    </resultMap>

    <resultMap type="CmcDevice" id="CmcDeviceResult">
        <result property="deviceId"    column="device_id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="series"    column="series"    />
        <result property="brand"    column="brand"    />
        <result property="dayCost"    column="day_cost"    />
    </resultMap>

    <sql id="selectCmcBudgetDeviceVo">
        select bd.budget_device_id, bd.budget_id, d.code, d.name, d.series, d.brand, d.day_cost, bd.device_id, bd.days, bd.depreciation, bd.remark from cmc_budget_device as bd
        left join cmc_device as d on d.device_id = bd.device_id
    </sql>

    <select id="selectCmcBudgetDeviceList" parameterType="CmcBudgetDevice" resultMap="CmcBudgetDeviceResult">
        <include refid="selectCmcBudgetDeviceVo"/>
        <where>  
            <if test="budgetId != null  and budgetId != ''"> and bd.budget_id = #{budgetId}</if>
            <if test="deviceId != null "> and bd.device_id = #{deviceId}</if>
            <if test="days != null "> and bd.days = #{days}</if>
            <if test="depreciation != null "> and bd.depreciation = #{depreciation}</if>
        </where>
    </select>
    
    <select id="selectCmcBudgetDeviceByBudgetId" parameterType="String" resultMap="CmcBudgetDeviceResult">
        <include refid="selectCmcBudgetDeviceVo"/>
        where bd.budget_id = #{budgetId}
    </select>
        
    <insert id="insertCmcBudgetDevice" parameterType="CmcBudgetDevice">
        insert into cmc_budget_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="budgetDeviceId != null">budget_device_id,</if>
            <if test="budgetId != null">budget_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="days != null">days,</if>
            <if test="depreciation != null">depreciation,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="budgetDeviceId != null">#{budgetDeviceId},</if>
            <if test="budgetId != null">#{budgetId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="days != null">#{days},</if>
            <if test="depreciation != null">#{depreciation},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCmcBudgetDevice" parameterType="CmcBudgetDevice">
        update cmc_budget_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="budgetId != null">budget_id = #{budgetId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="days != null">days = #{days},</if>
            <if test="depreciation != null">depreciation = #{depreciation},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where budget_device_id = #{budgetDeviceId}
    </update>

    <delete id="deleteCmcBudgetDeviceByBudgetId" parameterType="String">
        delete from cmc_budget_device where budget_id = #{budgetId}
    </delete>

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