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
<?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.OrgUserGroupDao">
 
    <!-- 可根据自己的需求,是否要使用 -->
    <resultMap type="com.landtool.lanbase.modules.org.entity.OrgUserGroup" id="usergroupMap">
        <result property="userId" column="userid"/>
        <result property="groupId" column="groupid"/>
        <result property="createUnitId" column="createunitid"/>
        <result property="rCreateDate" column="rcreatedate"/>
        <result property="rCreateUser" column="rcreateuser"/>
    </resultMap>
 
    <select id="queryObject" resultType="com.landtool.lanbase.modules.org.entity.OrgUserGroup">
        select * from ORG_USERGROUP where userid = #{value}
    </select>
 
    <select id="queryList" resultType="com.landtool.lanbase.modules.org.entity.OrgUserGroup">
 
        SELECT *
        FROM(
            SELECT ROW_NUMBER() OVER(ORDER BY B.userid) AS rownumber,B.*
            FROM (
 
                        select * from ORG_USERGROUP
 
            ) B
        ) A
        WHERE rownumber > #{lowerOffset} AND  <![CDATA[ rownumber <= ${upperOffset} ]]>
    </select>
    
     <select id="queryTotal" resultType="int">
        select count(*) from ORG_USERGROUP
    </select>
 
    <select id="queryListForUserJoinUnitWithRoles" resultType="map">
 
        SELECT USERID  "userId",
        LOGINNAME  "loginName",
        CHINESENAME  "chineseName",
        USERSTATUS  "userStatus",
        GROUPS   "groups",
        UNITNAMES  "unitNames",
        UNITTYPE  "unitType"
        FROM(
            SELECT ROW_NUMBER() OVER(ORDER BY B.USERID) AS rownumber,B.*
            FROM (
 
                SELECT
                  ou.USERID AS userId,
                  ou.LOGINNAME AS loginName,
                  ou.CHINESENAME AS chineseName,
                  ou.USERSTATUS AS userStatus,
                  b.GROUPS AS  groups,
                  b2.UNITNAMES AS unitNames,
                  b2.UNITTYPES AS unitType
                FROM ORG_USER ou
                  LEFT JOIN (
                        SELECT DISTINCT
                            A.userid,
                            (SELECT STRING_AGG(cast(B.groupname as varchar),',') FROM (
                                    SELECT
                                        oug.userid,og.groupname
                                    FROM ORG_USERGROUP oug
                                    LEFT JOIN ORG_GROUP og ON oug.groupid = og.groupid
                                ) B
                                WHERE B.userid = A.userid group by A.userid order by A.userid) AS GROUPS
                        FROM (
                            SELECT
                                oug.userid,og.groupname
                            FROM ORG_USERGROUP oug
                            LEFT JOIN ORG_GROUP og ON oug.groupid = og.groupid
                        ) A
                    ) b ON b.userid = ou.userid
                  LEFT JOIN (
                    SELECT distinct A.userid,C.unitname AS UNITNAMES,
                        (SELECT STRING_AGG(cast(B.unittype as varchar),',') FROM (
                            SELECT
                                ouu.userid,ou.unittype
                            FROM  ORG_USERUNIT ouu
                            LEFT JOIN ORG_UNIT ou ON ouu.unitid = ou.unitid
                            ) B WHERE  B.userid = A.userid group by A.userid order by A.userid) as UNITTYPES
                    FROM (
                        SELECT
                            ouu.userid,ou.unittype
                        FROM  ORG_USERUNIT ouu
                        LEFT JOIN ORG_UNIT ou ON ouu.unitid = ou.unitid
                    )A
                    LEFT JOIN (
                        SELECT
                            ouu.userid,max(ou.unitname) AS unitname ,max(ou.rcreatedate) AS currentdate
                        FROM  ORG_USERUNIT ouu
                        LEFT JOIN ORG_UNIT ou ON ouu.unitid = ou.unitid
                        GROUP BY ouu.userid
                    )C ON A.userid = C.userid
                  ) b2 ON b2.userid = ou.userid
                  <where>
                      <if test="excludeUserIds != null ">
                          ou.userstatus = 0
                          <choose>
                              <when test="excludeUserIds.size() > 0" >
                                  AND ou.userid NOT IN
                                  <foreach item="userId" collection="excludeUserIds" open="(" separator="," close=")">
                                      #{userId}
                                  </foreach>
                              </when>
                          </choose>
                      </if>
                      <if test="includeUserIds != null">
                          <choose>
                              <when test="includeUserIds.size() > 0">
                                  AND ou.userid IN
                                  <foreach item="userId" collection="includeUserIds" open="(" separator="," close=")">
                                      #{userId}
                                  </foreach>
                              </when>
                              <otherwise>
                                  AND ou.userid IN ('')
                              </otherwise>
                          </choose>
                      </if>
                      <if test="userName != null and userName != ''">
                          AND (ou.LOGINNAME LIKE ('%'|| #{userName} ||'%') or ou.CHINESENAME LIKE ('%'|| #{userName} ||'%') or LOWER(ou.spellfirst) LIKE ('%'|| LOWER(#{userName}) ||'%'))
                      </if>
                      <if test="userNameWithSelected != null and userNameWithSelected != ''">
                            AND (ou.LOGINNAME LIKE ('%'|| #{userNameWithSelected} ||'%') or ou.CHINESENAME LIKE ('%'|| #{userNameWithSelected} ||'%')  or LOWER(ou.spellfirst) LIKE ('%'|| LOWER(#{userNameWithSelected}) ||'%'))
                      </if>
                      <if test="unitType != null and unitType != ''">
                          AND b2.UNITTYPES LIKE ('%'|| #{unitType} ||'%')
                      </if>
                  </where>
 
            ) B
        ) A
        WHERE rownumber > #{lowerOffset} AND  <![CDATA[ rownumber <= ${upperOffset} ]]>
    </select>
 
    <select id="queryTotalOfListForUserJoinUnitWithRoles" resultType="int">
        SELECT
            count(*)
        FROM ORG_USER ou
        LEFT JOIN (
            SELECT DISTINCT
                A.userid,
                (SELECT STRING_AGG(cast(B.groupname as varchar),',') FROM (
                    SELECT
                        oug.userid,og.groupname
                    FROM ORG_USERGROUP oug
                    LEFT JOIN ORG_GROUP og ON oug.groupid = og.groupid
                ) B
                WHERE B.userid = A.userid group by A.userid order by A.userid) AS GROUPS
            FROM (
                SELECT
                    oug.userid,og.groupname
                FROM ORG_USERGROUP oug
                LEFT JOIN ORG_GROUP og ON oug.groupid = og.groupid
            ) A
        ) b ON b.userid = ou.userid
        LEFT JOIN (
            SELECT distinct A.userid,C.unitname AS UNITNAMES,
                (SELECT STRING_AGG(cast(B.unittype as varchar),',') FROM (
                    SELECT
                        ouu.userid,ou.unittype
                    FROM  ORG_USERUNIT ouu
                    LEFT JOIN ORG_UNIT ou ON ouu.unitid = ou.unitid
                    ) B WHERE  B.userid = A.userid group by A.userid order by A.userid) as UNITTYPES
            FROM (
                SELECT
                    ouu.userid,ou.unittype
                FROM  ORG_USERUNIT ouu
                LEFT JOIN ORG_UNIT ou ON ouu.unitid = ou.unitid
            )A
            LEFT JOIN (
                SELECT
                    ouu.userid,max(ou.unitname) AS unitname,max(ou.rcreatedate) AS currentdate
                FROM  ORG_USERUNIT ouu
                LEFT JOIN ORG_UNIT ou ON ouu.unitid = ou.unitid
                GROUP BY ouu.userid
            )C ON A.userid = C.userid
        ) b2 ON b2.userid = ou.userid
        <where>
            <if test="excludeUserIds != null ">
                ou.userstatus = 0
                <choose>
                    <when test="excludeUserIds.size() > 0" >
                        AND ou.userid NOT IN
                        <foreach item="userId" collection="excludeUserIds" open="(" separator="," close=")">
                            #{userId}
                        </foreach>
                    </when>
                </choose>
            </if>
            <if test="includeUserIds != null">
                <choose>
                    <when test="includeUserIds.size() > 0">
                        AND ou.userid IN
                        <foreach item="userId" collection="includeUserIds" open="(" separator="," close=")">
                            #{userId}
                        </foreach>
                    </when>
                    <otherwise>
                        AND ou.userid IN ('')
                    </otherwise>
                </choose>
            </if>
            <if test="userName != null and userName != ''">
                AND (ou.LOGINNAME LIKE ('%'|| #{userName} ||'%') or ou.chinesename LIKE ('%'|| #{userName} ||'%') or LOWER(ou.spellfirst) LIKE ('%'|| LOWER(#{userName}) ||'%'))
            </if>
            <if test="userNameWithSelected != null and userNameWithSelected != ''">
                AND (ou.LOGINNAME LIKE ('%'|| #{userNameWithSelected} ||'%') or ou.chinesename LIKE ('%'|| #{userNameWithSelected} ||'%') or LOWER(ou.spellfirst) LIKE ('%'|| LOWER(#{userNameWithSelected}) ||'%'))
            </if>
            <if test="unitType != null and unitType != ''">
                AND b2.UNITTYPES LIKE ('%'|| #{unitType} ||'%')
            </if>
        </where>
    </select>
 
    <insert id="save" parameterType="com.landtool.lanbase.modules.org.entity.OrgUserGroup">
        insert into ORG_USERGROUP
        (
            userid,
            groupid,
            createunitid,
            rcreatedate,
            rcreateuser
        )
        values
        (
            #{userId},
            #{groupId},
            #{createUnitId},
            #{rCreateDate},
            #{rCreateUser}
        )
    </insert>
     
    <update id="update" parameterType="com.landtool.lanbase.modules.org.entity.OrgUserGroup">
        update ORG_USERGROUP
        <set>
            <if test="groupId != null">groupid = #{groupId}, </if>
            <if test="createUnitId != null">createunitid = #{createUnitId}, </if>
            <if test="rCreateDate != null">rcreatedate = #{rCreateDate}, </if>
            <if test="rCreateUser != null">rcreateuser = #{rCreateUser}</if>
        </set>
        where userid = #{userId}
    </update>
    
    <delete id="delete">
        delete from ORG_USERGROUP where userid = #{value}
    </delete>
 
    <delete id="deleteBatchByGroupIdAndUserIds">
        DELETE FROM ORG_USERGROUP
        WHERE
        groupid = #{groupId}
        AND userid IN
        <foreach item="userId" collection="userIds" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
    
    <delete id="deleteBatch">
        delete from ORG_USERGROUP where userid in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>
 
    <select id="getUserIdsByGroupId" resultType="java.lang.Long">
        SELECT
            ou.userid
        FROM ORG_USERGROUP oug
        LEFT JOIN ORG_USER ou ON ou.userid = oug.userid
        WHERE oug.groupid = #{groupId}
    </select>
 
    <delete id="deleteBatchByGroupId">
        DELETE FROM ORG_USERGROUP WHERE groupid IN
        <foreach item="groupId" collection="array" open="(" separator="," close=")">
            #{groupId}
        </foreach>
    </delete>
</mapper>