UserMapper.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.jeeplus.modules.sys.mapper.UserMapper">
  4. <resultMap id="userResult" type="User">
  5. <id property="id" column="id" />
  6. <result property="company.id" column="company.id" />
  7. <result property="loginName" column="loginName" />
  8. <result property="password" column="password" />
  9. <result property="no" column="no" />
  10. <result property="name" column="name" />
  11. <result property="email" column="email" />
  12. <result property="phone" column="phone" />
  13. <result property="mobile" column="mobile" />
  14. <result property="photo" column="photo"/>
  15. <result property="company.name" column="company.name" />
  16. <result property="office.id" column="office.id" />
  17. <result property="office.name" column="office.name" />
  18. <result property="office.parentIds" column="office.parentIds" />
  19. <collection property="roleList" ofType="Role">
  20. <id property="id" column="role.id" />
  21. <result property="name" column="role.name"/>
  22. <result property="enname" column="role.enname"/>
  23. </collection>
  24. </resultMap>
  25. <sql id="userColumns">
  26. a.id,
  27. a.company_id AS "company.id",
  28. a.office_id AS "office.id",
  29. a.login_name AS "loginName",
  30. a.password,
  31. a.no,
  32. a.name,
  33. a.email,
  34. a.phone,
  35. a.mobile,
  36. a.login_ip,
  37. a.login_date,
  38. a.remarks,
  39. a.login_flag,
  40. a.photo,
  41. a.qrcode,
  42. a.sign,
  43. a.create_by AS "createBy.id",
  44. a.create_date,
  45. a.update_by AS "updateBy.id",
  46. a.update_date,
  47. a.del_flag,
  48. c.name AS "company.name",
  49. c.parent_id AS "company.parent.id",
  50. c.parent_ids AS "company.parentIds",
  51. ca.id AS "company.area.id",
  52. ca.name AS "company.area.name",
  53. ca.parent_id AS "company.area.parent.id",
  54. ca.parent_ids AS "company.area.parentIds",
  55. o.name AS "office.name",
  56. o.parent_id AS "office.parent.id",
  57. o.parent_ids AS "office.parentIds",
  58. oa.id AS "office.area.id",
  59. oa.name AS "office.area.name",
  60. oa.parent_id AS "office.area.parent.id",
  61. oa.parent_ids AS "office.area.parentIds",
  62. cu.id AS "company.primaryPerson.id",
  63. cu.name AS "company.primaryPerson.name",
  64. cu2.id AS "company.deputyPerson.id",
  65. cu2.name AS "company.deputyPerson.name",
  66. ou.id AS "office.primaryPerson.id",
  67. ou.name AS "office.primaryPerson.name",
  68. ou2.id AS "office.deputyPerson.id",
  69. ou2.name AS "office.deputyPerson.name"
  70. </sql>
  71. <sql id="userJoins">
  72. LEFT JOIN sys_office c ON c.id = a.company_id
  73. LEFT JOIN sys_area ca ON ca.id = c.area_id
  74. LEFT JOIN sys_office o ON o.id = a.office_id
  75. LEFT JOIN sys_area oa ON oa.id = o.area_id
  76. LEFT JOIN sys_user cu ON cu.id = c.primary_person
  77. LEFT JOIN sys_user cu2 ON cu2.id = c.deputy_person
  78. LEFT JOIN sys_user ou ON ou.id = o.primary_person
  79. LEFT JOIN sys_user ou2 ON ou2.id = o.deputy_person
  80. </sql>
  81. <!-- 根据编号获得用户 -->
  82. <select id="get" resultMap="userResult">
  83. SELECT
  84. <include refid="userColumns"/>,
  85. r.id AS "role.id",
  86. r.name AS "role.name",
  87. r.enname AS "role.enname"
  88. FROM sys_user a
  89. <include refid="userJoins"/>
  90. LEFT JOIN sys_user_role ur ON ur.user_id = a.id
  91. LEFT JOIN sys_role r ON r.id = ur.role_id
  92. WHERE a.id = #{id}
  93. </select>
  94. <!-- 根据登录名查询用户 -->
  95. <select id="getByLoginName" resultMap="userResult" parameterType="User">
  96. SELECT
  97. <include refid="userColumns"/>,
  98. r.id AS "role.id",
  99. r.name AS "role.name",
  100. r.enname AS "role.enname"
  101. FROM sys_user a
  102. <include refid="userJoins"/>
  103. LEFT JOIN sys_user_role ur ON ur.user_id = a.id
  104. LEFT JOIN sys_role r ON r.id = ur.role_id
  105. WHERE a.login_name = #{loginName} AND a.del_flag = #{DEL_FLAG_NORMAL}
  106. </select>
  107. <!-- 分页查询用户信息 -->
  108. <select id="findList" resultMap="userResult">
  109. SELECT
  110. <include refid="userColumns"/>
  111. <if test="role != null and role.id != null and role.id != ''">
  112. ,
  113. r.id AS "role.id",
  114. r.name AS "role.name",
  115. r.enname AS "role.enname"
  116. </if>
  117. FROM sys_user a
  118. <include refid="userJoins"/>
  119. <if test="role != null and role.id != null and role.id != ''">
  120. LEFT JOIN sys_user_role ur ON ur.user_id = a.id
  121. LEFT JOIN sys_role r ON r.id = ur.role_id
  122. </if>
  123. WHERE a.del_flag = #{DEL_FLAG_NORMAL}
  124. <if test="role != null and role.id != null and role.id != ''">
  125. AND ur.role_id = #{role.id}
  126. </if>
  127. <if test="company != null and company.id != null and company.id != ''">
  128. AND (c.id = #{company.id} OR c.parent_ids LIKE
  129. <if test="dbName == 'oracle'">'%,'||#{company.id}||',%')</if>
  130. <if test="dbName == 'mysql'">CONCAT('%,', #{company.id}, ',%'))</if>
  131. <if test="dbName == 'mssql'">'%'+#{company.id}+'%')</if>
  132. </if>
  133. <if test="office != null and office.id != null and office.id != ''">
  134. AND (o.id = #{office.id} OR o.parent_ids LIKE
  135. <if test="dbName == 'oracle'">'%,'||#{office.id}||',%')</if>
  136. <if test="dbName == 'mysql'">CONCAT('%,', #{office.id}, ',%'))</if>
  137. <if test="dbName == 'mssql'">'%'+#{office.id}+'%')</if>
  138. </if>
  139. <!-- 如果不是超级管理员,则不显示超级管理员用户 -->
  140. <if test="!currentUser.admin">
  141. AND a.id != '1'
  142. </if>
  143. <if test="loginName != null and loginName != ''">
  144. AND a.login_name like
  145. <if test="dbName == 'oracle'">'%'||#{loginName}||'%'</if>
  146. <if test="dbName == 'mysql'">CONCAT('%', #{loginName}, '%')</if>
  147. <if test="dbName == 'mssql'">'%'+#{loginName}+'%'</if>
  148. </if>
  149. <if test="name != null and name != ''">
  150. AND a.name like
  151. <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
  152. <if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
  153. <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
  154. </if>
  155. <!-- 数据范围过滤 -->
  156. ${dataScope}
  157. <choose>
  158. <when test="page !=null and page.orderBy != null and page.orderBy != ''">
  159. ORDER BY ${page.orderBy}
  160. </when>
  161. <otherwise>
  162. ORDER BY c.code, o.code, a.name
  163. </otherwise>
  164. </choose>
  165. </select>
  166. <!-- 根据部门查询用户信息 -->
  167. <select id="findListByOffice" resultMap="userResult">
  168. SELECT
  169. <include refid="userColumns"/>
  170. FROM sys_user a
  171. <include refid="userJoins"/>
  172. WHERE a.del_flag = #{DEL_FLAG_NORMAL}
  173. <if test="company != null and company.id != null and company.id != ''">
  174. AND c.id = #{company.id}
  175. </if>
  176. <if test="office != null and office.id != null and office.id != ''">
  177. AND o.id = #{office.id}
  178. </if>
  179. <if test="office == null">
  180. AND (o.id = '' or o.id is null)
  181. </if>
  182. <if test="loginName != null and loginName != ''">
  183. AND a.login_name like
  184. <if test="dbName == 'oracle'">'%'||#{loginName}||'%'</if>
  185. <if test="dbName == 'mysql'">CONCAT('%', #{loginName}, '%')</if>
  186. <if test="dbName == 'mssql'">'%'+#{loginName}+'%'</if>
  187. </if>
  188. <if test="name != null and name != ''">
  189. AND a.name like
  190. <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
  191. <if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
  192. <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
  193. </if>
  194. <!-- 数据范围过滤 -->
  195. ${dataScope}
  196. <!-- 排序 -->
  197. ORDER BY a.name
  198. </select>
  199. <!-- 根据OfficeId获取用户(树查询用户时用) -->
  200. <select id="findUserByOfficeId" resultMap="userResult" useCache="true">
  201. SELECT
  202. a.id, a.name, a.login_name
  203. FROM sys_user a
  204. WHERE a.del_flag = #{DEL_FLAG_NORMAL}
  205. AND a.office_id = #{office.id}
  206. ORDER BY a.name
  207. </select>
  208. <!-- 查询全部用户 -->
  209. <select id="findAllList" resultMap="userResult">
  210. SELECT
  211. <include refid="userColumns"/>
  212. FROM sys_user a
  213. <include refid="userJoins"/>
  214. WHERE a.del_flag = #{DEL_FLAG_NORMAL}
  215. ORDER BY c.code, o.code, a.name
  216. </select>
  217. <!-- 查询全部用户数目 -->
  218. <select id="findAllCount" resultType="long">
  219. SELECT
  220. COUNT(1)
  221. FROM sys_user a
  222. WHERE a.del_flag = #{DEL_FLAG_NORMAL}
  223. </select>
  224. <!-- 插入用户 -->
  225. <insert id="insert">
  226. INSERT INTO sys_user(
  227. id,
  228. company_id,
  229. office_id,
  230. login_name,
  231. password,
  232. no,
  233. name,
  234. email,
  235. phone,
  236. mobile,
  237. create_by,
  238. create_date,
  239. update_by,
  240. update_date,
  241. remarks,
  242. login_flag,
  243. photo,
  244. qrcode,
  245. del_flag
  246. ) VALUES (
  247. #{id},
  248. #{company.id},
  249. #{office.id},
  250. #{loginName},
  251. #{password},
  252. #{no},
  253. #{name},
  254. #{email},
  255. #{phone},
  256. #{mobile},
  257. #{createBy.id},
  258. #{createDate},
  259. #{updateBy.id},
  260. #{updateDate},
  261. #{remarks},
  262. #{loginFlag},
  263. #{photo},
  264. #{qrCode},
  265. #{delFlag}
  266. )
  267. </insert>
  268. <!-- 更新用户 -->
  269. <update id="update">
  270. UPDATE sys_user SET
  271. company_id = #{company.id},
  272. office_id = #{office.id},
  273. login_name = #{loginName},
  274. password = #{password},
  275. no = #{no},
  276. name = #{name},
  277. email = #{email},
  278. phone = #{phone},
  279. mobile = #{mobile},
  280. update_by = #{updateBy.id},
  281. update_date = #{updateDate},
  282. remarks = #{remarks},
  283. login_flag = #{loginFlag},
  284. photo = #{photo},
  285. qrcode = #{qrCode}
  286. WHERE id = #{id}
  287. </update>
  288. <!-- 删除用户和角色关联表数据 -->
  289. <delete id="deleteUserRole">
  290. DELETE FROM sys_user_role WHERE user_id = #{id}
  291. </delete>
  292. <!-- 插入用户和角色关联表数据 -->
  293. <insert id="insertUserRole">
  294. INSERT INTO sys_user_role(user_id, role_id)
  295. <foreach collection="roleList" item="role" separator=" union all ">
  296. SELECT #{id}, #{role.id} ${dual}
  297. </foreach>
  298. </insert>
  299. <!-- 更新用户信息 -->
  300. <update id="updateUserInfo">
  301. UPDATE sys_user SET
  302. name = #{name},
  303. email = #{email},
  304. phone = #{phone},
  305. mobile = #{mobile},
  306. update_by = #{updateBy.id},
  307. update_date = #{updateDate},
  308. remarks = #{remarks},
  309. photo = #{photo},
  310. qrcode = #{qrCode},
  311. sign = #{sign}
  312. WHERE id = #{id}
  313. </update>
  314. <!-- 更新用户密码 -->
  315. <update id="updatePasswordById">
  316. UPDATE sys_user SET
  317. password = #{password}
  318. WHERE id = #{id}
  319. </update>
  320. <!-- 更新登录信息,如登录IP、登录时间 -->
  321. <update id="updateLoginInfo">
  322. UPDATE sys_user SET
  323. login_ip = #{loginIp},
  324. login_Date = #{loginDate}
  325. WHERE id = #{id}
  326. </update>
  327. <!-- 物理删除用户 -->
  328. <update id="delete">
  329. DELETE FROM sys_user
  330. WHERE id = #{id}
  331. </update>
  332. <!-- 逻辑删除用户 -->
  333. <update id="deleteByLogic">
  334. UPDATE sys_user SET
  335. del_flag = #{DEL_FLAG_DELETE}
  336. WHERE id = #{id}
  337. </update>
  338. <!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
  339. <select id="findUniqueByProperty" resultType="User" statementType="STATEMENT">
  340. select * from sys_user where ${propertyName} = '${value}'
  341. </select>
  342. <!-- 添加好友 -->
  343. <insert id="insertFriend">
  344. INSERT INTO sys_user_friend(
  345. id,
  346. userId,
  347. friendId
  348. ) VALUES (
  349. #{id},
  350. #{userId},
  351. #{friendId}
  352. )
  353. </insert>
  354. <!-- 根据用户id和好友id获取唯一记录 -->
  355. <select id="findFriend" resultType="User">
  356. SELECT
  357. *
  358. FROM sys_user a
  359. LEFT JOIN sys_user_friend p ON p.userId = a.id
  360. WHERE p.userId = #{userId} and p.friendId = #{friendId}
  361. </select>
  362. <!-- 删除好友 -->
  363. <select id="deleteFriend">
  364. DELETE FROM sys_user_friend WHERE userId = #{userId} and friendId = #{friendId}
  365. </select>
  366. <!-- 查询我的好友列表 -->
  367. <select id="findFriends" resultMap="userResult">
  368. SELECT
  369. <include refid="userColumns"/>
  370. FROM sys_user a
  371. <include refid="userJoins"/>
  372. LEFT JOIN sys_user_friend p ON p.friendId = a.id
  373. WHERE p.userId = #{id}
  374. </select>
  375. <!-- 根据条件检索用户,添加到好友列表 -->
  376. <select id="searchUsers" resultMap="userResult">
  377. SELECT
  378. <include refid="userColumns"/>
  379. FROM sys_user a
  380. <include refid="userJoins"/>
  381. <if test="name != null and name != ''">
  382. WHERE a.name like
  383. <if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
  384. <if test="dbName == 'mysql'">CONCAT('%', #{name}, '%')</if>
  385. <if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
  386. </if>
  387. </select>
  388. </mapper>