P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
<?xml version="1.0" encoding="UTF-8" ?>
    
<sqls>
    <dataSpace name="importSet">
        <sql name="importStateTemplate">
            <![CDATA[
                update @{toTempTable} 
                set state_code = (
                    select distinct code from sys_state_machine_state
                    where EXISTS (
                        select 1 from sys_state_machine
                        inner join sys_state_machine_detail on sys_state_machine_detail.parent_id = sys_state_machine.id 
                        inner join sys_state_machine_target on sys_state_machine_target.package_name = '@{dataName}' and sys_state_machine_target.state_machine_id = sys_state_machine.id 
                        where sys_state_machine.state_category_code = sys_state_machine_state.category_code and  sys_state_machine_detail.@{original_field} = sys_state_machine_state.code
                    )
                ),
                state_name = (
                    select distinct name from sys_state_machine_state
                    where EXISTS (
                        select 1 from sys_state_machine
                        inner join sys_state_machine_detail on sys_state_machine_detail.parent_id = sys_state_machine.id 
                        inner join sys_state_machine_target on sys_state_machine_target.package_name = '@{dataName}' and sys_state_machine_target.state_machine_id = sys_state_machine.id 
                        where sys_state_machine.state_category_code = sys_state_machine_state.category_code and  sys_state_machine_detail.@{original_field} = sys_state_machine_state.code
                    )
                )
                where io_batch_id = '@{ioBatchId}' and @{field} is null
            ]]>    
        </sql>
 
        <sql name="importCodeTemplate">
            <![CDATA[
                update @{toTempTable} set @{field} = temp_code
                where io_batch_id = '@{ioBatchId}' and @{field} is null
            ]]>    
        </sql>
        
        <sql name="importSetNewId">
            <![CDATA[
                update @{toTable} set id = @{guid}
                where io_batch_id = '@{ioBatchId}' and id is null
            ]]>    
        </sql>
        
        <sql name="importSetDictionaryTemplate">
            <![CDATA[
                update @{toTempTable} set @{field} = (
                    select sys_dictionary_detail.code from sys_dictionary
                    left join sys_dictionary_detail on sys_dictionary_detail.parent_id = sys_dictionary.id
                    where sys_dictionary.code = @{dictionaryKey} and sys_dictionary_detail.value = @{toTempTable}.type_name
                )
                where  io_batch_id = '@{ioBatchId}' and @{field} is null
            ]]>    
        </sql>
        
        <sql name="importDatanameTemplate">
            <![CDATA[
                update @{toTempTable} set @{field} = (
                    select @{original_field} from @{dataName} 
                    where @{mapping}
                )
                where  io_batch_id = '@{ioBatchId}' and @{field} is null
            ]]>    
        </sql>
        
        <sql name="importUpdateCustomerId">
            <![CDATA[
                update @{toTempTable}
                set customer_id = (
                    select id from dscn_crm.hm_institution 
                    where hm_institution.institution_code = @{toTempTable}.customer_code 
                )
                where io_batch_id = '@{ioBatchId}' and customer_id is null
            ]]>    
        </sql>
        
        <sql name="importUpdateProvinceCodeByCustomerId">
            <![CDATA[
                update @{toTempTable}
                set province_code = (
                    select province_id from dscn_crm.hm_institution 
                    where hm_institution.id = @{toTempTable}.customer_id 
                )
                where io_batch_id = '@{ioBatchId}' and province_code is null
            ]]>    
        </sql>
        
        <sql name="importUpdateCityCodeByCustomerId">
            <![CDATA[
                update @{toTempTable}
                set city_code = (
                    select city_id from dscn_crm.hm_institution 
                    where hm_institution.id = @{toTempTable}.customer_id 
                )
                where io_batch_id = '@{ioBatchId}' and city_code is null
            ]]>    
        </sql>
        
        <sql name="importUpdateProvinceByProvinceCode">
            <![CDATA[
                update @{toTempTable}
                inner join md_geo_city on md_geo_city.code = @{toTempTable}.city_code 
                set @{toTempTable}.province = md_geo_city.province
                where io_batch_id = '@{ioBatchId}' and @{toTempTable}.province is null 
            ]]>    
        </sql>
        
        <sql name="importUpdateCityByCityCode">
            <![CDATA[
                update @{toTempTable}
                inner join md_geo_city on md_geo_city.code = @{toTempTable}.city_code 
                set @{toTempTable}.city = md_geo_city.name 
                where io_batch_id = '@{ioBatchId}' and @{toTempTable}.city is null
            ]]>    
        </sql>
        
        <sql name="importSetTypeByImportType">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.type_code = (
                        select sys_dictionary_detail.code from sys_dictionary
                        left join sys_dictionary_detail on sys_dictionary_detail.parent_id = sys_dictionary.id
                        where sys_dictionary.code = concat('@{taskName}','_type'),  
                    @{toTempTable}.type_name = (
                        select sys_dictionary_detail.value from sys_dictionary
                        left join sys_dictionary_detail on sys_dictionary_detail.parent_id = sys_dictionary.id
                        where sys_dictionary.code = concat('@{taskName}','_type')
                where  io_batch_id = '@{ioBatchId}' and type_code is null
            ]]>    
        </sql>    
        
        <sql name="importSetCategoryCode">
            <![CDATA[
                update @{toTempTable} set category_code = (
                    select sys_dictionary_detail.code from sys_dictionary
                    left join sys_dictionary_detail on sys_dictionary_detail.parent_id = sys_dictionary.id
                    where sys_dictionary.code = 'authorization_sort' and sys_dictionary_detail.value = @{toTempTable}.category_name
                )
                where  io_batch_id = '@{ioBatchId}' and category_code is null
            ]]>    
        </sql>     
        
        <sql name="importSetStateCodeInit">
            <![CDATA[
                update @{toTempTable} 
                set @{toTempTable}.state_code = (
                        select sys_dictionary_detail.code from sys_dictionary
                        left join sys_dictionary_detail on sys_dictionary_detail.parent_id = sys_dictionary.id
                        where sys_dictionary.code = concat('@{toTempTable}','_state')),  
                    @{toTempTable}.state_name = (
                        select sys_dictionary_detail.value from sys_dictionary
                        left join sys_dictionary_detail on sys_dictionary_detail.parent_id = sys_dictionary.id
                        where sys_dictionary.code = concat('@{toTempTable}','_state'))
                where io_batch_id = '@{ioBatchId}' and  state_code is null
            ]]>    
        </sql>    
        
        <sql name="importSetInitState">
            <![CDATA[
                update @{toTempTable} 
                set @{toTempTable}.state_code = 'InitVersion',  
                    @{toTempTable}.state_name = '初版'
                where io_batch_id = '@{ioBatchId}' and  state_code is null
            ]]>    
        </sql>    
        
        <sql name="importSetDefaultState">
            <![CDATA[
                update @{toTempTable} 
                set  @{toTempTable}.state_code = 'Input' , @{toTempTable}.state_name = '录入'
                where  io_batch_id = '@{ioBatchId}' and  state_code is null
            ]]>    
        </sql>
        
        <sql name="importSetDefaultValidState">
            <![CDATA[
                update @{toTempTable} 
                set  @{toTempTable}.state_code = 'Open' , @{toTempTable}.state_name = '生效'
                where  io_batch_id = '@{ioBatchId}' and  state_code is null
            ]]>    
        </sql>
         
        <sql name="importSetProductIdByProductName">
            <![CDATA[
                update @{toTempTable} set product_id = (
                    select DISTINCT product_id from md_prod_sku
                    where md_prod_sku.product_name = @{toTempTable}.product_name and product_id is not null
                )
                where io_batch_id = '@{ioBatchId}' and product_id is null
            ]]>    
        </sql>    
        
        <sql name="importUpdateProductId">
            <![CDATA[
                update @{toTempTable}
                set product_id = (
                    select max(id)
                    from hm_product 
                    where hm_product.product_code = @{toTempTable}.product_code
                )
                where io_batch_id = '@{ioBatchId}' and product_id is null  
            ]]>    
        </sql>
        
        <sql name="importUpdateSkuId">
            <![CDATA[
                update @{toTempTable}
                set sku_id = (
                    select sku_id
                    from md_product_sku 
                    where md_product_sku.product_id = @{toTempTable}.product_id and md_product_sku.sku_code = @{toTempTable}.sku_code
                )
                where io_batch_id = '@{ioBatchId}' and sku_id is null  
            ]]>    
        </sql>
        
        <sql name="importUpdatePriceByProductId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.price = (
                    select ifnull(hm_rsm_product_province.unit_price,0)
                    from (
                        select product_code, quarter_no, max(unit_price) unit_price 
                        from dscn_crm.hm_rsm_product_province 
                        group by product_code, quarter_no
                    )  hm_rsm_product_province 
                    where hm_rsm_product_province.product_code = @{toTempTable}.sku_code
                    and hm_rsm_product_province.quarter_no = @{toTempTable}.quarter_no )
                where io_batch_id = '@{ioBatchId}' and price is null
            ]]>    
        </sql>
        
        <sql name="importSetEmployeeId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.employee_id = (
                    select min(id) from md_employee 
                    where md_employee.code = @{toTempTable}.employee_code
                    and md_employee.is_active != 'F')
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.employee_id is null
            ]]>    
        </sql>    
        
        <sql name="importUpdatePositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.position_id = (
                    select @{IfEmpty}(md_position.id, @{toTempTable}.position_code)
                    from md_position 
                    where md_position.code = @{toTempTable}.position_code)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.position_id is null 
                    and exists ( select 1 from md_position where md_position.code = @{toTempTable}.position_code)
            ]]>    
        </sql>    
        
        <sql name="importUpdateTypeByPositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.position_type = (
                    select duty from md_position 
                    where md_position.id = @{toTempTable}.position_id)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.position_type is null 
            ]]>    
        </sql>    
        
        <sql name="importUpdateParentByPositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.parent_position_id = (
                    select parent_id from md_position 
                    where md_position.id = @{toTempTable}.position_id)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.parent_position_id is null 
            ]]>    
        </sql>    
        
        <sql name="importUpdateTerritoryByPositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.territory_id = (
                    select territory_id from md_position 
                    where md_position.id = @{toTempTable}.position_id)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.territory_id is null 
            ]]>    
        </sql>    
        
        <sql name="importUpdateParentByTerritoryId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.parent_territory_id = (
                    select parent_id from md_territory 
                    where md_territory.id = @{toTempTable}.territory_id)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.parent_territory_id is null 
            ]]>    
        </sql>    
        
        <sql name="importSetPositionIdByEmployee">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.position_id = (
                    select position_id from (
                        select position_id, md_employee.code empployee_code, md_position.name position_name 
                        from md_position 
                        left join md_position_employee on md_position.id  = md_position_employee.position_id 
                        left join md_employee on md_employee.id = md_position_employee.employee_id  
                        where  md_position.is_delete = 'F' ) positionEmployee 
                 where positionEmployee.empployee_code = @{toTempTable}.position_employee_code
                    and positionEmployee.position_name = @{toTempTable}.position_name ) 
                    where @{toTempTable}.io_batch_id = '@{ioBatchId}' 
            ]]>    
        </sql>    
        
        <sql name="importSetPositionIdByEmployeeId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.position_id = (
                    select distinct md_position.id from md_position 
                        left join md_position_employee on md_position.id  = md_position_employee.position_id 
                        left join md_employee on md_employee.id = md_position_employee.employee_id  
                        where  md_position.is_delete = 'F'and md_position_employee.employee_id = @{toTempTable}.employee_id )  
                 where @{toTempTable}.position_id is null and @{toTempTable}.io_batch_id = '@{ioBatchId}' 
            ]]>    
        </sql>    
        
        <sql name="importSetPositionNameById">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.position_name = (
                    select distinct md_position.name from md_position 
                    where md_position.id  = @{toTempTable}.position_id and md_position.is_delete = 'F' )  
                 where @{toTempTable}.position_name is null and @{toTempTable}.io_batch_id = '@{ioBatchId}' 
            ]]>    
        </sql>    
        
        <sql name="importSetRegionIdByPositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.region_id = (
                    select distinct level2 from md_position_hierarchy 
                    where md_position_hierarchy.position_id  = @{toTempTable}.position_id)  
                 where @{toTempTable}.region_id is null and @{toTempTable}.io_batch_id = '@{ioBatchId}' 
            ]]>    
        </sql>    
        
        <sql name="importSetRegionNameByPositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.region_name = (
                    select distinct md_position.region from md_position 
                    where md_position.id  = @{toTempTable}.position_id and md_position.is_delete = 'F' )  
                 where @{toTempTable}.region_name is null and @{toTempTable}.io_batch_id = '@{ioBatchId}' 
            ]]>    
        </sql>
        
        <sql name="importSetRegionByPositionId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.region = (
                    select distinct md_position.region from md_position 
                    where md_position.id  = @{toTempTable}.position_id and md_position.is_delete = 'F' )  
                 where @{toTempTable}.region is null and @{toTempTable}.io_batch_id = '@{ioBatchId}' 
            ]]>    
        </sql>
        
        <sql name="importSetPositionLevelPositionId">
            <![CDATA[
                update @{toTempTable}
                set  @{toTempTable}.position_level = (
                    select level_no from md_position
                    where md_position.id = @{toTempTable}.position_id )
                where  @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.position_level is null
            ]]>    
        </sql>    
        
        <sql name="importUpdateQuarterNo">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.quarter_no = concat(year, 'Q', quarter)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.quarter_no is null 
            ]]>    
        </sql>
        
        <sql name="importUpdateCoManagerRatio">
            <![CDATA[
                update @{toTempTable} 
                inner join md_territory on md_territory.id = @{toTempTable}.territory_id
                inner join dscn_crm.hm_territory_hospital_product_relation on 
                hm_territory_hospital_product_relation.product_code = @{toTempTable}.product_code 
                and  hm_territory_hospital_product_relation.hospital_code = @{toTempTable}.customer_code
                and  hm_territory_hospital_product_relation.territory_code = md_territory.code
                set  @{toTempTable}.co_manage_ratio = hm_territory_hospital_product_relation.co_manage_ratio
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.co_manage_ratio is null 
            ]]>    
        </sql>
        
        <sql name="importUpdateCoManagerType">
            <![CDATA[
                update @{toTempTable} 
                left join (
                    select customer_id, sku_id , count(*) co_manage_cnt from @{toTempTable} 
                    group by customer_id, sku_id 
                ) co_manage on co_manage.customer_id = @{toTempTable}.customer_id and co_manage.sku_id = @{toTempTable}.sku_id
                set @{toTempTable}.co_manage_type_code = 
                        case when co_manage_cnt = 1 then '1Call'  
                        when co_manage_cnt = 2 then '2Call'  
                        else 'nCall' end  ,
                    @{toTempTable}.co_manage_type_name = 
                        case when co_manage_cnt = 1 then null
                        when co_manage_cnt = 2 then '双Call'
                        else '多Call' end  
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and  @{toTempTable}.co_manage_type_code is null 
            ]]>    
        </sql>
        
        <sql name="importSetCurrentPlanningId">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.planning_id = (select id from tgt_planning order by quarter_no desc limit 1 )
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.planning_id is null 
            ]]>    
        </sql>
        
        <sql name="importUpdateInitAmt">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.init_amt = init_qty * price
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.init_amt is null 
            ]]>    
        </sql>
 
        <sql name="importUpdateDefaultFinal">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.final_amt = init_amt, final_qty = init_qty
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.final_amt is null 
            ]]>    
        </sql>
        
        <sql name="importUpdateFinal">
            <![CDATA[
                update tgt_planning_detail 
                left join @{toTempTable} on @{toTempTable}.quarter_no = tgt_planning_detail.quarter_no
                    and @{toTempTable}.position_id = tgt_planning_detail.position_id 
                    and @{toTempTable}.territory_id = tgt_planning_detail.territory_id
                    and @{toTempTable}.customer_id = tgt_planning_detail.customer_id  
                    and @{toTempTable}.sku_id = tgt_planning_detail.sku_id and @{toTempTable}.io_batch_id = '@{ioBatchId}'
                set tgt_planning_detail.final_amt = ifnull(@{toTempTable}.final_amt, 0),
                    tgt_planning_detail.final_qty = ifnull(@{toTempTable}.final_qty, 0)
            ]]>    
        </sql>
        
        <sql name="importUpdateDefaultIssue">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.issue_amt = final_amt, issue_qty = final_qty
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.issue_amt is null 
            ]]>    
        </sql>
        
        <sql name="importUpdateDefaultPre">
            <![CDATA[
                update @{toTempTable}
                set @{toTempTable}.pre_amt = 0, pre_qty = 0
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.pre_amt is null 
            ]]>    
        </sql>
        
        <sql name="importCalculatePre3MSales">
            <![CDATA[
                update @{toTempTable} 
                left join tgt_flow_last3 on tgt_flow_last3.customer_id = @{toTempTable}.customer_id and tgt_flow_last3.sku_id = @{toTempTable}.sku_id 
                    and tgt_flow_last3.position_id =  @{toTempTable}.position_id and tgt_flow_last3.quarter_no =  @{toTempTable}.quarter_no 
                set @{toTempTable}.sales_amt = ifnull(tgt_flow_last3.position_amt, 0) , @{toTempTable}.sales_qty = ifnull(tgt_flow_last3.position_qty , 0) 
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.sales_amt is null 
            ]]>    
        </sql>
        
        <sql name="importCalculateProductivity">
            <![CDATA[
                update @{toTempTable} 
                left join tgt_productity_quarter on tgt_productity_quarter.institution_id = @{toTempTable}.customer_id 
                    and tgt_productity_quarter.sku_id = @{toTempTable}.sku_id 
                set @{toTempTable}.vbp_productivity_amt = if(tgt_productity_quarter.productivity is null or tgt_productity_quarter.tag != 'VBP', 0, tgt_productity_quarter.productivity),  
                    @{toTempTable}.no_vbp_productivity_amt = if(tgt_productity_quarter.productivity is null or tgt_productity_quarter.tag != 'NoVBP', 0, tgt_productity_quarter.productivity)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.vbp_productivity_amt is null
            ]]>    
        </sql>
        
        <sql name="importCalculateYoy">
            <![CDATA[
                update @{toTempTable} 
                left join (
                    select year, quarter, territory_id, position_id, customer_id, sku_id,sum(final_amt) final_amt, sum(final_qty) final_qty 
                    from tgt_working_detail group by year, quarter, territory_id, position_id,customer_id,sku_id
                ) history_planning_detail on history_planning_detail.year + 1 = @{toTempTable}.year 
                    and history_planning_detail.quarter = @{toTempTable}.quarter 
                    and history_planning_detail.territory_id = @{toTempTable}.territory_id 
                    and history_planning_detail.position_id = @{toTempTable}.position_id 
                    and history_planning_detail.sku_id = @{toTempTable}.sku_id 
                    and history_planning_detail.customer_id = @{toTempTable}.customer_id 
                set @{toTempTable}.history_amt = ifnull(history_planning_detail.final_amt, 0) ,  
                    @{toTempTable}.history_qty = ifnull(history_planning_detail.final_qty, 0)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.history_amt is null 
            ]]>    
        </sql>
        
        <sql name="importCalculateMom">
            <![CDATA[
                update @{toTempTable} 
                left join (
                    select year, quarter, territory_id, position_id, customer_id, sku_id,sum(final_amt) final_amt, sum(final_qty) final_qty 
                    from tgt_working_detail group by year, quarter, territory_id, position_id,customer_id,sku_id
                ) history_planning_detail on history_planning_detail.year*4 + history_planning_detail.quarter + 1= @{toTempTable}.year*4 + @{toTempTable}.quarter
                    and history_planning_detail.territory_id = @{toTempTable}.territory_id 
                    and history_planning_detail.position_id = @{toTempTable}.position_id 
                    and history_planning_detail.sku_id = @{toTempTable}.sku_id 
                    and history_planning_detail.customer_id = @{toTempTable}.customer_id 
                set @{toTempTable}.mom_amt = ifnull(history_planning_detail.final_amt, 0) ,  
                    @{toTempTable}.mom_qty = ifnull(history_planning_detail.final_qty, 0)
                where @{toTempTable}.io_batch_id = '@{ioBatchId}' and @{toTempTable}.mom_amt is null
            ]]>    
        </sql>
        
        <sql name="importSummaryPlanning">
            <![CDATA[
                update tgt_planning 
                inner join (
                    select planning_id, sum(init_amt) init_amt, sum(issue_amt) actual_amt, sum(history_amt) history_amt, 
                        sum(final_amt) final_amt, sum(init_qty) init_qty, sum(issue_qty) actual_qty, sum(history_qty) history_qty, 
                        sum(final_qty) final_qty from tgt_planning_detail group by planning_id 
                ) v_planning on v_planning.planning_id = tgt_planning.id 
                set tgt_planning.init_amt = v_planning.init_amt, tgt_planning.init_qty = v_planning.init_qty,
                tgt_planning.actual_amt = v_planning.actual_amt, tgt_planning.actual_qty = v_planning.actual_qty,  
                tgt_planning.final_amt = v_planning.final_amt, tgt_planning.final_qty = v_planning.final_qty
            ]]>    
        </sql>
        
        <sql name="importCalculateInitRate">
            <![CDATA[
                update tgt_planning_detail 
                inner join tgt_planning on tgt_planning.id = tgt_planning_detail.planning_id 
                set tgt_planning_detail.init_rate = tgt_planning_detail.init_qty/tgt_planning.init_qty
            ]]>    
        </sql>
        
        <sql name="importCalculateActualRate">
            <![CDATA[
                update tgt_planning_detail 
                inner join tgt_planning on tgt_planning.id = tgt_planning_detail.planning_id 
                set tgt_planning_detail.actual_rate = tgt_planning_detail.final_qty/ tgt_planning.final_qty
            ]]>    
        </sql>
        
        <sql name="importCalculateInitTeamRate">
            <![CDATA[
                update tgt_planning_detail 
                inner join (
                    select planning_id, territory_id, position_id,sum(init_qty) init_qty 
                    from tgt_planning_detail group by planning_id, territory_id, position_id
                ) tgt_planning_position on tgt_planning_position.planning_id = tgt_planning_detail.planning_id 
                    and tgt_planning_position.position_id = tgt_planning_detail.position_id and tgt_planning_position.territory_id = tgt_planning_detail.territory_id 
                set tgt_planning_detail.init_rate_team = tgt_planning_detail.init_qty / tgt_planning_position.init_qty
            ]]>    
        </sql>
        
        <sql name="importCalculateActualTeamRate">
            <![CDATA[
                update tgt_planning_detail 
                inner join (
                    select planning_id, territory_id, position_id, sum(final_qty) final_qty 
                    from tgt_planning_detail group by planning_id, territory_id, position_id
                ) tgt_planning_position on tgt_planning_position.planning_id = tgt_planning_detail.planning_id 
                    and tgt_planning_position.position_id = tgt_planning_detail.position_id and tgt_planning_position.territory_id = tgt_planning_detail.territory_id 
                set tgt_planning_detail.actual_rate_team = tgt_planning_detail.final_qty / tgt_planning_position.final_qty
            ]]>    
        </sql>
    </dataSpace>
    
    <dataSpace name="importSetError">
    
        <sql name="importCheckMatchDocument">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '业务数据不匹配'
                where io_batch_id = '@{ioBatchId}' and is_error is null and not @{filter}
            ]]>    
        </sql>
        
        <sql name="importCheckCompanyId">
            <![CDATA[
                update @{toTempTable} set error_message = '公司主体无法识别'
                where io_batch_id = '@{ioBatchId}' and company_id is null
            ]]>    
        </sql>    
    
        <sql name="importCheckBuId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = 'BU无法识别'
                where io_batch_id = '@{ioBatchId}' and bu_id is null and is_error is null
            ]]>    
        </sql>
 
        <sql name="importCheckCustomerId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '客户无法识别'
                where io_batch_id = '@{ioBatchId}' and customer_id is null and is_error is null
            ]]>    
        </sql>
 
        <sql name="importCheckAccountId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '客户无法识别'
                where io_batch_id = '@{ioBatchId}' and account_id is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckOrgId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '客商无法识别'
                where io_batch_id = '@{ioBatchId}' and org_id is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckNCId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '客商无首营'
                where io_batch_id = '@{ioBatchId}' and org_ncc_id is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckConnectedOrgId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '授权客商无法识别'
                where io_batch_id = '@{ioBatchId}' and connected_org_id is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckDateFrom">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '开始日期为空'
                where io_batch_id = '@{ioBatchId}' and date_from is null and is_error is null
            ]]>    
        </sql>
        
        
        <sql name="importCheckDocDate">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '开始日期为空'
                where io_batch_id = '@{ioBatchId}' and doc_date is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckExpireDate">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '到期日期为空'
                where io_batch_id = '@{ioBatchId}' and expire_date is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckDateTo">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '到期日期为空'
                where io_batch_id = '@{ioBatchId}' and date_to is null and is_error is null
            ]]>    
        </sql>        
        
        <sql name="importCheckValidFrom">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '生产日期格式未能识别'
                where io_batch_id = '@{ioBatchId}' and valid_from is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckValidTo">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '效期格式未能识别'
                where io_batch_id = '@{ioBatchId}' 
                    and valid_to is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckValidFromUnique">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '生产日期与已存在库存不一致'
                where io_batch_id = '@{ioBatchId}'  and is_error is null and exists (
                    select 1 from wm_book_detail 
                    where wm_book_detail.product_id = @{toTempTable}.product_id
                        and wm_book_detail.warehouse_id = @{toTempTable}.warehouse_id
                        and wm_book_detail.batch_no = @{toTempTable}.batch_no
                        and wm_book_detail.batch_sn = @{toTempTable}.batch_sn
                        and wm_book_detail.valid_from != @{toTempTable}.valid_from)
            ]]>    
        </sql>    
        
        <sql name="importCheckValidToUnique">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '有效期与已存在库存不一致'
                where io_batch_id = '@{ioBatchId}'  and is_error is null and exists (
                    select 1 from wm_book_detail 
                    where wm_book_detail.product_id = @{toTempTable}.product_id
                        and wm_book_detail.warehouse_id = @{toTempTable}.warehouse_id
                        and wm_book_detail.batch_no = @{toTempTable}.batch_no
                        and wm_book_detail.batch_sn = @{toTempTable}.batch_sn
                        and wm_book_detail.valid_to != @{toTempTable}.valid_to)
            ]]>    
        </sql>    
        
        <sql name="importCheckType">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '类型无法识别'
                where io_batch_id = '@{ioBatchId}' and type_code is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckProductId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '产品无法识别'
                where io_batch_id = '@{ioBatchId}' and product_id is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckSkuId"> 
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '无法识别规格'
                where io_batch_id = '@{ioBatchId}' and sku_id is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckDistinctSkuId">
            <![CDATA[
            update @{toTempTable} 
            set is_error = 'T', error_message = 'SKU不唯一'
            where io_batch_id = '@{ioBatchId}'  and is_error is null 
                and EXISTS (
                    select 1 from  ( 
                        select  company_id, bu_id, sku_id  from @{toTempTable} 
                        where io_batch_id = '@{ioBatchId}'
                        group by sku_id, company_id, bu_id
                        HAVING count(1)  > 1   
                )  tempSkuGroup 
                where @{toTempTable}.sku_id = tempSkuGroup.sku_id 
                and  @{toTempTable}.bu_id = tempSkuGroup.bu_id 
                and  @{toTempTable}.company_id = tempSkuGroup.company_id 
            ) 
            ]]>    
        </sql>
        
        <sql name="importCheckGiftProductId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '买赠产品无法识别'
                where io_batch_id = '@{ioBatchId}' and gift_product_id is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckPolicyName">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '政策名称为空'
                where io_batch_id = '@{ioBatchId}' and policy_name is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckPrice">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '价格无法识别'
                where io_batch_id = '@{ioBatchId}' 
                    and price is null
                    and is_error is null 
            ]]>    
        </sql>
        
        <sql name="importCheckAmtBook">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '折扣金额无法识别'
                where io_batch_id = '@{ioBatchId}' 
                    and amt_book is null 
                    and is_error is null 
            ]]>    
        </sql>
        
        <sql name="importCheckQtyBook">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '记账数量无法识别'
                where io_batch_id = '@{ioBatchId}' 
                    and qty_book is null and is_error is null 
            ]]>    
        </sql>
        
        <sql name="importCheckEmptionQty">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '购买数量无法识别'
                where io_batch_id = '@{ioBatchId}' 
                    and emption_qty is null 
                    and is_error is null 
            ]]>    
        </sql>
        
        <sql name="importCheckGiftQty">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '买赠数量无法识别'
                where io_batch_id = '@{ioBatchId}' 
                    and gift_qty is null 
                    and is_error is null 
            ]]>    
        </sql>
        
        <sql name="importCheckQty">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '数量无法识别'
                where io_batch_id = '@{ioBatchId}' 
                    and qty is null
                    and is_error is null 
            ]]>    
        </sql>    
            
        <sql name="importCheckStockQty">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '库存数量不足'
                where io_batch_id = '@{ioBatchId}' and is_error is null 
                    and ( EXISTS (
                        select 1 from (
                            select book_detail_id from ( 
                                select book_detail_id, sum(qty) qty 
                                from @{toTempTable} 
                                where io_batch_id = '@{ioBatchId}'
                                group by book_detail_id ) document 
                            left join wm_book_detail on document.book_detail_id = wm_book_detail.id 
                            where wm_book_detail.qty_available < document.qty ) errorStock 
                        where errorStock.book_detail_id = @{toTempTable}.book_detail_id 
                         )  or @{toTempTable}.book_detail_id is null)
            ]]>    
        </sql>
        
        <sql name="importCheckHospital">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '医院无法识别'
                where io_batch_id = '@{ioBatchId}' and hospital_id is null and is_error is null
            ]]>    
        </sql>        
        
        <sql name="importCheckEmployeeId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '员工编号为空'
                where io_batch_id = '@{ioBatchId}' and employee_id is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckEmployeeMatch">
            <![CDATA[
                update @{toTempTable} set error_message = '员工无法识别'
                where io_batch_id = '@{ioBatchId}' and employee_code is not null and employee_id is null and is_error is null
            ]]>    
        </sql>        
        
        <sql name="importCheckPositionId">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '岗位编码为空'
                where io_batch_id = '@{ioBatchId}' and position_id is null and is_error is null
            ]]>    
        </sql>    
        
        <sql name="importCheckDistinctPosition">
            <![CDATA[
            update @{toTempTable} set is_error = 'T', error_message = '岗位编码不唯一' 
            where io_batch_id = '@{ioBatchId}' and is_error is null 
                and EXISTS ( 
                    select 1 from (
                        select position_id from @{toTempTable} position 
                        where position.io_batch_id = '@{ioBatchId}' 
                        group by position_id 
                        having count(position_id) > 1 
                    ) v  where  @{toTempTable}.position_id = v.position_id )
            ]]>    
        </sql>
        
        <sql name="importCheckDistinctPositionHospital">
            <![CDATA[
            update @{toTempTable} 
            set is_error = 'T', error_message = '医院+BU+科室不唯一'
            where io_batch_id = '@{ioBatchId}'  and is_error is null 
                and EXISTS (
                    select 1 from  ( 
                        select  bu_id, hospital_id ,department from temp_position_hospital 
                        where io_batch_id = '@{ioBatchId}'
                        group by bu_id, hospital_id ,department 
                        HAVING count(1)  > 1   
                    )  tempHospitalGroup 
                    where  temp_position_hospital.hospital_id = tempHospitalGroup.hospital_id 
                        and tempHospitalGroup.bu_id = temp_position_hospital.bu_id 
                        and @{IfEmpty}(tempHospitalGroup.department,'--') = @{IfEmpty}(temp_position_hospital.department ,'--') 
                ) 
            ]]>    
        </sql>
        
        <sql name="importCheckDistinctTargetHospital">
            <![CDATA[
            update @{toTempTable} 
            set is_error = 'T', error_message = '年份+公司+BU+医院+科室不唯一'
            where io_batch_id = '@{ioBatchId}'  and is_error is null 
                and EXISTS (
                    select 1 from  ( 
                        select year, company_id, bu_id, hospital_id ,department from @{toTempTable} 
                        where io_batch_id = '@{ioBatchId}'
                        group by year, company_id, bu_id, hospital_id ,department 
                        HAVING count(1)  > 1   
                    )  tempHospitalGroup 
                    where  @{toTempTable}.hospital_id = tempHospitalGroup.hospital_id 
                        and tempHospitalGroup.company_id = @{toTempTable}.company_id 
                        and tempHospitalGroup.bu_id = @{toTempTable}.bu_id 
                        and tempHospitalGroup.year = @{toTempTable}.year 
                        and @{IfEmpty}(tempHospitalGroup.department,'--') = @{IfEmpty}(@{toTempTable}.department ,'--') 
                ) 
            ]]>    
        </sql>
        
        <sql name="importCheckTargetHospitalExist">
            <![CDATA[
            update @{toTempTable} 
            set is_error = 'T', error_message = '年份+公司+BU+医院+科室系统已存在'
            where io_batch_id = '@{ioBatchId}' and is_error is null 
                and EXISTS (
                    select 1 from @{toTable}  
                    where @{toTempTable}.hospital_id = @{toTable}.hospital_id 
                        and @{toTempTable}.company_id = @{toTable}.company_id 
                        and @{toTempTable}.bu_id = @{toTable}.bu_id 
                        and @{toTempTable}.year = @{toTable}.year
                        and @{IfEmpty}(@{toTempTable}.department,'--') = @{IfEmpty}(@{toTable}.department ,'--')
                    ) 
            ]]>    
        </sql>
        
        <sql name="importCheckTargetHospitalExist">
            <![CDATA[
            update @{toTempTable} 
            set is_error = 'T', error_message = '年份+公司+BU+医院+科室系统已存在'
            where io_batch_id = '@{ioBatchId}' and is_error is null 
                and EXISTS (
                    select 1 from @{toTable}  
                    where @{toTempTable}.hospital_id = @{toTable}.hospital_id 
                        and @{toTempTable}.company_id = @{toTable}.company_id 
                        and @{toTempTable}.bu_id = @{toTable}.bu_id 
                        and @{toTempTable}.year = @{toTable}.year
                        and @{IfEmpty}(@{toTempTable}.department,'--') = @{IfEmpty}(@{toTable}.department ,'--')
                    ) 
            ]]>    
        </sql>
        
        <sql name="importCheckCategoryCode">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '授权分类无法识别'
                where io_batch_id = '@{ioBatchId}' and category_code is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckTermCode">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '授权类型无法识别'
                where io_batch_id = '@{ioBatchId}' and term_code is null and is_error is null
            ]]>    
        </sql>
        
        <sql name="importCheckActorCode">
            <![CDATA[
                update @{toTempTable} set is_error = 'T', error_message = '角色无法匹配'
                where io_batch_id = '@{ioBatchId}' and actor_code is null and is_error is null
            ]]>    
        </sql>
    </dataSpace>
    
    <dataSpace name="imporSelectError">
    
        <sql name="importGetImportErrors">
            <![CDATA[
                select error_message, count(1) error_count from @{toTempTable}
                where io_batch_id = '@{ioBatchId}' and is_error = 'T'
                group by error_message
            ]]>    
        </sql>        
    
    </dataSpace>
</sqls>