USE [YL_MESDB]
GO

ALTER PROCEDURE [dbo].[生产管理_生产计划跟踪]
    @订单号 int = 0,
    @物料类型 nvarchar(50) = '',
    @合同号 nvarchar(50) = '',
    @产品编码 nvarchar(50) = '',
    @产品名称 nvarchar(50) = '',
    @工序名称 nvarchar(50) = '',
    @要求完工日期1 nvarchar(50) = '',
    @要求完工日期2 nvarchar(50) = '',
    @计划开始1 nvarchar(50) = '',
    @计划开始2 nvarchar(50) = '',
    @计划完成1 nvarchar(50) = '',
    @计划完成2 nvarchar(50) = '',
    @排产状态 nvarchar(50) = '全部',
    @指派对象 nvarchar(50) = '全部',
    @自制件属性 nvarchar(50) = '全部',
    @计划号 nvarchar(50) = '',
    @加急状态 nvarchar(50) = '',
    @单据状态 nvarchar(50) = ''
AS
BEGIN
    DECLARE @selectStr NVARCHAR(MAX)
    DECLARE @selectStrWhere NVARCHAR(4000) = ' WHERE [物料类型] = ''pit_Resource'' '
    
    SET @selectStr = N'
        SELECT top 500
            v.计划号,
            加急总数,
            拣配状态,
            自制件属性,
            入库数量,
            外协分组,
            case 
                when isnull(任务状态,0)=4 then ''已完成'' 
                when isnull(任务状态,0)=2 then ''已开工'' 
                when isnull(任务状态,0)=0 then ''待执行'' 
                when isnull(任务状态,0)=1 then ''可执行''
                else '''' 
            end as [任务状态],
            case 
                when isnull(加工状态,0)=2 then ''辅助开始''
                when isnull(加工状态,0)=3 then ''辅助结束''
                when isnull(加工状态,0)=4 then ''开始加工''
                when isnull(加工状态,0)=5 then ''暂停''
                when isnull(加工状态,0)=6 then ''报工''
                else '''' 
            end as [加工状态],
            [计划数量订单],
            ISNULL(开工时间, 0) AS 开工时间,
            ISNULL(进度, 0) AS 进度,
            v.[TaskAID],
            [合同号],
            v.[订单编号] AS 订单号,
            [订单行号],
            [订单拆分内码] AS 拆分内码,
            [物料编号] AS 产品编码,
            [物料描述] AS 产品名称,
            TRIM(''.'' FROM TRIM(''0'' FROM CAST([计划数量] AS VARCHAR(30)))) AS 计划数量,
            ISNULL(gy.标准工时, 0) AS 标准工时,
            [计划完成] AS 要求完工日期,
            [工序名称],
            完成数量,
            收料数量,
            CASE 
                WHEN [加急状态] = 1 THEN ''加急'' 
                ELSE '''' 
            END AS 加急状态,
            加急数量,
            性质,
            ISNULL(总工时, 0) AS 总工时,
            [占用情况],
            [排产状态],
            [工序计划数量],
            指派对象编号,
            指派对象,
            TRIM(''.'' FROM TRIM(''0'' FROM CAST([指派数量] AS VARCHAR(30)))) AS 指派数量,
            计划开始时间,
            计划完成时间,
            最晚开始时间,
            [派工状态],
            [外协通知],
            [优先级],
            [未完成说明],
            [派工特殊说明],
            [最后编辑人],
            [最后编辑时间],
            [二次派工],
            CASE 
                WHEN zj_终检.计划号 IS NOT NULL THEN ''是'' 
                ELSE ''否'' 
            END AS 终检,
            ISNULL(zj_收检.合格数合计, 0) AS 收检合格数,
            ISNULL(zj_收检.不合格数合计, 0) AS 收检不合格数,
            ISNULL(zj_收检.合格数合计, 0) + ISNULL(zj_收检.不合格数合计, 0) AS 收检总数,
            ISNULL(jl.开始时间, 0) AS 开始时间,
            ISNULL(jl.结束时间, 0) AS 结束时间,
            ISNULL(jl.外协结束时间, 0) AS 外协结束时间,
            STUFF((
                SELECT '','' + d2.工序名称
                FROM [dbo].[View_生产订单_MES] d2
                WHERE d2.订单编号 = v.订单编号 order by d2.订单行号
                FOR XML PATH('''')
            ), 1, 1, '''') AS 工艺路线
        FROM [dbo].[View_生产订单全部（包括已关闭）] v
        LEFT JOIN (
            SELECT DISTINCT 计划号
            FROM dbo.YL_质量检验_质检记录
            WHERE 质检类型 = ''终检''
        ) zj_终检 ON v.[订单编号] = zj_终检.计划号
        LEFT JOIN (
            SELECT 
                TaskAID,
                SUM(ISNULL(合格数, 0)) AS 合格数合计,
                SUM(ISNULL(不合格数, 0)) AS 不合格数合计
            FROM dbo.YL_质量检验_质检记录
            WHERE 质检类型 = ''收检''
            GROUP BY TaskAID
        ) zj_收检 ON v.[TaskAID] = zj_收检.TaskAID
        LEFT JOIN (
            SELECT 
                TaskAID,
                min(ISNULL(开始时间, 0)) AS 开始时间,
                MAX(CASE WHEN 操作类别 = ''报工'' THEN ISNULL(开始时间, 0) ELSE NULL END) AS 外协结束时间,
                MAX(CASE WHEN 操作类别 = ''报工'' THEN ISNULL(结束时间, 0) ELSE NULL END) AS 结束时间
            FROM [dbo].[YL_加工中心_操作记录表]
            GROUP BY TaskAID
        ) jl ON v.[TaskAID] = jl.TaskAID
        LEFT JOIN (
            SELECT 
                订单编号,
                工艺顺序,
                MAX(ISNULL(标准工时, 0)) AS 标准工时
            FROM dbo.[车间生产管理工艺_零件生产工艺_工艺库]
            GROUP BY 订单编号, 工艺顺序
        ) gy ON v.[订单编号] = gy.[订单编号] AND v.[订单行号] = gy.[工艺顺序]'
    
    IF (@订单号 > 0)
        SET @selectStrWhere = @selectStrWhere + ' AND v.[订单编号] = ' + CONVERT(VARCHAR(10), @订单号)
    IF (@计划号 > 0)
        SET @selectStrWhere = @selectStrWhere + ' AND v.[计划号] = '''+@计划号+''''
    IF (@合同号 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [合同号] LIKE ''%' + @合同号 + '%'''
    IF (@产品编码 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [物料编号] LIKE ''%' + @产品编码 + '%'''
    IF (@产品名称 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [物料描述] LIKE ''%' + @产品名称 + '%'''
    IF (@工序名称 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [工序名称] = ''' + @工序名称 + ''''
    IF (@要求完工日期1 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [计划完成] >= ''' + @要求完工日期1 + ''''
    IF (@要求完工日期2 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [计划完成] <= ''' + @要求完工日期2 + ''''
    IF (@计划开始1 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [计划开始时间] >= ''' + @计划开始1 + ''''
    IF (@计划开始2 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [计划开始时间] <= ''' + @计划开始2 + ''''
    IF (@计划完成1 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [计划完成时间] >= ''' + @计划完成1 + ''''
    IF (@计划完成2 <> '')
        SET @selectStrWhere = @selectStrWhere + ' AND [计划完成时间] <= ''' + @计划完成2 + ''''
    IF (@排产状态 = '未完成')
        SET @selectStrWhere = @selectStrWhere + ' AND ISNULL([排产状态], 0) = 0'
    ELSE IF (@排产状态 = '已完成')
        SET @selectStrWhere = @selectStrWhere + ' AND [排产状态] = 1'
    IF (@自制件属性 = '机加')
        SET @selectStrWhere = @selectStrWhere + ' AND [自制件属性] = ''机加件'''
    ELSE IF (@自制件属性 = '装配')
        SET @selectStrWhere = @selectStrWhere + ' AND [自制件属性] LIKE ''%装配'''
    IF (@加急状态 = '加急')
        SET @selectStrWhere = @selectStrWhere + ' AND [加急状态] = 1'
    IF (@单据状态 = '正常')
        SET @selectStrWhere = @selectStrWhere + ' AND [单据状态] is null'
    ELSE IF  (@单据状态 = '关闭')
        SET @selectStrWhere = @selectStrWhere + ' AND [单据状态] is not null'
    IF (@指派对象 <> '全部')
        SET @selectStrWhere = @selectStrWhere + ' AND [指派对象] IN (SELECT value FROM STRING_SPLIT(''' + @指派对象 + ''', '',''))'
    
    SET @selectStrWhere = @selectStrWhere + ' ORDER BY v.[计划完成] ASC, v.[订单编号] ASC'
    SET @selectStr = @selectStr + @selectStrWhere
    PRINT @selectStr
    EXEC sp_executesql @selectStr
END
GO
