<?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.zehong.system.mapper.TBorrowingApplyForMapper">

    <resultMap type="TBorrowingApplyFor" id="TBorrowingApplyForResult">
        <result property="borrowing"    column="borrowing"    />
        <result property="feeName"    column="fee_name"    />
        <result property="borrowingDeptId"    column="borrowing_dept_id"    />
        <result property="handledByUserId"    column="handled_by_user_id"    />
        <result property="applicationDescription"    column="application_description"    />
        <result property="userId"    column="user_id"    />
        <result property="totalWords"    column="total_words"    />
        <result property="totalFigures"    column="total_figures"    />
        <result property="approvedUserId"    column="approved_user_id"    />
        <result property="registrationDate"    column="registration_date"    />
        <result property="remark"    column="remark"    />
        <result property="attachmentUrl"    column="attachment_url"    />
        <result property="isDel"    column="is_del"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="approvalStatus"    column="approval_status"    />

    </resultMap>

    <sql id="selectTBorrowingApplyForVo">
        select borrowing, fee_name, borrowing_dept_id, handled_by_user_id, application_description, user_id, total_words, total_figures, approved_user_id, registration_date, remark, attachment_url, is_del, create_time, update_time,approval_status from t_borrowing_apply_for
    </sql>

    <select id="selectTBorrowingApplyForList" parameterType="TBorrowingApplyFor" resultMap="TBorrowingApplyForResult">
        SELECT a.*,b.nick_name handledByUserIdName,c.nick_name userIdName,d.nick_name ApprovedUserIdName FROM t_borrowing_apply_for a
        left join sys_user b on  a.handled_by_user_id=b.user_id
        left join sys_user c on  a.user_id=c.user_id
        left join sys_user d on  a.approved_user_id=d.user_id
        <where>
            <if test="feeName != null  and feeName != ''"> and a.fee_name like concat('%', #{feeName}, '%')</if>
            <if test="borrowingDeptId != null "> and a.borrowing_dept_id = #{borrowingDeptId}</if>
            <if test="handledByUserId != null "> and a.handled_by_user_id = #{handledByUserId}</if>
            <if test="applicationDescription != null  and applicationDescription != ''"> and a.application_description = #{applicationDescription}</if>
            <if test="userId != null "> and a.user_id = #{userId}</if>
            <if test="totalWords != null  and totalWords != ''"> and a.total_words = #{totalWords}</if>
            <if test="totalFigures != null "> and a.total_figures = #{totalFigures}</if>
            <if test="approvedUserId != null "> and a.approved_user_id = #{approvedUserId}</if>
            <if test="registrationDate != null "> and a.registration_date = #{registrationDate}</if>
            <if test="attachmentUrl != null  and attachmentUrl != ''"> and a.attachment_url = #{attachmentUrl}</if>
            <if test="isDel != null  and isDel != ''"> and a.is_del = #{isDel}</if>
        </where>
        order by a.borrowing desc
    </select>

    <select id="selectTBorrowingApplyForById" parameterType="Long" resultMap="TBorrowingApplyForResult">
        <include refid="selectTBorrowingApplyForVo"/>
        where borrowing = #{borrowing}
    </select>


    <insert id="insertTBorrowingApplyFor" parameterType="TBorrowingApplyFor">
        insert into t_borrowing_apply_for
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="borrowing != null">borrowing,</if>
            <if test="feeName != null">fee_name,</if>
            <if test="borrowingDeptId != null">borrowing_dept_id,</if>
            <if test="handledByUserId != null">handled_by_user_id,</if>
            <if test="applicationDescription != null">application_description,</if>
            <if test="userId != null">user_id,</if>
            <if test="totalWords != null">total_words,</if>
            <if test="totalFigures != null">total_figures,</if>
            <if test="approvedUserId != null">approved_user_id,</if>
            <if test="registrationDate != null">registration_date,</if>
            <if test="remark != null">remark,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="isDel != null">is_del,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="borrowing != null">#{borrowing},</if>
            <if test="feeName != null">#{feeName},</if>
            <if test="borrowingDeptId != null">#{borrowingDeptId},</if>
            <if test="handledByUserId != null">#{handledByUserId},</if>
            <if test="applicationDescription != null">#{applicationDescription},</if>
            <if test="userId != null">#{userId},</if>
            <if test="totalWords != null">#{totalWords},</if>
            <if test="totalFigures != null">#{totalFigures},</if>
            <if test="approvedUserId != null">#{approvedUserId},</if>
            <if test="registrationDate != null">#{registrationDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTBorrowingApplyFor" parameterType="TBorrowingApplyFor">
        update t_borrowing_apply_for
        <trim prefix="SET" suffixOverrides=",">
            <if test="feeName != null">fee_name = #{feeName},</if>
            <if test="borrowingDeptId != null">borrowing_dept_id = #{borrowingDeptId},</if>
            <if test="handledByUserId != null">handled_by_user_id = #{handledByUserId},</if>
            <if test="applicationDescription != null">application_description = #{applicationDescription},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="totalWords != null">total_words = #{totalWords},</if>
            <if test="totalFigures != null">total_figures = #{totalFigures},</if>
            <if test="approvedUserId != null">approved_user_id = #{approvedUserId},</if>
            <if test="registrationDate != null">registration_date = #{registrationDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="approvalStatus != null">approval_status = #{approvalStatus},</if>
        </trim>
        where borrowing = #{borrowing}
    </update>

    <delete id="deleteTBorrowingApplyForById" parameterType="Long">
        delete from t_borrowing_apply_for where borrowing = #{borrowing}
    </delete>

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

    <!--查询部门余额是否足够-->
    <select id="selectTacc" resultType="java.lang.Integer">
        select count(account_id) from t_account where  dept_id=#{borrowingDeptId} and able_amount >= #{totalFigures}
    </select>

    <!--修改部门余额-->
    <update id="deletebBalance">
        update t_account set able_amount= able_amount- #{totalFigures} where dept_id=#{borrowingDeptId}
    </update>
</mapper>
