SysDeptMapper.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.aidex.system.mapper.SysDeptMapper">
  6. <sql id="deptColumns">
  7. t.id as "id",
  8. t.dept_code AS "deptCode",
  9. t.dept_name AS "deptName",
  10. t.dept_full_name AS "deptFullName",
  11. t.dept_type AS "deptType",
  12. t.address AS "address",
  13. t.zip_code AS "zipCode",
  14. t.dept_pinyin AS "deptPinyin",
  15. t.leader AS "leader",
  16. t.phone AS "phone",
  17. t.email AS "email",
  18. t.parent_id AS "parentId",
  19. t.parent_ids AS "parentIds",
  20. t.tree_sort AS "treeSort",
  21. t.tree_sorts AS "treeSorts",
  22. t.tree_level AS "treeLevel",
  23. t.tree_leaf AS "treeLeaf",
  24. t.status AS "status",
  25. t.create_by AS "createBy",
  26. t.create_dept AS "createDept",
  27. t.create_time AS "createTime",
  28. t.update_by AS "updateBy",
  29. t.update_time AS "updateTime",
  30. t.update_ip AS "updateIp",
  31. t.remark AS "remark",
  32. t.version AS "version",
  33. t.del_flag AS "delFlag"
  34. </sql>
  35. <sql id="deptJoins">
  36. INNER JOIN sys_user su ON su.id = a.user_id
  37. </sql>
  38. <sql id="commonWhere">
  39. <if test="parentId != null and parentId != ''">
  40. AND t.parent_id = #{parentId}
  41. </if>
  42. <if test="parentIds != null and parentIds != ''">
  43. and t.parent_ids like concat(#{parentIds},'%') and t.parent_ids &lt;&gt; #{parentIds}
  44. </if>
  45. <if test="deptName != null and deptName != ''">
  46. AND t.dept_name like concat('%', #{deptName}, '%')
  47. </if>
  48. <if test="deptCode != null and deptCode != ''">
  49. AND t.dept_code like concat('%', #{deptCode}, '%')
  50. </if>
  51. <if test="status != null and status != ''">
  52. AND t.status = #{status}
  53. </if>
  54. </sql>
  55. <sql id="commonSorts">
  56. order by t.tree_sorts
  57. </sql>
  58. <select id="get" resultType="SysDept">
  59. SELECT
  60. <include refid="deptColumns"/>
  61. FROM sys_dept t
  62. where t.id = #{id}
  63. </select>
  64. <select id="findList" resultType="SysDept">
  65. SELECT
  66. <include refid="deptColumns"/>
  67. FROM sys_dept t
  68. where t.del_flag = #{DEL_FLAG_NORMAL}
  69. <include refid="commonWhere"/>
  70. <include refid="commonSorts"/>
  71. </select>
  72. <select id="findListWithUnique" resultType="SysDept">
  73. SELECT
  74. t.id
  75. FROM sys_dept t
  76. WHERE t.del_flag = #{DEL_FLAG_NORMAL}
  77. <if test="deptName != null and deptName != ''">
  78. AND t.dept_name = #{deptName}
  79. </if>
  80. <if test="deptCode != null and deptCode != ''">
  81. AND t.dept_code = #{deptCode}
  82. </if>
  83. <if test="parentId != null and parentId != ''">
  84. AND t.parent_id = #{parentId}
  85. </if>
  86. <if test="notEqualId != null and notEqualId != ''">
  87. AND t.id != #{notEqualId}
  88. </if>
  89. </select>
  90. <select id="findCount" resultType="integer">
  91. SELECT
  92. count(1)
  93. FROM sys_dept t
  94. where t.del_flag = #{DEL_FLAG_NORMAL}
  95. <include refid="commonWhere"/>
  96. </select>
  97. <select id="findMaxSort" resultType="integer">
  98. SELECT max(t.tree_sort) + 120
  99. FROM sys_dept t
  100. WHERE t.del_flag = #{DEL_FLAG_NORMAL}
  101. <if test="parentId != null and parentId != ''">
  102. and t.parent_id = #{parentId}
  103. </if>
  104. </select>
  105. <insert id="insert" >
  106. insert into sys_dept(
  107. id,
  108. dept_code,
  109. dept_name,
  110. dept_full_name,
  111. dept_type,
  112. address,
  113. zip_code,
  114. dept_pinyin,
  115. leader,
  116. phone,
  117. email,
  118. parent_id,
  119. parent_ids,
  120. tree_sort,
  121. tree_sorts,
  122. tree_level,
  123. tree_leaf,
  124. status,
  125. create_by,
  126. create_dept,
  127. create_time,
  128. update_by,
  129. update_time,
  130. update_ip,
  131. remark,
  132. del_flag,
  133. version
  134. )values(
  135. #{id},
  136. #{deptCode},
  137. #{deptName},
  138. #{deptFullName},
  139. #{deptType},
  140. #{address},
  141. #{zipCode},
  142. #{deptPinyin},
  143. #{leader},
  144. #{phone},
  145. #{email},
  146. #{parentId},
  147. #{parentIds},
  148. #{treeSort},
  149. #{treeSorts},
  150. #{treeLevel},
  151. #{treeLeaf},
  152. #{status},
  153. #{createBy},
  154. #{createDept},
  155. #{createTime},
  156. #{updateBy},
  157. #{updateTime},
  158. #{updateIp},
  159. #{remark},
  160. #{DEL_FLAG_NORMAL},
  161. 1
  162. )
  163. </insert>
  164. <update id="update">
  165. update sys_dept
  166. <set>
  167. dept_code = #{deptCode},
  168. dept_name = #{deptName},
  169. dept_full_name = #{deptFullName},
  170. dept_type = #{deptType},
  171. address = #{address},
  172. zip_code = #{zipCode},
  173. dept_pinyin = #{deptPinyin},
  174. leader = #{leader},
  175. phone = #{phone},
  176. email = #{email},
  177. parent_id = #{parentId},
  178. parent_ids = #{parentIds},
  179. leader = #{leader},
  180. tree_sort = #{treeSort},
  181. tree_sorts = #{treeSorts},
  182. tree_level = #{treeLevel},
  183. tree_leaf = #{treeLeaf},
  184. status = #{status},
  185. update_by = #{updateBy},
  186. update_time = #{updateTime},
  187. update_ip = #{updateIp},
  188. remark = #{remark},
  189. version = version + 1
  190. </set>
  191. where id = #{id} and version = #{version}
  192. </update>
  193. <update id="updateBatch" parameterType="java.util.List">
  194. <foreach collection="list" item="item" index="index" open="" close="" separator=";">
  195. update sys_dept
  196. <set>
  197. dept_code = #{item.deptCode},
  198. dept_name = #{item.deptName},
  199. dept_full_name = #{item.deptFullName},
  200. dept_type = #{item.deptType},
  201. address = #{item.address},
  202. zip_code = #{item.zipCode},
  203. dept_pinyin = #{item.deptPinyin},
  204. leader = #{item.leader},
  205. phone = #{item.phone},
  206. email = #{item.email},
  207. parent_id = #{item.parentId},
  208. parent_ids = #{item.parentIds},
  209. leader = #{item.leader},
  210. tree_sort = #{item.treeSort},
  211. tree_sorts = #{item.treeSorts},
  212. tree_level = #{item.treeLevel},
  213. tree_leaf = #{item.treeLeaf},
  214. status = #{item.status},
  215. update_by = #{item.updateBy},
  216. update_time = #{item.updateTime},
  217. update_ip = #{item.updateIp},
  218. remark = #{item.remark},
  219. version = version + 1
  220. </set>
  221. where id = #{item.id} and version = #{item.version}
  222. </foreach>
  223. </update>
  224. <delete id="delete">
  225. UPDATE sys_dept SET
  226. del_flag = #{DEL_FLAG_DELETE}
  227. WHERE id = #{id}
  228. </delete>
  229. <delete id="deleteBatch" parameterType="java.util.List">
  230. <foreach collection="list" item="item" index="index" open="" close="" separator=";">
  231. UPDATE sys_dept SET
  232. del_flag = #{DEL_FLAG_DELETE}
  233. WHERE id = #{item.id}
  234. </foreach>
  235. </delete>
  236. <select id="selectDeptListByRoleId" resultType="string">
  237. select d.id
  238. from sys_dept d
  239. left join sys_role_dept rd on d.id = rd.dept_id
  240. where rd.role_id = #{roleId}
  241. <if test="deptCheckStrictly">
  242. and d.id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.id = rd.dept_id and rd.role_id = #{roleId})
  243. </if>
  244. order by d.parent_id, d.tree_sorts
  245. </select>
  246. <select id="checkDeptExistUser" resultType="int">
  247. select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
  248. </select>
  249. <select id="hasChildByDeptId" resultType="int">
  250. select count(1) from sys_dept
  251. where del_flag = '0' and parent_id = #{PARENT}
  252. </select>
  253. <select id="selectChildrenDeptById" resultType="SysDept">
  254. select * from sys_dept t
  255. where t.parent_ids like CONCAT(#{parentIds},'%')
  256. </select>
  257. <select id="selectNormalChildByParentIds" resultType="int">
  258. select count(id) from sys_dept t where t.status = 0 and t.del_flag = '0'
  259. and t.parent_ids like concat(#{parentIds},'%') and t.parent_ids &lt;&gt; #{parentIds}
  260. </select>
  261. <select id="checkDeptNameUnique" resultType="SysDept">
  262. SELECT
  263. <include refid="deptColumns"/>
  264. FROM sys_dept
  265. where dept_name=#{deptName} and del_flag = '0' and parent_id = #{parentId} limit 1
  266. </select>
  267. <update id="updateDeptChildren" parameterType="java.util.List">
  268. update sys_dept set parent_ids =
  269. <foreach collection="depts" item="item" index="index"
  270. separator=" " open="case id" close="end">
  271. when #{item.id} then #{item.parentIds}
  272. </foreach>
  273. , tree_sorts =
  274. <foreach collection="depts" item="item" index="index"
  275. separator=" " open="case id" close="end">
  276. when #{item.id} then #{item.treeSorts}
  277. </foreach>
  278. , tree_level =
  279. <foreach collection="depts" item="item" index="index"
  280. separator=" " open="case id" close="end">
  281. when #{item.id} then #{item.treeLevel}
  282. </foreach>
  283. where id in
  284. <foreach collection="depts" item="item" index="index"
  285. separator="," open="(" close=")">
  286. #{item.id}
  287. </foreach>
  288. </update>
  289. <update id="updateDeptStatus">
  290. update sys_dept
  291. <set>
  292. <if test="status != null and status != ''">status = #{status},</if>
  293. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  294. update_time = sysdate()
  295. </set>
  296. where dept_id in (${ancestors})
  297. </update>
  298. <!--根据ids获取所有的节点-->
  299. <select id="searchSysDeptTreeByIds" parameterType="java.util.List" resultType="SysDept">
  300. select
  301. <include refid="deptColumns"/>
  302. from sys_dept t
  303. <where>
  304. <foreach collection="ids" item="idsItem">
  305. or t.id in
  306. <foreach collection="idsItem" item="id" open="(" separator="," close=")">
  307. #{id}
  308. </foreach>
  309. </foreach>
  310. </where>
  311. order by t.tree_sorts asc
  312. </select>
  313. <!-- 根据条件查询节点 -->
  314. <select id="searchSysDeptTree" parameterType="java.util.Map" resultType="SysDept">
  315. select
  316. <include refid="deptColumns"/>
  317. from sys_dept t
  318. <where>
  319. t.del_flag = #{DEL_FLAG_NORMAL}
  320. <if test="deptName != null and deptName != ''">
  321. and t.dept_name like concat(concat('%',#{deptName}),'%')
  322. </if>
  323. <if test="deptCode != null and deptCode != ''">
  324. and t.dept_code like concat(concat('%',#{deptCode}),'%')
  325. </if>
  326. <if test="searchText != null and searchText != ''">
  327. and (
  328. t.dept_name like concat(concat('%',#{searchText}),'%') or
  329. t.dept_code like concat(concat('%',#{searchText}),'%') or
  330. t.dept_pinyin like concat(concat('%',#{searchText}),'%')
  331. )
  332. </if>
  333. </where>
  334. order by t.tree_sorts asc
  335. </select>
  336. <sql id="seletUserDeptColumns">
  337. SELECT
  338. t.id,
  339. t.parent_id as 'parentId',
  340. t.parent_ids as 'parentIds',
  341. t.dept_name as 'deptName',
  342. t.dept_name as 'subtitle',
  343. t.dept_type as 'deptType',
  344. 'n' AS treeLeaf,
  345. 0 AS groupSort,
  346. t.tree_sorts as 'treeSorts',
  347. t.dept_pinyin as 'deptPinyin'
  348. FROM
  349. sys_dept t
  350. </sql>
  351. <sql id="seletUserUserColumns">
  352. SELECT
  353. t.id,
  354. t.dept_id as 'parentId',
  355. concat(d.parent_ids,'/',t.id) as 'parentIds',
  356. t.NAME as 'deptName',
  357. d.dept_name as 'subtitle',
  358. 'user' as 'deptType',
  359. 'y' AS treeLeaf,
  360. 1 AS groupSort,
  361. t.sort as 'treeSorts',
  362. t.user_pinyin as 'deptPinyin'
  363. FROM
  364. sys_user t
  365. left join sys_dept d on t.dept_id = d.id
  366. </sql>
  367. <!--根据ids获取所有的节点-->
  368. <select id="findDeptAndUserChildListByParentId" resultType="SysDept">
  369. select
  370. *
  371. from (
  372. <include refid="seletUserDeptColumns"/>
  373. WHERE t.del_flag = #{DEL_FLAG_NORMAL}
  374. <if test="parentId != null and parentId != ''">
  375. and t.parent_id = #{parentId}
  376. </if>
  377. UNION ALL
  378. <include refid="seletUserUserColumns"/>
  379. WHERE t.del_flag = #{DEL_FLAG_NORMAL}
  380. <if test="parentId != null and parentId != ''">
  381. and t.dept_id = #{parentId}
  382. </if>
  383. ) t
  384. order by groupSort,treeSorts
  385. </select>
  386. <select id="searchSysDeptUserTree" resultType="SysDept">
  387. <include refid="seletUserUserColumns"/>
  388. WHERE t.del_flag = #{DEL_FLAG_NORMAL}
  389. <if test="deptName != null and deptName != ''">
  390. and t.name like concat(concat('%',#{deptName}),'%')
  391. </if>
  392. <if test="searchText != null and searchText != ''">
  393. and (
  394. t.name like concat(concat('%',#{searchText}),'%') or
  395. t.user_name like concat(concat('%',#{searchText}),'%') or
  396. t.user_pinyin like concat(concat('%',#{searchText}),'%')
  397. )
  398. </if>
  399. order by treeSorts
  400. </select>
  401. <!--根据ids获取所有的节点-->
  402. <select id="searchSysDeptUserTreeByIds" parameterType="java.util.List" resultType="SysDept">
  403. select
  404. *
  405. from (
  406. <include refid="seletUserDeptColumns"/>
  407. WHERE t.del_flag = 0
  408. UNION ALL
  409. <include refid="seletUserUserColumns"/>
  410. WHERE t.del_flag = 0
  411. ) t
  412. <where>
  413. <foreach collection="ids" item="idsItem">
  414. or t.id in
  415. <foreach collection="idsItem" item="id" open="(" separator="," close=")">
  416. #{id}
  417. </foreach>
  418. </foreach>
  419. </where>
  420. order by groupSort,treeSorts
  421. </select>
  422. <select id="getDeptInfoByIds" parameterType="java.util.List" resultType="java.util.HashMap">
  423. SELECT
  424. t.id,
  425. t.parent_ids as 'parentIds',
  426. t.dept_name as 'name',
  427. 'apartment' as 'icon'
  428. FROM
  429. sys_dept t
  430. <where>
  431. t.del_flag = 0
  432. and ( 1 != 1
  433. <foreach collection="ids" item="idsItem">
  434. or t.id in
  435. <foreach collection="idsItem" item="id" open="(" separator="," close=")">
  436. #{id}
  437. </foreach>
  438. </foreach>
  439. )
  440. </where>
  441. </select>
  442. <select id="searchChildrenTypeByParentId" resultType="SysDept">
  443. SELECT
  444. 1
  445. FROM
  446. sys_dept
  447. WHERE
  448. parent_ids like concat(concat('%',#{id}),'/%')
  449. AND del_flag = #{DEL_FLAG_NORMAL}
  450. AND dept_type = #{deptType}
  451. </select>
  452. <select id="selectDeptByDeptCode" parameterType="java.util.Map" resultType="SysDept">
  453. SELECT
  454. <include refid="deptColumns"/>
  455. FROM sys_dept t
  456. where t.dept_code = #{deptCode} and t.del_flag = #{DEL_FLAG_NORMAL}
  457. </select>
  458. </mapper>