<?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="cn.ezhizao.project.business.deposit.mapper.BizDepositMapper">

    <resultMap type="cn.ezhizao.project.business.deposit.domain.BizDeposit" id="BizDepositResult">
        <id column="id" property="id"/>
        <collection property="files" column="id" select="getFiles" />
    </resultMap>


    <resultMap id="FileResultMap" type="cn.ezhizao.project.system.domain.SysFileStorage">
        <id column="id" jdbcType="INTEGER" property="id"/>
    </resultMap>

    <select id="getFiles" resultMap="FileResultMap">
        select *
        from sys_file_storage
        where master_id = #{id} and master_table_name = 'biz_deposit'
    </select>

    <select id="getList" parameterType="BizDeposit" resultMap="BizDepositResult">
        SELECT t1.*, t4.form_no, t2.account_name signing_subject, t3.account_name as contact_company FROM biz_deposit t1 left join biz_tenant t2 on t2.id = t1.tenant_id left join biz_tenant t3 on t3.id = t1.from_tenant_id
        left join biz_entrust_order t4 on t4.id = t1.entrust_order_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="id != null ">AND t1.id = #{id}</if>
            <if test="tenantId != null "> AND (t1.tenant_id = #{tenantId} or t1.from_tenant_id = #{tenantId})</if>
            <if test="amount != null ">AND t1.amount = #{amount}</if>
            <if test="remainAmount != null ">AND t1.remain_amount = #{remainAmount}</if>
            <if test="type != null ">AND t1.type = #{type}</if>
            <if test="depositDate != null ">AND t1.deposit_date = #{depositDate}</if>
            <if test="status != null ">AND t1.status = #{status}</if>
            <if test="verifyDate != null ">AND t1.verify_date = #{verifyDate}</if>
            <if test="verifyRemark != null  and verifyRemark != ''">AND t1.verify_remark = #{verifyRemark}</if>
            <if test="verifierId != null ">AND t1.verifier_id = #{verifierId}</if>
        </trim>
    </select>

    <select id="getTotalRemain" parameterType="BizDeposit" resultType="java.math.BigDecimal">
        SELECT sum(remain_amount) FROM biz_deposit
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and type = 1 and status = 1
            <if test="id != null ">AND id = #{id}</if>
            <if test="tenantId != null ">AND from_tenant_id = #{tenantId}</if>
            <if test="entrustOrderId != null ">AND entrust_order_id = #{entrustOrderId}</if>
        </trim>
    </select>


    <select id="getDonateRemain" parameterType="BizDeposit" resultType="java.math.BigDecimal">
        SELECT sum(remain_amount) FROM biz_deposit
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and type = 2 and status = 1
            <if test="id != null ">AND id = #{id}</if>
            <if test="tenantId != null ">AND from_tenant_id = #{tenantId}</if>
            <if test="entrustOrderId != null ">AND entrust_order_id = #{entrustOrderId}</if>
        </trim>
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_deposit
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
            <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
