<?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.borrow.mapper.TbAssetBorrowRecordMapper">
    
    <resultMap type="TbAssetBorrowRecord" id="TbAssetBorrowRecordResult">
        <result property="id"    column="id"    />
        <result property="assetId"    column="asset_id"    />
        <result property="userName"    column="user_name"    />
        <result property="borrowTime"    column="borrow_time"    />
        <result property="borrowLocationNumber"    column="borrow_location_number"    />
        <result property="expectedReturnTime"    column="expected_return_time"    />
        <result property="isReturn"    column="is_return"    />
        <result property="returnTime"    column="return_time"    />
        <result property="returnLocationNumber"    column="return_location_number"    />
        <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="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTbAssetBorrowRecordVo">
        select id, asset_id, user_name, borrow_time, borrow_location_number, expected_return_time, is_return, return_time, return_location_number, create_by, create_time, update_by, update_time, remark from tb_asset_borrow_record
    </sql>

    <sql id="selectTbAssetBorrowRecord">
        select
            a.id,
            a.asset_id assetId,
            b.name assetName,
            b.number assetNumber,
            a.user_name userName,
            a.borrow_time borrowTime,
            a.borrow_location_number borrowLocationNumber,
            concat_ws('-',IFNULL(NULL,e.name),IFNULL(NULL,d.name),c.name) borrowLocationName,
            a.expected_return_time expectedReturnTime,
            a.is_return isReturn,
            a.return_time returnTime,
            a.return_location_number returnLocationNumber,
            concat_ws('-',IFNULL(NULL,h.name),IFNULL(NULL,g.name),f.name) returnLocationName,
            a.create_by createBy,
            a.create_time createTime,
            a.update_by updateBy,
            a.update_time updateTime,
            a.remark
        from tb_asset_borrow_record a
         LEFT JOIN tb_asset_information b ON b.id = a.asset_id
         LEFT JOIN tb_location c ON c.number = a.borrow_location_number
         LEFT JOIN tb_location d ON c.parent_id = d.id
         LEFT JOIN tb_location e ON d.parent_id = e.id
         LEFT JOIN tb_location f ON f.number = a.return_location_number
         LEFT JOIN tb_location g ON f.parent_id = g.id
         LEFT JOIN tb_location h ON g.parent_id = h.id
    </sql>

    <select id="selectTbAssetBorrowRecordList" parameterType="TbAssetBorrowRecord" resultType="TbAssetBorrowRecord">
        <include refid="selectTbAssetBorrowRecord"/>
        <where>  
            <if test="assetNumber != null and assetNumber != ''"> and b.number = #{assetNumber} or b.number like concat('%', #{assetNumber}, '%')</if>
            <if test="assetName != null and assetName != ''"> and b.name = #{assetName} or b.name like concat('%', #{assetName}, '%')</if>
            <if test="userName != null  and userName != ''"> and a.user_name like concat('%', #{userName}, '%')</if>
            <if test="borrowTime != null "> and a.borrow_time = #{borrowTime}</if>
            <if test="borrowLocationNumber != null  and borrowLocationNumber != ''"> and a.borrow_location_number = #{borrowLocationNumber}</if>
            <if test="expectedReturnTime != null "> and a.expected_return_time = #{expectedReturnTime}</if>
            <if test="isReturn != null "> and a.is_return = #{isReturn}</if>
            <if test="returnTime != null "> and a.return_time = #{returnTime}</if>
            <if test="returnLocationNumber != null  and returnLocationNumber != ''"> and a.return_location_number = #{returnLocationNumber}</if>
        </where>
        ORDER BY a.is_return ASC
    </select>
    
    <select id="selectTbAssetBorrowRecordById" parameterType="Long" resultType="TbAssetBorrowRecord">
        <include refid="selectTbAssetBorrowRecord"/>
        where a.id = #{id}
    </select>
    
    <select id="selectLentRecordByAssetId" parameterType="Long" resultType="TbAssetBorrowRecord">
        <include refid="selectTbAssetBorrowRecord"/>
        where a.asset_id = #{assetId} and is_return = 0
    </select>
        
    <insert id="insertTbAssetBorrowRecord" parameterType="TbAssetBorrowRecord" useGeneratedKeys="true" keyProperty="id">
        insert into tb_asset_borrow_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="assetId != null">asset_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="borrowTime != null">borrow_time,</if>
            <if test="borrowLocationNumber != null and borrowLocationNumber != ''">borrow_location_number,</if>
            <if test="expectedReturnTime != null">expected_return_time,</if>
            <if test="isReturn != null">is_return,</if>
            <if test="returnTime != null">return_time,</if>
            <if test="returnLocationNumber != null and returnLocationNumber != ''">return_location_number,</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>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="assetId != null">#{assetId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="borrowTime != null">#{borrowTime},</if>
            <if test="borrowLocationNumber != null and borrowLocationNumber != ''">#{borrowLocationNumber},</if>
            <if test="expectedReturnTime != null">#{expectedReturnTime},</if>
            <if test="isReturn != null">#{isReturn},</if>
            <if test="returnTime != null">#{returnTime},</if>
            <if test="returnLocationNumber != null and returnLocationNumber != ''">#{returnLocationNumber},</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>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTbAssetBorrowRecord" parameterType="TbAssetBorrowRecord">
        update tb_asset_borrow_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="assetId != null">asset_id = #{assetId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="borrowTime != null">borrow_time = #{borrowTime},</if>
            <if test="borrowLocationNumber != null and borrowLocationNumber != ''">borrow_location_number = #{borrowLocationNumber},</if>
            <if test="expectedReturnTime != null">expected_return_time = #{expectedReturnTime},</if>
            <if test="isReturn != null">is_return = #{isReturn},</if>
            <if test="returnTime != null">return_time = #{returnTime},</if>
            <if test="returnLocationNumber != null and returnLocationNumber != ''">return_location_number = #{returnLocationNumber},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateIsReturn" parameterType="TbAssetBorrowRecord">
        update tb_asset_borrow_record
        set is_return = #{isReturn}, return_time = #{returnTime}, return_location_number = #{returnLocationNumber}
        where id = #{id}
    </update>

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

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