<?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.TDebitCreditMapper">
    
    <resultMap type="TDebitCredit" id="TDebitCreditResult">
        <result property="debitId"    column="debit_id"    />
        <result property="debitDeptId"    column="debit_dept_id"    />
        <result property="operatorId"    column="operator_id"    />
        <result property="lendDeptId"    column="lend_dept_id"    />
        <result property="lendDeptManageId"    column="lend_dept_manage_id"    />
        <result property="registerTime"    column="register_time"    />
        <result property="useDescribe"    column="use_describe"    />
        <result property="useId"    column="use_id"    />
        <result property="capitalTotal"    column="capital_total"    />
        <result property="littleTotal"    column="little_total"    />
        <result property="dayRate"    column="day_rate"    />
        <result property="sumInterestDate"    column="sum_interest_date"    />
        <result property="expectedRepaymentDate"    column="expected_repayment_date"    />
        <result property="approvalId"    column="approval_id"    />
        <result property="debitStatus"    column="debit_status"    />
        <result property="attachmentUrl"    column="attachment_url"    />
        <result property="realPaymentDate"    column="real_payment_date"    />
        <result property="realPaymentAcount"    column="real_payment_acount"    />
        <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="debitDeptName"    column="debit_dept_name"    />
        <result property="operatorName"    column="operator_name"    />
        <result property="lendDeptName"    column="lend_dept_name"    />
        <result property="useName"    column="use_name"    />
        <result property="approvalName"    column="approval_name"    />
        <result property="lendManageName"    column="lend_dept_manage_name"    />
    </resultMap>

    <sql id="selectTDebitCreditVo">
        SELECT
            debit_id,
            debit_dept_id,
            operator_id,
            lend_dept_id,
            lend_dept_manage_id,
            register_time,
            use_describe,
            use_id,
            capital_total,
            little_total,
            day_rate,
            sum_interest_date,
            expected_repayment_date,
            approval_id,
            debit_status,
            attachment_url,
            real_payment_date,
            real_payment_acount,
            create_time,
            update_time,
            is_del,
            remark,
          (SELECT dept_name FROM sys_dept WHERE dept_id = debit_dept_id) AS debit_dept_name,
          (SELECT nick_name FROM sys_user WHERE user_id = operator_id) AS operator_name,
          (SELECT dept_name FROM sys_dept WHERE dept_id = lend_dept_id) AS lend_dept_name,
          (SELECT nick_name FROM sys_user WHERE user_id = use_id) AS use_name,
          (SELECT nick_name FROM sys_user WHERE user_id = approval_id) AS approval_name,
          (SELECT nick_name FROM sys_user WHERE user_id = lend_dept_manage_id) AS lend_dept_manage_name
        FROM
            t_debit_credit
    </sql>

    <select id="selectTDebitCreditList" parameterType="TDebitCredit" resultMap="TDebitCreditResult">
        <include refid="selectTDebitCreditVo"/>
        <where>  
            <if test="operatorId != null "> and operator_id = #{operatorId}</if>
            <if test="queryType == 'all' and debitDeptId != null and lendDeptId != null">
                and (debit_dept_id = #{debitDeptId} or lend_dept_id = #{lendDeptId})
            </if>
            <if test="queryType != 'all'">
                <if test="debitDeptId != null "> and debit_dept_id = #{debitDeptId}</if>
                <if test="lendDeptId != null "> and lend_dept_id = #{lendDeptId}</if>
            </if>
            <if test="registerTime != null "> and register_time = #{registerTime}</if>
            <if test="beginRegisterTime != null and endRegisterTime != null"> and register_time BETWEEN #{beginRegisterTime} and #{endRegisterTime}</if>
            <if test="useDescribe != null  and useDescribe != ''"> and use_describe = #{useDescribe}</if>
            <if test="useId != null "> and use_id = #{useId}</if>
            <if test="capitalTotal != null "> and capital_total = #{capitalTotal}</if>
            <if test="littleTotal != null "> and little_total = #{littleTotal}</if>
            <if test="dayRate != null "> and day_rate = #{dayRate}</if>
            <if test="sumInterestDate != null "> and sum_interest_date = #{sumInterestDate}</if>
            <if test="expectedRepaymentDate != null "> and expected_repayment_date = #{expectedRepaymentDate}</if>
            <if test="approvalId != null "> and approval_id = #{approvalId}</if>
            <if test="debitStatus != null  and debitStatus != ''"> and debit_status = #{debitStatus}</if>
            <if test="attachmentUrl != null  and attachmentUrl != ''"> and attachment_url = #{attachmentUrl}</if>
            <if test="realPaymentDate != null "> and real_payment_date = #{realPaymentDate}</if>
            <if test="realPaymentAcount != null "> and real_payment_acount = #{realPaymentAcount}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
            <if test="queryDebitDeptId != null "> and debit_dept_id = #{queryDebitDeptId}</if>
            <if test="queryLendDeptId != null "> and lend_dept_id = #{queryLendDeptId}</if>
        </where>
        ORDER BY create_time DESC
    </select>
    
    <select id="selectTDebitCreditById" parameterType="Long" resultMap="TDebitCreditResult">
        <include refid="selectTDebitCreditVo"/>
        where debit_id = #{debitId}
    </select>
        
    <insert id="insertTDebitCredit" parameterType="TDebitCredit" useGeneratedKeys="true" keyProperty="debitId">
        insert into t_debit_credit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="debitDeptId != null">debit_dept_id,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="lendDeptId != null">lend_dept_id,</if>
            <if test="lendDeptManageId != null">lend_dept_manage_id,</if>
            <if test="registerTime != null">register_time,</if>
            <if test="useDescribe != null">use_describe,</if>
            <if test="useId != null">use_id,</if>
            <if test="capitalTotal != null">capital_total,</if>
            <if test="littleTotal != null">little_total,</if>
            <if test="dayRate != null">day_rate,</if>
            <if test="sumInterestDate != null">sum_interest_date,</if>
            <if test="expectedRepaymentDate != null">expected_repayment_date,</if>
            <if test="approvalId != null">approval_id,</if>
            <if test="debitStatus != null">debit_status,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="realPaymentDate != null">real_payment_date,</if>
            <if test="realPaymentAcount != null">real_payment_acount,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="debitDeptId != null">#{debitDeptId},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="lendDeptId != null">#{lendDeptId},</if>
            <if test="lendDeptManageId != null">#{lendDeptManageId},</if>
            <if test="registerTime != null">#{registerTime},</if>
            <if test="useDescribe != null">#{useDescribe},</if>
            <if test="useId != null">#{useId},</if>
            <if test="capitalTotal != null">#{capitalTotal},</if>
            <if test="littleTotal != null">#{littleTotal},</if>
            <if test="dayRate != null">#{dayRate},</if>
            <if test="sumInterestDate != null">#{sumInterestDate},</if>
            <if test="expectedRepaymentDate != null">#{expectedRepaymentDate},</if>
            <if test="approvalId != null">#{approvalId},</if>
            <if test="debitStatus != null">#{debitStatus},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="realPaymentDate != null">#{realPaymentDate},</if>
            <if test="realPaymentAcount != null">#{realPaymentAcount},</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>
         </trim>
    </insert>

    <update id="updateTDebitCredit" parameterType="TDebitCredit">
        update t_debit_credit
        <trim prefix="SET" suffixOverrides=",">
            <if test="debitDeptId != null">debit_dept_id = #{debitDeptId},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="lendDeptId != null">lend_dept_id = #{lendDeptId},</if>
            <if test="lendDeptManageId != null">lend_dept_manage_id = #{lendDeptManageId},</if>
            <if test="registerTime != null">register_time = #{registerTime},</if>
            <if test="useDescribe != null">use_describe = #{useDescribe},</if>
            <if test="useId != null">use_id = #{useId},</if>
            <if test="capitalTotal != null">capital_total = #{capitalTotal},</if>
            <if test="littleTotal != null">little_total = #{littleTotal},</if>
            <if test="dayRate != null">day_rate = #{dayRate},</if>
            <if test="sumInterestDate != null">sum_interest_date = #{sumInterestDate},</if>
            <if test="expectedRepaymentDate != null">expected_repayment_date = #{expectedRepaymentDate},</if>
            <if test="approvalId != null">approval_id = #{approvalId},</if>
            <if test="debitStatus != null">debit_status = #{debitStatus},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="realPaymentDate != null">real_payment_date = #{realPaymentDate},</if>
            <if test="realPaymentAcount != null">real_payment_acount = #{realPaymentAcount},</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>
        </trim>
        where debit_id = #{debitId}
    </update>

    <delete id="deleteTDebitCreditById" parameterType="Long">
        delete from t_debit_credit where debit_id = #{debitId}
    </delete>

    <delete id="deleteTDebitCreditByIds" parameterType="String">
        delete from t_debit_credit where debit_id in 
        <foreach item="debitId" collection="array" open="(" separator="," close=")">
            #{debitId}
        </foreach>
    </delete>
</mapper>