SysDeptMapper.xml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.railway.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="parentName" column="parent_name"/>
  18. <result property="createBy" column="create_by"/>
  19. <result property="createTime" column="create_time"/>
  20. <result property="updateBy" column="update_by"/>
  21. <result property="updateTime" column="update_time"/>
  22. <result property="remark" column="remark"/>
  23. <result property="userCount" column="user_count"/>
  24. </resultMap>
  25. <sql id="selectDeptVo">
  26. select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone,
  27. d.email, d.status, d.del_flag, d.create_by, d.create_time, d.remark, t.user_count
  28. from sys_dept d
  29. left join (select dept_id, count(*) as user_count from sys_user where del_flag = '0' group by dept_id) t on t.dept_id = d.dept_id
  30. </sql>
  31. <select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
  32. <include refid="selectDeptVo"/>
  33. where d.del_flag = '0'
  34. <if test="deptId != null and deptId != 0">
  35. AND d.dept_id = #{deptId}
  36. </if>
  37. <if test="parentId != null and parentId != 0">
  38. AND d.parent_id = #{parentId}
  39. </if>
  40. <if test="deptName != null and deptName != ''">
  41. AND d.dept_name like concat('%', #{deptName}, '%')
  42. </if>
  43. <if test="status != null and status != ''">
  44. AND d.status = #{status}
  45. </if>
  46. <!-- 数据范围过滤 -->
  47. ${params.dataScope}
  48. order by d.parent_id, d.order_num
  49. </select>
  50. <select id="selectDeptListByRoleId" resultType="Long">
  51. select d.dept_id
  52. from sys_dept d
  53. left join sys_role_dept rd on d.dept_id = rd.dept_id
  54. where rd.role_id = #{roleId}
  55. <if test="deptCheckStrictly">
  56. and d.dept_id not in (select d.parent_id from sys_dept d inner join sys_role_dept rd on
  57. d.dept_id = rd.dept_id and rd.role_id = #{roleId})
  58. </if>
  59. order by d.parent_id, d.order_num
  60. </select>
  61. <select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
  62. <include refid="selectDeptVo"/>
  63. where dept_id = #{deptId}
  64. </select>
  65. <select id="checkDeptExistUser" parameterType="Long" resultType="int">
  66. select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
  67. </select>
  68. <select id="hasChildByDeptId" parameterType="Long" resultType="int">
  69. select count(1) from sys_dept
  70. where del_flag = '0' and parent_id = #{deptId} limit 1
  71. </select>
  72. <select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
  73. select * from sys_dept where find_in_set(#{deptId}, ancestors)
  74. </select>
  75. <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
  76. select count(*) from sys_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId},
  77. ancestors)
  78. </select>
  79. <select id="checkDeptNameUnique" resultMap="SysDeptResult">
  80. <include refid="selectDeptVo"/>
  81. where dept_name=#{deptName} and parent_id = #{parentId} limit 1
  82. </select>
  83. <insert id="insertDept" parameterType="SysDept">
  84. insert into sys_dept(
  85. <if test="deptId != null and deptId != 0">dept_id,</if>
  86. <if test="parentId != null and parentId != 0">parent_id,</if>
  87. <if test="deptName != null and deptName != ''">dept_name,</if>
  88. <if test="ancestors != null and ancestors != ''">ancestors,</if>
  89. <if test="orderNum != null and orderNum != ''">order_num,</if>
  90. <if test="leader != null and leader != ''">leader,</if>
  91. <if test="phone != null and phone != ''">phone,</if>
  92. <if test="email != null and email != ''">email,</if>
  93. <if test="status != null">status,</if>
  94. <if test="createBy != null and createBy != ''">create_by,</if>
  95. <if test="remark != null and remark != ''">remark,</if>
  96. create_time
  97. )values(
  98. <if test="deptId != null and deptId != 0">#{deptId},</if>
  99. <if test="parentId != null and parentId != 0">#{parentId},</if>
  100. <if test="deptName != null and deptName != ''">#{deptName},</if>
  101. <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
  102. <if test="orderNum != null and orderNum != ''">#{orderNum},</if>
  103. <if test="leader != null and leader != ''">#{leader},</if>
  104. <if test="phone != null and phone != ''">#{phone},</if>
  105. <if test="email != null and email != ''">#{email},</if>
  106. <if test="status != null">#{status},</if>
  107. <if test="createBy != null and createBy != ''">#{createBy},</if>
  108. <if test="remark != null and remark != ''">#{remark},</if>
  109. sysdate()
  110. )
  111. </insert>
  112. <update id="updateDept" parameterType="SysDept">
  113. update sys_dept
  114. <set>
  115. <if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
  116. <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
  117. <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
  118. <if test="orderNum != null and orderNum != ''">order_num = #{orderNum},</if>
  119. <if test="leader != null">leader = #{leader},</if>
  120. <if test="phone != null">phone = #{phone},</if>
  121. <if test="email != null">email = #{email},</if>
  122. <if test="status != null and status != ''">status = #{status},</if>
  123. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  124. <if test="remark != null and remark != ''">remark = #{remark},</if>
  125. update_time = sysdate()
  126. </set>
  127. where dept_id = #{deptId}
  128. </update>
  129. <update id="updateDeptChildren" parameterType="java.util.List">
  130. update sys_dept set ancestors =
  131. <foreach collection="depts" item="item" index="index"
  132. separator=" " open="case dept_id" close="end">
  133. when #{item.deptId} then #{item.ancestors}
  134. </foreach>
  135. where dept_id in
  136. <foreach collection="depts" item="item" index="index"
  137. separator="," open="(" close=")">
  138. #{item.deptId}
  139. </foreach>
  140. </update>
  141. <update id="updateDeptStatusNormal" parameterType="Long">
  142. update sys_dept set status = '0' where dept_id in
  143. <foreach collection="array" item="deptId" open="(" separator="," close=")">
  144. #{deptId}
  145. </foreach>
  146. </update>
  147. <delete id="deleteDeptById" parameterType="Long">
  148. update sys_dept set del_flag = '1' where dept_id = #{deptId}
  149. </delete>
  150. </mapper>