<?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,(select new_carrier_name from biz_outsourced_order_detail where t1.detail_id
        =biz_outsourced_order_detail.id and biz_outsourced_order_detail.deleted = 0 ) as newCarrierName
        , t2.is_first_process FROM biz_outsourced_order_detail_process t1
        right join biz_outsourced_order t2 on t1.master_id = t2.id
        left join biz_supplier t3 on t3.id = t2.supplier_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.is_submit = 1 and t2.deleted = 0
            <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="masterId != null ">AND t1.master_id = #{masterId}</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>
            <if test="newCarrierName != null">AND case when t3.is_inner_outsource = 1 then
                (exists (select 1 from biz_daywork_carrier t where t.daywork_id = t1.daywork_id and t.is_changed = 0 and t.carrier_code like concat('%', #{newCarrierName}, '%')))
                else
                (exists (select 1
                FROM
                biz_outsourced_order_detail where t1.detail_id =biz_outsourced_order_detail.id
                and new_carrier_name like concat('%',#{newCarrierName},'%')
                )) end
            </if>
            <if test="status != null  ">AND t1.status = #{status}</if>
            <if test="returnStatus ==0">AND t1.detail_id not in
                <if test="hasReturnDetailId.size > 0">
                    <foreach collection="hasReturnDetailId" index="index" item="id" separator="," open="(" close=")">
                        #{id,jdbcType=BIGINT}
                    </foreach>
                </if>
                <if test="hasReturnDetailId.size == 0">
                    ( 0 )
                </if>
            </if>
            <if test="returnStatus ==1  and hasReturnDetailId.size > 0 ">AND t1.detail_id in
                <foreach collection="hasReturnDetailId" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
        </trim>
        order by t1.form_date ,t1.lot_code,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>
