<?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.CmcDeviceExpenseMapper">
    
    <resultMap type="CmcDeviceExpense" id="CmcDeviceExpenseResult">
        <result property="deviceExpenseId"    column="device_expense_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="carId"    column="car_id"    />
        <result property="userId"    column="user_id"    />
        <result property="expenseType"    column="expense_type"    />
        <result property="expense"    column="expense"    />
        <result property="occurDate"    column="occur_date"    />
        <result property="remark"    column="remark"    />
        <result property="document"    column="document"    />
        <association property="user"    javaType="SysUser"         resultMap="SysUserResult" />
        <association property="device"    javaType="CmcDevice"         resultMap="CmcDeviceResult" />
        <association property="car"    javaType="CmcCar"         resultMap="CmcCarResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </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>

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

    <sql id="selectCmcDeviceExpenseVo">
        select de.device_expense_id, de.device_id, de.car_id, c.license_plate, d.device_number, d.code, d.name, d.brand, d.series, de.user_id, u.nick_name, de.expense_type, de.expense, de.occur_date, de.remark, de.document from cmc_device_expense as de
        left join sys_user as u on u.user_id = de.user_id
        left join cmc_device as d on d.device_id = de.device_id
        left join cmc_car as c on c.car_id = de.car_id
    </sql>

    <select id="selectCmcDeviceExpenseList" parameterType="CmcDeviceExpense" resultMap="CmcDeviceExpenseResult">
        <include refid="selectCmcDeviceExpenseVo"/>
        <where>  
            <if test="deviceId != null "> and de.device_id = #{deviceId}</if>
            <if test="carId != null "> and de.car_id = #{carId}</if>
            <if test="userId != null "> and de.user_id = #{userId}</if>
            <if test="expenseType != null  and expenseType != ''"> and de.expense_type = #{expenseType}</if>
            <if test="expense != null "> and de.expense = #{expense}</if>
            <if test="occurDate != null "> and de.occur_date = #{occurDate}</if>
            <if test="document != null  and document != ''"> and de.document = #{document}</if>
        </where>
        order by de.occur_date desc
    </select>
    
    <select id="selectCmcDeviceExpenseByDeviceExpenseId" parameterType="Integer" resultMap="CmcDeviceExpenseResult">
        <include refid="selectCmcDeviceExpenseVo"/>
        where de.device_expense_id = #{deviceExpenseId}
    </select>
        
    <insert id="insertCmcDeviceExpense" parameterType="CmcDeviceExpense">
        insert into cmc_device_expense
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="carId != null">car_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="expenseType != null">expense_type,</if>
            <if test="expense != null">expense,</if>
            <if test="occurDate != null">occur_date,</if>
            <if test="remark != null">remark,</if>
            <if test="document != null">document,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceExpenseId != null">#{deviceExpenseId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="carId != null">#{carId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="expenseType != null">#{expenseType},</if>
            <if test="expense != null">#{expense},</if>
            <if test="occurDate != null">#{occurDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="document != null">#{document},</if>
         </trim>
    </insert>

    <update id="updateCmcDeviceExpense" parameterType="CmcDeviceExpense">
        update cmc_device_expense
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="carId != null">car_id = #{carId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="expenseType != null">expense_type = #{expenseType},</if>
            <if test="expense != null">expense = #{expense},</if>
            <if test="occurDate != null">occur_date = #{occurDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="document != null">document = #{document},</if>
        </trim>
        where device_expense_id = #{deviceExpenseId}
    </update>

    <delete id="deleteCmcDeviceExpenseByDeviceExpenseId" parameterType="Integer">
        delete from cmc_device_expense where device_expense_id = #{deviceExpenseId}
    </delete>

    <delete id="deleteCmcDeviceExpenseByDeviceExpenseIds" parameterType="Integer">
        delete from cmc_device_expense where device_expense_id in 
        <foreach item="deviceExpenseId" collection="array" open="(" separator="," close=")">
            #{deviceExpenseId}
        </foreach>
    </delete>
</mapper>