SysDeptMapper.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.ruoyi.system.mapper.SysDeptMapper">
  6. <resultMap type="SysDept" id="SysDeptResult">
  7. <id property="deptId" column="dept_id" />
  8. <result property="parentId" column="parent_id" />
  9. <result property="ancestors" column="ancestors" />
  10. <result property="deptName" column="dept_name" />
  11. <result property="orderNum" column="order_num" />
  12. <result property="leader" column="leader" />
  13. <result property="phone" column="phone" />
  14. <result property="depType" column="dep_type" />
  15. <result property="orgCode" column="org_code" />
  16. <result property="email" column="email" />
  17. <result property="status" column="status" />
  18. <result property="delFlag" column="del_flag" />
  19. <result property="parentName" column="parent_name" />
  20. <result property="createBy" column="create_by" />
  21. <result property="createTime" column="create_time" />
  22. <result property="updateBy" column="update_by" />
  23. <result property="updateTime" column="update_time" />
  24. </resultMap>
  25. <sql id="selectDeptVo">
  26. select d.dept_id ,d.org_code, d.dep_type,d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time
  27. from sys_dept d
  28. </sql>
  29. <select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
  30. <include refid="selectDeptVo"/>
  31. where d.del_flag = '0'
  32. <if test="deptId != null and deptId != 0">
  33. AND dept_id = #{deptId}
  34. </if>
  35. <if test="parentId != null and parentId != 0">
  36. AND parent_id = #{parentId}
  37. </if>
  38. <if test="deptName != null and deptName != ''">
  39. AND dept_name like concat('%', #{deptName}, '%')
  40. </if>
  41. <if test="status != null and status != ''">
  42. AND status = #{status}
  43. </if>
  44. <!-- 数据范围过滤 -->
  45. ${params.dataScope}
  46. order by d.parent_id, d.order_num
  47. </select>
  48. <select id="selectDeptListByParents" resultMap="SysDeptResult">
  49. <include refid="selectDeptVo"/>
  50. where d.del_flag = '0' AND d.status = '0'
  51. <if test="deptIds != null and deptIds.size>0">
  52. AND parent_id in
  53. <foreach collection="deptIds" item="item" index="index"
  54. separator="," open="(" close=")">
  55. #{item.parentId}
  56. </foreach>
  57. </if>
  58. <if test="deptType != null and deptType != '' ">
  59. AND dep_type = #{deptType}
  60. </if>
  61. order by d.parent_id, d.order_num
  62. </select>
  63. <select id="selectDeptListByRoleId" resultType="Long">
  64. select d.dept_id
  65. from sys_dept d
  66. left join sys_role_dept rd on d.dept_id = rd.dept_id
  67. where rd.role_id = #{roleId}
  68. <if test="deptCheckStrictly">
  69. and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
  70. </if>
  71. order by d.parent_id, d.order_num
  72. </select>
  73. <select id="selectDeptListByRoleIdAndDeptType" >
  74. select d.dept_id
  75. from sys_dept d
  76. left join sys_role_dept rd on d.dept_id = rd.dept_id
  77. where rd.role_id = #{roleId}
  78. <if test="deptType != null ">
  79. and d.dep_type=#{deptType}
  80. </if>
  81. <if test="deptCheckStrictly">
  82. and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
  83. </if>
  84. order by d.parent_id, d.order_num
  85. </select>
  86. <select id="selectDeptListByUserIdAndDeptType" >
  87. select d.dept_id
  88. from sys_dept d
  89. inner join sys_user_dep rd on d.dept_id = rd.dep_id
  90. where rd.user_id = #{userId}
  91. <if test="deptType != null ">
  92. and d.dep_type=#{deptType}
  93. </if>
  94. order by d.parent_id, d.order_num
  95. </select>
  96. <select id="selectDeptListByUserIdChildAndDeptType" >
  97. select concat(d.ancestors,',',d.dept_id) as ancestors
  98. from sys_dept d
  99. inner join sys_user_dep rd on d.dept_id = rd.dep_id
  100. where rd.user_id = #{userId}
  101. <if test="deptType != null ">
  102. and d.dep_type=#{deptType}
  103. </if>
  104. order by d.parent_id, d.order_num
  105. </select>
  106. <select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
  107. <include refid="selectDeptVo"/>
  108. where dept_id = #{deptId}
  109. </select>
  110. <select id="checkDeptExistUser" parameterType="Long" resultType="int">
  111. select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
  112. </select>
  113. <select id="hasChildByDeptId" parameterType="Long" resultType="int">
  114. select count(1) from sys_dept
  115. where del_flag = '0' and parent_id = #{deptId} limit 1
  116. </select>
  117. <select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
  118. select * from sys_dept where find_in_set(#{deptId}, ancestors)
  119. </select>
  120. <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
  121. select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
  122. </select>
  123. <select id="checkDeptNameUnique" resultMap="SysDeptResult">
  124. <include refid="selectDeptVo"/>
  125. where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1
  126. </select>
  127. <insert id="insertDept" parameterType="SysDept">
  128. insert into sys_dept(
  129. <if test="deptId != null and deptId != 0">dept_id,</if>
  130. <if test="parentId != null and parentId != 0">parent_id,</if>
  131. <if test="deptName != null and deptName != ''">dept_name,</if>
  132. <if test="ancestors != null and ancestors != ''">ancestors,</if>
  133. <if test="orderNum != null">order_num,</if>
  134. <if test="depType != null">dep_type,</if>
  135. <if test="orgCode != null">org_code,</if>
  136. <if test="leader != null and leader != ''">leader,</if>
  137. <if test="phone != null and phone != ''">phone,</if>
  138. <if test="email != null and email != ''">email,</if>
  139. <if test="status != null">status,</if>
  140. <if test="createBy != null and createBy != ''">create_by,</if>
  141. create_time
  142. )values(
  143. <if test="deptId != null and deptId != 0">#{deptId},</if>
  144. <if test="parentId != null and parentId != 0">#{parentId},</if>
  145. <if test="deptName != null and deptName != ''">#{deptName},</if>
  146. <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
  147. <if test="orderNum != null">#{orderNum},</if>
  148. <if test="depType != null">#{depType},</if>
  149. <if test="orgCode != null">#{orgCode},</if>
  150. <if test="leader != null and leader != ''">#{leader},</if>
  151. <if test="phone != null and phone != ''">#{phone},</if>
  152. <if test="email != null and email != ''">#{email},</if>
  153. <if test="status != null">#{status},</if>
  154. <if test="createBy != null and createBy != ''">#{createBy},</if>
  155. sysdate()
  156. )
  157. </insert>
  158. <update id="updateDept" parameterType="SysDept">
  159. update sys_dept
  160. <set>
  161. <if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
  162. <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
  163. <if test="depType != null and depType != ''">dep_type = #{depType},</if>
  164. <if test="orgCode != null and orgCode != ''">org_code = #{orgCode},</if>
  165. <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
  166. <if test="orderNum != null">order_num = #{orderNum},</if>
  167. <if test="leader != null">leader = #{leader},</if>
  168. <if test="phone != null">phone = #{phone},</if>
  169. <if test="email != null">email = #{email},</if>
  170. <if test="status != null and status != ''">status = #{status},</if>
  171. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  172. update_time = sysdate()
  173. </set>
  174. where dept_id = #{deptId}
  175. </update>
  176. <update id="updateDeptChildren" parameterType="java.util.List">
  177. update sys_dept set ancestors =
  178. <foreach collection="depts" item="item" index="index"
  179. separator=" " open="case dept_id" close="end">
  180. when #{item.deptId} then #{item.ancestors}
  181. </foreach>
  182. where dept_id in
  183. <foreach collection="depts" item="item" index="index"
  184. separator="," open="(" close=")">
  185. #{item.deptId}
  186. </foreach>
  187. </update>
  188. <update id="updateDeptStatusNormal" parameterType="Long">
  189. update sys_dept set status = '0' where dept_id in
  190. <foreach collection="array" item="deptId" open="(" separator="," close=")">
  191. #{deptId}
  192. </foreach>
  193. </update>
  194. <delete id="deleteDeptById" parameterType="Long">
  195. update sys_dept set del_flag = '2' where dept_id = #{deptId}
  196. </delete>
  197. <select id="selectMyDeptIdsListByUserId" resultMap="SysDeptResult">
  198. select d1.* from sys_dept d1 inner join sys_user_dep d2 on d1.dept_id=d2.dep_id where d2.user_id=#{userId}
  199. </select>
  200. <select id="selectDeptIdsListByUserId" parameterType="Long">
  201. select d.dept_id
  202. from sys_dept d
  203. left join sys_role_dept rd on d.dept_id = rd.dept_id
  204. where rd.role_id in (select sys_user_role.role_id from sys_user_role,sys_role where sys_user_role.role_id=sys_role.role_id and sys_role.data_scope=2 and sys_user_role.user_id=#{userId})
  205. and d.dept_id not in (
  206. select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id in (select sys_user_role.role_id from sys_user_role,sys_role where sys_user_role.role_id=sys_role.role_id and sys_role.data_scope=2 and sys_user_role.user_id=#{userId})
  207. )
  208. union
  209. select t5.parent_id from (
  210. select t1.parent_id,count(t1.dept_id) ct1 from (
  211. select d.parent_id,d.dept_id
  212. from sys_dept d
  213. left join sys_role_dept rd on d.dept_id = rd.dept_id
  214. where rd.role_id in (select sys_user_role.role_id from sys_user_role,sys_role where sys_user_role.role_id=sys_role.role_id and sys_role.data_scope=2 and sys_user_role.user_id=#{userId})
  215. and d.dept_id not in (
  216. select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id in (select sys_user_role.role_id from sys_user_role,sys_role where sys_user_role.role_id=sys_role.role_id and sys_role.data_scope=2 and sys_user_role.user_id=#{userId})
  217. )
  218. ) t1 group by t1.parent_id
  219. ) t5
  220. inner join
  221. (
  222. select t2.parent_id,count(d3.dept_id) ct2 from (
  223. select d.parent_id,d.dept_id
  224. from sys_dept d
  225. left join sys_role_dept rd on d.dept_id = rd.dept_id
  226. where rd.role_id in (select sys_user_role.role_id from sys_user_role,sys_role where sys_user_role.role_id=sys_role.role_id and sys_role.data_scope=2 and sys_user_role.user_id=#{userId})
  227. and d.dept_id not in (
  228. select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id = rd.dept_id and rd.role_id in (select sys_user_role.role_id from sys_user_role,sys_role where sys_user_role.role_id=sys_role.role_id and sys_role.data_scope=2 and sys_user_role.user_id=#{userId}))
  229. ) t2 inner join sys_dept d3 on d3.parent_id=t2.parent_id
  230. group by t2.parent_id
  231. ) t6 on t5.parent_id=t6.parent_id and t5.ct1=t6.ct2
  232. </select>
  233. </mapper>