1
13693261870
2022-09-16 762f2fb45db004618ba099aa3c0bd89dba1eb843
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.landtool.lanbase.modules.org.dao.OrgUnitDao">
 
    <!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="com.landtool.lanbase.modules.org.entity.OrgUnit" id="unitMap">
        <result property="unitid" column="UNITID"/>
        <result property="parentid" column="PARENTID"/>
        <result property="unitname" column="UNITNAME"/>
        <result property="unidcode" column="UNIDCODE"/>
        <result property="unidorgcode" column="UNIDORGCODE"/>
        <result property="spellfirst" column="SPELLFIRST"/>
        <result property="unitshort" column="UNITSHORT"/>
        <result property="unittype" column="UNITTYPE"/>
        <result property="rorder" column="RORDER"/>
        <result property="contactnumber" column="CONTACTNUMBER"/>
        <result property="faxnumber" column="FAXNUMBER"/>
        <result property="email" column="EMAIL"/>
        <result property="address" column="ADDRESS"/>
        <result property="postalcode" column="POSTALCODE"/>
        <result property="photourl" column="PHOTOURL"/>
        <result property="websiteurl" column="WEBSITEURL"/>
        <result property="status" column="STATUS"/>
        <result property="rcreatedate" column="RCREATEDATE"/>
        <result property="rcreateuser" column="RCREATEUSER"/>
        <result property="rlasteditdate" column="RLASTEDITDATE"/>
        <result property="remark" column="REMARK"/>
        <result property="isdept" column="ISDEPT"/>
    </resultMap>
    
    <resultMap type="com.landtool.lanbase.modules.org.entity.OrgUnitJoinRegion" id="UnitJoinRegion">
        <result property="unitid" column="UNITID"/>
        <result property="parentid" column="PARENTID"/>
        <result property="unitname" column="UNITNAME"/>
        <result property="unidcode" column="UNIDCODE"/>
        <result property="unidorgcode" column="UNIDORGCODE"/>
        <result property="spellfirst" column="SPELLFIRST"/>
        <result property="unitshort" column="UNITSHORT"/>
        <result property="unittype" column="UNITTYPE"/>
        <result property="rorder" column="RORDER"/>
        <result property="contactnumber" column="CONTACTNUMBER"/>
        <result property="faxnumber" column="FAXNUMBER"/>
        <result property="email" column="EMAIL"/>
        <result property="address" column="ADDRESS"/>
        <result property="postalcode" column="POSTALCODE"/>
        <result property="photourl" column="PHOTOURL"/>
        <result property="websiteurl" column="WEBSITEURL"/>
        <result property="status" column="STATUS"/>
        <result property="rcreatedate" column="RCREATEDATE"/>
        <result property="rcreateuser" column="RCREATEUSER"/>
        <result property="rlasteditdate" column="RLASTEDITDATE"/>
        <result property="remark" column="REMARK"/>
        <result property="isdept" column="ISDEPT"/>
        <collection property="orgunitregion" ofType="com.landtool.lanbase.modules.org.entity.OrgRegion" resultMap="com.landtool.lanbase.modules.org.dao.OrgUnitRegionDao.unitregionMap"/>
    </resultMap>
    
    <resultMap type="com.landtool.lanbase.common.utils.NameBindId" id="NameBindIdMap">
        <result property="name" column="NAME"/>
        <result property="id" column="ID"/>
    </resultMap>
    
    <sql id="unitColumns">
        un.UNITID,un.PARENTID,un.UNITNAME,un.UNIDCODE,un.UNIDORGCODE,un.SPELLFIRST,un.UNITSHORT,un.UNITTYPE,un.RORDER,
        un.CONTACTNUMBER,un.FAXNUMBER,un.EMAIL,un.ADDRESS,un.POSTALCODE,un.PHOTOURL,un.WEBSITEURL,un.STATUS,
        un.RCREATEDATE,un.RCREATEUSER,un.RLASTEDITDATE,
        un.REMARK,un.ISDEPT
    </sql>
    
    <sql id="unitRegionColumns">
        ux.regionid,ux.unitid,ux.rcreatedate,ux.rcreateuser,ux.rlasteditdate
    </sql>
 
    <select id="queryObject" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select un.*,ux.regionid,ux.regionname,ur.managerids,ur.managernames from ORG_UNIT un
              LEFT JOIN (
                    SELECT distinct A.unitid,
                    (SELECT STRING_AGG(cast(B.regionid as varchar),',') FROM (
                        SELECT ou.unitid,qh.regionid,qh.regionname
                        FROM ORG_UNIT ou
                            INNER JOIN ORG_UNITREGION ux ON ou.unitid = ux.unitid
                            left join ORG_REGION qh on qh.regionid = replace(ux.regionid,' ','')
                        ) B WHERE B.unitid = A.unitid group by A.unitid order by A.unitid) as regionid,
                    (SELECT STRING_AGG(cast(C.regionname as varchar),',') FROM (
                        SELECT ou.unitid,qh.regionid,qh.regionname
                        FROM ORG_UNIT ou
                            INNER JOIN ORG_UNITREGION ux ON ou.unitid = ux.unitid
                            left join ORG_REGION qh on qh.regionid = replace(ux.regionid,' ','')
                        ) C WHERE C.unitid = A.unitid group by A.unitid order by A.unitid) as regionname
                    FROM (
                        SELECT ou.unitid,qh.regionid,qh.regionname
                        FROM ORG_UNIT ou
                            INNER JOIN ORG_UNITREGION ux ON ou.unitid = ux.unitid
                            left join ORG_REGION qh on qh.regionid = replace(ux.regionid,' ','')
                    )A GROUP BY A.unitid
              )ux on ux.unitid=un.unitid
              LEFT JOIN (
                 SELECT distinct A.unitid,
                    (SELECT STRING_AGG(cast(B.userid as varchar),',') FROM (
                        SELECT ui.unitid,um.userid,gu.chinesename
                        from ORG_UNIT ui
                                 inner join ORG_UNITMANAGER um on ui.unitid = um.unitid
                                 left join ORG_USER gu on gu.userid = um.userid
                        ) B WHERE B.unitid = A.unitid group by A.unitid order by A.unitid) as managerids,
                    (SELECT STRING_AGG(cast(C.chinesename as varchar),',') FROM (
                        SELECT ui.unitid,um.userid,gu.chinesename
                        from ORG_UNIT ui
                                 inner join ORG_UNITMANAGER um on ui.unitid = um.unitid
                                 left join ORG_USER gu on gu.userid = um.userid
                        ) C WHERE C.unitid = A.unitid group by chinesename order by chinesename) as managernames
                    FROM (
                        SELECT ui.unitid,um.userid,gu.chinesename
                        from ORG_UNIT ui
                                 inner join ORG_UNITMANAGER um on ui.unitid = um.unitid
                                 left join ORG_USER gu on gu.userid = um.userid
                    )A
            )ur on ur.unitid = un.unitid
              where un.UNITID = #{value}
    </select>
 
    <select id="queryList" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select * from (
        SELECT *
        FROM(
            SELECT ROW_NUMBER() OVER(ORDER BY B.unitid) AS rownumber,B.*
                FROM(
                    select un.*,f.vtext unittypetext from ORG_UNIT un
                    LEFT join SYS_FIELDVALUE f on f.vcode = un.unittype and f.fkey ='UnitType'
                    <where>
                        <if test="unitname != null and unitname != ''">
                            AND (un.unitname LIKE ('%' || #{unitname} || '%') or LOWER(un.spellfirst) LIKE ('%' || LOWER(#{unitname}) || '%'))
                        </if>
                        <if test="unittype != null and unittype != ''">
                            AND un.unittype LIKE ('%' || #{unittype} || '%')
                        </if>
                        <if test="parentid != null and parentid != 0">
                            AND un.parentid = #{parentid}
                        </if>
                        <if test="isdept != null">
                            AND un.isdept = #{isdept}
                        </if>
                        <if test="isdept == null">
                            AND un.isdept != 1
                        </if>
                        and un.status = 1
                    </where>
                ) B
            ) A
            WHERE rownumber > #{lowerOffset} AND  <![CDATA[ rownumber <= ${upperOffset} ]]>
            )a order by
        <choose>
            <when test="sidx != null and sidx != ''">
                ${sidx} ${order}
            </when>
            <otherwise>
                unitid
            </otherwise>
        </choose>
    </select>
    
     <select id="queryTotal" resultType="int">
        select count(*) from ORG_UNIT un
        <where>
            <if test="unitname != null and unitname != ''">
                AND (un.unitname LIKE ('%' || #{unitname} || '%') or LOWER(un.spellfirst) LIKE ('%' || LOWER(#{unitname}) || '%') ) 
            </if>
            <if test="unittype != null and unittype != ''">
                AND un.unittype LIKE ('%' || #{unittype} || '%')
            </if>
            <if test="parentid != null and parentid != 0">
                AND un.parentid = #{parentid}
            </if>
            <if test="isdept != null">
                AND un.isdept = #{isdept}
            </if>
            <if test="isdept == null">
                AND un.isdept != 1
            </if>
            and un.status = 1
        </where>
    </select>
     
    <insert id="save" parameterType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        insert into ORG_UNIT
        (
            PARENTID, 
            UNITNAME, 
            UNIDCODE, 
            UNIDORGCODE, 
            SPELLFIRST, 
            UNITSHORT, 
            UNITTYPE, 
            RORDER, 
            CONTACTNUMBER, 
            FAXNUMBER, 
            EMAIL, 
            ADDRESS, 
            POSTALCODE, 
            PHOTOURL, 
            WEBSITEURL, 
            STATUS, 
            RCREATEDATE, 
            RCREATEUSER, 
            RLASTEDITDATE, 
            REMARK,
            ISDEPT
        )
        values
        (
            #{parentid}, 
            #{unitname}, 
            #{unidcode,jdbcType=VARCHAR}, 
            #{unidorgcode,jdbcType=VARCHAR}, 
            #{spellfirst,jdbcType=VARCHAR}, 
            #{unitshort,jdbcType=VARCHAR}, 
            #{unittype}, 
            #{rorder}, 
            #{contactnumber,jdbcType=VARCHAR}, 
            #{faxnumber,jdbcType=VARCHAR}, 
            #{email,jdbcType=VARCHAR}, 
            #{address,jdbcType=VARCHAR}, 
            #{postalcode,jdbcType=VARCHAR}, 
            #{photourl,jdbcType=VARCHAR}, 
            #{websiteurl,jdbcType=VARCHAR}, 
            #{status}, 
            #{rcreatedate}, 
            #{rcreateuser}, 
            #{rlasteditdate}, 
            #{remark,jdbcType=VARCHAR},
            #{isdept}
        )
    </insert>
     
    <update id="update" parameterType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        update ORG_UNIT
        <set>
            <if test="parentid != null">PARENTID = #{parentid}, </if>
            <if test="unitname != null">UNITNAME = #{unitname}, </if>
            <if test="unidcode != null">UNIDCODE = #{unidcode}, </if>
            <if test="unidorgcode != null">UNIDORGCODE = #{unidorgcode}, </if>
            <if test="spellfirst != null">SPELLFIRST = #{spellfirst}, </if>
            <if test="unitshort != null">UNITSHORT = #{unitshort}, </if>
            <if test="unittype != null">UNITTYPE = #{unittype}, </if>
            <if test="rorder != null">RORDER = #{rorder}, </if>
            <if test="contactnumber != null">CONTACTNUMBER = #{contactnumber}, </if>
            <if test="faxnumber != null">FAXNUMBER = #{faxnumber}, </if>
            <if test="email != null">EMAIL = #{email}, </if>
            <if test="address != null">ADDRESS = #{address}, </if>
            <if test="postalcode != null">POSTALCODE = #{postalcode}, </if>
            <if test="photourl != null">PHOTOURL = #{photourl}, </if>
            <if test="websiteurl != null">WEBSITEURL = #{websiteurl}, </if>
            <if test="status != null">STATUS = #{status}, </if>
            <if test="rcreatedate != null">RCREATEDATE = #{rcreatedate}, </if>
            <if test="rcreateuser != null">RCREATEUSER = #{rcreateuser}, </if>
            <if test="rlasteditdate != null">RLASTEDITDATE = #{rlasteditdate}, </if>
            <if test="remark != null">REMARK = #{remark}, </if>
            <if test="isdept != null">ISDEPT = #{isdept}</if>
        </set>
        where UNITID = #{unitid}
    </update>
    
    <delete id="delete">
        delete from ORG_UNIT where UNITID = #{value}
    </delete>
    
    <delete id="deleteBatch">
        delete from ORG_UNIT where UNITID in
        <foreach item="unitid" collection="array" open="(" separator="," close=")">
            #{unitid}
        </foreach>
    </delete>
    
    <select id="queryObjectJoinRegion" resultMap="UnitJoinRegion">
        SELECT
            <include refid="unitColumns"/>,
            <include refid="unitRegionColumns"/>
        FROM ORG_UNIT un
        LEFT JOIN ORG_UNITREGION ux ON un.UNITID=ux.UNITID
        WHERE un.UNITID = #{unitid} and isdept != 1 and STATUS = 1
    </select>
 
    <select id="queryListByTime" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        SELECT
            <include refid="unitColumns"/>
        FROM ORG_UNIT un
        WHERE un.RLASTEDITDATE > #{time} and isdept != 1 and STATUS = 1
    </select>
    
    <select id="findUnitNames" resultType="com.landtool.lanbase.common.utils.NameBindId">
        select 
            un.UNITNAME AS NAME,un.UNITID AS ID
        FROM ORG_UNIT un
        <where>
            <!-- 查询状态为正常的机构 alert ykm 2019/03/19 -->
            STATUS = 1
            and isdept != 1
            <if test="keyWord != null and keyWord != ''">
                AND un.UNITNAME LIKE ('%' || #{keyWord} || '%')
            </if>
        </where> 
    </select>
    
    <select id="findPinyin" resultType="java.lang.String">
        select 
            un.UNITNAME 
        from ORG_UNIT un
        <where>
            <!-- 查询状态为正常的机构 alert ykm 2019/03/19 -->
            STATUS = 1
            and isdept != 1
        <if test="keyWord != null and keyWord != ''">
           and (LOWER(un.SPELLFIRST) LIKE ('%' || LOWER(#{keyWord}) || '%')
            or un.unitname LIKE ('%' || #{keyWord} || '%'))
        </if>
        </where>
    </select>
    
    <select id="findPinyinList" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select *
        from ORG_UNIT un
        <where>
            and isdept != 1
        <if test="keyWord != null and keyWord != ''">
            AND LOWER(un.SPELLFIRST) LIKE ('%' || LOWER(#{keyWord}) || '%')
        </if>
        </where>
    </select>
    
    <select id="queryObjectByFullName" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select * from ORG_UNIT un where un.UNITNAME = #{unitname} and isdept != 1
    </select>
    
    <select id="queryUnitWithSEQ" resultType="Long">
        SELECT currval('ORG_UNIT_SEQ') FROM DUAL
    </select>
    
    <select id="queryUnitTreeList" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select * from ORG_UNIT where status = 1 and isdept != 1 order by rorder
    </select>
    
    <select id="queryTreeChildList" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        with RECURSIVE tab(UNITID,PARENTID,
            UNITNAME,
            UNIDCODE,
            UNIDORGCODE,
            SPELLFIRST,
            UNITSHORT,
            UNITTYPE,
            RORDER,
            CONTACTNUMBER,
            FAXNUMBER,
            EMAIL,
            ADDRESS,
            POSTALCODE,
            PHOTOURL,
            WEBSITEURL,
            STATUS,
            RCREATEDATE,
            RCREATEUSER,
            RLASTEDITDATE,
            REMARK,
            ISDEPT) as (
          select UNITID,PARENTID,
            UNITNAME,
            UNIDCODE,
            UNIDORGCODE,
            SPELLFIRST,
            UNITSHORT,
            UNITTYPE,
            RORDER,
            CONTACTNUMBER,
            FAXNUMBER,
            EMAIL,
            ADDRESS,
            POSTALCODE,
            PHOTOURL,
            WEBSITEURL,
            STATUS,
            RCREATEDATE,
            RCREATEUSER,
            RLASTEDITDATE,
            REMARK,
            ISDEPT from ORG_UNIT
        where cast(unitid as varchar) = #{unitid} and isdept != 1
        union all
        select b.UNITID,b.PARENTID,
            b.UNITNAME,
            b.UNIDCODE,
            b.UNIDORGCODE,
            b.SPELLFIRST,
            b.UNITSHORT,
            b.UNITTYPE,
            b.RORDER,
            b.CONTACTNUMBER,
            b.FAXNUMBER,
            b.EMAIL,
            b.ADDRESS,
            b.POSTALCODE,
            b.PHOTOURL,
            b.WEBSITEURL,
            b.STATUS,
            b.RCREATEDATE,
            b.RCREATEUSER,
            b.RLASTEDITDATE,
            b.REMARK,
            b.ISDEPT from tab a,
          ORG_UNIT b
          where b.parentid = a.unitid
        )
        SELECT *
        FROM(
            SELECT ROW_NUMBER() OVER(ORDER BY C.parentid,C.rorder) AS rownumber,C.*,f.vtext unittypetext
                FROM
                    tab
                 C LEFT join SYS_FIELDVALUE f on f.vcode = C.unittype and f.fkey ='UnitType' where C.status = 1 and C.isdept != 1
            ) D
            WHERE rownumber > #{lowerOffset} AND  <![CDATA[ rownumber <= ${upperOffset} ]]>
    </select>
    
    <select id="queryTreeChildTotal" resultType="int">
        with RECURSIVE tab(UNITID,PARENTID,
            UNITNAME,
            UNIDCODE,
            UNIDORGCODE,
            SPELLFIRST,
            UNITSHORT,
            UNITTYPE,
            RORDER,
            CONTACTNUMBER,
            FAXNUMBER,
            EMAIL,
            ADDRESS,
            POSTALCODE,
            PHOTOURL,
            WEBSITEURL,
            STATUS,
            RCREATEDATE,
            RCREATEUSER,
            RLASTEDITDATE,
            REMARK,
            ISDEPT) as (
          select UNITID,PARENTID,
            UNITNAME,
            UNIDCODE,
            UNIDORGCODE,
            SPELLFIRST,
            UNITSHORT,
            UNITTYPE,
            RORDER,
            CONTACTNUMBER,
            FAXNUMBER,
            EMAIL,
            ADDRESS,
            POSTALCODE,
            PHOTOURL,
            WEBSITEURL,
            STATUS,
            RCREATEDATE,
            RCREATEUSER,
            RLASTEDITDATE,
            REMARK,
            ISDEPT from ORG_UNIT
        where cast(unitid as varchar) = #{unitid}
        union all
        select b.UNITID,b.PARENTID,
            b.UNITNAME,
            b.UNIDCODE,
            b.UNIDORGCODE,
            b.SPELLFIRST,
            b.UNITSHORT,
            b.UNITTYPE,
            b.RORDER,
            b.CONTACTNUMBER,
            b.FAXNUMBER,
            b.EMAIL,
            b.ADDRESS,
            b.POSTALCODE,
            b.PHOTOURL,
            b.WEBSITEURL,
            b.STATUS,
            b.RCREATEDATE,
            b.RCREATEUSER,
            b.RLASTEDITDATE,
            b.REMARK,
            b.ISDEPT from tab a,
          ORG_UNIT b
          where b.parentid = a.unitid
        )
        select Count(*) from tab where status = 1 and isdept != 1
    </select>
    
    <update id="queryUnitById" >
        update ORG_UNIT
        set STATUS = 2, RLASTEDITDATE = now()  where UNITID in
        <foreach item="id" collection="unitid" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    
    <select id="queryListByParentid" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select * from ORG_UNIT where parentid = #{parentid} and status = 1 and isdept != 1 order by rorder
    </select>
    
    <update id="updateRorder" parameterType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        update ORG_UNIT
        <set>
            RLASTEDITDATE=now(),
            <if test="rorder != null">RORDER = #{rorder}</if>
        </set>
        where unitid = #{unitid}
    </update>
    
    <select id="queryMaxRorder" resultType="int">
        select case when max(rorder) > 0 then max(rorder) else 0 end as rorder from ORG_UNIT where parentid=#{parentid} and isdept != 1
    </select>
 
    <select id="queryAllList" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        SELECT
            *
        FROM ORG_UNIT un where status = 1 and isdept != 1
    </select>
 
    <select id="queryDeptList" resultType="com.landtool.lanbase.modules.org.entity.OrgUnit">
        select * from ORG_UNIT where status = 1 and isdept = 1 and parentid = (select unitid from ORG_UNIT where unitname = #{unitname})
    </select>
</mapper>