<?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.TAccountMapper">
    
    <resultMap type="TAccount" id="TAccountResult">
        <result property="accountId"    column="account_id"    />
        <result property="deptId"    column="deptId"    />
        <result property="ableAmount"    column="able_amount"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remark"    column="remark"    />
        <result property="deptName" column="dept_name"/>
        <result property="payableTotalAmount" column="payableTotalAmount"/>
        <result property="incomeAbleTotalAmount" column="incomeAbleTotalAmount"/>
        <result property="totalAmount" column="totalAmount"/>
        <result property="transferSwitch" column="transfer_switch"/>
    </resultMap>

    <sql id="selectTAccountVo">
        select account_id, dept_id AS deptId , able_amount, create_time, update_time, is_del,transfer_switch, remark,(SELECT dept.dept_name FROM sys_dept dept WHERE dept.dept_id = deptId)AS dept_name from t_account
    </sql>

    <select id="selectTAccountList" parameterType="TAccount" resultMap="TAccountResult">
        select tt.*,(able_amount+incomeAbleTotalAmount- payableTotalAmount) as totalAmount from(
        select account_id, dept_id AS deptId , able_amount,transfer_switch, IFNULL((select sum(pending_payment) from t_trade_project where trade_status=3 and apply_dept_id =t.dept_id ),0)incomeAbleTotalAmount,IFNULL((select sum(pending_payment) from t_trade_project where trade_status=3 and trade_dept_id =t.dept_id ),0)payableTotalAmount, create_time, update_time, is_del, remark,(SELECT dept.dept_name FROM sys_dept dept WHERE dept.dept_id = deptId)AS dept_name from t_account t
        <where>
            <if test="deptId != null "> and t.dept_id = #{deptId}</if>
            <if test="ableAmount != null "> and t.able_amount = #{ableAmount}</if>
            <if test="isDel != null  and isDel != ''"> and t.is_del = #{isDel}</if>
        </where>
        ) tt
    </select>
    
    <select id="selectTAccountById" parameterType="Long" resultMap="TAccountResult">
        <include refid="selectTAccountVo"/>
        where account_id = #{accountId}
    </select>
        
    <insert id="insertTAccount" parameterType="TAccount" useGeneratedKeys="true" keyProperty="accountId">
        insert into t_account
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="ableAmount != null">able_amount,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remark != null">remark,</if>
            <if test="transferSwitch != null">transfer_switch,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="ableAmount != null">#{ableAmount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remark != null">#{remark},</if>
            <if test="transferSwitch != null">#{transferSwitch},</if>
         </trim>
    </insert>

    <update id="updateTAccount" parameterType="TAccount">
        update t_account
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="ableAmount != null">able_amount = #{ableAmount},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="transferSwitch != null">transfer_switch = #{transferSwitch},</if>
        </trim>
        where account_id = #{accountId}
    </update>

    <delete id="deleteTAccountById" parameterType="Long">
        delete from t_account where account_id = #{accountId}
    </delete>

    <delete id="deleteTAccountByIds" parameterType="String">
        delete from t_account where account_id in 
        <foreach item="accountId" collection="array" open="(" separator="," close=")">
            #{accountId}
        </foreach>
    </delete>
</mapper>