P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
<?xml version="1.0" encoding="UTF-8" ?>
    
<sqls>
    <dataSpace name="updateSaleFramework">
       
        <sql name="deleteNotExistsSalesPosition">
        <![CDATA[
            delete from md_position 
            where category_code = 'Sales' and  
            not exists (
                select 1 from temp_position 
                where temp_position.position_code = md_position.name 
                    and ifnull(is_error, '') <> 'T' 
                    and io_batch_id = '@{batchNo}'
            )
        ]]>
        </sql>        
 
        <sql name="updatePositionSalesPosition">
        <![CDATA[
            update md_position 
            inner join (
                select * from temp_position 
                where ifnull(is_error, '') <> 'T' and io_batch_id = '@{batchNo}'
            ) temp_position on md_position.id = temp_position.position_id and  md_position.category_code = 'Sales'
            set md_position.company_id = temp_position.company_id,
                md_position.bu_id = temp_position.bu_id,
                md_position.parent_id = temp_position.parent_id,
                md_position.code = temp_position.position_code,
                md_position.name = temp_position.position_name,                
                md_position.type_code = temp_position.type_code,
                md_position.type_name = temp_position.type_name,
                md_position.remark = temp_position.remark,
                md_position.region = temp_position.region,
                md_position.index_no = temp_position.index_no           
        ]]>
        </sql>       
       
        <sql name="appendNewSalesPosition">
        <![CDATA[
            insert into md_position  (
                id,  company_id, bu_id, parent_id, category_code, code, name, 
                type_code, type_name, remark, region, index_no,  
                superior_1, superior_2, superior_3, superior_4
            ) 
            select position_id, company_id, bu_id, parent_id, 'Sales' category_code, position_code, position_name, 
                type_code, type_name, remark, region, index_no, 
                superior_1, superior_2, superior_3, superior_4
            from temp_position where io_batch_id = '@{batchNo}'
            and not exists (
                select 1 from md_position 
                where temp_position.position_id = md_position.id
            )
        ]]>
        </sql>
        
        <sql name="inactiveNotExistsSalesEmployee">
        <![CDATA[
            update md_employee 
            set is_active = 'F', update_time = now()
            where id is not null and actor_code like '%Sales%' and is_active = 'T' and not exists (
                select 1 from temp_position 
                where io_batch_id = '@{batchNo}'
                    and md_employee.id = temp_position.employee_id
            ) 
        ]]>
        </sql>        
 
        
        <sql name="appendNewSalesEmployee">
        <![CDATA[
            insert into md_employee (
                id, org_id, org_name, account_type_code, actor_code, code, name, 
                job_title, mail, phone, nc_org_id, nc_department_id, nc_employee_id, 
                is_error, error_message, is_active, creator_id, creator_name, create_time, update_time
            )
            select employee_id, company_id, company_name, account_type_code, actor_code, employee_code , remark,
                job_title, employee_mail, employee_phone, nc_company_id, nc_department_id, nc_employee_id,
                is_employee_match_error, employee_match_error_message, is_active, '@{creatorId}', '@{creatorName}',
                create_time,update_time
            from temp_position where io_batch_id = '@{batchNo}' and not exists (
                select 1 from md_employee 
                where md_employee.id = temp_position.employee_id
            )
        ]]>
        </sql>
        
        <sql name="UpdateEmployeeInvalid">
        <![CDATA[
            update  md_employee
                left join mirror_oa_v_gmd_hrmresource oaEmployee on oaEmployee.`主键` = md_employee.id 
            set md_employee.is_active = 'F'
            where not Exists (
                select 1 from mirror_oa_v_gmd_hrmresource
                where mirror_oa_v_gmd_hrmresource.`主键` = md_employee.id
            )
        ]]>
        </sql>
 
        <sql name="deleteNotExistsPositionEmployee">
        <![CDATA[
            delete from md_position_employee where not exists (
                select 1 from md_position
                where md_position.id = md_position_employee.position_id 
            )
            or not exists (
                select 1 from md_employee
                where md_employee.id = md_position_employee.employee_id
            )            
        ]]>
        </sql>        
       
        <sql name="updatePositionEmployee">
        <![CDATA[
            update md_position_employee 
            inner join temp_position on temp_position.parent_id = md_position_employee.position_id 
                    and temp_position.io_batch_id = '@{batchNo}'
            set md_position_employee.employee_id = temp_position.employee_id,
                md_position_employee.remark = temp_position.remark      
        ]]>
        </sql>
       
        <sql name="appendPositionEmployee">
        <![CDATA[
            INSERT INTO md_position_employee (id, company_id, bu_id, employee_id, position_id, remark) 
            select temp_position.id, temp_position.company_id, temp_position.bu_id, temp_position.employee_id, 
            temp_position.parent_id, temp_position.remark
            from temp_position 
            left join md_position_employee on  md_position_employee.position_id = temp_position.parent_id 
            where md_position_employee.id is null and io_batch_id = '@{batchNo}'  
        ]]>
        </sql> 
        
        <!-- <sql name="deleteNotExistsEmployeeUser">
        <![CDATA[
            delete from sys_user where exists (
                select 1 from md_employee 
                where md_employee.id = sys_user.employee_id
                    and md_employee.is_active = 'F'
            )
        ]]>
        </sql> -->
        <sql name="appendNewEmployee">
        <![CDATA[
            INSERT INTO md_employee (id, org_id, account_type_code, 
                        code, name, job_title, mail, phone, is_active,
                        creator_id, creator_name, create_time, update_time)
            select 主键 id , `所属分部` org_id, `账号类型` account_type_code,`工号` code,`姓名` name,
                    `部门名称` `job_title`, `邮箱` `mail`, `手机号` `phone`,  'T' `is_active`, 
                    'admin' `creator_id`,'admin' `creator_name`,
                    `修改时间` `create_time`, `修改时间` `update_time`
            from mirror_oa_v_gmd_hrmresource
            where not exists (
                select 1 from md_employee
                where mirror_oa_v_gmd_hrmresource.`主键` = md_employee.id
            ) 
        ]]>
        </sql>
        
        <sql name="updateEmployeeInfoOA">
        <![CDATA[
            update  md_employee
                left join mirror_oa_v_gmd_hrmresource oaEmployee on oaEmployee.`主键` = md_employee.id 
                inner join md_org on md_org.id = oaEmployee.`所属分部`  and md_org.is_master = 'T'
            set 
                md_employee.account_type_code = oaEmployee.`账号类型`,
                md_employee.code = oaEmployee.`工号` ,
                md_employee.job_title = oaEmployee.`部门名称` ,
                md_employee.mail = oaEmployee.`邮箱` ,
                md_employee.phone = oaEmployee.`手机号` ,
                md_employee.org_name = md_org.name ,
                md_employee.job_title_code = oaEmployee.`所属部门` ,
                md_employee.is_active = if(oaEmployee.`主键`  is null, 'F', 'T')
        ]]>
        </sql>
        
        <sql name="updateEmployeeInfoNC">
        <![CDATA[
            update  md_employee
                left join mirror_oa_v_gmd_hrmresource oaEmployee on oaEmployee.`主键` = md_employee.id 
                inner join md_org on md_org.id = oaEmployee.`所属分部`  and md_org.is_master = 'T'
                left join mirror_nc_v_psn_user ncUser on ncUser.`人员编码` = oaEmployee.`工号` and ncUser.`所属公司主键` = md_org.nc_id
                left join mirror_nc_v_org_dept_v ncDept on ncDept.PK_DEPT  = ncUser.`所属部门主键`
                left join mirror_nc_v_org_dept_v ncUpDept on ncDept.PK_FATHERORG = ncUpDept.PK_DEPT 
            set 
                md_employee.nc_org_id = ncUser.`所属公司主键` ,
                md_employee.nc_department_id = ncUser.`所属部门主键`,
                md_employee.nc_department_code = ncDept.`CODE`,
                md_employee.nc_employee_id  = ncUser.`人员主键`,
                md_employee.is_error = if(ncUser.TS is null ,'T',null) ,
                md_employee.error_message = if(ncUser.TS is null ,'未匹配上NC数据',null) ,
                md_employee.nc_region_id = ifnull(ncUpDept.PK_DEPT, ncDept.PK_DEPT),
                md_employee.nc_region_code = ifnull(ncUpDept.`CODE`, ncDept.`CODE`) ,
                md_employee.nc_region_name = ifnull(ncUpDept.`NAME`, ncDept.`NAME`),
                md_employee.nc_area_id = if(ncUpDept.PK_DEPT is null ,'~', ncDept.PK_DEPT),
                md_employee.nc_area_code = if(ncUpDept.PK_DEPT is null ,'~',ncDept.`CODE`),
                md_employee.nc_area_name = if(ncUpDept.PK_DEPT is null ,'~',ncDept.`NAME`)
            where ncDept.ENABLESTATE = '已启用' 
        ]]>
        </sql>
        
        <sql name="inValidNotExistsEmployeeUser">
        <![CDATA[
            update sys_user
            set is_active = 'F'
            where not exists (
                select 1 from md_employee 
                where md_employee.id = sys_user.employee_id
                    and md_employee.is_active = 'T'
            ) and employee_id is not null and id != 'admin'
        ]]>
        </sql>
        
        <sql name="validExistsEmployeeUser">
        <![CDATA[
            update sys_user
            set is_active = 'T'
            where exists (
                select 1 from md_employee 
                where md_employee.id = sys_user.employee_id
                    and md_employee.is_active = 'T'
            )
        ]]>
        </sql>
        
        <sql name="appendNewEmployeeUser">
        <![CDATA[        
            insert into sys_user ( id, org_id, code, name, password, employee_id, pass_need_change,
                is_active, secret, remark, create_time, update_time ) 
            select md_employee.id, md_employee.org_id, md_employee.code, md_employee.code name, '123456' password,
                 md_employee.id employee_id, 'F' , 'T', mirror_oa_v_gmd_hrmresource.SECRET, md_employee.name remark,
                 md_employee.create_time,md_employee.update_time 
             from md_employee 
             left join mirror_oa_v_gmd_hrmresource on mirror_oa_v_gmd_hrmresource.`主键` = md_employee.id 
             where md_employee.is_active = 'T' and not exists (
                  select 1 from sys_user where md_employee.id = sys_user.employee_id and sys_user.is_active = 'T' 
                  )
        ]]>
        </sql>
        
        <sql name="appendNewUserOrg">
        <![CDATA[        
            insert into sys_user_org (id,idx,org_id,user_id,bu_id)
            select  UUID_SHORT()id, null idx, md_employee.org_id, sys_user.id user_id, null bu_id 
            from  md_employee 
            inner join sys_user on sys_user.employee_id = md_employee.id 
            left join sys_user_org on sys_user_org.org_id  =md_employee.org_id and sys_user.id =  sys_user_org.user_id 
            where sys_user_org.id is null and sys_user.is_active = 'T' 
            and md_employee.is_active = 'T' and md_employee.actor_code like '%Sales%'
        ]]>
        </sql>        
        
        
        <sql name = "queryEmployeeMatchNCError">
          <![CDATA[
               select *
               from md_employee 
               where is_error = 'T' and actor_code like '%Sales%'
          ]]>
       </sql>
        
        <sql name="deleteNotExistsRightActorTarget">
        <![CDATA[        
            delete from sys_right_actor_target 
            where type_code = 'Position' and  actor_id like '%Sales%' and not EXISTS (
            select 1 from  md_position
            where  is_delete != 'T' and  sys_right_actor_target.target_id = md_position.id ) 
 
        ]]>
        
        </sql>    <sql name="getNewRightActorTarget">
        <![CDATA[        
            select md_position.id, null actor_id, md_position.duty, 'Position' type_code , 
                md_position.company_id, md_position.bu_id, md_position.id target_id, remark 
            from md_position 
            where is_delete != 'T' and not EXISTS (
            select 1 from  sys_right_actor_target
            where  sys_right_actor_target.target_id = md_position.id ) and category_code = 'Sales' 
        ]]>
        </sql>    
       
        <sql name="refreshHospitalPosition">
        <![CDATA[
            update md_position_hospital 
            left join temp_position_hospital on md_position_hospital.hospital_id = temp_position_hospital.hospital_id    
                    and md_position_hospital.bu_id = temp_position_hospital.bu_id 
                    and md_position_hospital.company_id = temp_position_hospital.company_id    
            set md_position_hospital.position_id = temp_position_hospital.position_id,
                md_position_hospital.department = temp_position_hospital.department,
                md_position_hospital.remark = temp_position_hospital.remark
            where io_batch_id = '@{batchNo}' 
          ]]>
       </sql>
           
        <sql name="updateChannelStructurePosition">
            <![CDATA[
                update md_channel_structure 
                left join md_position_hospital  on md_position_hospital.hospital_id = md_channel_structure.hospital_id 
                        and md_position_hospital.company_id = md_channel_structure.company_id 
                        and md_position_hospital.bu_id = md_channel_structure.bu_id 
                        and ifnull(md_position_hospital.department, '--') = md_channel_structure.department
                set md_channel_structure.position_id = md_position_hospital.position_id
            ]]>    
        </sql>
    </dataSpace>
       
    <dataSpace name="checkTempSaleFramework">
    
       <sql name = "checkSuperiorPositionCodeExist">
          <![CDATA[
               update temp_position
               set error_msg = '上级岗位不存在',
                   is_error = 'T'
               where ids = '@{ids}'
                 and ifnull(is_error, '') <> 'T'
                 and ifnull(parent_code, '') <> ''
                 and not exists (select 1
                                 from md_position
                                 where temp_position.parent_code = md_position.code )              
          ]]>
       </sql>
       
       <sql name = "checkPositionCodeExist">
          <![CDATA[
                update temp_position
                set error_msg = '岗位不存在',
                    is_error = 'T'
                where ids = '@{ids}'
                  and ifnull(is_error, '') <> 'T'
                  and not exists (select 1
                                  from md_position
                                  where temp_position.position_code = md_position.code )                   
          ]]>
       </sql>
       
       <sql name="checkPositionCodeNull">
            <![CDATA[
                 update temp_position
                 set error_msg = '岗位编号为空',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(code, '') = ''
                   and ifnull(is_error, '') <> 'T'
            ]]>
       </sql>
       
       <sql name="checkPositionNameNull">
            <![CDATA[
                 update temp_position
                 set error_msg = '岗位名称为空',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(name, '') = ''
                   and ifnull(is_error, '') <> 'T'
            ]]>
       </sql>
       
       <sql name="checkDepartmentNull">
            <![CDATA[
                 update temp_position
                 set error_msg = '部门为空',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(department_name, '') = ''
                   and ifnull(is_error, '') <> 'T'
            ]]>
       </sql>
       
       <sql name="checkRegionNull">
            <![CDATA[
                 update temp_position
                 set error_msg = '区域为空',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(region, '') = ''
                   and ifnull(is_error, '') <> 'T'
            ]]>
       </sql>
       
        <sql name="checkSalesPersonNull">
           <![CDATA[
                 update temp_position
                 set error_msg = '岗位人员为空',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(remark, '') = ''
                   and ifnull(is_error, '') <> 'T'
           ]]>
        </sql>
        
        <sql name="checkSuperiorCodeNull">
           <![CDATA[
                 update temp_position
                 set error_msg = '上级岗位编码',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(parent_code, '') = ''
                   and ifnull(is_error, '') <> 'T'
           ]]>
        </sql>
        
        <sql name="checkSuperiorNameNull">
           <![CDATA[
                 update temp_position
                 set error_msg = '上级岗位名称',
                     is_error = 'T'
                 where ids = '@{ids}'
                   and ifnull(parent_name, '') = ''
                   and ifnull(is_error, '') <> 'T'
           ]]>
        </sql>
        
        <sql name="checkSalesPersonExist">
           <![CDATA[
                update temp_position
                set error_msg = '岗位人员在员工主数据中不存在',
                    is_error = 'T'
                where ids = '@{ids}'
                  and ifnull(is_error, '') <> 'T'
                  and not exists (select 1
                                  from md_employee
                                  where temp_position.remark = md_employee.name )                
           ]]>
        </sql>
        
        <sql name = "fillOrg">
           <![CDATA[
                 update temp_position
                 set org_id = 'OG00001'
                 where ids = '@{ids}'
           ]]>        
        </sql>
        
        <sql name = "fillSuperiorId">
           <![CDATA[
                update temp_position
                inner join md_position superior_position on temp_position.parent_code = superior_position.code
                set temp_position.parent_id = superior_position.id
                where ids = '@{ids}'
           ]]>        
        </sql>
 
        <sql name = "fillDepartmentId">
           <![CDATA[
                update temp_position
                inner join md_department on temp_position.department_name = md_department.name
                set temp_position.department_id = md_department.id
                where ids = '@{ids}'
           ]]>        
        </sql>
        
        <sql name = "fillRegion">
           <![CDATA[
                update temp_position
                inner join md_region on temp_position.region = md_region.name
                set temp_position.zone = md_region.short_name,
                    temp_position.zone_cn = md_region.name
                where ids = '@{ids}'
           ]]>
        </sql>
        
        <sql name = "fillRootInfo">
           <![CDATA[
                update temp_position
                inner join md_region on temp_position.region = md_region.name
                set temp_position.short_name = temp_position.position_name,
                    temp_position.type_code = 'Country',
                    temp_position.index_no = 0
                where ids = '@{ids}'
                  and ifnull(parent_code, '') = ''                
           ]]>
        </sql>
        
        <sql name = "fillRegionInfo">
           <![CDATA[
                update temp_position
                inner join (select * 
                            from temp_position 
                            where ifnull(parent_code, '') = '') root_position on root_position.code = temp_position.parent_code
                set temp_position.short_name = position_name,
                    temp_position.type_code = 'Region',
                    temp_position.type_name = temp_position.zone_cn,
                    temp_position.index_no = 1
                where temp_position.io_batch_id = '@{batchNo}'
           ]]>
        </sql>                    
        
        <sql name = "fillAreaInfo">
           <![CDATA[
                update temp_position
                inner join (select * 
                            from temp_position 
                            where ifnull(type_code, '') = 'Region') tmp_position on tmp_position.code = temp_position.parent_code
                set temp_position.type_code = 'Area'
                where temp_position.io_batch_id = '@{batchNo}'
           ]]>
        </sql>
        
        <sql name = "fillTerritoryInfo">
           <![CDATA[
                update temp_position
                inner join (select * 
                            from temp_position
                            where type_code = 'Area') tmp_position on tmp_position.code = temp_position.parent_code
                set temp_position.type_code = 'Territory'
                where temp_position.io_batch_id = '@{batchNo}'           
           ]]>
        </sql>
        
        <sql name = "fillTherapyInfo">
           <![CDATA[
                update temp_position
                set short_name = name
                where ids = '@{ids}'
                  and code like '%疗法%' 
           ]]>
        </sql>
        
        <sql name = "fillIndexNo">
           <![CDATA[
                update temp_position
                inner join (select zone, type_code, code, short_name,
                                   case when @name <> zone then @pids := 2
                                        when @name = zone then @pids := @pids + 1 end index_num,
                                   case when @name <> zone then @name := zone end zonename
                            from (select zone, type_code, code, short_name
                                  from temp_position
                                  where department_id = 'Dep-sales' and type_code not in ('Country', 'Region')
                                  order by zone, type_code desc, short_name, code) md_position, (select @pids := 1, @name := 2) math_common    
                            group by zone, type_code, short_name, code
                            order by zone, type_code desc, short_name, code) tmp_position on temp_position.position_code = tmp_position.code
                set temp_position.index_no = index_num 
                where ids = '@{ids}'
           ]]>
        </sql>
        
        <sql name = "refreshPositionId">
           <![CDATA[
                update temp_position
                inner join md_position on temp_position.position_code = md_position.code
                set temp_position.parent_id = md_position.id
                where ids = '@{ids}'
           ]]>        
        </sql>            
        
       <sql name = "querySaleFrameworkErrorMsg">
          <![CDATA[
               select concat('第', ifnull(line_no, ''), '条记录:', error_msg) as error_msg
               from temp_position 
               where ids = '@{ids}'
                 and ifnull(error_msg, '') <> ''
          ]]>
       </sql>
       
       <sql name = "querySaleFrameworkWarnMsg">
          <![CDATA[
               select concat('第', ifnull(line_no, ''), '条记录:', warn_msg) as warn_msg
               from temp_position 
               where ids = '@{ids}'
                 and ifnull(warn_msg, '') <> ''
          ]]>
       </sql>
       
        <sql name="exportSaleFrameWork">
           <![CDATA[
                select *
                from temp_position
                where @{filter}        
                @{orderby}
           ]]>
        </sql>
       
       <sql name = "getParentPositionEmployee">
          <![CDATA[
              select parent_position.id parent_position_id ,    parent_position.company_id parent_position_company_id,
                parent_position.bu_id parent_position_bu_id,    parent_position.parent_id parent_position_parent_id,
                parent_position.category_code parent_position_category_code,    parent_position.code parent_position_code,
                parent_position.name parent_position_name,    parent_position.type_code parent_position_type_code,
                parent_position.type_name parent_position_type_name,    parent_position.remark parent_position_remark,
                parent_position.region parent_position_region,    parent_position.index_no parent_position_index_no,
                parent_position.level_no parent_position_level_no,    parent_position.superior_1 parent_position_superior_1,
                parent_position.superior_2 parent_position_superior_2,    parent_position.superior_3 parent_position_superior_3,
                parent_position.superior_4 parent_position_superior_4,    parent_employee.id parent_employee_id,
                parent_employee.org_id parent_employee_org_id,    parent_employee.org_name parent_employee_org_name,
                parent_employee.account_type_code parent_employee_account_type_code,    parent_employee.actor_code parent_employee_actor_code,
                parent_employee.code parent_employee_code,    parent_employee.name parent_employee_name,
                parent_employee.job_title_code parent_employee_job_title_code,    parent_employee.job_title parent_employee_job_title,
                parent_employee.mail parent_employee_mail,    parent_employee.account_type_code parent_employee_account_type_code,
                parent_employee.actor_code parent_employee_actor_code,    parent_employee.code parent_employee_code,
                parent_employee.name parent_employee_name,    parent_employee.phone parent_employee_phone,
                parent_employee.nc_org_code parent_employee_nc_org_code,    parent_employee.nc_region_id parent_employee_nc_region_id,
                parent_employee.nc_region_code parent_employee_nc_region_code,    parent_employee.nc_region_name parent_employee_nc_region_name,
                parent_employee.nc_area_id parent_employee_nc_area_id,    parent_employee.nc_area_code parent_employee_nc_area_code,
                parent_employee.nc_area_name parent_employee_nc_area_name,    parent_employee.nc_department_id parent_employee_nc_department_id,
                parent_employee.nc_department_code parent_employee_nc_department_code,    parent_employee.nc_employee_id parent_employee_nc_employee_id
            from md_position
            left join md_position parent_position on md_position.parent_id = parent_position.id and md_position.category_code = 'Sales'
            left join md_position_employee ON parent_position.id = md_position_employee.position_id
            LEFT JOIN md_employee parent_employee ON md_position_employee.employee_id = parent_employee.id  
            where @{filter} @{orderby} @{limit}
          ]]>
       </sql>
                
    </dataSpace>
    
    <dataSpace name="salesHierarchy">
    
        <sql name = "getLeaderPosition">
        <![CDATA[
            select md_position.parent_id as parent_position_id, md_position.id as position_id, 
                md_position.name as position_title, md_position.level_code, md_position.duty,
                md_employee.id as employee_id, md_employee.name as employee_name, sys_user.id as user_id
            from md_position
            left join md_position_employee on md_position_employee.position_id = md_position.id
            left join md_employee on md_employee.id = md_position_employee.employee_id
            left join sys_user_employee on sys_user_employee.employee_id = md_employee.id
            left join sys_user on sys_user.id = sys_user_employee.user_id
            where md_position.id = '@{positionId}'
       ]]>
       </sql>    
    </dataSpace>    
 
</sqls>