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

    <resultMap type="cn.ezhizao.project.business.domain.BizReturnReceiptDetail" id="BizReturnReceiptDetailResult">
        <id column="id" property="id"/>
        <result column="outsource_detail_id" property="outsourceDetailId"/>
        <association property="productionDeptId" column="outsource_detail_id" select="selectProductionDeptId"/>
    </resultMap>


    <select id="getList" parameterType="BizReturnReceiptDetail" resultMap="BizReturnReceiptDetailResult">
        SELECT * FROM biz_return_receipt_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;= #{startTime}</if>
            <if test="supplierName != null  and supplierName != ''"> AND supplier_name like concat('%', #{supplierName}, '%')</if>
            <if test="lotCode != null  and lotCode != ''"> AND lot_code like concat('%', #{lotCode}, '%')</if>
            <if test="productDescription != null  and productDescription != ''"> AND product_description like concat('%', #{productDescription}, '%')</if>
            <if test="productNum != null "> AND product_num = #{productNum}</if>
            <if test="receiptNum != null "> AND receipt_num = #{receiptNum}</if>
            <if test="auditNum != null "> AND audit_num = #{auditNum}</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>
        order by create_time desc
    </select>
    <select id="selectProductionDeptId" resultType="long">
        SELECT
        production_dept_id
        FROM
        biz_outsourced_order_detail
        WHERE
        deleted = 0
        AND id = #{outsource_detail_id}
    </select>
    <select id="getTurnoverDeptList" resultType="cn.ezhizao.project.system.domain.SysDept">
        SELECT DISTINCT dp.dept_id as value, d.dept_name as label,d.is_sort_packaging,d.tenant_id
        FROM sys_dept d
        INNER JOIN biz_dept_process dp ON d.dept_id = dp.dept_id
        INNER JOIN
        (SELECT DISTINCT btpd.process_id
        FROM biz_technological_process_detail btpd
        INNER JOIN biz_production_plan_detail bppd
        ON bppd.technological_process_id = btpd.technological_process_id
        WHERE bppd.id = #{productionPlanDetailId} and btpd.tenant_id !=9  and btpd.process_step_number > #{processStepNumber}) AS m ON m.process_id = dp.process_id where( d.is_work_section = 1 or d.dept_code = '170000')
        <if test="tenantId != null and tenantId != 0"> AND d.tenant_id = #{tenantId}</if>
        UNION
        SELECT dept_id as value, dept_name as label,is_sort_packaging,tenant_id
        FROM sys_dept
        WHERE dept_code = '170000'
        <if test="tenantId != null and tenantId != 0"> AND tenant_id = #{tenantId}</if>
<!--        AND d.tenant_id = #{tenantId}-->
    </select>
    <select id="getTurnoverAbDeptList" resultType="cn.ezhizao.project.system.domain.SysDept">
        SELECT DISTINCT dp.dept_id as value, d.dept_name as label,d.is_sort_packaging
        FROM sys_dept d
        INNER JOIN biz_dept_process dp ON d.dept_id = dp.dept_id
        INNER JOIN
        (SELECT DISTINCT btwrd.process_id
        FROM biz_lot_technological_process_detail btwrd
        WHERE btwrd.lot_technological_process_id = #{technologicalProcessId} and btwrd.process_step_number >#{processStepNumber}) AS m ON m.process_id = dp.process_id AND d.is_work_section = 1
        UNION
        SELECT dept_id as value, dept_name as label,is_sort_packaging
        FROM sys_dept
        WHERE dept_code = '170000'
        <if test="tenantId != null and tenantId != 0"> AND tenant_id = #{tenantId}</if>
<!--        AND d.tenant_id = #{tenantId}-->
    </select>

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