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

    <resultMap type="cn.ezhizao.project.business.domain.BizOutsourcedOrderDetail" id="BizOutsourcedOrderDetailResult">
        <id column="id" property="id"/>
        <association property="processNames" column="id" select="getProcessNames"/>
        <association property="receiptNum" column="id" select="getReceiptNum"/>
        <collection property="outsourcedOrderDetailProcessList" column="id" ofType="cn.ezhizao.project.business.domain.BizOutsourcedOrderDetailProcess" select="getOutsourcedOrderDetailProcessList"/>
        <collection property="returnReceiptDetailList" column="id" ofType="cn.ezhizao.project.business.domain.BizReturnReceiptDetail" select="getReturnReceiptDetailList"/>
    </resultMap>

    <select id="getList" parameterType="BizOutsourcedOrderDetail" resultMap="BizOutsourcedOrderDetailResult">
        SELECT * FROM biz_outsourced_order_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="formCode != null  and formCode != ''"> AND form_code = #{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="deliveryMethod != null "> AND delivery_method = #{deliveryMethod}</if>
            <if test="packagingMethod != null  and packagingMethod != ''"> AND packaging_method = #{packagingMethod}</if>
            <if test="productDescription != null  and productDescription != ''"> AND product_description = #{productDescription}</if>
            <if test="technologyVersion != null  and technologyVersion != ''"> AND technology_version = #{technologyVersion}</if>
            <if test="originalCarrier != null  and originalCarrier != ''"> AND original_carrier = #{originalCarrier}</if>
            <if test="originalCarrierCount != null "> AND original_carrier_count = #{originalCarrierCount}</if>
            <if test="newCarrier != null  and newCarrier != ''"> AND new_carrier = #{newCarrier}</if>
            <if test="newCarrierCount != null "> AND new_carrier_count = #{newCarrierCount}</if>
        </trim>
    </select>
    <select id="getFinishedOutsourceDetailList" parameterType="BizOutsourcedOrderDetail" resultMap="BizOutsourcedOrderDetailResult">
        SELECT distinct t1.id,t1.lot_code,t1.product_description,t1.original_carrier,t1.new_carrier,
        (select form_date from biz_return_receipt_detail where biz_return_receipt_detail.outsource_detail_id = t1.id and biz_return_receipt_detail.deleted = 0 and t1.deleted = 0 limit 1) as form_date
        FROM biz_outsourced_order_detail t1 left join biz_outsourced_order_detail_process t2 on t1.id = t2.detail_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.status = 1
            <if test="formCode != null  and formCode != ''"> AND t1.form_code = #{formCode}</if>
            <if test="startTime != null "> AND (
                SELECT brrd.form_date
                FROM biz_return_receipt_detail brrd
                WHERE brrd.outsource_detail_id = t1.id
                AND brrd.deleted = 0
                AND t1.deleted = 0
                LIMIT 1
                ) >= #{startTime}</if>
            <if test="endTime != null "> AND (
                SELECT brrd.form_date
                FROM biz_return_receipt_detail brrd
                WHERE brrd.outsource_detail_id = t1.id
                AND brrd.deleted = 0
                AND t1.deleted = 0
                LIMIT 1
                ) &lt;= #{endTime}</if>
            <if test="notFinishedDetailIds != null and notFinishedDetailIds.size > 0">
                AND t1.id NOT IN
                <foreach collection="notFinishedDetailIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="supplierName != null  and supplierName != ''"> AND t1.supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="lotCode != null "> AND t1.lot_code like concat('%', #{lotCode}, '%')</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="originalCarrier != null  and originalCarrier != ''"> AND t1.original_carrier = #{originalCarrier}</if>
            <if test="originalCarrierCount != null "> AND t1.original_carrier_count = #{originalCarrierCount}</if>
            <if test="newCarrier != null  and newCarrier != ''"> AND t1.new_carrier = #{newCarrier}</if>
            <if test="newCarrierCount != null "> AND t1.new_carrier_count = #{newCarrierCount}</if>
        </trim>
        order by t1.create_time desc
    </select>
    <select id="getOutsourcedOrderDetailProcessList" resultType="cn.ezhizao.project.business.domain.BizOutsourcedOrderDetailProcess">
        select * from biz_outsourced_order_detail_process
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
        deleted = 0
        <if test="id != null"> AND detail_id = #{id}</if>
    </trim>
        order by process_step_number
    </select>
    <select id="getReturnReceiptDetailList" resultType="cn.ezhizao.project.business.domain.BizReturnReceiptDetail">
        select * from biz_return_receipt_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="id != null"> AND outsource_detail_id = #{id}</if>
        </trim>
        order by process_step_number
    </select>

    <select id="getProcessNames" resultType="String">
        SELECT
            GROUP_CONCAT(t.process_alias)
        FROM biz_outsourced_order_detail_process t
        WHERE t.detail_id = #{id}
    </select>
    <select id="getReceiptNum" resultType="Integer">
        SELECT t1.qualified_num as receiptNum
        FROM biz_outsourced_order_detail_process t1
        WHERE t1.detail_id = #{id} order by t1.process_step_number desc limit 1
    </select>
    <select id="getListForReceipt" parameterType="BizOutsourcedOrderDetail" resultMap="BizOutsourcedOrderDetailResult">
        SELECT
            t1.*
        FROM biz_outsourced_order_detail t1 left join biz_outsourced_order t2 on t1.master_id = t2.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.deleted = 0 and t2.is_submit = 1
            <if test="existingOutsourceDetailIds != null and existingOutsourceDetailIds.size > 0">
                AND t1.id NOT IN
                <foreach collection="existingOutsourceDetailIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="notFinishedDetailIds != null and notFinishedDetailIds.size > 0">
                AND t1.id NOT IN
                <foreach collection="notFinishedDetailIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="alreadyAddedDetailIds != null and alreadyAddedDetailIds.size > 0">
                AND t1.id NOT IN
                <foreach collection="alreadyAddedDetailIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="productDescription != null  and productDescription != ''"> AND t1.product_description LIKE CONCAT('%', #{productDescription}, '%')</if>
            <if test="originalCarrier != null "> AND t1.original_carrier LIKE CONCAT('%', #{originalCarrier}, '%')</if>
            <if test="newCarrier != null "> AND t1.new_carrier LIKE CONCAT('%', #{newCarrier}, '%')</if>
            <if test="startTime != null ">AND form_date &gt; #{startTime}</if>
            <if test="endTime != null ">AND form_date &lt; #{endTime}</if>
            <if test="supplierId != null "> AND t1.supplier_id = #{supplierId}</if>
        </trim>
    </select>

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