<?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.app.dao.butler.AppArticleBorrowDao">
    <select id="list" resultType="com.api.vo.app.AppArticleBorrowVo">
        select sa.id,sa.name,count(article_id) as quantity
        from sys_article_detail as sad,sys_article as sa
        where sad.article_id = sa.id and sad.status = 1
        GROUP BY sad.article_id
    </select>

    <select id="findBorrowNumById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) as num
        from sys_article_borrow as sab,sys_article_detail as sad
        where sab.article_detail_id = sad.id and borrow_status in (1,3) and sad.article_id = #{id}
    </select>

    <select id="myList" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppMyArticleBorrowVo">
        select sab.*,sa.name,sad.code
        from sys_article_borrow as sab,sys_article_detail as sad,sys_article as sa
        where sab.article_detail_id = sad.id and sad.article_id = sa.id and sab.create_id = #{id}
        ORDER BY sab.create_date DESC
    </select>

    <update id="frmLoss" parameterType="com.api.model.app.UserIdAndArticleBorrowId">
        update sys_article_borrow set status = 2
        where id = #{articleBorrowId} and create_id = #{id}
    </update>

    <select id="findDetailById" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppArticleBorrowDetailVo">
        select * from sys_article_detail
        where article_id = #{articleId} and status = 1
        and id not in (select article_detail_id from sys_article_borrow where borrow_status in (1,3))
    </select>
    <select id="findBorrowOrCheckArticleId" resultType="java.lang.Integer">
        select article_detail_id from sys_article_borrow where borrow_status in (1,3)
    </select>

    <insert id="borrow" parameterType="com.api.model.app.AppArticleBorrow">
        insert into sys_article_borrow(
            article_detail_id,
            borrow_status,
            status,
            begin_date,
            end_date,
            create_id,
            create_date,
            modify_id,
            modify_date,
            remake
        ) values (
            #{articleDetailId},
            #{borrowStatus},
            #{status},
            #{beginDate},
            #{endDate},
            #{createId},
            #{createDate},
            #{modifyId},
            #{modifyDate},
            #{remake}
        )
    </insert>

    <select id="findBorrowByUserId" parameterType="java.lang.Integer" resultType="com.api.vo.app.AppArticleBorrowReturnVo">
        select sab.id,sad.name,sad.code,sab.begin_date from sys_article_borrow as sab,sys_article_detail as sad
        where sab.article_detail_id = sad.id and sab.borrow_status in (1,4) and sab.create_id = #{id}
    </select>

    <select id="findBorrowArticleIdByUserId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select id from sys_article_borrow where borrow_status in (1,4) and create_id = #{userId}
    </select>

    <update id="articleReturn" parameterType="com.api.model.app.AppArticleBorrow">
        update sys_article_borrow set borrow_status = #{borrowStatus},end_date = #{endDate} where id = #{id}
    </update>

</mapper>