<?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.CmcDeviceScrapMapper">
    
    <resultMap type="CmcDeviceScrap" id="CmcDeviceScrapResult">
        <result property="deviceScrapId"    column="device_scrap_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="carId"    column="car_id"    />
        <result property="cost"    column="cost"    />
        <result property="scrapDate"    column="scrap_date"    />
        <result property="reason"    column="reason"    />
        <result property="deal"    column="deal"    />
        <result property="remark"    column="remark"    />
        <result property="document"    column="document"    />
        <association property="device"    javaType="CmcDevice"         resultMap="CmcDeviceResult" />
        <association property="car"    javaType="CmcCar"         resultMap="CmcCarResult" />
    </resultMap>

    <resultMap type="CmcCar" id="CmcCarResult">
        <result property="carId"    column="car_id"    />
        <result property="licensePlate"    column="license_plate"    />
    </resultMap>

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

    <sql id="selectCmcDeviceScrapVo">
        select ds.device_scrap_id, ds.device_id, ds.car_id, c.license_plate, d.device_number, d.code, d.name, d.brand, d.series, ds.cost, ds.scrap_date, ds.reason, ds.deal, ds.remark, ds.document from cmc_device_scrap as ds
        left join cmc_device as d on d.device_id = ds.device_id
        left join cmc_car as c on c.car_id = ds.car_id
    </sql>

    <select id="selectCmcDeviceScrapList" parameterType="CmcDeviceScrap" resultMap="CmcDeviceScrapResult">
        <include refid="selectCmcDeviceScrapVo"/>
        <where>  
            <if test="deviceId != null "> and ds.device_id = #{deviceId}</if>
            <if test="carId != null "> and ds.car_id = #{carId}</if>
            <if test="cost != null "> and ds.cost = #{cost}</if>
            <if test="scrapDate != null "> and ds.scrap_date = #{scrapDate}</if>
            <if test="reason != null  and reason != ''"> and ds.reason = #{reason}</if>
            <if test="deal != null  and deal != ''"> and ds.deal = #{deal}</if>
            <if test="document != null  and document != ''"> and ds.document = #{document}</if>
        </where>
        order by ds.scrap_date desc
    </select>
    
    <select id="selectCmcDeviceScrapByDeviceScrapId" parameterType="Integer" resultMap="CmcDeviceScrapResult">
        <include refid="selectCmcDeviceScrapVo"/>
        where ds.device_scrap_id = #{deviceScrapId}
    </select>
        
    <insert id="insertCmcDeviceScrap" parameterType="CmcDeviceScrap">
        insert into cmc_device_scrap
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="carId != null">car_id,</if>
            <if test="cost != null">cost,</if>
            <if test="scrapDate != null">scrap_date,</if>
            <if test="reason != null">reason,</if>
            <if test="deal != null">deal,</if>
            <if test="remark != null">remark,</if>
            <if test="document != null">document,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceScrapId != null">#{deviceScrapId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="carId != null">#{carId},</if>
            <if test="cost != null">#{cost},</if>
            <if test="scrapDate != null">#{scrapDate},</if>
            <if test="reason != null">#{reason},</if>
            <if test="deal != null">#{deal},</if>
            <if test="remark != null">#{remark},</if>
            <if test="document != null">#{document},</if>
         </trim>
    </insert>

    <update id="updateCmcDeviceScrap" parameterType="CmcDeviceScrap">
        update cmc_device_scrap
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="carId != null">car_id = #{carId},</if>
            <if test="cost != null">cost = #{cost},</if>
            <if test="scrapDate != null">scrap_date = #{scrapDate},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="deal != null">deal = #{deal},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="document != null">document = #{document},</if>
        </trim>
        where device_scrap_id = #{deviceScrapId}
    </update>

    <delete id="deleteCmcDeviceScrapByDeviceScrapId" parameterType="Integer">
        delete from cmc_device_scrap where device_scrap_id = #{deviceScrapId}
    </delete>

    <delete id="deleteCmcDeviceScrapByDeviceScrapIds" parameterType="Integer">
        delete from cmc_device_scrap where device_scrap_id in 
        <foreach item="deviceScrapId" collection="array" open="(" separator="," close=")">
            #{deviceScrapId}
        </foreach>
    </delete>
</mapper>