﻿
CREATE PROCEDURE [dbo].[计划排产_阀类任务查询]
@订单号 int=0,
@物料类型 nvarchar(50)='',   --自制件、装配件
@合同号 nvarchar(50)='',
@产品编码  nvarchar(50)='',
@产品名称  nvarchar(50)='',
@工序名称  nvarchar(50)='',
@要求完工日期1  nvarchar(50)='',
@要求完工日期2  nvarchar(50)='',
@计划开始 nvarchar(50)='',
@计划完成 nvarchar(50)='',
@排产状态  nvarchar(50)='全部' ,  --全部，已完成、未完成
@指派对象  nvarchar(50)='全部',
@辅助配件  nvarchar(50)='不含'
AS
BEGIN
    declare @selectStr nvarchar(4000)
    declare @selectStrWhere nvarchar(4000)=' where [物料类型]=''pit_Resource'' and [自制件属性] like ''%装配%'' and 物料描述 not like ''%系统%'' and isnull(任务状态,0)<4'
    
    if(@物料类型='自制件')   --自制件

		set @selectStr='
            SELECT  [计划数量订单],isnull(开工时间,0) as 开工时间,isnull(进度,0) as 进度,[TaskAID],v.[合同号],v.[订单编号] as 订单号,
            [订单行号],[订单拆分内码] as 拆分内码,[物料编号] as 产品编码,[物料描述] as 产品名称,TRIM(''.'' FROM TRIM(''0'' FROM CAST(v.[计划数量] AS VARCHAR(30)))) AS 计划数量,
            [计划完成] as 要求完工日期,[工序名称],完成数量,v.计划号,收料数量,外协分组,v.任务状态,v.齐套时间,v.派工时间,v.齐套,v.要求发货日期,
            case when [加急状态]=1 then ''加急'' else '''' end as 加急状态,性质,isnull(总工时,0) as 总工时,[占用情况]
            ,[排产状态],[工序计划数量],指派对象编号,指派对象,TRIM(''.'' FROM TRIM(''0'' FROM CAST([指派数量] AS VARCHAR(30)))) AS 指派数量,计划开始时间,计划完成时间,最晚开始时间,
            [派工状态],[外协通知],[优先级],[未完成说明],[派工特殊说明],[最后编辑人],[最后编辑时间],[二次派工],s.交货日期理论齐套日期 as 交货日期,
            case when zj.计划号 is not null then ''是'' else ''否'' end as 终检,ISNULL(g.标准工时, 0) as 标准工时,
            ISNULL(zp.装配工时, 0) as 任务装配工时,
            ISNULL(zj1.质检工时, 0) as 任务质检工时,
            ISNULL(htzp.合同装配工时, 0) as 合同装配工时,
            ISNULL(htzj.合同质检工时, 0) as 合同质检工时,
			ISNULL(zp.装配工序说明, '''') as 装配工序说明,
            ISNULL(zj1.质检工序说明, '''') as 质检工序说明
            FROM [View_生产订单_MES] v
            LEFT JOIN (
                SELECT DISTINCT 计划号
                FROM dbo.YL_质量检验_质检记录
                WHERE 质检类型 = ''终检''
            ) zj ON v.[订单编号] = zj.计划号
			LEFT JOIN [dbo].[车间生产管理工艺_零件生产工艺_工艺库] g  -- 新增LEFT JOIN
                ON v.[订单编号] = g.订单编号 
                AND v.[订单行号] = g.工艺顺序
			LEFT JOIN (
                SELECT gs.订单号, SUM(gs.总时长) as 装配工时,max(gs.工序说明) as 装配工序说明
                FROM [dbo].[View_生产工时视图] gs
                INNER JOIN [dbo].[登录基础数据_人员信息] ry ON gs.操作人 = ry.Personnel_Name
                WHERE ry.班组 = ''装配''
                GROUP BY gs.订单号
            ) zp ON v.[订单编号] = zp.订单号
            LEFT JOIN (
                SELECT gs.订单号, SUM(gs.总时长) as 质检工时,max(gs.工序说明) as 质检工序说明
                FROM [dbo].[View_生产工时视图] gs
                INNER JOIN [dbo].[登录基础数据_人员信息] ry ON gs.操作人 = ry.Personnel_Name
                WHERE ry.班组 = ''质检''
                GROUP BY gs.订单号
            ) zj1 ON v.[订单编号] = zj1.订单号
            LEFT JOIN (
                SELECT gs.合同号, SUM(gs.总时长) as 合同装配工时
                FROM [dbo].[View_生产工时视图] gs
                INNER JOIN [dbo].[登录基础数据_人员信息] ry ON gs.操作人 = ry.Personnel_Name
                WHERE ry.班组 = ''装配''
                GROUP BY gs.合同号
            ) htzp ON v.[合同号] = htzp.合同号
            LEFT JOIN (
                SELECT gs.合同号, SUM(gs.总时长) as 合同质检工时
                FROM [dbo].[View_生产工时视图] gs
                INNER JOIN [dbo].[登录基础数据_人员信息] ry ON gs.操作人 = ry.Personnel_Name
                WHERE ry.班组 = ''质检''
                GROUP BY gs.合同号
            ) htzj ON v.[合同号] = htzj.合同号
			left join SAP.SBO_YL.dbo.UBT_MES_ORDROPEN2 s 
			on v.合同号 = s.项目号 and v.物料编号 = s.物料号'
   
    if(@订单号>0)set @selectStrWhere=@selectStrWhere + ' and v.[订单编号]='+Convert(VARCHAR(10),@订单号)
    if(@合同号<>'')set @selectStrWhere=@selectStrWhere + ' and [合同号] like ''%'+@合同号+'%'''
    if(@产品编码<>'')set @selectStrWhere=@selectStrWhere + ' and [物料编号] like ''%'+@产品编码+'%'''
    if(@产品名称<>'')set @selectStrWhere=@selectStrWhere + ' and [物料描述] like ''%'+@产品名称+'%'''
	if(@辅助配件 = '不含')set @selectStrWhere=@selectStrWhere + ' and [物料描述] not like ''%软管总成%'' and [物料描述] not like ''%发货部件%'''
    if(@工序名称<>'')set @selectStrWhere=@selectStrWhere + ' and [工序名称] = '''+@工序名称+''''
    if(@要求完工日期1 <>'')set @selectStrWhere=@selectStrWhere + ' and [计划完成] >= '''+@要求完工日期1+''''
    if(@要求完工日期2 <>'')set @selectStrWhere=@selectStrWhere + ' and [计划完成] <= '''+@要求完工日期2+''''
    if(@计划开始 <>'')set @selectStrWhere=@selectStrWhere + ' and [计划开始时间] >= '''+@计划开始+''''
    if(@计划完成 <>'')set @selectStrWhere=@selectStrWhere + ' and [计划完成时间] <= '''+@计划完成+''''
    if(@排产状态='未完成')set @selectStrWhere=@selectStrWhere + ' and isnull([排产状态],0) = 0'
    else if(@排产状态='已完成')set @selectStrWhere=@selectStrWhere + ' and [排产状态] = 1'
    if(@物料类型='自制件') set @selectStrWhere=@selectStrWhere + ' and [不装配] <> ''否'''
    if(@物料类型='装配件') set @selectStrWhere=@selectStrWhere + ' and [不装配] = ''否'''
    if(@指派对象<>'全部') set @selectStrWhere=@selectStrWhere + ' and [指派对象] in (SELECT value FROM STRING_SPLIT('''+@指派对象+''', '',''))'
    set @selectStrWhere =  @selectStrWhere+ 'ORDER BY 
    CASE WHEN 齐套时间 IS NULL THEN 1 ELSE 0 END,
    齐套时间,计划开始时间 '
    set @selectStr=@selectStr+@selectStrWhere
    print @selectStr
    EXEC sp_executesql @selectStr
END