<?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.asset.mapper.TbAssetBorrowRecordMapper">
    
    <resultMap type="TbAssetBorrowRecord" id="TbAssetBorrowRecordResult">
        <result property="id"    column="id"    />
        <result property="assetBarCode"    column="asset_bar_code"    />
        <result property="userId"    column="user_id"    />
        <result property="tel"    column="tel"    />
        <result property="isreturn"    column="isReturn"    />
        <result property="borrowDate"    column="borrow_date"    />
        <result property="returnDate"    column="return_date"    />
        <result property="actualReturnDate"    column="actual_return_date"    />
        <result property="borrowPlaceName"    column="borrow_place_name"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTbAssetBorrowRecordVo">
        select id, asset_bar_code, user_id, tel, isReturn, borrow_date, return_date, actual_return_date, borrow_place_name, remark from tb_asset_borrow_record
    </sql>

    <select id="selectTbAssetBorrowRecordList" parameterType="TbAssetBorrowRecord" resultMap="TbAssetBorrowRecordResult">
        <include refid="selectTbAssetBorrowRecordVo"/>
        <where>  
            <if test="assetBarCode != null  and assetBarCode != ''"> and asset_bar_code = #{assetBarCode}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="isreturn != null "> and isReturn = #{isreturn}</if>
            <if test="borrowDate != null "> and borrow_date = #{borrowDate}</if>
            <if test="returnDate != null "> and return_date = #{returnDate}</if>
            <if test="actualReturnDate != null "> and actual_return_date = #{actualReturnDate}</if>
        </where>
    </select>
    
    <select id="selectTbAssetBorrowRecordById" parameterType="Long" resultMap="TbAssetBorrowRecordResult">
        <include refid="selectTbAssetBorrowRecordVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTbAssetBorrowRecord" parameterType="TbAssetBorrowRecord" useGeneratedKeys="true" keyProperty="id">
        insert into tb_asset_borrow_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="assetBarCode != null and assetBarCode != ''">asset_bar_code,</if>
            <if test="userId != null">user_id,</if>
            <if test="tel != null and tel != ''">tel,</if>
            <if test="isreturn != null">isReturn,</if>
            <if test="borrowDate != null">borrow_date,</if>
            <if test="returnDate != null">return_date,</if>
            <if test="actualReturnDate != null">actual_return_date,</if>
            <if test="borrowPlaceName != null and borrowPlaceName != ''">borrow_place_name,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="assetBarCode != null and assetBarCode != ''">#{assetBarCode},</if>
            <if test="userId != null">#{userId},</if>
            <if test="tel != null and tel != ''">#{tel},</if>
            <if test="isreturn != null">#{isreturn},</if>
            <if test="borrowDate != null">#{borrowDate},</if>
            <if test="returnDate != null">#{returnDate},</if>
            <if test="actualReturnDate != null">#{actualReturnDate},</if>
            <if test="borrowPlaceName != null and borrowPlaceName != ''">#{borrowPlaceName},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTbAssetBorrowRecord" parameterType="TbAssetBorrowRecord">
        update tb_asset_borrow_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="assetBarCode != null and assetBarCode != ''">asset_bar_code = #{assetBarCode},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="tel != null and tel != ''">tel = #{tel},</if>
            <if test="isreturn != null">isReturn = #{isreturn},</if>
            <if test="borrowDate != null">borrow_date = #{borrowDate},</if>
            <if test="returnDate != null">return_date = #{returnDate},</if>
            <if test="actualReturnDate != null">actual_return_date = #{actualReturnDate},</if>
            <if test="borrowPlaceName != null and borrowPlaceName != ''">borrow_place_name = #{borrowPlaceName},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTbAssetBorrowRecordById" parameterType="Long">
        delete from tb_asset_borrow_record where id = #{id}
    </delete>

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

    <select id="countAssetDto" resultType="com.ruoyi.asset.domain.dto.TbCountAssetDTO">
        SELECT SUM(a.borrow_number) countNum , b.name typeName
        FROM
            tb_asset_status_record a
                LEFT JOIN
            tb_asset c
            ON
                a.asset_bar_code = c.bar_code
                LEFT JOIN
            tb_asset_category b
            ON
                c.category_number = b.number
        GROUP BY
            b.name
    </select>
</mapper>