<?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="com.fjhx.mes.mapper.production.ProductionTaskDetailMapper">
    <select id="getPage" resultType="com.fjhx.mes.entity.production.vo.ProductionTaskDetailVo">
        select
            ptd.id,
            ptd.product_id,
            ptd.production_task_id,
            ptd.product_sn,
            ptd.production_processes_id,
            ptd.finish_time,
            ptd.finish_status,
            ptd.person_liable_id,
            ptd.create_user,
            ptd.create_time,
            ptd.update_user,
            ptd.update_time,
            ptd.next_production_processes_id,
            ptd.circulation_user_id,
            ptd.circulation_status,
            ptd.previous_processes_id,
            scd.product_remark,
            sc.customer_id,
            pt.due_date
        from production_task_detail ptd
            left JOIN production_task_processes_detail ptpd ON ptpd.production_task_id = ptd.production_task_id
            AND ptpd.production_processes_id = ptd.production_processes_id
            LEFT JOIN production_task pt ON ptd.production_task_id = pt.id
            LEFT JOIN production_plan pp ON pt.production_plan_id = pp.id
            LEFT JOIN work_order wo ON pp.work_order_id = wo.id
            LEFT JOIN bytesailing_jxst.sales_contract_details scd ON wo.contract_details_id = scd.id
            AND scd.del_flag =0
            LEFT JOIN bytesailing_jxst.sales_contract sc ON scd.sales_contract_id = sc.id
            AND sc.del_flag = 0
            ${ew.customSqlSegment}
    </select>
    <select id="getWorkOrderByProductionTask" resultType="com.fjhx.mes.entity.work.vo.WorkOrderVo">
        SELECT
            wo.*,
            pt.id productionTaskId
        FROM
            work_order wo
                LEFT JOIN production_plan pp ON pp.work_order_id = wo.id
                LEFT JOIN production_task pt ON pt.production_plan_id = pp.id
            ${ew.customSqlSegment}
    </select>
    <select id="snInfo" resultType="com.fjhx.mes.entity.production.vo.ProductionTaskDetailVo">
        SELECT
            ptd.id,
            ptd.product_id,
            ptd.product_sn,
            sc.`code` as contractCode,
            wo.id workOrderId,
            sc.customer_id
        FROM
            production_task_detail ptd
                JOIN production_task pt ON ptd.production_task_id = pt.id
                JOIN production_plan pp ON pt.production_plan_id = pp.id
                JOIN work_order wo ON pp.work_order_id = wo.id
                JOIN bytesailing_jxst.sales_contract sc ON wo.source_id = sc.id
        WHERE
            ptd.product_sn = #{sn}
    </select>
    <select id="taskProgressPage" resultType="com.fjhx.mes.entity.production.vo.ProductionTaskDetailVo">
        SELECT
            *
        FROM
            (
                SELECT
                    ptd.id,
                    sc.`code` AS contractCode,
                    wo.`code` AS workOrderCode,
                    pp.`code` AS productionPlanCode,
                    pt.`code` AS productionTaskCode,
                    pi.`code` AS productCode,
                    pi.`name` AS productName,
                    pi.`spec` AS productSpec,
                    ptd.product_sn,
                    pt.due_date,
                    IF(
                        wo.is_customized=0,
                            (
                                SELECT
                                    GROUP_CONCAT( ppr.`name` SEPARATOR '->' )
                                FROM
                                    technology tec
                                        LEFT JOIN production_processes ppr ON FIND_IN_SET( ppr.id, tec.process_route )
                                        LEFT JOIN applicable_products apr ON apr.technology_id = tec.id
                                WHERE
                                    apr.product_id = wo.product_id
                            ) ,
                        ( SELECT GROUP_CONCAT( wopp.`name` SEPARATOR '->' ) FROM work_order_production_processes wopp WHERE wopp.work_order_id = wo.id )
                      ) AS productionProcessesNames,
                    IF(
                        wo.is_customized=0,
                            ( SELECT ppr.`name` FROM production_processes ppr WHERE ppr.id = ptd.production_processes_id ) , ( SELECT wopp.`name` FROM work_order_production_processes wopp WHERE wopp.id = ptd.production_processes_id )
                        ) AS productionProcessesName,
                    su.nick_name AS personLiableName,
                    ptd.finish_status,
                    ptd.finish_time,
                    wo.id AS workOrderId,
                    ptd.product_id,
                    wo.is_customized
                FROM
                    production_task_detail ptd
                        LEFT JOIN production_task pt ON ptd.production_task_id = pt.id
                        LEFT JOIN production_plan pp ON pt.production_plan_id = pp.id
                        LEFT JOIN work_order wo ON pp.work_order_id = wo.id
                        LEFT JOIN bytesailing_jxst.sales_contract sc ON wo.source_id = sc.id
                        LEFT JOIN bytesailing_item.product_info pi ON wo.product_id = pi.id
                        LEFT JOIN bytesailing_base.sys_user su ON ptd.person_liable_id = su.user_id
            ) t1
            ${ew.customSqlSegment}
    </select>
    <select id="getDetail" resultType="com.fjhx.mes.entity.production.vo.ProductionTaskDetailVo">
        SELECT
            ptd.id,
            ptd.product_id,
            ptd.production_task_id,
            ptd.product_sn,
            ptd.production_processes_id,
            ptd.finish_time,
            ptd.finish_status,
            ptd.person_liable_id,
            ptd.tenant_id,
            ptd.create_user,
            ptd.create_time,
            ptd.update_user,
            ptd.update_time,
            ptd.del_flag,
            ptd.next_production_processes_id,
            ptd.circulation_user_id,
            ptd.circulation_status,
            ptd.received_user_id,
            ptd.received_time,
            ptd.previous_processes_id,
            ptd.processes_status,
            scd.product_remark,
            wo.id workOrderId,
            sc.delivery_date contractDeliveryDate,
            pt.due_date
        FROM
            production_task_detail ptd
                LEFT JOIN production_task pt ON ptd.production_task_id = pt.id
                LEFT JOIN production_plan pp ON pt.production_plan_id = pp.id
                LEFT JOIN work_order wo ON pp.work_order_id = wo.id
                LEFT JOIN bytesailing_jxst.sales_contract_details scd ON wo.contract_details_id = scd.id
                AND scd.del_flag =0
                LEFT JOIN bytesailing_jxst.sales_contract sc ON scd.sales_contract_id = sc.id
                AND sc.del_flag =0
            ${ew.customSqlSegment}
    </select>
    <select id="getUserTaskCount" resultType="java.lang.Long">
        SELECT
            count( ptd.id )
        FROM
            production_task_detail ptd
                LEFT JOIN production_task_processes_detail ptpd ON ptpd.production_task_id = ptd.production_task_id
                AND ptpd.production_processes_id = ptd.production_processes_id
            ${ew.customSqlSegment}
    </select>

</mapper>
