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

    <resultMap type="cn.ezhizao.project.business.domain.BizDaywork" id="BizDayworkResult">
        <id column="id" property="id"/>
        <result column="lot_id" property="lotId"/>
        <result column="production_plan_detail_id" property="productionPlanDetailId"/>
        <result column="production_plan_detail_sub_detail_id" property="productionPlanDetailSubDetailId"/>
        <result column="technological_process_id" property="technologicalProcessId"/>
        <result column="lot_code" property="lotCode"/>
        <result column="description" property="productDescription"/>
        <result column="furnace_number" property="furnaceNumber"/>
        <result column="production_quantity" property="productionQuantity"/>
        <result column="carrier_name" property="carrierName"/>
        <result column="dept_id" property="deptId"/>
        <result column="product_id" property="productId"/>
        <result column="process_qualified_num" property="processQualifiedNum"/>
        <association property="productCode" column="product_id" javaType="java.lang.String"
                     select="getProductCode"/>
        <association property="furnaceNumber" column="id" javaType="java.lang.String"
                     select="getFurnaceNumber"/>
        <association property="lastLotQuantity" javaType="java.lang.Integer" column="production_plan_detail_id"
                     select="getLastLotQuantity"/>
        <association property="totalWorkingHours" column="{id=id,deptId=dept_id}" select="getTotalWorkingHours"/>
        <collection property="dayworkCarriers" column="id" ofType="cn.ezhizao.project.business.domain.BizDayworkCarrier"
                    select="getDayworkCarrierList"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.domain.BizDaywork" id="BizDayworkResultForOutsource">
        <id column="id" property="id"/>
        <collection property="originalCarrierList" ofType="cn.ezhizao.project.business.domain.BizDayworkCarrier"
                    column="id" select="getOriginalCarrierList"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.domain.BizDaywork" id="BizDayworkResultForInnerOutsource">
        <id column="id" property="id"/>
        <association property="lastDayworkItem" column="id" select="getLastDayworkItem"/>
        <collection property="originalCarrierList" ofType="cn.ezhizao.project.business.domain.BizDayworkCarrier"
                    column="id" select="getOriginalCarrierList"/>
    </resultMap>

    <resultMap id="BizDayworkItemResult" type="cn.ezhizao.project.business.domain.BizDayworkItem">
        <id column="id" property="id"/>
        <result column="process_alias" property="processAlias"/>
        <result column="prod_num" property="prodNum"/>
        <result column="daywork_id" property="dayworkId"/>
        <result column="equipment_detail_id" property="equipmentDetailId"/>
        <result column="equipment_detail_code" property="equipmentDetailCode"/>
        <result column="lot_id" property="lotId"/>
        <result column="production_plan_detail_id" property="productionPlanDetailId"/>
        <result column="technological_process_detail_id" property="technologicalProcessDetailId"/>
        <association property="lotCode" column="lot_id" javaType="java.lang.String" select="getLotCode"/>
        <association property="processAlias" column="technological_process_detail_id" javaType="java.lang.String"
                     select="getProcessName"/>
        <association property="technologyVersion" column="lot_id" javaType="java.lang.String"
                     select="getTechnologyVersion"/>
        <association property="productDescription" column="production_plan_detail_id" javaType="java.lang.String"
                     select="getProductDescription"/>
        <association property="rejectNum" column="id" javaType="java.lang.String" select="getRejectNum" />
        <association property="originalRejectNum" column="id"  select="getOringinalRejectNum" />
        <association property="firstProcessId" column="daywork_id" select="getFirstProcess"/>
    </resultMap>

    <resultMap id="BizDayworkItemResultForOutsource" type="cn.ezhizao.project.business.domain.BizDayworkItem">
        <id column="id" property="id"/>
    </resultMap>

    <resultMap type="cn.ezhizao.project.business.domain.BizDaywork" id="BizLotResult">
        <id column="id" property="id"/>
        <result column="lot_id" property="lotId"/>
        <result column="production_plan_detail_id" property="productionPlanDetailId"/>
        <result column="production_plan_detail_sub_detail_id" property="productionPlanDetailSubDetailId"/>
        <result column="technological_process_id" property="technologicalProcessId"/>
        <result column="lot_code" property="lotCode"/>
        <result column="from_code" property="fromCode"/>
        <result column="from_id" property="fromId"/>
        <result column="description" property="productDescription"/>
        <result column="furnace_number" property="furnaceNumber"/>
        <result column="production_quantity" property="productionQuantity"/>
        <result column="carrier_name" property="carrierName"/>
        <result column="dept_id" property="deptId"/>
        <association property="lastLotQuantity" javaType="java.lang.Integer" column="production_plan_detail_id"
                     select="getLastLotQuantity"/>
        <association property="processAlias" column="id" select="getProcessAlias"/>
        <!--        <association property="lotWaste" column="lot_id" select="getLotWaste"/>-->
        <association property="totalWorkingHours" column="{id=id,deptId=dept_id}" select="getTotalWorkingHours"/>
    </resultMap>
    <select id="getDayworkInfo" resultMap="BizLotResult">
        select t1.id AS lot_id,t1.lot_code,t1.technology_version, t5.is_history,t1.production_quantity,(select lot_code FROM biz_lot
        where from_id = id) as
        fromName,t1.is_waste_recycling,t1.is_fallback,t1.is_waste,t2.company_alias,t2.production_plan_no,t2.product_description
        AS description,t3.id,t3.create_time,t3.temporary_process_qualified_num,t4.dept_name,t4.dept_id FROM biz_lot t1
        inner join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        left join biz_technological_process t5 on t5.id = t1.technological_process_id
        LEFT JOIN biz_daywork t3 on t1.id = t3.lot_id
        LEFT JOIN sys_dept t4 on t3.dept_id = t4.dept_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t2.production_plan_no = #{productionPlanNo} AND t2.line_number = #{lineNumber}
            <if test="deptId != null  AND deptId != 0">AND t3.dept_id = #{deptId}</if>
            <if test="productDescription != null  AND productDescription != ''">AND t2.product_description like
                concat('%',#{productDescription},'%')
            </if>
            <if test="lotCode != null  AND lotCode != ''">AND t1.lot_code like concat('%',#{lotCode},'%')</if>
            <if test="isWasteRecycling != null ">AND t1.is_waste_recycling = #{isWasteRecycling}</if>
            <if test="isFallback != null ">AND t1.is_fallback = #{isFallback}</if>
        </trim>
    </select>
    <select id="getDayworkCarrierList" resultType="cn.ezhizao.project.business.domain.BizDayworkCarrier">
        select t1.carrier_id,t1.carrier_code,COALESCE((SELECT t4.is_abandoned FROM biz_carrier_reject t4 WHERE t3.id =
        t4.carrier_id ORDER BY t4.create_time DESC LIMIT 1), 0) AS is_abandoned from biz_daywork_carrier t1
        left join biz_carrier t3 on t1.carrier_id = t3.id
        where t1.is_changed = 0 and t1.deleted = 0 and t3.deleted = 0 and t3.is_inspection != 1
        and t3.is_abandoned = 0
        and t1.daywork_id = #{id}
    </select>
    <select id="getProducedLot" resultMap="BizLotResult">
        SELECT DISTINCT
        t2.id AS lot_id,
        t2.lot_code,
        t2.technology_version,
        t5.is_history,
        t2.production_quantity,
        t2.from_id,
        (select lot_code from biz_lot where t2.from_id =biz_lot.id ) as from_code,
        t2.is_waste_recycling,
        t2.is_superaddition,
        t2.is_amend,
        t2.is_fallback,
        t2.is_waste,
        t3.company_alias,
        t3.production_plan_no,
        t3.product_description AS description,
        t1.id,
        t1.create_time,
        t1.temporary_process_qualified_num,
        t4.dept_name,
        t4.dept_id
        FROM biz_daywork t1
        LEFT JOIN biz_lot t2 on t2.id = t1.lot_id
        INNER JOIN biz_production_plan_detail t3 on t2.production_plan_detail_id = t3.id
        LEFT JOIN sys_dept t4 on t1.dept_id = t4.dept_id
        left join biz_technological_process t5 on t5.id = t2.technological_process_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t3.production_plan_no = #{productionPlanNo} AND t3.line_number = #{lineNumber}
            <if test="deptId != null ">AND t1.dept_id = #{deptId}</if>
            <if test="productDescription != null and productDescription != ''">
                AND t3.product_description like concat('%',#{productDescription},'%')
            </if>
            <if test="lotCode != null  and lotCode != ''">AND t2.lot_code like concat('%',#{lotCode},'%')</if>
            <if test="technologyVersion != null  and technologyVersion != ''">AND t2.technology_version =
                #{technologyVersion}
            </if>
            <if test="processId != null">AND (select process_id from biz_daywork_item where lot_id = t2.id order by
                create_time desc limit 1) = #{processId}
            </if>
            <if test="flags != null and flags.size() > 0">
                AND
                <foreach collection="flags" item="flag" separator=" OR " open="(" close=")">
                    <if test="flag != null and flag == 2">t2.is_amend= 1</if>
                    <if test="flag != null and flag == 1">t2.is_waste_recycling= 1</if>
                    <if test="flag != null and flag == 0">t2.is_waste= 1</if>
                    <if test="flag != null and flag == 4"> t2.is_superaddition= 1</if>
                    <if test="flag != null and flag == 3">t2.is_waste= 0 and t2.is_waste_recycling= 0 and t2.is_amend =
                        0 and t2.is_superaddition = 0
                    </if>
                </foreach>
            </if>
        </trim>
    </select>
    <!--    <select id="getLotWaste" resultType="cn.ezhizao.project.business.domain.BizLotWaste">
            select * FROM biz_lot_waste t1 where t1.lot_id = #{lotId} AND t1.deleted = 0
        </select>-->
    <select id="getFirstProcess" resultType="long">
        SELECT process_id AS firstProcessId
        FROM biz_daywork_item
        WHERE daywork_id = #{dayworkId}
        ORDER BY create_time
        LIMIT 1
    </select>
    <select id="getProcessAlias" resultType="String">
        SELECT process_alias
        FROM biz_process t1
        LEFT JOIN biz_daywork_item t2 ON t1.id =
        t2.process_id
        WHERE t2.daywork_id = #{id}
        ORDER BY t2.create_time DESC
        LIMIT 1
    </select>
    <select id="getTotalWorkingHours" resultType="long">
        SELECT sum(working_hours)
        FROM biz_daywork_item
        WHERE daywork_id = #{id}
        AND dept_id = #{deptId}
        AND deleted = 0
    </select>
    <select id="getLastLotQuantity" resultType="java.lang.Integer">
        SELECT last_lot_quantity
        FROM biz_production_plan_detail
        WHERE deleted = 0
        AND id = #{productionPlanDetailId}
    </select>

    <select id="getOringinalRejectNum" resultType="int">
        SELECT sum(reject_num) rejectNum
        FROM biz_daywork_item_reject
        WHERE daywork_item_id = #{id} and daywork_auxiliary_item_id = 0
        AND deleted = 0
    </select>
    <select id="getRejectNum" resultType="int">
        SELECT sum(reject_num) rejectNum
        FROM biz_daywork_item_reject
        WHERE daywork_item_id = #{id}
        AND deleted = 0
    </select>
    <select id="getProcessList" resultType="cn.ezhizao.project.business.domain.BizProcess">
        SELECT id AS Value, process_alias AS Label
        FROM biz_process
        WHERE deleted = 0
        AND tenant_id != 9
        AND id IN (SELECT process_id
        FROM biz_technological_process_detail
        WHERE deleted = 0
        AND tenant_id != 9
        AND technological_process_id = #{technologicalProcessId})
    </select>
<!--    <select id="getList" resultMap="BizDayworkResult">-->
<!--        SELECT-->
<!--        t1.*,-->
<!--        t4.production_plan_no as productionPlanNo,-->
<!--        t4.company_alias as companyAlias,-->
<!--        t2.description,-->
<!--        t4.furnace_number,-->
<!--        t3.production_quantity,-->
<!--        t4.technology_version as technologyVersion,-->
<!--        t4.one_lot_quantity as oneLotQuantity,-->
<!--        t5.dept_name,-->
<!--        t1.is_waste_recycling,-->
<!--        t1.is_amend,-->
<!--        t4.total_lot_number,-->
<!--        &lt;!&ndash; 完工日期 &ndash;&gt;-->
<!--        (select end_time from biz_daywork_item item left join biz_process bp on bp.id = item.process_id where item.daywork_id = t1.id and item.status = 3 and bp.process_code = '99000' and item.deleted = 0) finished_time,-->
<!--        &lt;!&ndash; 生产状态 &ndash;&gt;-->
<!--        (select status from biz_daywork_item where daywork_id = t1.id and technological_process_detail_id = (select technological_process_detail_id from biz_daywork_item where daywork_id = t1.id order by create_time desc limit 1) order by status desc limit 1) report_status-->
<!--        FROM biz_daywork t1-->
<!--        LEFT JOIN biz_lot t3 ON t1.lot_id = t3.id-->
<!--        LEFT JOIN biz_production_plan_detail t4 ON t1.production_plan_detail_id = t4.id-->
<!--        LEFT JOIN biz_product t2 ON t4.product_id = t2.id-->
<!--        LEFT JOIN sys_dept t5 ON t1.dept_id = t5.dept_id-->

<!--        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">-->
<!--            t1.deleted = 0-->
<!--            <if test="tenantId != null and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>-->
<!--            <if test="deptId != null  and deptId != 0">AND (t1.dept_id = #{deptId}  OR-->
<!--             (SELECT from_dept_id FROM biz_daywork_item WHERE daywork_id = t1.id AND deleted = 0 ORDER BY create_time DESC LIMIT 1)=#{deptId})  </if>-->
<!--            <if test="productDescription != null  and productDescription != ''">-->
<!--                AND t2.description LIKE CONCAT('%', #{productDescription}, '%')-->
<!--            </if>-->
<!--            <if test="lotIds != null and lotIds.size > 0">-->
<!--                AND t1.lot_id IN-->
<!--                <foreach collection="lotIds" index="index" item="id" separator="," open="(" close=")">-->
<!--                    #{id,jdbcType=BIGINT}-->
<!--                </foreach>-->
<!--            </if>-->
<!--            <if test="equipmentDetailCode != null">AND t1.id in(select daywork_id from biz_daywork_item where-->
<!--                equipment_detail_code like concat('%', #{equipmentDetailCode},'%'))-->
<!--            </if>-->

<!--            <if test="queryStartTime != null">-->
<!--                and date_format(t1.create_time,'%y%m%d') &gt;= date_format(#{queryStartTime},'%y%m%d')-->
<!--            </if>-->
<!--            <if test="queryEndTime != null">-->
<!--                and date_format(t1.create_time,'%y%m%d') &lt;= date_format(#{queryEndTime},'%y%m%d')-->
<!--            </if>-->
<!--            <if test="lotId != null  and lotId != ''">AND t3.id = #{lotId}</if>-->
<!--            <if test="lotCode != null  and lotCode != ''">AND t3.lot_code LIKE CONCAT('%', #{lotCode}, '%')</if>-->
<!--            <if test="productionPlanNo != null  and productionPlanNo != ''">-->
<!--                AND (t4.production_plan_no LIKE CONCAT('%', #{productionPlanNo}, '%')-->
<!--                OR t3.lot_code LIKE CONCAT('%', #{productionPlanNo}, '%'))-->
<!--            </if>-->
<!--        </trim>-->
<!--        ORDER BY t1.create_time DESC-->
<!--    </select>-->
    <select id="getList" resultMap="BizDayworkResult">
        SELECT
        t1.*
        FROM biz_daywork t1
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="tenantId != null and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="deptId != null  and deptId != 0">
                AND (
                t1.dept_id = #{deptId}
                or
                <if test="dayworkIds != null and dayworkIds.size > 0">
                t1.id IN
                <foreach collection="dayworkIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
                <if test="dayworkIds == null or dayworkIds.size == 0">
                    1 = 0
                </if>
                )
<!--                OR-->
<!--                (SELECT from_dept_id FROM biz_daywork_item WHERE daywork_id = t1.id AND deleted = 0 ORDER BY create_time DESC LIMIT 1)=#{deptId})  -->
            </if>
            <if test="productDescription != null  and productDescription != ''">
                AND t1.production_plan_detail_id IN (select id from biz_production_plan_detail where product_description LIKE CONCAT('%', #{productDescription}, '%'))
            </if>
            <if test="lotIds != null and lotIds.size > 0">
                AND t1.lot_id IN
                <foreach collection="lotIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="equipmentDetailCode != null">AND t1.id in(select daywork_id from biz_daywork_item where
                equipment_detail_code like concat('%', #{equipmentDetailCode},'%') and deleted = 0)
            </if>

            <if test="queryStartTime != null">
                and date_format(t1.create_time,'%y%m%d') &gt;= date_format(#{queryStartTime},'%y%m%d')
            </if>
            <if test="queryEndTime != null">
                and date_format(t1.create_time,'%y%m%d') &lt;= date_format(#{queryEndTime},'%y%m%d')
            </if>
            <if test="lotId != null  and lotId != ''">AND t1.lot_id = #{lotId}</if>
            <if test="lotCode != null  and lotCode != ''">AND t1.lot_code LIKE CONCAT('%', #{lotCode}, '%')</if>
            <if test="productionPlanNo != null  and productionPlanNo != ''">
                AND (t1.production_plan_detail_id in  (select production_plan_no from biz_production_plan_detail where production_plan_no LIKE CONCAT('%', #{productionPlanNo}, '%'))
                OR t1.lot_code LIKE CONCAT('%', #{productionPlanNo}, '%'))
            </if>
        </trim>
        ORDER BY t1.create_time DESC
    </select>
    <select id="getLastDayworkItem" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        select t1.*,t2.process_alias from biz_daywork_item t1 left join biz_process t2 on t1.process_id = t2.id
        where t1.daywork_id = #{id} and t1.deleted = 0 order by t1.create_time desc limit 1
    </select>
    <select id="getCertificateList" resultMap="BizDayworkResult">
        SELECT
        t1.*,
        t4.production_plan_no as productionPlanNo,
        t4.line_number as lineNumber,
        t2.company_alias as companyAlias,
        t2.description,
        t4.furnace_number,
        t3.production_quantity,
        t4.technology_version as technologyVersion,
        t4.one_lot_quantity as oneLotQuantity,
        t5.dept_name,
        t1.is_waste_recycling,
        t1.is_amend,
        (select count(1) from biz_certificate tc where tc.daywork_id = t1.id and tc.deleted = 0 and print_time is not null) as print_count,
        (SELECT end_time FROM biz_daywork_item WHERE daywork_id = t1.id and biz_daywork_item.deleted = 0
        order by create_time DESC limit 1) as submit_time
        FROM biz_daywork t1
        LEFT JOIN biz_lot t3 ON t1.lot_id = t3.id
        LEFT JOIN biz_production_plan_detail t4 ON t1.production_plan_detail_id = t4.id
        LEFT JOIN biz_product t2 ON t4.product_id = t2.id
        LEFT JOIN sys_dept t5 ON t1.dept_id = t5.dept_id
        LEFT JOIN  biz_furnace_no_info t6 ON t6.daywork_id=t1.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t1.is_certificate = 1
            <if test="tenantId != null and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="deptId != null  and deptId != 0">AND t1.dept_id = #{deptId}</if>
            <if test="furnaceNumber != null  and furnaceNumber != ''">
                AND t6.furnace_number LIKE CONCAT('%', #{furnaceNumber}, '%')
            </if>
            <if test="productDescription != null  and productDescription != ''">
                AND t2.description LIKE CONCAT('%', #{productDescription}, '%')
            </if>
            <if test="equipmentDetailCode != null">AND t1.id in(select daywork_id from biz_daywork_item where
                equipment_detail_code like concat('%', #{equipmentDetailCode},'%'))
            </if>

            <if test="lotId != null  and lotId != ''">AND t3.id = #{lotId}</if>
            <if test="lotCode != null  and lotCode != ''">AND t3.lot_code LIKE CONCAT('%', #{lotCode}, '%')</if>
            <if test="productionPlanNo != null  and productionPlanNo != ''">
                AND (t4.production_plan_no LIKE CONCAT('%', #{productionPlanNo}, '%')
                OR t3.lot_code LIKE CONCAT('%', #{productionPlanNo}, '%'))
            </if>
            <if test="packageResourceGroupId != null"> AND t1.package_resource_group_id = #{packageResourceGroupId}</if>
        </trim>
<!--        ORDER BY t1.create_time DESC-->
        ORDER BY (SELECT end_time FROM biz_daywork_item WHERE daywork_id = t1.id and deleted = 0 order by create_time DESC limit 1) DESC
    </select>
    <select id="getListForAuxiliary" resultMap="BizDayworkResult">
        SELECT
        t1.*,
        t4.production_plan_no as productionPlanNo,
        t4.company_alias as companyAlias,
        t2.description,
        t4.furnace_number,
        t3.production_quantity,
        t4.technology_version as technologyVersion,
        t4.one_lot_quantity as oneLotQuantity,
        t5.dept_name,
        t1.is_waste_recycling,
        t1.is_amend
        FROM biz_daywork t1
        LEFT JOIN biz_lot t3 ON t1.lot_id = t3.id
        LEFT JOIN biz_production_plan_detail t4 ON t1.production_plan_detail_id = t4.id
        LEFT JOIN biz_product t2 ON t4.product_id = t2.id
        LEFT JOIN sys_dept t5 ON t1.dept_id = t5.dept_id

        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="tenantId != null and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="deptId != null  and deptId != 0">AND t1.dept_id = #{deptId}</if>
            <if test="productDescription != null  and productDescription != ''">
                AND t2.description LIKE CONCAT('%', #{productDescription}, '%')
            </if>
            <if test="equipmentDetailCode != null">AND t1.id in(select daywork_id from biz_daywork_item where
                equipment_detail_code like concat('%', #{equipmentDetailCode},'%'))
            </if>

            <if test="lotId != null  and lotId != ''">AND t3.id = #{lotId}</if>
            <if test="lotCode != null  and lotCode != ''">AND t3.lot_code LIKE CONCAT('%', #{lotCode}, '%')</if>
            <if test="productionPlanNo != null  and productionPlanNo != ''">
                AND (t4.production_plan_no LIKE CONCAT('%', #{productionPlanNo}, '%')
                OR t3.lot_code LIKE CONCAT('%', #{productionPlanNo}, '%'))
            </if>
                AND exists (select 1 from biz_daywork_auxiliary_item ai where ai.daywork_id = t1.id and ai.deleted = 0
            <if test="auxiliaryDeptIds != null and auxiliaryDeptIds.size > 0">
                and ai.dept_id
                in
                <foreach collection="auxiliaryDeptIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
                )
        </trim>
        ORDER BY t1.create_time DESC
    </select>
    <select id="selectNeedTakeStock" resultType="cn.ezhizao.project.business.domain.BizDaywork">
        SELECT
        t1.*,t3.drawing_number,t3.product_description,t7.from_id,t7.is_waste,t7.is_superaddition,
        COALESCE((SELECT t2.prod_num FROM biz_daywork_item t2 WHERE t1.id = t2.daywork_id AND t2.deleted = 0 ORDER BY
        t2.create_time DESC LIMIT 1), 0) AS prod_num,
        COALESCE((SELECT t4.process_id FROM biz_daywork_item t4 WHERE t1.id = t4.daywork_id AND t4.deleted = 0 ORDER BY
        t4.create_time DESC LIMIT 1), 0) AS process_id,
        COALESCE((SELECT t6.process_alias FROM biz_daywork_item t5 left join biz_process t6 on t5.process_id = t6.id
        WHERE t1.id = t5.daywork_id AND t5.deleted = 0 ORDER BY t5.create_time DESC LIMIT 1), 0) AS processAlias,
        (select t.status from biz_daywork_item t where t.deleted = 0 and t.daywork_id = t1.id and technological_process_detail_id = (select technological_process_detail_id from biz_daywork_item where daywork_id = t1.id order by create_time desc limit 1) order by status desc limit 1) report_status,
        (select tn.form_code from biz_daywork_item t left join biz_outsourced_order_detail tn on tn.id = t.outsource_detail_id where t.deleted = 0 and tn.deleted = 0 and t.daywork_id = t1.id and t.technological_process_detail_id = (select technological_process_detail_id from biz_daywork_item where daywork_id = t1.id and deleted = 0 order by create_time desc limit 1) order by status desc limit 1) as outsource_form_no
        FROM
        biz_daywork t1 left join biz_production_plan_detail t3 on t1.production_plan_detail_id = t3.id
        left join biz_lot t7 on t7.id = t1.lot_id
        WHERE
        t1.deleted = 0;
    </select>

    <select id="getListFromNoCurrent" resultMap="BizDayworkResult">
        SELECT
        t1.*
        FROM biz_daywork t1
        LEFT JOIN biz_lot t2 ON t1.lot_id = t2.id
        LEFT JOIN biz_production_plan_detail t4 ON t1.production_plan_detail_id = t4.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="keyword != null  and keyword != ''">
                AND (t4.product_description LIKE CONCAT('%', #{keyword}, '%') OR t1.lot_code LIKE CONCAT('%',
                #{keyword}, '%'))
            </if>
        </trim>
        ORDER BY t1.create_time DESC
    </select>

    <select id="selectScreenDaywork" resultMap="BizDayworkResult" parameterType="list">
        SELECT
        d.*,
        (SELECT status FROM biz_daywork_item WHERE daywork_id=d.id AND deleted=0 ORDER BY create_time DESC LIMIT 1)AS
        item_status,
        (SELECT technological_process_detail_id FROM biz_daywork_item WHERE daywork_id=d.id AND deleted=0 ORDER BY
        create_time DESC LIMIT 1)AS special_process_id,
        (SELECT special_is_first FROM biz_daywork_item WHERE daywork_id=d.id AND deleted=0 ORDER BY id DESC LIMIT 1)AS
        special_is_first
        FROM
        biz_daywork d
        WHERE
        d.id IN
        <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND
        d.deleted = 0
        ORDER BY start_time DESC
    </select>

    <select id="getLotNumber" resultType="java.lang.Integer">
        select sum(lot_number) from biz_production_plan_detail_sub_detail where deleted = 0 and
        production_plan_detail_id=#{productionPlanDetailId}
    </select>

    <select id="getdayworkNum" resultType="java.lang.Integer">
        select sum(1) from biz_daywork t1 left join biz_lot t2 on t1.lot_id = t2.id where t1.deleted = 0 and
        t1.production_plan_detail_id=#{productionPlanDetailId} and t1.is_waste_recycling = 0 and t2.from_id = 0
    </select>


    <select id="getOriginalCarrierList" resultType="cn.ezhizao.project.business.domain.BizDayworkCarrier">
        SELECT t1.*
        FROM biz_daywork_carrier t1
        WHERE t1.daywork_id = #{id}
        AND t1.is_changed = 0
        and process_inspection_id = 0
        AND t1.deleted = 0
    </select>
    <select id="getListForOutsource" resultMap="BizDayworkResultForOutsource">
        SELECT
        t1.*,
        t2.product_code,
        t2.description AS product_description,
        t4.id as productionPlanDetailId,
        t4.production_plan_no,
        t4.line_number,
        t3.technology_version,
        (select is_waste from biz_lot where id = t1.lot_id and deleted = 0) as isWaste,
        (
        SELECT
        GROUP_CONCAT(carrier_code ORDER BY carrier_code SEPARATOR ',')
        FROM biz_daywork_carrier
        WHERE daywork_id = t1.id
        and process_inspection_id = 0
        AND is_changed=0
        AND deleted = 0
        ) AS original_carrier
        FROM biz_daywork t1
        LEFT JOIN biz_product t2 ON t1.product_id = t2.id
        LEFT JOIN biz_lot t3 ON t1.lot_id=t3.id
        left join biz_production_plan_detail t4 on t1.production_plan_detail_id = t4.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t3.deleted = 0
            <if test="tenantId != null and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="dayworkIds != null and dayworkIds.size > 0">
                AND t1.id IN
                <foreach collection="dayworkIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="existingDayworkIds != null and existingDayworkIds.size > 0">
                AND t1.id NOT IN
                <foreach collection="existingDayworkIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="productDescription != null and productDescription != ''">
                AND t2.description LIKE CONCAT('%', #{productDescription}, '%')
            </if>
            <if test="lotCode != null and lotCode != ''">AND t1.lot_code LIKE CONCAT('%', #{lotCode}, '%')</if>
            <if test="keyword != null  and keyword != ''">AND (t1.lot_code LIKE concat('%', #{keyword}, '%')or
                t2.description LIKE concat('%', #{keyword}, '%'))
            </if>
            <if test="isSupplierProducts == 1 and supplierId != null and supplierId != ''">
                AND t1.product_id IN (SELECT product_id FROM biz_supplier_product WHERE supplier_id=#{supplierId} AND
                deleted=0)
            </if>
        </trim>
        ORDER BY t1.create_time DESC
    </select>
    <select id="getListForInnerOutsource" resultMap="BizDayworkResultForInnerOutsource">
        SELECT
        t1.*,
        t2.product_code,
        t2.description AS product_description,
        t4.production_plan_no,
        t4.line_number,
        t3.technology_version,
        (select is_waste from biz_lot where id = t1.lot_id and deleted = 0) as isWaste,
        (
        SELECT
        GROUP_CONCAT(carrier_code ORDER BY carrier_code SEPARATOR ',')
        FROM biz_daywork_carrier
        WHERE daywork_id = t1.id
        and process_inspection_id = 0
        AND is_changed=0
        AND deleted = 0
        ) AS original_carrier
        FROM biz_daywork t1
        LEFT JOIN biz_product t2 ON t1.product_id = t2.id
        LEFT JOIN biz_lot t3 ON t1.lot_id=t3.id
        left join biz_production_plan_detail t4 on t1.production_plan_detail_id = t4.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="tenantId != null and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="dayworkIds != null and dayworkIds.size > 0">
                AND t1.id IN
                <foreach collection="dayworkIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="existingDayworkIds != null and existingDayworkIds.size > 0">
                AND t1.id NOT IN
                <foreach collection="existingDayworkIds" index="index" item="id" separator="," open="(" close=")">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="productDescription != null and productDescription != ''">
                AND t2.description LIKE CONCAT('%', #{productDescription}, '%')
            </if>
            <if test="lotCode != null and lotCode != ''">AND t1.lot_code LIKE CONCAT('%', #{lotCode}, '%')</if>
            <if test="keyword != null  and keyword != ''">AND (t1.lot_code LIKE concat('%', #{keyword}, '%')or
                t2.description LIKE concat('%', #{keyword}, '%'))
            </if>
            <if test="isSupplierProducts == 1 and supplierId != null and supplierId != ''">
                AND t1.product_id IN (SELECT product_id FROM biz_supplier_product WHERE supplier_id=#{supplierId} AND
                deleted=0)
            </if>
        </trim>
        ORDER BY t1.create_time DESC
    </select>
    <!--    之前遗留暂时注释
        <select id="getItemList" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
            SELECT
            DISTINCT
            t1.id,
            t1.start_time,
            t1.end_time,
            t1.daywork_id,
            t1.working_hours,
            t1.status,
            t1.prod_num,
            t1.qualified_num,
            t2.nick_name,
            t1.process_id,
            t2.user_name,
            t4.process_alias ,
            t4.process_code,
            t1.equipment_detail_id,
            t1.equipment_detail_code,
            t4.technological_process_id
            FROM biz_daywork_item t1
            LEFT JOIN sys_user t2 ON t1.user_id = t2.user_id
            LEFT JOIN biz_technological_process_detail t4 ON t1.technological_process_id = t4.technological_process_id and
            t1.process_id = t4.process_id

            <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t1.status &lt; 4
            <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
                <if test="processId != null">AND t1.process_id = #{processId}</if>
            <if test="isAmend != null and isAmend != 0">AND t1.form_daywork_item_id !=0 </if>
            ORDER BY t1.process_step_number ASC ,t1.start_time DESC
                t1.deleted = 0 and t1.status &lt; 4
                <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
                <if test="tenantId != null  and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
                <if test="processId != null  and processId != 0">AND t1.process_id = #{processId}</if>
                # and t4.process_id = t1.process_id
            </trim>
            ORDER BY t4.process_step_number ASC ,t1.start_time DESC
    </select>-->
    <select id="getItemList" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT
        t1.*,
        t2.user_name
        FROM biz_daywork_item t1
        LEFT JOIN sys_user t2 ON t1.user_id = t2.user_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND t1.status &lt; 4
            <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
            <if test="isAmend != null and isAmend != 0">AND t1.form_daywork_item_id !=0</if>
            <if test="processId != null  and processId != 0">AND t1.process_id = #{processId}</if>
            <if test="processStepNumber != null  and processStepNumber != ''">AND t1.process_step_number = #{processStepNumber}</if>
            ORDER BY t1.process_step_number ASC ,t1.create_time ASC
        </trim>
    </select>
    <select id="getDayworkList" resultType="cn.ezhizao.project.business.domain.BizDaywork">
        select t1.id,t1.lot_code,t1.product_id,t3.technology_version,t1.technological_process_id,t3.production_quantity
        ,(select t6.process_alias from biz_daywork_item t5 left join biz_process t6 on t5.process_id = t6.id where t1.id = t5.daywork_id and t5.deleted = 0 order by t5.create_time desc limit 1 ) as processAlias,
        (select
        lot_code from biz_lot
        where from_id = id) as
        fromName,t1.is_waste_recycling,t1.is_superaddition,t3.is_waste,t1.is_amend,t1.production_plan_detail_id,t2.company_alias,t2.production_plan_no,t2.product_description ,t2.line_number,t1.create_time,t4.dept_name,t4.dept_id,(select lot_code from biz_lot where t3.from_id =biz_lot.id ) as fromCode
        from biz_daywork t1
        left join biz_production_plan_detail t2 on t1.production_plan_detail_id = t2.id
        left join biz_lot t3 on t1.lot_id = t3.id
        left join sys_dept t4 on t1.dept_id = t4.dept_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 and t2.deleted = 0 and t3.deleted = 0 and t1.status &lt; 2
            <if test="deptId != null  and deptId != 0">AND t1.dept_id = #{deptId}</if>
            <if test="tenantId != null  and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="productDescription != null  and productDescription != ''">AND t2.product_description like
                concat('%', #{productDescription}, '%')
            </if>
            <if test="lotCode != null  and lotCode != ''">AND t1.lot_code like concat('%', #{lotCode},'%')</if>
            <if test="productionPlanNo != null  and productionPlanNo != ''">AND t2.production_plan_no like concat('%',
                #{productionPlanNo},'%')
            </if>
            <if test="lineNumber != null ">AND t2.line_number = #{lineNumber}</if>
            <if test="isAmend != null">AND t1.is_amend = #{isAmend}</if>
            <if test="isWasteRecycling != null">AND t1.is_waste_recycling = #{isWasteRecycling}</if>
            <if test="isWaste != null">AND t1.is_waste = #{isWaste}</if>
            <if test="flags != null and flags.size() > 0">
                AND
                <foreach collection="flags" item="flag" separator=" OR " open="(" close=")">
                    <if test="flag != null and flag == 2"> t1.is_amend= 1</if>
                    <if test="flag != null and flag == 1"> t1.is_waste_recycling= 1</if>
                    <if test="flag != null and flag == 0"> t3.is_waste= 1</if>
                    <if test="flag != null and flag == 4"> t1.is_superaddition= 1</if>
                    <if test="flag != null and flag == 3"> t3.is_waste= 0 and  t1.is_waste_recycling= 0 and t1.is_amend = 0 and t1.is_superaddition = 0 </if>
                    <!--                    t1.is_waste_recycling = #{flag} OR t1.is_waste = #{flag} OR t1.is_amend = #{flag}-->
                </foreach>
            </if>
            <if test="startTime != null ">AND date_format(t1.create_time,'%y%m%d') &gt;=date_format(#{startTime},'%y%m%d') </if>
            <if test="endTime != null ">AND date_format(t1.create_time,'%y%m%d') &lt;=date_format(#{endTime},'%y%m%d')</if>
        </trim>
        order by t1.lot_code asc
    </select>
    <select id="getProcessName" resultType="java.lang.String">
        select process_alias
        from biz_technological_process_detail
        where deleted = 0
        and id = #{technologicalProcessDetailId}
    </select>
    <select id="getProductDescription" resultType="java.lang.String">
        select product_description
        from biz_production_plan_detail
        where deleted = 0
        and id = #{productionPlanDetailId}
    </select>
    <select id="getProductCode" resultType="java.lang.String">
        select product_code
        from biz_product
        where deleted = 0
        and id = #{productId}
    </select>
    <select id="getFurnaceNumber" resultType="java.lang.String">
        select furnace_number
        from biz_furnace_no_info
        where deleted = 0
          and daywork_id = #{id}
    </select>

    <select id="getTechnologyVersion" resultType="java.lang.String">
        select technology_version
        from biz_lot
        where deleted = 0
        and id = #{lotId}
    </select>
    <select id="getLotCode" resultType="java.lang.String">
        select lot_code
        from biz_lot
        where deleted = 0
        and id = #{lotId}
    </select>

    <select id="listReportItem" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT
        t1.*,
        t2.user_name,
        t3.process_alias,
        (
        SELECT
        GROUP_CONCAT(carrier_code ORDER BY carrier_code SEPARATOR ',')
        FROM biz_daywork_carrier
        WHERE daywork_id = t1.daywork_id
        AND is_changed=0
        AND deleted = 0
        ) AS original_carrier
        FROM biz_daywork_item t1
        LEFT JOIN sys_user t2 ON t1.user_id = t2.user_id
        left join biz_process t3 ON t1.process_id = t3.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND t1.status &lt; 4
            <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
            <if test="deptId != null and deptId != 0">AND t1.dept_id = #{deptId}</if>
            <if test="isAmend != null and isAmend != 0">AND t1.form_daywork_item_id !=0</if>
            <if test="startTime != null ">AND t1.end_time &gt;= #{startTime}</if>
            <if test="endTime != null ">AND t1.end_time &lt;= #{endTime}</if>
            <if test="productDescription != null and productDescription != ''">AND t1.production_plan_detail_id in
                (select id from biz_production_plan_detail where deleted = 0 and product_description LIKE CONCAT('%',
                #{productDescription}, '%'))
            </if>
            <if test="lotCode != null and lotCode != ''">AND t1.lot_id in (select id from biz_lot where deleted = 0 and
                lot_code LIKE CONCAT('%', #{lotCode}, '%'))
            </if>
            <if test="nickName != null and nickName != ''">AND t1.nick_name LIKE CONCAT('%', #{nickName}, '%')</if>
            <if test="originalCarrier != null and originalCarrier != ''">AND t1.daywork_id in (select daywork_id from
                biz_daywork_carrier where deleted = 0 AND is_changed=0 and carrier_code LIKE CONCAT('%',
                #{originalCarrier}, '%'))
            </if>
            <if test="processId != null  and processId != 0">AND t1.process_id = #{processId}</if>
            ORDER BY t1.lot_id, t1.end_time DESC
        </trim>
    </select>
    <select id="getItemListFromOutsourced" parameterType="BizDayworkItem" resultMap="BizDayworkItemResult">
        SELECT
        t1.*
        FROM biz_daywork_item t1
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
            ORDER BY t1.create_time DESC
        </trim>
    </select>
    <select id="getLotPosition" resultMap="BizDayworkResult">
        SELECT
        t1.id,
        t1.dept_id,
        t2.company_alias,
        t2.production_plan_no,
        t2.line_number,
        t2.product_description as description,
        t2.specification,
        t3.lot_code,
        t3.technology_version,
        t3.production_quantity,
        t5.NAME AS workshopName,
        t4.dept_name,
        (
        SELECT
        process_alias
        FROM
        biz_process
        LEFT JOIN biz_daywork_item ON biz_process.id = biz_daywork_item.process_id
        WHERE
        biz_daywork_item.daywork_id = t1.id and biz_daywork_item.deleted = 0
        ORDER BY
        biz_daywork_item.create_time DESC
        LIMIT 1
        ) AS processAlias,
        ( SELECT place FROM biz_daywork_item WHERE biz_daywork_item.daywork_id = t1.id and biz_daywork_item.deleted = 0 ORDER BY
        biz_daywork_item.create_time DESC LIMIT 1 ) AS place,
        ( SELECT turnover_area FROM biz_daywork_item WHERE biz_daywork_item.daywork_id = t1.id and biz_daywork_item.deleted = 0 ORDER BY
        biz_daywork_item.create_time DESC LIMIT 1 ) AS turnoverArea,
        (
        SELECT
        GROUP_CONCAT( carrier_code ORDER BY carrier_code SEPARATOR ',' )
        FROM
        biz_daywork_carrier
        WHERE
        biz_daywork_carrier.daywork_id = t1.id
        AND biz_daywork_carrier.is_changed = 0
        AND biz_daywork_carrier.deleted = 0
        ) AS carrier_name,
        t6.furnace_number
        FROM
        biz_daywork t1
        LEFT JOIN biz_production_plan_detail t2 ON t1.production_plan_detail_id = t2.id
        LEFT JOIN biz_lot t3 ON t1.lot_id = t3.id
        LEFT JOIN sys_dept t4 ON t1.dept_id = t4.dept_id
        LEFT JOIN biz_workshop t5 ON t4.workshop_id = t5.id
        left join biz_furnace_no_info t6 on t6.daywork_id = t1.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            AND t2.deleted = 0
            AND t3.deleted = 0
            AND t4.del_flag = 0
            AND t5.deleted = 0
            <if test="deptId != null ">AND t1.dept_id = #{deptId}</if>
            <if test="tenantId != null  and tenantId != 0">AND t1.tenant_id = #{tenantId}</if>
            <if test="workshopId != null ">AND t5.id = #{workshopId}</if>
            <if test="productDescription != null and productDescription != ''">
                AND t2.product_description like concat('%',#{productDescription},'%')
            </if>
            <if test="specification != null and specification != ''">
                AND t2.specification  = #{specification}
            </if>
            <if test="furnaceNumber != null  and furnaceNumber != ''">
                AND t6.furnace_number = #{furnaceNumber}
            </if>
            <if test="lotCode != null  and lotCode != ''">AND t3.lot_code like concat('%',#{lotCode},'%')</if>
            <if test="carrierCode != null ">AND exists (select 1
                FROM
                biz_daywork_carrier
                WHERE
                biz_daywork_carrier.daywork_id = t1.id
                AND biz_daywork_carrier.is_changed = 0
                AND biz_daywork_carrier.deleted = 0
                and biz_daywork_carrier.carrier_code like concat('%',#{carrierCode},'%')
                )
            </if>
        </trim>
    </select>

    <select id="getNoFirstLotList" resultMap="BizDayworkResult">
        SELECT
        t1.id,
        t1.is_amend,
        t3.id as lot_id,
        t1.is_waste_recycling,
        t1.technological_process_id,
        t1.process_qualified_num,
        t1.dept_id,
        t2.id as production_plan_detail_id,
        t2.company_alias,
        t2.production_plan_no,
        t2.line_number,
        t2.product_description as description,
        t3.lot_code,
        t3.technology_version,
        t3.production_quantity,
        (
        SELECT
        GROUP_CONCAT( carrier_code ORDER BY carrier_code SEPARATOR ',' )
        FROM
        biz_daywork_carrier
        WHERE
        biz_daywork_carrier.daywork_id = t1.id
        AND biz_daywork_carrier.is_changed = 0
        AND biz_daywork_carrier.deleted = 0
        ) AS carrier_name
        FROM
        biz_daywork t1
        LEFT JOIN biz_production_plan_detail t2 ON t1.production_plan_detail_id = t2.id
        LEFT JOIN biz_lot t3 ON t1.lot_id = t3.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            AND t2.deleted = 0
            AND t3.deleted = 0
            <if test="deptId != null ">AND t1.dept_id = #{deptId}</if>

            <if test="productDescription != null and productDescription != ''">
                AND t2.product_description like concat('%',#{productDescription},'%')
            </if>
            <if test="lotCode != null  and lotCode != ''">AND t3.lot_code like concat('%',#{lotCode},'%')</if>
            <if test="ids != null and ids.size() > 0">
                AND t1.id IN
                <foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="carrierCode != null ">AND exists (select 1
                FROM
                biz_daywork_carrier
                WHERE
                biz_daywork_carrier.daywork_id = t1.id
                AND biz_daywork_carrier.is_changed = 0
                AND biz_daywork_carrier.deleted = 0
                and biz_daywork_carrier.carrier_code like concat('%',#{carrierCode},'%')
                )
            </if>
        </trim>
    </select>


    <!--    -->
<!--    <select id="getItemListForOutsource" parameterType="BizDayworkItem" resultMap="BizDayworkItemResultForOutsource">-->
<!--        SELECT-->
<!--        t1.*-->
<!--        FROM biz_daywork_item t1-->
<!--        LEFT JOIN sys_dept t2 ON t1.dept_id=t2.dept_id-->
<!--        LEFT JOIN biz_daywork t3 ON t1.daywork_id = t3.id-->
<!--        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">-->
<!--            t1.deleted = 0 AND-->
<!--            t1.status >= 4 AND-->
<!--            # 查找创建时间最新的一条-->
<!--            t1.create_time = (-->
<!--            SELECT MAX(t5.create_time)-->
<!--            FROM biz_daywork_item t5-->
<!--            WHERE t5.daywork_id = t1.daywork_id AND t5.deleted = 0-->
<!--            )-->
<!--            <if test="deptCode != null and deptCode != ''">AND t2.dept_code = #{deptCode}</if>-->
<!--            <if test="isSupplierProducts != null and isSupplierProducts != 1 ">-->
<!--                AND t3.product_id IN (SELECT product_id FROM biz_supplier_product WHERE supplier_id=#{supplierId} AND-->
<!--                deleted=0)-->
<!--            </if>-->

<!--            ORDER BY t1.process_step_number ASC-->
<!--        </trim>-->
<!--    </select>-->
    <select id="getItemListForOutsource" parameterType="BizDayworkItem" resultMap="BizDayworkItemResultForOutsource">
        SELECT
        t1.*
        FROM biz_daywork_item t1
        INNER JOIN (
        SELECT daywork_id, MAX(create_time) AS max_create_time
        FROM biz_daywork_item
        WHERE deleted = 0
        GROUP BY daywork_id
        ) t5 ON t1.daywork_id = t5.daywork_id AND t1.create_time = t5.max_create_time
        LEFT JOIN sys_dept t2 ON t1.dept_id = t2.dept_id
        LEFT JOIN biz_daywork t3 ON t1.daywork_id = t3.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            AND t1.status >= 4
            AND t2.dept_code = 170000
            <if test="isSupplierProducts != null and isSupplierProducts != 1 ">
                AND t3.product_id IN (SELECT product_id FROM biz_supplier_product WHERE supplier_id=#{supplierId} AND
                deleted=0)
            </if>
            <if test="tenantId != null  and tenantId != 0">AND t2.tenant_id = #{tenantId}</if>

            ORDER BY t1.process_step_number ASC
        </trim>
    </select>

    <select id="getDeptList" resultType="cn.ezhizao.project.system.domain.SysDept">
        SELECT DISTINCT t3.dept_id AS Value,
        t3.dept_name AS Label
        FROM biz_daywork t1
        LEFT JOIN biz_production_plan_detail t2 ON t2.id = t1.production_plan_detail_id
        LEFT JOIN sys_dept t3 ON t1.dept_id = t3.dept_id
        WHERE t2.production_plan_no = #{productionPlanNo}
        AND t2.line_number = #{lineNumber}
    </select>

    <select id="getDayworkProcessList" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        SELECT distinct t3.id AS processId,
        t3.process_alias AS processAlias,t2.process_step_number as processStepNumber
        FROM biz_daywork t1
        LEFT JOIN biz_daywork_item t2 ON t1.id = t2.daywork_id
        LEFT JOIN biz_process t3 ON t2.process_id = t3.id
        WHERE t1.id = #{id} and t1.deleted = 0 and t2.deleted = 0
        AND t2.dept_id = #{deptId}
        AND t2.status &lt; 4
        ORDER BY t2.create_time
    </select>
    <select id="getDayworkWasteProcessList" resultType="cn.ezhizao.project.business.domain.BizDayworkItem">
        SELECT distinct t3.id AS processId,t2.technological_process_id AS technologicalProcessId,t2.technological_process_detail_id AS technologicalProcessDetailId,
        t3.process_alias AS processAlias,t2.process_step_number as processStepNumber
        FROM biz_daywork t1
        LEFT JOIN biz_daywork_item t2 ON t1.id = t2.daywork_id
        LEFT JOIN biz_process t3 ON t2.process_id = t3.id
        WHERE t1.id = #{id} and t1.deleted = 0 and t2.deleted = 0
        AND t2.dept_id = #{deptId}
        AND t2.status &lt; 4
        ORDER BY t2.create_time
    </select>

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

    <select id="getItemListWithTurnover" resultMap="BizDayworkItemResult">
        SELECT
        t1.*,
        t2.user_name
        FROM biz_daywork_item t1
        LEFT JOIN sys_user t2 ON t1.user_id = t2.user_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="dayworkId != null and dayworkId != 0">AND t1.daywork_id = #{dayworkId}</if>
<!--            0313修改，首序单批单改导致新的报工数据的form_daywork_item_id = 0-->
<!--            <if test="isAmend != null and isAmend != 0">AND t1.form_daywork_item_id !=0</if>-->
            <if test="processId != null  and processId != 0">AND t1.process_id = #{processId}</if>
            <if test="processStepNumber != null  and processStepNumber != ''">AND t1.process_step_number = #{processStepNumber}</if>
            ORDER BY t1.process_step_number ASC ,t1.create_time ASC
        </trim>
    </select>

</mapper>
