<?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.BizOutsourcedOrderDetailProcessMapper">

    <resultMap type="cn.ezhizao.project.business.domain.BizOutsourcedOrderDetailProcess" id="BizOutsourcedOrderDetailProcessResult">
        <id column="id" property="id"/>
        <association property="auditNum" column="{processStepNumber=process_step_number,lotId=lot_id}" select="getReturnReceiptDetailAuditNum"/>
        <collection property="returnReceiptDetails" ofType="cn.ezhizao.project.business.domain.BizReturnReceiptDetail" column="{processStepNumber=process_step_number,lotId=lot_id}" select="getReturnReceiptDetailList"/>
    </resultMap>


    <select id="getList" parameterType="BizOutsourcedOrderDetailProcess" resultMap="BizOutsourcedOrderDetailProcessResult">
        SELECT t1.*,t2.loss_limit FROM biz_outsourced_order_detail_process t1 right join biz_outsourced_order t2 on t1.master_id = t2.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.is_submit = 1
            <if test="formCode != null  and formCode != ''"> AND t1.form_code like concat('%', #{formCode}, '%')</if>
            <if test="lotCode != null  and lotCode != ''"> AND t1.lot_code like concat('%', #{lotCode}, '%')</if>
            <if test="startTime != null ">AND t1.form_date &gt;= #{startTime}</if>
            <if test="endTime != null ">AND t1.form_date &lt;= #{endTime}</if>
            <if test="supplierId != null "> AND t1.supplier_id = #{supplierId}</if>
            <if test="supplierName != null  and supplierName != ''"> AND t1.supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="deliveryMethod != null "> AND t1.delivery_method = #{deliveryMethod}</if>
            <if test="packagingMethod != null  and packagingMethod != ''"> AND t1.packaging_method = #{packagingMethod}</if>
            <if test="productDescription != null  and productDescription != ''"> AND t1.product_description like concat('%', #{productDescription}, '%')</if>
            <if test="technologyVersion != null  and technologyVersion != ''"> AND t1.technology_version = #{technologyVersion}</if>
            <if test="processAlias != null  and processAlias != ''"> AND t1.process_alias = #{processAlias}</if>
        </trim>
        order by t1.form_code desc ,t1.process_step_number
    </select>
    <select id="getDetailList" resultMap="BizOutsourcedOrderDetailProcessResult">
        select t1.*,t2.original_carrier,t2.new_carrier,t1.qualified_num as receipt_num,t2.original_carrier_count,t2.new_carrier_count  from biz_outsourced_order_detail_process t1
        left join biz_outsourced_order_detail t2 on t1.detail_id = t2.id
        where t1.deleted = 0 and t2.deleted = 0 and t1.detail_id = #{id} order by t1.process_step_number
    </select>
    <select id="getReturnReceiptDetailAuditNum" resultType="Integer">
        SELECT t2.audit_num FROM biz_outsourced_order_detail_process t1 left join biz_return_receipt_detail t2 on t1.detail_id = t2.outsource_detail_id and t1.lot_id = t2.lot_id
        WHERE t1.process_step_number = #{processStepNumber} AND  t1.lot_id = #{lotId} and t2.process_step_number = #{processStepNumber} AND  t2.lot_id = #{lotId} and t1.deleted = 0 and t2.deleted = 0
    </select>
    <select id="getReturnReceiptDetailList" resultType="cn.ezhizao.project.business.domain.BizReturnReceiptDetail">
        select * from biz_return_receipt_detail where lot_id = #{lotId} and process_step_number = #{processStepNumber} and deleted = 0
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_outsourced_order_detail_process
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
