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
<?xml version="1.0" encoding="UTF-8" ?>
    
<sqls>
    <dataSpace name="account">
    
        <sql name="getAccountNccList">
        <![CDATA[
            select md_org_account_ncc.*, (
                    select count(1) from md_org_account where md_org_account.org_ncc_id = md_org_account_ncc.id
                ) as account_count,
                md_org.id as md_org__id, md_org.oa_no as md_org__oa_no, 
                md_org.state_code as md_org__state_code, md_org.state_name as md_org__state_name, 
                md_org.source as md_org__source, md_org.code as md_org__code, md_org.ncc_code as md_org__ncc_code, 
                md_org.name as md_org__name, md_org.short_name as md_org__short_name, 
                md_org.category_code as md_org__category_code, md_org.category_name as md_org__category_name, 
                md_org.license_business_no as md_org__license_business_no, 
                md_org.province as md_org__province, md_org.city as md_org__city, md_org.county as md_org__county, 
                md_org.register_address as md_org__register_address, md_org.business_address as md_org__business_address,
                md_org.business_person as md_org__business_person, md_org.business_phone as md_org__business_phone, 
                md_org.business_email as md_org__business_email,md_org.receiver_name md_org__receiver_name,
                md_org.receiver_phone md_org__receiver_phone, md_org.receive_address md_org__receive_address,
                 md_org.is_frozen as md_org__is_frozen, 
                md_org.is_distributor as md_org__is_distributor
            from md_org_account_ncc left join md_org on md_org_account_ncc.org_id = md_org.id 
            where @{filter} @{orderby} @{limit}
            ]]>    
        </sql>
            
        <sql name="activateAgreementRecord">
            <![CDATA[
                update @{tableName} set agreement_no = '@{agreement_no}',
                    date_from = ifnull(date_from, @{date_from}), date_to = ifnull(date_to, @{date_to}),
                    state_code = '@{state_code}', state_name = '@{state_name}'
                where agreement_id = '@{agreement_id}'
            ]]>    
        </sql>
        
        <sql name="stopAgreementRecord">
            <![CDATA[
                update @{tableName} set state_code = '@{state_code}', state_name = '@{state_name}'
                where agreement_id = '@{agreement_id}'
            ]]>    
        </sql>
        
        <sql name="getChannelStructureLastTime">
            <![CDATA[
                select max(run_time) from md_channel_structure_log
            ]]>    
        </sql>
        
        <sql name="createChannelStructure">
            <![CDATA[
            insert into md_channel_structure (
                id,    company_id, company_name, bu_id, bu_name, org_id, account_ncc_id, 
                authorize_id, category_code,  state_code, state_name,
                account_id, account_code, account_name, 
                distributor_id, distributor_code, distributor_name,
                hospital_id, hospital_code, hospital_name,
                authorization_code, file_authorization, file_name_authorization,
                position_id,
                date_from, date_to,    create_time, update_time
            )
            select UUID_SHORT(), md_org_account_hospital.company_id, md_company.short_name company_name, 
                md_org_account_hospital.bu_id, md_bu.name bu_name, md_org_account_hospital.org_id, md_org_account.org_ncc_id,
                md_org_account_hospital.id, category_code, md_org_account_hospital.state_code, md_org_account_hospital.state_name,
                account_id, md_org_account_hospital.org_code, md_org_account_hospital.org_name,
                account_id, md_org_account_hospital.org_code, md_org_account_hospital.org_name,
                md_org_account_hospital.hospital_id, md_hospital.code, md_hospital.name, 
                md_org_account_hospital.code, file_authorization, file_name_authorization,
                md_position_hospital.position_id,
                date_from, date_to,    now(), now()
            from md_org_account_hospital 
            left join  md_bu on md_bu.id = md_org_account_hospital.bu_id 
            left join (select id, short_name, name from md_org where is_master = 'T') md_company on md_company.id = md_org_account_hospital.company_id 
            left join md_org_account on md_org_account.id = md_org_account_hospital.account_id
            left join md_hospital on md_org_account_hospital.hospital_id = md_hospital.id
            left join md_position_hospital on md_org_account_hospital.hospital_id = md_position_hospital.hospital_id
            where md_org_account_hospital.update_time >= @{updateTime}
                and md_org_account_hospital.state_code = 'Open' and md_org_account_hospital.term_code <> 'terminate' 
                and category_code = 'Dealer' and is_content_dealer = 'T' and is_content_distributor = 'T'
                and not exists (
                    select 1 from md_channel_structure 
                    where md_channel_structure.authorize_id = md_org_account_hospital.id
                )
            ]]>    
        </sql>            
        
        <sql name="createChannelStructure_dealerOnly">
            <![CDATA[
            insert into md_channel_structure (
                id,    company_id, company_name, bu_id, bu_name, org_id, account_ncc_id, 
                authorize_id, category_code, state_code, state_name,
                account_id, account_code, account_name, 
                hospital_id, hospital_code, hospital_name,department,
                authorization_code, file_authorization, file_name_authorization,
                position_id,
                date_from, date_to,    create_time, update_time
            )
            select UUID_SHORT(), md_org_account_hospital.company_id, md_company.short_name company_name, 
                md_org_account_hospital.bu_id, md_bu.name bu_name, md_org_account_hospital.org_id, md_org_account.org_ncc_id,
                md_org_account_hospital.id, category_code, md_org_account_hospital.state_code, md_org_account_hospital.state_name,
                account_id, md_org_account_hospital.org_code, md_org_account_hospital.org_name,
                md_org_account_hospital.hospital_id, md_hospital.code, md_hospital.name, md_org_account_hospital.department, 
                md_org_account_hospital.code, file_authorization, file_name_authorization,
                md_position_hospital.position_id,
                date_from, date_to,    now(), now()
            from md_org_account_hospital 
            left join  md_bu on md_bu.id = md_org_account_hospital.bu_id 
            left join (select id, short_name, name from md_org where is_master = 'T') md_company on md_company.id = md_org_account_hospital.company_id 
            left join md_org_account on md_org_account.id = md_org_account_hospital.account_id
            left join md_hospital on md_org_account_hospital.hospital_id = md_hospital.id
            left join md_position_hospital on md_org_account_hospital.hospital_id = md_position_hospital.hospital_id
            where md_org_account_hospital.update_time >= @{updateTime}
                and md_org_account_hospital.state_code = 'Open' and md_org_account_hospital.term_code <> 'terminate' 
                and category_code = 'Dealer' and is_content_dealer = 'T' and is_content_distributor is null
                and not exists (
                    select 1 from md_channel_structure 
                    where md_channel_structure.authorize_id = md_org_account_hospital.id
                ) 
            ]]>    
        </sql>
        
        <sql name="createChannelStructure_dealerDistributor">
            <![CDATA[
            insert into md_channel_structure (
                id,    company_id, company_name, bu_id, bu_name, org_id, account_ncc_id, 
                authorize_id, category_code, state_code, state_name,
                account_id, account_code, account_name, 
                distributor_id, distributor_code, distributor_name,
                hospital_id, hospital_code, hospital_name, department,
                authorization_code, file_authorization, file_name_authorization,
                position_id,
                date_from, date_to,    create_time, update_time
            )
            select UUID_SHORT(), md_org_account_hospital.company_id, md_company.short_name company_name, 
                md_org_account_hospital.bu_id, md_bu.name bu_name, md_org_account_hospital.org_id, md_org_account.org_ncc_id,
                md_org_account_hospital.id, category_code, md_org_account_hospital.state_code, md_org_account_hospital.state_name, 
                account_id, md_org_account_hospital.org_code, md_org_account_hospital.org_name,
                account_id, md_org_account_hospital.org_code, md_org_account_hospital.org_name,
                md_org_account_hospital.hospital_id, md_hospital.code, md_hospital.name, md_org_account_hospital.department,  
                md_org_account_hospital.code, file_authorization, file_name_authorization,
                md_position_hospital.position_id,
                date_from, date_to,    now(), now()
            from md_org_account_hospital 
            left join  md_bu on md_bu.id = md_org_account_hospital.bu_id 
            left join (select id, short_name, name from md_org where is_master = 'T') md_company on md_company.id = md_org_account_hospital.company_id 
            left join md_org_account on md_org_account.id = md_org_account_hospital.account_id
            left join md_hospital on md_org_account_hospital.hospital_id = md_hospital.id
            left join md_position_hospital on md_org_account_hospital.hospital_id = md_position_hospital.hospital_id
            where md_org_account_hospital.update_time >= @{updateTime}
                and md_org_account_hospital.state_code = 'Open' and md_org_account_hospital.term_code <> 'terminate' 
                and category_code = 'Dealer'
                and not exists (
                    select 1 from md_channel_structure 
                    where md_channel_structure.authorize_id = md_org_account_hospital.id
                ) 
            ]]>    
        </sql>        
        
        <sql name="createChannelStructure_distributor">
            <![CDATA[
            insert into md_channel_structure (
                id,    company_id, company_name, bu_id, bu_name, org_id, account_ncc_id, 
                account_code,account_name,
                authorize_id, category_code, state_code, state_name, 
                distributor_id, distributor_code, distributor_name,
                hospital_id, hospital_code, hospital_name, department,
                authorization_code, file_authorization, file_name_authorization,
                position_id,
                date_from, date_to,    create_time, update_time
            )
            select UUID_SHORT(), md_org_account_hospital.company_id, md_company.short_name company_name, 
                md_org_account_hospital.bu_id, md_bu.name bu_name, md_org_account_hospital.org_id, md_org_account.org_ncc_id,
                md_org_account_hospital.org_code, md_org_account_hospital.org_name, 
                md_org_account_hospital.id, category_code, md_org_account_hospital.state_code, md_org_account_hospital.state_name, connected_org_id, 
                md_org_account_hospital.connected_org_code, md_org_account_hospital.connected_org_name,
                md_org_account_hospital.hospital_id, md_hospital.code, md_hospital.name, md_org_account_hospital.department, 
                md_org_account_hospital.code, file_authorization, file_name_authorization,
                md_position_hospital.position_id,
                date_from, date_to,    now(), now()
            from md_org_account_hospital 
            left join  md_bu on md_bu.id = md_org_account_hospital.bu_id 
            left join (select id, short_name, name from md_org where is_master = 'T') md_company on md_company.id = md_org_account_hospital.company_id 
            left join md_org_account on md_org_account.id = md_org_account_hospital.account_id
            left join md_hospital on md_org_account_hospital.hospital_id = md_hospital.id
            left join md_position_hospital on md_org_account_hospital.hospital_id = md_position_hospital.hospital_id
            where md_org_account_hospital.update_time >= @{updateTime}
                and md_org_account_hospital.state_code = 'Open' and md_org_account_hospital.term_code <> 'terminate' 
                and category_code = 'Distributor' and not exists (
                    select 1 from md_channel_structure 
                    where md_channel_structure.authorize_id = md_org_account_hospital.id
                )
            ]]>    
        </sql>
        
        <sql name="closeChannelStructure">
            <![CDATA[
            update md_channel_structure left join md_org_account_hospital
                on md_channel_structure.authorize_id = md_org_account_hospital.id
                
            set md_channel_structure.state_code = 'Close', md_channel_structure.state_name = '已终止',
                md_channel_structure.suspend_date = md_org_account_hospital.suspend_date,
                md_channel_structure.suspend_remark = md_org_account_hospital.suspend_remark
 
            where md_org_account_hospital.update_time >= @{updateTime}
                and md_org_account_hospital.state_code = 'Closed'
                and md_org_account_hospital.id is not null
            ]]>    
        </sql>            
        
        <sql name="createChannelStructure_platform">
            <![CDATA[
            insert into md_channel_structure (
                id,    type_code,
                company_name, bu_name, 
                hospital_id, hospital_code, hospital_name, department,
                org_id, account_ncc_id, 
                platform_id, platform_code, platform_name,
                create_time, update_time
            )
            select UUID_SHORT(), md_org_account.actor_code,
                md_company.short_name company_name, md_bu.name bu_name,
                md_hospital.id, md_hospital.code, md_hospital.name, md_org_account_hospital.department, 
                md_org_account_hospital.org_id, md_org_account.org_ncc_id, 
                md_org_account.id, md_org_account_hospital.org_code, md_org_account_hospital.org_name,
                now(), now()
            from md_org_account_hospital 
            left join  md_bu on md_bu.id = md_org_account_hospital.bu_id 
            left join (select id, short_name, name from md_org where is_master = 'T') md_company on md_company.id = md_org_account_hospital.company_id 
            left join md_org_account on md_org_account.id = md_org_account_hospital.account_id
            left join md_hospital on md_org_account_hospital.hospital_id = md_hospital.id
            left join agm_authorization on agm_authorization.org_hospital_id = md_org_account_hospital.id
            where md_org_account.actor_code = 'Platform' and md_org_account_hospital.state_code = 'Open'
                and not exists (
                select 1 from md_channel_structure where md_channel_structure.type_code = md_org_account.actor_code
                    and md_org_account.company_name = md_channel_structure.company_name
                    and md_org_account.bu_name = md_channel_structure.bu_name
                    and platform_id = md_org_account.id
            )
            ]]>    
        </sql>        
        
        <sql name="getCustomerSalesEmployee">
        <![CDATA[
            select md_employee.id, md_employee.code, md_employee.name,
                md_employee.nc_org_id, md_employee.nc_org_code,
                md_department_oa_nc.oa_department_id,md_department_oa_nc.oa_department_name,
                md_department_oa_nc.nc_region_id, md_department_oa_nc.nc_region_code, md_department_oa_nc.nc_region_name,
                md_department_oa_nc.nc_area_id, md_department_oa_nc.nc_area_code, md_department_oa_nc.nc_area_name
            from md_employee 
            left join md_department_oa_nc on md_employee.job_title_code = md_department_oa_nc.oa_department_code
            where exists (
                select 1 from md_org_account_position left join md_position_employee
                    on md_org_account_position.position_id = md_position_employee.position_id
                where @{filter} and md_employee.id = md_position_employee.employee_id
            ) and md_employee.is_active = 'T'
        ]]>    
        </sql>
        
        <sql name="getAccountEntity">
        <![CDATA[
            select md_org_account.id account_id, md_org_account.account_name account_name, 
                md_org_account_position.position_id  from md_org_account 
            left join md_org_account_position on md_org_account_position.parent_id = md_org_account.id 
            where  md_org_account.id = '@{accountId}'
        ]]>    
        </sql>
        
        <sql name="getPositionDistributor">
        <![CDATA[
            select count(distinct distributor_id) customerCount 
            from md_channel_structure 
            where @{filter} @{orderby} @{limit} 
        ]]>    
        </sql>
 
        <sql name="getEmployeePositions">
        <![CDATA[
             select md_position.* from md_position_employee
             left join md_position on md_position_employee.position_id = md_position.id 
             where md_position_employee.employee_id = '@{employeeId}' and md_position.is_delete = 'F'
        ]]>
        </sql>
               
    </dataSpace>
    
</sqls>