SysDeptMapper.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.zksy.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="email" column="email"/>
  15. <result property="status" column="status"/>
  16. <result property="delFlag" column="del_flag"/>
  17. <result property="createBy" column="create_by"/>
  18. <result property="createTime" column="create_time"/>
  19. <result property="updateBy" column="update_by"/>
  20. <result property="updateTime" column="update_time"/>
  21. <result property="remark" column="remark"/>
  22. </resultMap>
  23. <sql id="selectDeptVo">
  24. select d.dept_id, 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,d.remark
  25. from sys_dept d
  26. </sql>
  27. <select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
  28. <include refid="selectDeptVo"/>
  29. where d.del_flag = '0'
  30. <if test="deptId != null and deptId != 0">
  31. AND dept_id = #{deptId}
  32. </if>
  33. <if test="parentId != null and parentId != 0">
  34. AND parent_id = #{parentId}
  35. </if>
  36. <if test="deptName != null and deptName != ''">
  37. AND dept_name like concat('%', #{deptName}, '%')
  38. </if>
  39. <if test="status != null and status != ''">
  40. AND status = #{status}
  41. </if>
  42. <!-- 数据范围过滤 -->
  43. ${params.dataScope}
  44. order by d.parent_id, d.order_num
  45. </select>
  46. <select id="selectDeptListByRoleId" resultType="Long">
  47. select d.dept_id
  48. from sys_dept d
  49. left join sys_role_dept rd on d.dept_id = rd.dept_id
  50. where rd.role_id = #{roleId}
  51. <if test="deptCheckStrictly">
  52. and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on d.dept_id =
  53. rd.dept_id and rd.role_id = #{roleId})
  54. </if>
  55. order by d.parent_id, d.order_num
  56. </select>
  57. <select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
  58. <include refid="selectDeptVo"/>
  59. where dept_id = #{deptId}
  60. </select>
  61. <select id="checkDeptExistUser" parameterType="Long" resultType="int">
  62. select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
  63. </select>
  64. <select id="hasChildByDeptId" parameterType="Long" resultType="int">
  65. select count(1) from sys_dept
  66. where del_flag = '0' and parent_id = #{deptId} limit 1
  67. </select>
  68. <select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
  69. select * from sys_dept where find_in_set(#{deptId}, ancestors)
  70. </select>
  71. <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
  72. select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
  73. </select>
  74. <select id="checkDeptNameUnique" resultMap="SysDeptResult">
  75. <include refid="selectDeptVo"/>
  76. where dept_name=#{deptName} and parent_id = #{parentId} limit 1
  77. </select>
  78. <insert id="insertDept" parameterType="SysDept">
  79. insert into sys_dept(
  80. <if test="deptId != null and deptId != 0">dept_id,</if>
  81. <if test="parentId != null and parentId != 0">parent_id,</if>
  82. <if test="deptName != null and deptName != ''">dept_name,</if>
  83. <if test="ancestors != null and ancestors != ''">ancestors,</if>
  84. <if test="orderNum != null and orderNum != ''">order_num,</if>
  85. <if test="leader != null and leader != ''">leader,</if>
  86. <if test="phone != null and phone != ''">phone,</if>
  87. <if test="email != null and email != ''">email,</if>
  88. <if test="status != null">status,</if>
  89. <!--<if test="orgType != null and orgType != ''">org_type,</if>-->
  90. <if test="createBy != null and createBy != ''">create_by,</if>
  91. create_time
  92. )values(
  93. <if test="deptId != null and deptId != 0">#{deptId},</if>
  94. <if test="parentId != null and parentId != 0">#{parentId},</if>
  95. <if test="deptName != null and deptName != ''">#{deptName},</if>
  96. <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
  97. <if test="orderNum != null and orderNum != ''">#{orderNum},</if>
  98. <if test="leader != null and leader != ''">#{leader},</if>
  99. <if test="phone != null and phone != ''">#{phone},</if>
  100. <if test="email != null and email != ''">#{email},</if>
  101. <if test="status != null">#{status},</if>
  102. <!--<if test="orgType != null and orgType != ''">#{orgType},</if>-->
  103. <if test="createBy != null and createBy != ''">#{createBy},</if>
  104. sysdate()
  105. )
  106. </insert>
  107. <update id="updateDept" parameterType="SysDept">
  108. update sys_dept
  109. <set>
  110. <if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
  111. <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
  112. <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
  113. <if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
  114. <if test="leader != null">leader = #{leader},</if>
  115. <if test="phone != null">phone = #{phone},</if>
  116. <if test="email != null">email = #{email},</if>
  117. <if test="status != null and status != ''">status = #{status},</if>
  118. <!--<if test="orgType != null and orgType != ''">org_type = #{orgType},</if>-->
  119. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  120. update_time = sysdate()
  121. </set>
  122. where dept_id = #{deptId}
  123. </update>
  124. <update id="updateDeptChildren" parameterType="java.util.List">
  125. update sys_dept set ancestors =
  126. <foreach collection="depts" item="item" index="index"
  127. separator=" " open="case dept_id" close="end">
  128. when #{item.deptId} then #{item.ancestors}
  129. </foreach>
  130. where dept_id in
  131. <foreach collection="depts" item="item" index="index"
  132. separator="," open="(" close=")">
  133. #{item.deptId}
  134. </foreach>
  135. </update>
  136. <update id="updateDeptStatusNormal" parameterType="Long">
  137. update sys_dept set status = '0' where dept_id in
  138. <foreach collection="array" item="deptId" open="(" separator="," close=")">
  139. #{deptId}
  140. </foreach>
  141. </update>
  142. <delete id="deleteDeptById" parameterType="Long">
  143. update sys_dept set del_flag = '2' where dept_id = #{deptId}
  144. </delete>
  145. <!--查看下级部门-->
  146. <select id="selectFindNextDept" resultType="com.zksy.system.model.respone.FindNextDeptRes"
  147. parameterType="com.zksy.system.model.request.NextDeptReq">
  148. SELECT
  149. s.dept_id deptId,
  150. s.dept_name deptName,
  151. ( SELECT dept_name FROM sys_dept WHERE dept_id = s.parent_id ) superiorDept,
  152. sdd.dict_value deptType,
  153. sdd.dict_label deptTypeValue,
  154. s.charge_person chargePerson,
  155. s.charge_phone chargePhone,
  156. s.establish_time establishTime,
  157. s.status,
  158. res.num memRs
  159. FROM
  160. sys_dept s
  161. LEFT JOIN (
  162. SELECT
  163. count(0) num,
  164. u.dept_id
  165. FROM
  166. sys_user u
  167. WHERE
  168. u.del_flag = '0'
  169. AND u.user_source IN ( 0, 1 )
  170. AND user_type = '2'
  171. and u.`status`= '0'
  172. GROUP BY u.dept_id
  173. ) res ON s.dept_id = res.dept_id
  174. LEFT JOIN sys_dict_data sdd ON sdd.dict_value = s.dept_type
  175. WHERE
  176. s.parent_id = #{deptId} AND s.del_flag = '0' AND sdd.dict_type = 'dept_type' AND
  177. s.`status` = '0'
  178. <if test="deptName != null and deptName != '' ">
  179. AND s.dept_Name LIKE concat('%',#{deptName,jdbcType=VARCHAR},'%')
  180. </if>
  181. <if test="deptType != null and deptType != '' ">
  182. AND s.dept_type = #{deptType}
  183. </if>
  184. </select>
  185. <!-- 查看部门资料-->
  186. <select id="findDataDept" resultType="com.zksy.system.model.respone.DataDeptRecord" parameterType="string">
  187. SELECT
  188. s.*,
  189. dd.dict_label deptTypeValue,
  190. sd.dept_name superiorDept,
  191. sd.dept_id superiorDeptId,
  192. res.num memRs
  193. FROM
  194. sys_dept s
  195. LEFT JOIN (
  196. SELECT
  197. count(0) num,
  198. u.dept_id
  199. FROM
  200. sys_user u
  201. WHERE
  202. u.del_flag = '0'
  203. AND u.user_source IN ( 0, 1 )
  204. AND user_type = '2'
  205. GROUP BY u.dept_id
  206. ) res ON s.dept_id = res.dept_id
  207. LEFT JOIN sys_dict_data dd ON dd.dict_value = s.dept_type
  208. LEFT JOIN sys_dept sd ON sd.dept_id = s.parent_id -- 查上级部门名字
  209. WHERE s.dept_id = #{deptId} AND dd.dict_type = 'dept_type'
  210. </select>
  211. <select id="selectLevelAndChild" parameterType="Long" resultType="com.zksy.common.core.domain.entity.SysDept">
  212. SELECT
  213. ID.LEVEL,
  214. DATA.dept_id id,
  215. DATA.dept_name label,
  216. DATA.*
  217. FROM
  218. (
  219. SELECT
  220. @ids AS _ids,
  221. ( SELECT @ids := GROUP_CONCAT( dept_id ) FROM sys_dept WHERE FIND_IN_SET( parent_id , @ids ) /*and approve_status='1'*/ and del_flag='0') AS cids,
  222. @l := @l + 1 AS LEVEL
  223. FROM
  224. sys_dept,
  225. ( SELECT @ids := #{deptId} , @l := 0 ) b
  226. WHERE
  227. @ids IS NOT NULL
  228. ) ID,
  229. sys_dept DATA
  230. WHERE
  231. FIND_IN_SET( DATA.dept_id, ID._ids ) /*and DATA.approve_status='1'*/ and DATA.del_flag='0'
  232. ORDER BY
  233. LEVEL,
  234. DATA.dept_type,
  235. dept_id
  236. </select>
  237. <!--导出下级单位-->
  238. <select id="exportDept" resultType="com.zksy.system.model.respone.NextDeptExport"
  239. parameterType="com.zksy.system.model.request.NextDeptExportReq">
  240. SELECT
  241. s.dept_name deptName,
  242. ( SELECT dept_name FROM sys_dept WHERE dept_id = s.parent_id ) superiorDept,
  243. sdd.dict_label deptTypeValue,
  244. s.charge_person chargePerson,
  245. s.charge_phone chargePhone,
  246. s.establish_time establishTime,
  247. res.num memRs
  248. FROM
  249. sys_dept s
  250. LEFT JOIN (
  251. SELECT
  252. count(0) num,
  253. u.dept_id
  254. FROM
  255. sys_user u
  256. WHERE
  257. u.del_flag = '0'
  258. AND u.user_source IN ( 0, 1 )
  259. AND user_type = '2'
  260. and u.`status`= '0'
  261. GROUP BY u.dept_id
  262. ) res ON s.dept_id = res.dept_id
  263. LEFT JOIN sys_dict_data sdd ON sdd.dict_value = s.dept_type
  264. WHERE
  265. s.parent_id = #{deptId} AND s.del_flag = '0' AND sdd.dict_type = 'dept_type'
  266. <if test="deptName != null and deptName != '' ">
  267. AND s.dept_Name LIKE concat('%',#{deptName,jdbcType=VARCHAR},'%')
  268. </if>
  269. <if test="deptType != null and deptType != '' ">
  270. AND s.dept_type = #{deptType}
  271. </if>
  272. </select>
  273. <select id="findupDept" resultType="com.zksy.system.model.respone.FindUpDeptRes">
  274. SELECT
  275. dept_id,
  276. parent_id,
  277. dept_name
  278. FROM
  279. sys_dept
  280. WHERE
  281. dept_id = #{deptId}
  282. </select>
  283. <select id="findUpId" resultType="com.zksy.system.model.respone.FindUpDeptRes">
  284. SELECT
  285. dept_id,
  286. parent_id,
  287. dept_name
  288. FROM
  289. sys_dept
  290. WHERE
  291. dept_id = #{deptId}
  292. </select>
  293. <select id="findPare" resultType="com.zksy.system.model.respone.FindUpDeptRes">
  294. SELECT
  295. dept_id,
  296. parent_id,
  297. dept_name
  298. FROM
  299. sys_dept
  300. WHERE
  301. parent_id = #{parentId}
  302. </select>
  303. </mapper>