UserMapper.xml 9.7 KB

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