<?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.ButlerBorrowDao">
    <select id="list" parameterType="com.api.model.butlerApp.ButlerBorrowSearch" resultType="com.api.vo.butlerApp.ButlerBorrowVo">
        select sab.*,sad.name as article_name,ur.name as borrow_name,ur.tel as borrow_tel
        from sys_article_borrow as sab,sys_article_detail as sad,sys_article as sa,user_resident as ur
        <where>
            sab.article_detail_id = sad.id
            and sad.article_id = sa.id
            and sab.create_id = ur.id
            <if test="borrowStatus != null">
                and sab.borrow_status = #{borrowStatus}
            </if>
        </where>
        order by sab.create_date DESC
    </select>

    <select id="checkItems" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerCheckItemsVo">
        select sab.*,sa.name as article_name,sad.code
        from sys_article_borrow as sab,sys_article_detail as sad,sys_article as sa,user_resident as ur
        where sab.article_detail_id = sad.id and sad.article_id = sa.id and sab.create_id = ur.id and sab.id = #{articleBorrowId}
    </select>

    <update id="updateSAEByBorrowId" parameterType="com.api.model.butlerApp.ButlerSubmitCheck">
        update sys_article_borrow
        <set>
            status = #{articleStatus},
            end_date = #{endDate},
            borrow_status = #{borrowStatus},
            <if test="returnRejectReasons != null and returnRejectReasons != ''">
                return_reject_reasons = #{returnRejectReasons}
            </if>
        </set>
        where id = #{articleBorrowId}
    </update>

    <update id="updateStatusByDetailId" parameterType="com.api.model.butlerApp.ButlerSubmitCheck">
        update sys_article_detail
        set status = #{articleStatus}
        where id = #{articleDetailId}
    </update>

    <select id="findStatusByBorrowId" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerBorrowVo">
        select * from sys_article_borrow where id = #{articleBorrowId}
    </select>

    <select id="articleList" resultType="com.api.vo.butlerApp.ButlerArticleVo">
        select id,name from sys_article
    </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 = 1 and sad.article_id = #{id}
    </select>

    <insert id="insertArticle" parameterType="com.api.model.butlerApp.ButlerArticle">
        insert into sys_article(
            name,
            quantity,
            create_id,
            create_date
        )values (
            #{name},
            #{quantity},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerArticleDetailFBIVo">
        select * from sys_article_detail where id = #{articleDetailId}
    </select>

    <select id="countBorrowByADId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_article_borrow where article_detail_id = #{id} and borrow_status = 1
    </select>

    <select id="articleDetailList" parameterType="java.lang.Integer" resultType="com.api.vo.butlerApp.ButlerArticleDetailVo">
        select * from sys_article_detail where article_id = #{articleId} and status = 1
    </select>

    <update id="updateArticleDetail" parameterType="com.api.model.butlerApp.ButlerArticleDetail">
        update sys_article_detail
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <insert id="insertMessage" parameterType="com.api.model.butlerApp.ButlerMessage">
        insert into sys_message(
            title,
            content,
            generate_date,
            send_date,
            sender,
            type
        ) values (
            #{title},
            #{content},
            #{generateDate},
            #{sendDate},
            #{sender},
            #{type}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <select id="findBorrowerIdById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select create_id from sys_article_borrow where id = #{borrowId}
    </select>


    <insert id="insertSending" parameterType="com.api.model.butlerApp.ButlerSending">
        insert into sys_sending(
            message_id,
            receiver_account,
            send_status,
            send_date
        ) values (
            #{messageId},
            #{receiverAccount},
            #{sendStatus},
            #{sendDate}
        )
    </insert>
    <insert id="insertArticleDetail" parameterType="com.api.model.butlerApp.ButlerArticleDetail">
        insert into sys_article_detail(
            article_id,
            name,
            code,
            status,
            create_id,
            create_date
        ) values (
            #{articleId},
            #{name},
            #{code},
            #{status},
            #{createId},
            #{createDate}
        )
        <selectKey keyProperty="id" resultType="java.lang.Integer">
            select LAST_INSERT_ID() AS id
        </selectKey>
    </insert>

    <update id="incQuantityByArticleId" parameterType="java.lang.Integer">
        update sys_article set quantity = quantity + 1 where id = #{articleId}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from sys_article_detail where id = #{id}
    </delete>

    <select id="findArticleIdById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select article_id from sys_article_detail where id = #{id}
    </select>

    <update id="decQuantityById" parameterType="java.lang.Integer">
        update sys_article set quantity = quantity - 1 where id = #{articleId}
    </update>
    <update id="updateSARById" parameterType="com.api.model.butlerApp.ButlerBorrowLendingApproval">
        update sys_article_borrow
        <set>
            borrow_status = #{status},
            <if test="rejectReasons != null and rejectReasons != ''">
                reject_reasons = #{rejectReasons}
            </if>
        </set>
        where id = #{id}
    </update>

    <select id="countRecordNumById" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_article_borrow where article_detail_id =#{id}
    </select>
    <select id="findNormalNum" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from sys_article_detail where status = 1 and article_id = #{id}
    </select>

</mapper>