项目

一般

简介

内测BUG #7728 » usp_TrackCodeAssociate.sql

高 东阳, 2024-07-24 03:31

 
1
USE [E9_Track7.7]
2
GO
3
/****** Object:  StoredProcedure [dbo].[usp_TrackCodeAssociate]    Script Date: 2024-07-04 9:20:31 ******/
4
SET ANSI_NULLS ON
5
GO
6
SET QUOTED_IDENTIFIER ON
7
GO
8

    
9
ALTER  PROCEDURE [dbo].[usp_TrackCodeAssociate]                
10

    
11
    @action INT, --动作, 1:解除关系, 2:替换条码,3: 创建关系, 4: 检验,5: 完工后替换,6:查箱码      
12

    
13
    @scope INT,--范围 , 1: 父级码(外包装),2:子级码(内包装),3:双级(内外包装),4 全部        
14

    
15
    @code1 VARCHAR(64), --条码1,父级码        
16

    
17
    @code2 VARCHAR(64)='',--条码2,子级码        
18

    
19
    @par1 VARCHAR(64)='', -- 备用参数, 可用 创建关系中的包装类型     
20

    
21
    @user varchar(32)='001',
22

    
23
    @ip   varchar(50)=''
24

    
25
  --WITH ENCRYPTION         
26

    
27
AS         
28

    
29
      
30

    
31
BEGIN        
32

    
33
	DECLARE @result VARCHAR(MAX);--返回参数        
34

    
35
	SET @result='操作成功';        
36

    
37
	declare @ParentJCode nvarchar(64);      
38

    
39
	DECLARE @taskStatus1 INT,@taskStatus2 INT;        
40

    
41
	Declare @AppendCode varchar(64),@TaskNo varchar(64);        
42

    
43
	DECLARE @PackSpecLayer NVARCHAR(20);--当前包装类型       
44

    
45
	declare @TaskNoReplace nvarchar(60);      
46

    
47
	declare @EquipNo nvarchar(20);     
48

    
49
	declare @EquipNo1 nvarchar(20);     
50

    
51
	declare @EquipNo2 nvarchar(20);      
52

    
53
	declare @status int;       
54

    
55
	declare @scanNo varchar(32), @boxNo varchar(32);
56

    
57
	declare @LogDes varchar(1000);     
58
	
59
	declare @maxCodeLevel int;
60

    
61
	declare @TaskNo1 nvarchar(20);     
62

    
63
	declare @PackFlag int; 
64
	--declare @currentCode1Level int;
65
	declare @topCode varchar(100);
66

    
67
	SELECT @taskStatus1=w.Status ,@AppendCode=c.AppendCode,@TaskNo=w.TaskNo,@PackSpecLayer=w.PkgLayerAction ,@maxCodeLevel=ps.LevelCount
68

    
69
	FROM trc_worktask w JOIN uv_TrackCode c ON w.taskno=c.taskno        
70
	join TRC_PackSpec ps on w.PackSpecNo=ps.PackSpecNo
71
	WHERE c.Jcode=@code1; 
72
     
73

    
74
	IF  @taskStatus1 IS NULL and @action in (2,4)        
75

    
76
	BEGIN        
77

    
78
	 SET @result=@code1+N' 所属任务不存在'        
79

    
80
	 RAISERROR (@result, 12,1);        
81

    
82
	 goto goout;        
83

    
84
	END        
85

    
86
	        
87

    
88
	IF @taskStatus1>5 and @taskStatus1<>13  and @action in (1,2,3,4)        
89

    
90
	BEGIN        
91

    
92
	SET @result=@code1+N' 所属任务已导出关联关系'        
93

    
94
	  RAISERROR (@result,12,1);        
95

    
96
	  goto goout;        
97

    
98
	END        
99

    
100
	SELECT @taskStatus2=w.Status FROM trc_worktask w JOIN uv_TrackCode c ON w.taskno=c.taskno WHERE c.Jcode=@code2;       
101

    
102
	declare @isMaxLevel int;  
103

    
104
	set @isMaxLevel=0;      
105

    
106
	select @isMaxLevel=(case t.Level when LevelCount then 1 else 0 end)        
107

    
108
	from TRC_PackSpec p join TRC_WorkTask w on p.PackSpecNo=w.PackSpecNo        
109

    
110
	join TRC_TrackCode t  on t.TaskNo=w.TaskNo        
111

    
112
	where JCode=@code1;  
113

    
114
	       
115

    
116
	declare @isMinLevel int;        
117

    
118
	set @isMaxLevel=0;        
119

    
120
	select @isMinLevel= Level from TRC_TrackCode where JCode=@code1;        
121

    
122
	declare @currentTaskNo varchar(100);
123
	
124
	
125
	set @currentTaskNo=(select TaskNo from Trc_TrackCode with(nolock) where JCode=@code1);      
126

    
127
	IF @action=1--解除关系        
128

    
129
	BEGIN  
130

    
131
		IF @taskStatus1>=6        
132

    
133
		BEGIN        
134

    
135
		SET @result=@code1+N' 所属任务已导出,不能解除关系'        
136

    
137
		   RAISERROR (@result,12,1);        
138

    
139
		   goto goout;        
140

    
141
		END 
142
				
143
		IF isnull( (Select parentjcode From TRC_TrackCode WHERE JCode=@code1),'')!='' and @scope<=4  and @scope<>1      
144

    
145
		BEGIN        
146

    
147
			SET @result=@code1+N' 存在父级码,不允许解除下级'        
148

    
149
			RAISERROR (@result,12,1);        
150

    
151
			goto goout;        
152

    
153
		END   
154

    
155
		IF exists (Select * From TRC_TrackCode with(nolock) WHERE JCode=@code1 and status=4)
156

    
157
		BEGIN        
158

    
159
			SET @result=@code1+N' 已报废,不允许解除操作'        
160

    
161
			RAISERROR (@result,12,1);        
162

    
163
			goto goout;        
164

    
165
		END  
166

    
167
		declare @currentFlag int;
168
		declare @sanzhuangFanhui bit=1; --解除下级散装码是否返回到原任务
169
		declare @tempLevelCodes CodeLevelTable; -- Table(JCode varchar(100),Level int,PackFlag int,TaskNo varchar(100),TaskNo1 varchar(100)); --存储码和级别
170
		declare @tempMaxLevel int;
171
		declare @releaseAction varchar(100);
172

    
173
		set @currentFlag=(select PackFlag from Trc_TrackCode with(nolock) where JCode=@code1);
174

    
175
		if exists (SELECT * FROM TRC_ParaSet WITH(nolock) WHERE ParaCode='PinxiangCodeToOldTaskWhenReleasePack' and ParaValue='0')
176
		begin
177
		 set @sanzhuangFanhui=0;
178
		end
179

    
180
		insert into @tempLevelCodes(JCode,Level,PackFlag,TaskNo,TaskNo1) 
181
		select JCode,Level,PackFlag,TaskNo,TaskNo1 from dbo.fn_GetAllSubCodes(@code1);
182
			   
183
		set @tempMaxLevel=(select max([Level]) from @tempLevelCodes);
184

    
185
		if @scope<4 and @scope<>1  --解除下级  
186
		begin 
187

    
188
			set @releaseAction= @code1+'解除下级';
189

    
190
			if @sanzhuangFanhui=1 and @currentFlag in (4,5)
191
			begin
192

    
193
			    declare @subLevelValue int;
194

    
195
				set @subLevelValue=(select Level-1 from Trc_TrackCode with(nolock) where JCode=@code1);
196

    
197
			    exec Usp_ReturnMultiUnSanzhuangCodeToOrginState @tempLevelCodes,@currentTaskNo,@releaseAction,@subLevelValue;
198

    
199
				exec Usp_ReturnMultiSanzhuangCodeToOrginTask @tempLevelCodes,@currentTaskNo,@releaseAction;
200
				
201
			end
202
			else
203
			begin
204
				update t 
205
				set 
206
				t.Status=(case isnull(ScanNo2,0) when 0 then 1 else 2 end),
207
				t.ParentJCode=null,
208
				t.EquipNo=isnull(EquipNo2,null), 
209
				t.TaskNo=(case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode) when 0 then null else TaskNo end),
210
				t.TaskNo1=(case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode) when 0 then null else TaskNo1 end),
211
				t.EquipNo1=null, 
212
				t.ScanNo=0,
213
				t.ScanNo2=0 ,
214
				PackFlag=(
215
				            case Level 
216
							when 1 then 0 
217
							else (
218
							        case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode) 
219
								    when 0 then 0 
220
									else (
221
									        case PackFlag 
222
											when 4 then (case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode and PackFlag=4) when 0 then 2 else PackFlag end) 
223
											when 5 then (case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode and PackFlag in (3,5)) when 0 then 3 else PackFlag end) 
224
											else PackFlag 
225
											end) 
226
									end) 
227
							 end),
228
				t.Remark=(isnull(t.Remark,'')+'->[UnReturn_Sub_'+@releaseAction+']') 
229
				from TRC_TrackCode t
230
				where t.ParentJCode=@code1;
231
			end
232

    
233
			if (select Count(*) from Trc_TrackCode with(nolock) where JCode = @code1 and PackFlag in (4,5) and isnull(TaskNo,'')<>isnull(TaskNo1,''))=0 or @sanzhuangFanhui<>1
234
			begin
235
				--把当前的父级码状态给清掉
236
				update c 
237
				set Status=1,
238
				PackFlag=(case (select count(*) from Trc_TrackCode with(nolock) where JCode=c.JCode) when 0 then 0 else PackFlag end),
239
				ParentJCode=null,
240
				BoxNo=null,
241
				EquipNo2=null,
242
				ScanNo=0,
243
				TaskNo=(case TaskNo when TaskNo1 then null else TaskNo1 end),
244
				TaskNo1=(case TaskNo when TaskNo1 then null else TaskNo1 end),
245
				Remark=isnull(Remark,'')+'->[UnReturnPar_解除下级]' 
246
				from TRC_TrackCode c
247
				where  c.JCode=@code1;
248

    
249
				update TRC_TrackCode_StateRecord set IsCancel=1 where JCode=@code1 and ToTask=@currentTaskNo+'_P4' and IsCancel<>1;
250
			end
251
			else
252
			begin
253
				exec Usp_ReturnSanzhuangCodeToOrginTask @code1,@currentTaskNo,@releaseAction;
254
			end
255
			  
256
			set @LogDes =  '解除下级:'+@code1;
257

    
258
		end    
259

    
260
		else if @scope=4  --解除全部下级  
261
		begin   
262
		       set @releaseAction=@code1+'解除全部下级';
263

    
264
			   if @sanzhuangFanhui=1
265
			   begin
266

    
267
			    --把拼箱内的非散装码处理掉
268
			    exec Usp_ReturnMultiUnSanzhuangCodeToOrginState @tempLevelCodes,@currentTaskNo,@releaseAction;
269

    
270
			    --当前任务内的非拼箱的正常码再处理掉
271
				update TRC_TrackCode 
272
				set Status=1,
273
				ParentJCode=null,
274
				EquipNo=null,
275
				EquipNo1=null,
276
				EquipNo2=null,
277
				TaskNo=null,
278
				TaskNo1=null,
279
				ScanNo=0,
280
				ScanNo2=0,
281
				PackFlag=0,
282
				Remark=(isnull(Remark,'')+'->[Return_'+@releaseAction+']') 
283
				where JCode in (select JCode from @tempLevelCodes where PackFlag not in (4,5) or (PackFlag in (4,5) and TaskNo=TaskNo1))
284
				and TaskNo=@currentTaskNo;
285

    
286
				--3、将散装码都归还到原任务中去				
287
				exec Usp_ReturnMultiSanzhuangCodeToOrginTask @tempLevelCodes,@currentTaskNo,@releaseAction;
288

    
289

    
290
			   end
291
			   else
292
			   begin
293
					update TRC_TrackCode 
294
						set Status=1,
295
						ParentJCode=null,
296
						EquipNo=null,
297
						EquipNo1=null,
298
						EquipNo2=null,
299
						TaskNo=null,
300
						TaskNo1=null,
301
						ScanNo=0,
302
						ScanNo2=0 ,
303
						PackFlag=0,
304
						Remark=(isnull(Remark,'')+'->[UnReturn_'+@releaseAction+']') 
305
					where JCode in (select JCode from @tempLevelCodes) ;
306
			   end
307

    
308
			  set @LogDes =  '解除全部下级:'+@code1;      
309

    
310
		end 
311

    
312
		else if @scope=5 or @scope=1 --解除上级
313

    
314
			begin
315

    
316
			   set @releaseAction= @code1+'_解除上级';
317

    
318
			   if (select Count(*) from Trc_TrackCode with(nolock) where JCode = @code1 and PackFlag in (4,5) and isnull(TaskNo,'')<>isnull(TaskNo1,''))=0 or @sanzhuangFanhui<>1
319
			   begin
320
					update t 
321
					set Status=(case isnull(ScanNo2,0) when 0 then 1 else 2 end ),
322
					ParentJCode=null,
323
					BoxNo=null,
324
					EquipNo1=null,
325
					EquipNo=isnull(EquipNo2,null),
326
					ScanNo2=0 ,
327
					t.TaskNo=(case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode) when 0 then null else TaskNo end),
328
					t.TaskNo1=(case (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=t.JCode) when 0 then null else TaskNo1 end),
329
					Remark=(isnull(Remark,'')+'->[UnPinXiang_'+@releaseAction+']') 
330
					from TRC_TrackCode t where JCode=@code1;  
331
			   end
332
			   else
333
			   begin
334
			      --能走到这里说明当前的这个码是别的任务拼箱过来的
335

    
336
				  --1、将散装码都归还到原任务中去				
337
				  exec Usp_ReturnMultiSanzhuangCodeToOrginTask @tempLevelCodes,@currentTaskNo,@releaseAction;
338

    
339
				  --2、当前任务中的码
340
				  update TRC_TrackCode 
341
				  set 
342
				  Status=(case isnull(ScanNo2,0) when 0 then (case Level when 1 then 0 else 1 end) else 2 end),
343
				  ParentJCode=(case Level when @tempMaxLevel then null else ParentJCode end), --如果是当前解除下级码的直接子级那么清空父级码,否则要保留
344
				  EquipNo=isnull(EquipNo2,null),
345
				  TaskNo=TaskNo1,
346
				  EquipNo1=(case Level when @tempMaxLevel then null else EquipNo1 end),
347
				  ScanNo=(case Level when @tempMaxLevel then 0 else ScanNo end),
348
				  ScanNo2=(case Level when @tempMaxLevel then 0 else ScanNo2 end),
349
				  PackFlag=1, --恢复散装状态
350
				  Remark=(isnull(Remark,'')+'->[PinXiang_'+@releaseAction+']') 
351
				  where JCode in (Select JCode From @tempLevelCodes) and TaskNo=@currentTaskNo;
352

    
353
			   end
354

    
355
				set @LogDes =  '解除上级:'+@code1;    
356

    
357
			end
358

    
359
     
360
		update trc_worktask set FinishCount=(select count(*) from TRC_TrackCode with(nolock) where TaskNo=@currentTaskNo and Level=1 and status not in (0,4)) where TaskNo=@currentTaskNo; 
361

    
362
		exec usp_LogTrackCode_Asso '解除',@code1,'', @LogDes,@user,@ip,123,@currentTaskNo; 
363

    
364
		exec Usp_TrackCodeExceptionForWMS @code1,@code2,@action,@topCode;
365

    
366
		insert into IT_WMS_PreTransferInfo2PtsV8(JCode1,JCode2,Action,Scope,AddTime) values (@code1,@code2,@action,@scope,SYSDATETIME());
367

    
368
	END        
369

    
370
	ELSE IF @action=2 --替换条码        
371

    
372
	BEGIN  
373
	
374
		if isnull(@code2,'')=''
375
		begin
376
			SET @result=N'  新码为空,不能替换'        
377

    
378
			RAISERROR (@result,12,1);        
379

    
380
			goto goout; 
381
		end
382

    
383
		if @scope=1 and @code1=@code2    
384

    
385
		  begin        
386

    
387
			  SET @result=@code2+N'  新旧替换码不能相同'        
388

    
389
			  RAISERROR (@result,12,1);        
390

    
391
			  goto goout;        
392

    
393
		  end  
394

    
395
		  if @scope=1 and (exists (select * from Trc_TrackCode with(nolock) where ParentJCode=@code2))
396

    
397
		  begin        
398

    
399
			  SET @result=@code2+N'  存在子码,不能替换'        
400

    
401
			  RAISERROR (@result,12,1);        
402

    
403
			  goto goout;        
404

    
405
		  end  
406

    
407
		if exists(select * from TRC_TrackCode with(nolock) where JCode=@code1 and (Status=0 or Status=4))   
408
		begin        
409

    
410
			SET @result=@code1+N'  未使用或已废弃,不能替换'        
411

    
412
			RAISERROR (@result,12,1);        
413

    
414
			goto goout;        
415

    
416
		end  
417

    
418
		if exists(select * from TRC_TrackCode with(nolock) where JCode=@code2 and isnull(ParentJCode,'')<>'')   
419
		begin        
420

    
421
			SET @result=@code2+N'  已扫描,不能替换'        
422

    
423
			RAISERROR (@result,12,1);        
424

    
425
			goto goout;        
426

    
427
		end  
428

    
429
		if exists(select * from TRC_TrackCode with(nolock) where JCode=@code2 and Status=4)     
430
		begin        
431

    
432
			SET @result=@code2+N'  已废弃,不能替换'        
433

    
434
			RAISERROR (@result,12,1);        
435

    
436
			goto goout;        
437

    
438
		end  
439
		
440
		if @scope=2 and ( exists(select * from TRC_TrackCode with(nolock) where JCode=@code2 and (Status=0 or Status=9)))      
441
		begin        
442

    
443
			SET @result=@code2+N'  不存在或已扫描,不能替换'        
444

    
445
			RAISERROR (@result,12,1);        
446

    
447
			goto goout;        
448

    
449
		end     
450
		
451
		if @scope=1 and ( not exists(select * from TRC_TrackCode with(nolock) where JCode=@code2) )     
452

    
453
		  begin        
454

    
455
			  SET @result=@code2+N'  不存在,不能替换'        
456

    
457
			  RAISERROR (@result,12,1);        
458

    
459
			  goto goout;        
460

    
461
		  end  
462

    
463
	   --如果还没有建立关联关系,不能替换       
464

    
465
	   if  exists(select JCode from TRC_TrackCode where JCode=@code1 and (taskNO is  null or ( parentJCode is null and level=1)))      
466
		begin            
467

    
468
		  SET @result=@code1+N'  尚未建立关联关系,不能替换'        
469

    
470
		  RAISERROR (@result, 12, 1);        
471

    
472
		  goto goout;        
473

    
474
		end  
475
		
476
		declare @replaceCodeLevel int;
477

    
478
		select @status=Status,@parentJCode =ParentJCode,@TaskNoReplace=TaskNo,@replaceCodeLevel=[Level], @EquipNo=EquipNo,@EquipNo1=EquipNo1,@EquipNo2=EquipNo2,@scanNo=ScanNo,@boxNo=BoxNo ,@TaskNo1=TaskNo1,@PackFlag=PackFlag from TRC_TrackCode where JCode=@code1;      
479

    
480
		if @scope=1  --内包装替换:标签替换      
481
		begin      
482

    
483
		  update TRC_TrackCode set ParentJCode=@code2 where ParentJCode=@code1;      
484

    
485
		  update TRC_TrackCode SET  Status= @status,ParentJCode=@parentJCode, TaskNo=@TaskNoReplace,TaskNo1=@TaskNo1,EquipNo=@EquipNo,EquipNo1=@EquipNo1,EquipNo2=@EquipNo2,PackFlag=@PackFlag,ScanNo=@scanNo,BoxNo=@boxNo,EditOn=SYSDATETIME(),Remark=isnull(Remark,'')+'->标签替换:将原有'+@code1+'替换掉' where JCode=@code2;       
486

    
487
		  update TRC_TrackCode set ParentJCode = null,Status=4,PackFlag=0,Remark=isnull(Remark,'')+'->标签替换:被'+@code2+'替换' where JCode=@code1;  
488

    
489
		  set @LogDes ='追溯码内包装替换:' + @code1+'被替换成'+@code2+','+@code1+'码废弃同时自身信息及上下级关系被'+@code2+'取代';      
490

    
491
		end      
492
		else  if @scope=2 --外包装替换 :实物替换[场景是拿了一个实物中包直接替换了另外一个旧的实物中包,例如一个新的中包替换了一个旧的中包,新的中包是带着自己的下级的,原有的中包和内部的自己实物都被搬走了]     
493
		begin      
494

    
495
		   update TRC_TrackCode SET ParentJCode=@parentJCode,Status=2,TaskNo=@TaskNoReplace,TaskNo1=@TaskNo1,EquipNo=@EquipNo,EquipNo1=@EquipNo1,EquipNo2=@EquipNo2,PackFlag=@PackFlag,ScanNo=@scanNo,BoxNo=@boxNo,Remark=isnull(Remark,'')+'->整包替换:将原包'+@code1+'替换掉' where JCode=@code2;  
496

    
497
		   update Trc_TrackCode Set TaskNo=@TaskNoReplace,BoxNo=@boxNo,EquipNo=@EquipNo,Remark=isnull(Remark,'')+'->整包替换:将原包'+@code1+'的子级替换掉' where  ParentJCode=@code2;
498

    
499
		   declare @parLevelBoxType int;
500
		   declare @subLevelBoxType int;
501

    
502
		   --set @parLevelBoxType=(select dbo.fn_GetWorkBoxType(@TaskNoReplace,@replaceCodeLevel));
503

    
504
		   --set @parLevelBoxType=(select dbo.fn_GetWorkBoxType(@TaskNoReplace,@replaceCodeLevel-1));
505

    
506
		   update TRC_TrackCode set ParentJCode = null,Status=4,Remark=isnull(Remark,'')+'->整包替换:被'+@code2+'替换掉' where JCode=@code1; 
507

    
508
		   --update TRC_TrackCode set Status=4,Remark=isnull(Remark,'')+'->整包替换:被'+@code2+'的新子级替换掉' where ParentJCode=@code1; 
509

    
510
		   --一般中盒坏了把中盒废弃,子码还有在12级工位重新关联,因此子码状态变为已打印
511
		   update TRC_TrackCode set Status=1,TaskNo=null,TaskNo1=null,BoxNo='',ParentJCode=null where ParentJCode=@code1; 
512

    
513
		   --update TRC_TrackCode set ParentJCode = null,Status=(case @parLevelBoxType when 1 then 4 else 1 end) where JCode=@code1; 
514

    
515
		   --update TRC_TrackCode set Status=(case @subLevelBoxType when 1 then 1 else 2 end) where ParentJCode=@code1; 
516

    
517
		   set @LogDes ='追溯码外包装替换:' + @code1+'被替换成'+@code2+ ','+@code1+'码废弃的同时自身信息及同上级的关系被'+@code2+'取代';  
518

    
519
		end      
520

    
521
		exec usp_LogTrackCode_Asso '替换',@code1,@code2, @LogDes ,@user,@ip,122,@currentTaskNo;  
522
		
523
		exec Usp_TrackCodeExceptionForWMS @code1,@code2,@action,@topCode;
524

    
525
		insert into IT_WMS_PreTransferInfo2PtsV8(JCode1,JCode2,Action,Scope,AddTime) values (@code1,@code2,@action,@scope,SYSDATETIME());
526

    
527
	END        
528

    
529
	ELSE IF @action=5--完工后替换        
530
	BEGIN        
531

    
532
		IF NOT EXISTS (select * from TRC_TrackCode where JCode=@code2 and Status=1)        
533
		begin        
534

    
535
			SET @result=@code2+N'  不存在或已扫描,不能替换'        
536

    
537
			RAISERROR (@result,12,1);        
538

    
539
			goto goout;        
540

    
541
		end         
542

    
543
		--如果还没有建立关联关系,不能替换       
544
		IF NOT EXISTS(select ParentJcode from TRC_TrackCode_Export where JCode=@code1)        
545
		begin            
546

    
547
			SET @result=@code1+N'  不存在或尚未导出,不能替换'        
548

    
549
			RAISERROR (@result,12,1);        
550

    
551
			goto goout;        
552

    
553
		end   
554
		
555
		set @currentTaskNo=(select TaskNo from TRC_TrackCode_Export with(nolock) where JCode=@code1);    
556

    
557
		if exists (select * from Trc_TrackCode where JCode=@code2)
558
		begin
559
			INSERT INTO  TRC_TrackCode_Export(JCode,RID,RCode,TaskNo,TaskNo1,[LEVEL],Status,ParentJCode,remark,printCount,PrintBatchNo,ImportBatchNo,AppendCode,PackFlag,BoxNo,LineName,scanno,scanno2,equipno,addby,addon,editby,editon,BatchNum1,BatchNum2)        
560
			SELECT JCode,RID,RCode,TaskNo,TaskNo1,[LEVEL],Status,ParentJCode,remark,printCount,PrintBatchNo,ImportBatchNo,AppendCode,isnull(PackFlag,0) PackFlag,BoxNo,LineName,scanno,scanno2,equipno,addby,addon,editby,editon,BatchNum1,BatchNum2        
561
			FROM trc_trackCode WHERE JCode=@code2 or ParentJCode=@code2; 
562

    
563
			DELETE FROM  TRC_TrackCode WHERE JCode=@code2 or ParentJCode=@code2; 
564
		
565
		end
566
		else if exists (select * from TRC_TrackCode_Actived where JCode=@code2)
567
		begin
568
			INSERT INTO  TRC_TrackCode_Export(JCode,RID,RCode,TaskNo,TaskNo1,[LEVEL],Status,ParentJCode,remark,printCount,PrintBatchNo,ImportBatchNo,AppendCode,PackFlag,BoxNo,LineName,scanno,scanno2,equipno,addby,addon,editby,editon,BatchNum1,BatchNum2)        
569
			SELECT JCode,RID,RCode,TaskNo,TaskNo1,[LEVEL],Status,ParentJCode,remark,printCount,PrintBatchNo,ImportBatchNo,AppendCode,isnull(PackFlag,0) PackFlag,BoxNo,LineName,scanno,scanno2,equipno,addby,addon,editby,editon,BatchNum1,BatchNum2        
570
			FROM TRC_TrackCode_Actived WHERE JCode=@code2 or ParentJCode=@code2; 
571

    
572
			DELETE FROM  TRC_TrackCode_Actived WHERE JCode=@code2 or ParentJCode=@code2; 
573
		end
574

    
575
		select @status=Status,@parentJCode =ParentJCode,@TaskNoReplace=TaskNo,@EquipNo=EquipNo,@EquipNo1=EquipNo1,@EquipNo2=EquipNo2,@scanNo=ScanNo,@boxNo=BoxNo,@PackFlag=PackFlag from TRC_TrackCode_Export where JCode=@code1;      
576

    
577
		if @scope=1  --内包装替换       
578
		begin      
579

    
580
		  update TRC_TrackCode_Export set ParentJCode=@code2 where ParentJCode=@code1;      
581

    
582
		  update TRC_TrackCode_Export SET ParentJCode=@parentJCode, Status= @status,TaskNo=@TaskNoReplace,TaskNo1=@TaskNoReplace,EquipNo=@EquipNo,EquipNo1=@EquipNo1,EquipNo2=@EquipNo2,ScanNo=@scanNo,BoxNo=@boxNo,PackFlag=@PackFlag where JCode=@code2;       
583

    
584
		  update TRC_TrackCode_Export set ParentJCode = null,Status=4,TaskNo=null where JCode=@code1;      
585

    
586
		  set @LogDes ='追溯码内包装替换:' + @code1+'被替换成'+@code2+','+@code1+'码废弃同时自身信息及上下级关系被'+@code2+'取代'; 
587

    
588
		end      
589
		else  if @scope=2 --外包装替换      
590
		begin      
591

    
592
           --接管原有码信息
593

    
594
           update TRC_TrackCode_Export SET ParentJCode=@parentJCode,Status=2,TaskNo=@TaskNoReplace,TaskNo1=@TaskNoReplace,EquipNo=@EquipNo,EquipNo1=@EquipNo1,EquipNo2=@EquipNo2,PackFlag=2,ScanNo=@scanNo,BoxNo=@boxNo where JCode=@code2;       
595

    
596
		   --新的子码接收原有任务信息
597

    
598
           update TRC_TrackCode_Export Set TaskNo=@TaskNoReplace,BoxNo=@boxNo,EquipNo=@EquipNo where  ParentJCode=@code2;
599

    
600
		   --原有父级码解除与老上级的关系并废弃
601

    
602
           update TRC_TrackCode_Export set ParentJCode = null,Status=4 where JCode=@code1; 
603

    
604
		   --原有的子码废弃
605

    
606
           update TRC_TrackCode_Export set Status=4 where ParentJCode=@code1; 
607

    
608
		   set @LogDes ='控制端完工后追溯码外包装替换:' + @code1+'被替换成'+@code2+ ','+@code1+'码废弃的同时自身信息及同上级的关系被'+@code2+'取代';  
609

    
610

    
611
		end  
612

    
613
		exec usp_LogTrackCode_Asso '完工后替换',@code1,@code2, @LogDes,@user,@ip,122,@currentTaskNo;         
614

    
615
		exec Usp_TrackCodeExceptionForWMS @code1,@code2,@action,@topCode;
616

    
617
	END         
618

    
619
	ELSE IF @action=3--创建关系        
620
	BEGIN        
621

    
622
		declare @c1 int,@c2 int;  
623
		declare @sbc int;
624
		declare @tc int;
625

    
626
		set @c1=1;set @c1=1;  
627

    
628
		if exists(select * from TRC_TrackCode where JCode=@Code1)        
629

    
630
		set @c1=0;  
631

    
632
		if exists(select * from TRC_TrackCode where JCode=@Code2)        
633

    
634
		set @c2=0; 
635

    
636
		select @EquipNo1=EquipNo1 from trc_TrackCode with(nolock) where JCode=@code2;
637

    
638
		declare @fullPackCount int;       
639

    
640
		if @c1=0 and @c2=0        
641
		begin 
642
		
643
			IF EXISTS (select * from TRC_TrackCode where JCode=@code1 and Status=4)        
644
			begin        
645

    
646
				SET @result=@code1+N' 已废弃不能追加'        
647

    
648
				RAISERROR (@result,12,1);        
649

    
650
				goto goout;        
651

    
652
			end 
653

    
654
		  set @fullPackCount =( select isnull(pd.Count,0) from Trc_TrackCode c with(nolock) join Trc_WorkTask t with(nolock) on c.TaskNo=t.TaskNo join  TRC_PackSpec_Detail pd with(nolock) on pd.PackSpecNo=t.PackSpecNo and pd.Level=c.Level-1 where c.JCode=@code2);
655

    
656
		  if (select count(*) from TRC_TrackCode with(nolock) where ParentJCode=@code2)>=@fullPackCount
657
		   begin
658

    
659
			SET @result=@code2+N'  已经满包装,不能继续追加'        
660

    
661
			RAISERROR (@result, 12, 1);        
662

    
663
			goto goout;  
664
		   end
665

    
666
			UPDATE t 
667
			SET ParentJCode=@Code2,
668
			Status=2,
669
			LineName=(select LineName from Trc_TrackCode with(nolock) where JCode=@Code2),
670
			EquipNo=(select EquipNo from Trc_TrackCode with(nolock) where JCode=@Code2),
671
			BoxNo=(select BoxNo from Trc_TrackCode with(nolock) where JCode=@Code2),
672
			EquipNo1=(select EquipNo2 from Trc_TrackCode with(nolock) where JCode=@Code2),
673
			TaskNo=(select TaskNo from Trc_TrackCode with(nolock) where JCode=@Code2), 
674
			TaskNo1=(case isnull(TaskNo1,'') when '' then (select TaskNo from Trc_TrackCode with(nolock) where JCode=@Code2) else TaskNo1 end),
675
			PackFlag=(case isnull(PackFlag,0) when 0 then 2 else PackFlag end)
676
			from trc_trackCode t
677
			WHERE Jcode=@Code1;  
678
			
679
			set @sbc = (select count(*) from Trc_TrackCode with(nolock) where ParentJCode=@code2);
680
			set @tc=(select count(*) from (select distinct TaskNo1 from Trc_TrackCode with(nolock) where ParentJCode=@code2 and isnull(TaskNo1,'')<>'') v);
681

    
682
			UPDATE trc_trackCode SET packFlag=(CASE @sbc WHEN @fullPackCount THEN (case @tc when 1 then 2 else 4 end) else  (case @tc when 1 then 3 else 5 end) END) WHERE Jcode=@Code2;
683
          
684

    
685
		   set @LogDes ='生产表的码:' + @code1+'被追加到生产表的码:'+@code2;  
686

    
687
		   insert into IT_WMS_PreTransferInfo2PtsV8(JCode1,JCode2,Action,Scope,AddTime) values (@code1,@code2,@action,@scope,SYSDATETIME());
688

    
689
		end        
690
		else if @c1=1 and @c2=1        
691

    
692
		begin 
693

    
694
		 set @fullPackCount =( select isnull(pd.Count,0) from TRC_TrackCode_Export c with(nolock) join Trc_WorkTask t with(nolock) on c.TaskNo=t.TaskNo join  TRC_PackSpec_Detail pd with(nolock) on pd.PackSpecNo=t.PackSpecNo and pd.Level=c.Level-1 where c.JCode=@code2);
695

    
696
		  if (select count(*) from TRC_TrackCode_Export with(nolock) where ParentJCode=@code2)>=@fullPackCount
697
		   begin
698

    
699
			SET @result=@code2+N'  已经满包装,不能继续追加'        
700

    
701
			RAISERROR (@result, 12, 1);        
702

    
703
			goto goout;  
704
		   end
705
		
706
		    SET @currentTaskNo = (SELECT TaskNo FROM TRC_TrackCode_Export WITH(nolock) WHERE JCode=@Code1);
707

    
708
		    select @EquipNo1=EquipNo1 from TRC_TrackCode_Export with(nolock) where JCode=@code2;       
709

    
710
			UPDATE TRC_TrackCode_Export SET ParentJCode=@Code2,EquipNo=@EquipNo1 WHERE Jcode=@Code1;            
711

    
712
			set @sbc = (select count(*) from Trc_TrackCode_Export with(nolock) where ParentJCode=@code2);
713
			set @tc=(select count(*) from (select distinct TaskNo1 from Trc_TrackCode_Export with(nolock) where ParentJCode=@code2 and isnull(TaskNo1,'')<>'') v);
714

    
715
			UPDATE Trc_TrackCode_Export SET packFlag=(CASE @sbc WHEN @fullPackCount THEN (case @tc when 1 then 2 else 4 end) else  (case @tc when 1 then 3 else 5 end) END) WHERE Jcode=@Code2;
716
                         
717
			set @LogDes ='完工表的码:' + @code1+'被追加到完工表的码:'+@code2;     
718
		end        
719

    
720
		else         
721

    
722
		begin 
723

    
724
		    select @EquipNo1=EquipNo1 from TRC_TrackCode_Export with(nolock) where JCode=@code2;       
725

    
726
			INSERT INTO  TRC_TrackCode_Export(JCode,RID,RCode,TaskNo,TaskNo1,[LEVEL],Status,ParentJCode,remark,printCount,PrintBatchNo,ImportBatchNo,AppendCode,PackFlag,BoxNo,LineName,scanno,scanno2,equipno,addby,addon,editby,editon,BatchNum1,BatchNum2)  
727

    
728
			SELECT JCode,RID,RCode,TaskNo,TaskNo1,[LEVEL],Status,ParentJCode,remark,printCount,PrintBatchNo,ImportBatchNo,AppendCode,PackFlag,BoxNo,LineName,scanno,scanno2,equipno,addby,addon,editby,editon,BatchNum1,BatchNum2 FROM trc_trackCode  where JCode=@code1;    
729

    
730
			delete trc_trackCode  where JCode=@code1;        
731

    
732
			UPDATE TRC_TrackCode_Export SET ParentJCode=@Code2,EquipNo=@EquipNo1 WHERE Jcode=@Code1;            
733

    
734
			set @sbc = (select count(*) from Trc_TrackCode_Export with(nolock) where ParentJCode=@code2);
735
			set @tc=(select count(*) from (select distinct TaskNo1 from Trc_TrackCode_Export with(nolock) where ParentJCode=@code2 and isnull(TaskNo1,'')<>'') v);
736

    
737
			UPDATE Trc_TrackCode_Export SET packFlag=(CASE @sbc WHEN @fullPackCount THEN (case @tc when 1 then 2 else 4 end) else  (case @tc when 1 then 3 else 5 end) END) WHERE Jcode=@Code2;             
738

    
739
			set @LogDes ='生产表的码:' + @code1+'被追加到完工表的码:'+@code2;  
740
		end;         
741

    
742
		exec usp_LogTrackCode_Asso '追加',@code1,@Code2,@LogDes,@user,@ip,124,@currentTaskNo;   
743
		
744
		insert into IT_WMS_PreTransferInfo2PtsV8(JCode1,JCode2,Action,Scope,AddTime) values (@code1,@code2,@action,@scope,SYSDATETIME());
745

    
746
	END        
747

    
748
	ELSE IF @action=4--检验        
749

    
750
	BEGIN           
751

    
752
		IF EXISTS(SELECT * FROM uv_trackCode WHERE SourceFlag in (1,3) and JCode=@code2 AND ParentJCode=@code1)
753

    
754
		begin         
755

    
756
			SET @result='正确';		
757

    
758
		end        
759

    
760
		else 
761

    
762
		begin
763

    
764
			SET @result='错误';     
765

    
766
		end 
767

    
768
		SET @currentTaskNo = (SELECT TaskNo FROM TRC_TrackCode WITH(nolock) WHERE JCode=@Code1);
769

    
770
		set @LogDes='检验包装关系:'+@result;     
771

    
772
		exec usp_LogTrackCode_Asso '验证',@code1,@Code2,@LogDes,@user,@ip,121,@currentTaskNo;          
773

    
774
	END
775

    
776
	ELSE IF @action=6--检验2,通过物流码或追溯码检索父码,箱号,      
777

    
778
	BEGIN    
779

    
780
	       
781

    
782
		set @result='';      
783

    
784
		select @result= '追溯码:'+JCode +'\r\n 物流码:'+ ISNULL(appendCode,'')+'\r\n父码'+ISNULL(ParentJCode,'')+'\r\n箱号'+ISNULL(BoxNo,'') FROM uv_trackCode WHERE  JCode=@code1;      
785

    
786

    
787

    
788
		if ISNULL(@result,'')=''      
789

    
790
			SET @result='无效码';   
791

    
792
			--exec usp_LogTrackCode_Asso '验证',@code1,@Code2,'检验包装关系',@user,@ip;      
793

    
794
	END
795

    
796
		      
797

    
798
	goout:           
799

    
800
	SELECT @result;  
801

    
802
      
803

    
804
END
(5-5/5)