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

    <resultMap type="cn.ezhizao.project.business.domain.BizReturnReceipt" id="BizReturnReceiptResult">
        <id column="id" property="id"/>
        <collection property="outsourcedOrderDetails" ofType="cn.ezhizao.project.business.domain.BizOutsourcedOrderDetail" column="id" select="getDetails"/>
        <collection property="processInspecionList" ofType="cn.ezhizao.project.business.domain.BizProcessInspecion" column="id" select="getProcessInspection"/>
    </resultMap>
    <select id="getList" parameterType="BizReturnReceipt" resultMap="BizReturnReceiptResult">
        SELECT * FROM biz_return_receipt
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and is_submit = 1
            <if test="formCode != null  and formCode != ''"> AND form_code like concat('%', #{formCode}, '%')</if>
            <if test="startTime != null ">AND form_date &gt;= #{startTime}</if>
            <if test="endTime != null ">AND form_date &lt;= #{endTime}</if>
            <if test="supplierName != null  and supplierName != ''"> AND supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="status != null "> AND status = #{status}</if>
            <if test="tenantId != null and tenantId != 0 "> AND tenant_id = #{tenantId}</if>
        </trim>
        order by create_time desc
    </select>
    <select id="getAllList" resultType="cn.ezhizao.project.business.domain.BizReturnReceipt">
        select * from biz_return_receipt order by form_code desc
    </select>
    <select id="getDetails" resultType="cn.ezhizao.project.business.domain.BizOutsourcedOrderDetail">
        select * from biz_outsourced_order_detail t1 left join biz_return_receipt_detail t2 on t1.id = t2.outsource_detail_id
        left join biz_return_receipt t3 on t2.master_id = t3.id where t3.id = #{id} and t1.deleted = 0 and t2.deleted = 0 and t3.deleted = 0
    </select>
    <select id="getProcessInspection" resultType="cn.ezhizao.project.business.domain.BizProcessInspecion">
        select * from biz_process_inspecion t1 left join biz_outsourced_order_detail t2 on t1.outsource_order_detail_id = t2.id
        left join biz_return_receipt_detail t3 on t2.id = t3.outsource_detail_id left join biz_return_receipt t4 on t3.master_id = t4.id where t4.id = #{id} and t1.deleted = 0 and t2.deleted = 0
        and t3.deleted = 0 and t4.deleted = 0
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_return_receipt
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
