<?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.entrustOrder.mapper.BizEntrustOrderMapper">

    <resultMap type="cn.ezhizao.project.business.entrustOrder.domain.BizEntrustOrder" id="BizEntrustOrderResult">
        <id column="id" property="id"/>
    </resultMap>


    <select id="getList" parameterType="BizEntrustOrder" resultMap="BizEntrustOrderResult">
        SELECT t1.*, t2.account_name signing_subject, t3.account_name as contact_company
        , (select sum(amount) from biz_deposit where biz_deposit.status = 1 and biz_deposit.deleted = 0 and biz_deposit.entrust_order_id = t1.id) as deposit_amount
        , (select sum(remain_amount) from biz_deposit where biz_deposit.status = 1 and biz_deposit.deleted = 0 and biz_deposit.entrust_order_id = t1.id) as remain_amount
        FROM biz_entrust_order t1
        left join biz_tenant t2 on t2.id = t1.tenant_id
        left join biz_tenant t3 on t3.id = t1.from_tenant_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="id != null "> AND t1.id = #{id}</if>
            <if test="fromTenantId != null "> AND t1.from_tenant_id = #{fromTenantId}</if>
            <if test="tenantId != null "> AND (t1.tenant_id = #{tenantId} or t1.from_tenant_id = #{tenantId})</if>
            <if test="formNo != null  and formNo != ''"> AND t1.form_no = #{formNo}</if>
            <if test="contactCompany != null and contactCompany != ''">AND t3.account_name like concat('%', #{contactCompany}, '%')</if>
            <if test="formDate != null "> AND t1.form_date = #{formDate}</if>
            <if test="status != null "> AND t1.status = #{status}</if>
        </trim>
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_entrust_order
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
