<?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.api.butlerApp.dao.jurisdiction.ButlerKeyDao">

    <select id="list" parameterType="com.api.model.butlerApp.ButlerKeySearch" resultType="com.api.vo.butlerApp.ButlerKeyVo">
        select sk.id,sk.code,sk.facility_name,sk.num as total_num,sk.corresponding_position,
        sk.storage_location
        from sys_key as sk
    </select>
    <select id="findCreateDateByKeyIdAndBorrowerId" parameterType="com.api.model.butlerApp.ButlerKeyIdAndBorrowerId" resultType="java.util.Date">
        select create_date from sys_key_borrow
        where key_id = #{keyId} and borrower = #{borrowerId} and status in (2,4,5)
        order by create_date desc limit 1
    </select>
    <select id="noReturnList" parameterType="com.api.model.butlerApp.ButlerKeySearch" resultType="com.api.vo.butlerApp.ButlerKeyVo">
        select sk.id,sk.code,sk.facility_name,sk.num as total_num,sk.corresponding_position,
        sk.storage_location,skb.status,skb.create_date
        from sys_key as sk,sys_key_borrow as skb
		where skb.key_id = sk.id and skb.status in (2,4,5) and skb.borrower =#{id}
    </select>

    <insert id="apply" parameterType="com.api.model.butlerApp.ButlerKeyBorrow">
        insert into sys_key_borrow(
            code,
            key_id,
            borrower,
            status,
            create_date
        )values (
            #{code},
            #{keyId},
            #{borrower},
            #{status},
            #{createDate}
        )
    </insert>

    <update id="returnKey" parameterType="com.api.model.butlerApp.ButlerKeyBorrow">
        update sys_key_borrow
        set return_date = #{returnDate},
        status = #{status}
        where key_id = #{keyId} and borrower = #{borrower} and status in (2,5)
    </update>

    <select id="record" parameterType="com.api.model.butlerApp.ButlerRecordSearch" resultType="com.api.vo.butlerApp.ButlerRecordVo">
        select skb.id,skb.code,skb.status,sk.facility_name,sk.corresponding_position,sk.storage_location,skb.audit_date,skb.reason,skb.return_audit_date,skb.return_reason
        from sys_key_borrow as skb,sys_key as sk
        <where>
            skb.key_id = sk.id
            <if test="recordStatus != null">
                and skb.status = #{recordStatus}
            </if>
            and skb.borrower = #{id}
        </where>
        order by skb.create_date desc
    </select>
    <select id="findNumById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select num from sys_key where id = #{keyId}
    </select>

</mapper>