CompanyDeptMapper.xml 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.fs.company.mapper.CompanyDeptMapper">
  6. <resultMap type="CompanyDept" id="CompanyDeptResult">
  7. <result property="deptId" column="dept_id" />
  8. <result property="parentId" column="parent_id" />
  9. <result property="companyId" column="company_id" />
  10. <result property="ancestors" column="ancestors" />
  11. <result property="deptName" column="dept_name" />
  12. <result property="orderNum" column="order_num" />
  13. <result property="leader" column="leader" />
  14. <result property="phone" column="phone" />
  15. <result property="email" column="email" />
  16. <result property="status" column="status" />
  17. <result property="delFlag" column="del_flag" />
  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. </resultMap>
  23. <sql id="selectCompanyDeptVo">
  24. select dept_id, parent_id, company_id, ancestors, dept_name, order_num, leader, phone, email, status, del_flag, create_by, create_time, update_by, update_time from company_dept
  25. </sql>
  26. <select id="selectCompanyDeptList" parameterType="CompanyDept" resultMap="CompanyDeptResult">
  27. <include refid="selectCompanyDeptVo"/>
  28. <where>
  29. <if test="parentId != null "> and parent_id = #{parentId}</if>
  30. <if test="companyId != null "> and company_id = #{companyId}</if>
  31. <if test="ancestors != null and ancestors != ''"> and ancestors = #{ancestors}</if>
  32. <if test="deptName != null and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
  33. <if test="orderNum != null "> and order_num = #{orderNum}</if>
  34. <if test="leader != null and leader != ''"> and leader = #{leader}</if>
  35. <if test="phone != null and phone != ''"> and phone = #{phone}</if>
  36. <if test="email != null and email != ''"> and email = #{email}</if>
  37. <if test="status != null and status != ''"> and status = #{status}</if>
  38. </where>
  39. </select>
  40. <select id="selectCompanyDeptById" parameterType="Long" resultMap="CompanyDeptResult">
  41. <include refid="selectCompanyDeptVo"/>
  42. where dept_id = #{deptId}
  43. </select>
  44. <insert id="insertCompanyDept" parameterType="CompanyDept" useGeneratedKeys="true" keyProperty="deptId">
  45. insert into company_dept
  46. <trim prefix="(" suffix=")" suffixOverrides=",">
  47. <if test="parentId != null">parent_id,</if>
  48. <if test="companyId != null">company_id,</if>
  49. <if test="ancestors != null">ancestors,</if>
  50. <if test="deptName != null">dept_name,</if>
  51. <if test="orderNum != null">order_num,</if>
  52. <if test="leader != null">leader,</if>
  53. <if test="phone != null">phone,</if>
  54. <if test="email != null">email,</if>
  55. <if test="status != null">status,</if>
  56. <if test="delFlag != null">del_flag,</if>
  57. <if test="createBy != null">create_by,</if>
  58. <if test="createTime != null">create_time,</if>
  59. <if test="updateBy != null">update_by,</if>
  60. <if test="updateTime != null">update_time,</if>
  61. </trim>
  62. <trim prefix="values (" suffix=")" suffixOverrides=",">
  63. <if test="parentId != null">#{parentId},</if>
  64. <if test="companyId != null">#{companyId},</if>
  65. <if test="ancestors != null">#{ancestors},</if>
  66. <if test="deptName != null">#{deptName},</if>
  67. <if test="orderNum != null">#{orderNum},</if>
  68. <if test="leader != null">#{leader},</if>
  69. <if test="phone != null">#{phone},</if>
  70. <if test="email != null">#{email},</if>
  71. <if test="status != null">#{status},</if>
  72. <if test="delFlag != null">#{delFlag},</if>
  73. <if test="createBy != null">#{createBy},</if>
  74. <if test="createTime != null">#{createTime},</if>
  75. <if test="updateBy != null">#{updateBy},</if>
  76. <if test="updateTime != null">#{updateTime},</if>
  77. </trim>
  78. </insert>
  79. <update id="updateCompanyDept" parameterType="CompanyDept">
  80. update company_dept
  81. <trim prefix="SET" suffixOverrides=",">
  82. <if test="parentId != null">parent_id = #{parentId},</if>
  83. <if test="companyId != null">company_id = #{companyId},</if>
  84. <if test="ancestors != null">ancestors = #{ancestors},</if>
  85. <if test="deptName != null">dept_name = #{deptName},</if>
  86. <if test="orderNum != null">order_num = #{orderNum},</if>
  87. <if test="leader != null">leader = #{leader},</if>
  88. <if test="phone != null">phone = #{phone},</if>
  89. <if test="email != null">email = #{email},</if>
  90. <if test="status != null">status = #{status},</if>
  91. <if test="delFlag != null">del_flag = #{delFlag},</if>
  92. <if test="createBy != null">create_by = #{createBy},</if>
  93. <if test="createTime != null">create_time = #{createTime},</if>
  94. <if test="updateBy != null">update_by = #{updateBy},</if>
  95. <if test="updateTime != null">update_time = #{updateTime},</if>
  96. </trim>
  97. where dept_id = #{deptId}
  98. </update>
  99. <delete id="deleteCompanyDeptById" parameterType="Long">
  100. delete from company_dept where dept_id = #{deptId}
  101. </delete>
  102. <delete id="deleteCompanyDeptByIds" parameterType="String">
  103. delete from company_dept where dept_id in
  104. <foreach item="deptId" collection="array" open="(" separator="," close=")">
  105. #{deptId}
  106. </foreach>
  107. </delete>
  108. <delete id="deleteCompanyDeptByCompanyIds">
  109. delete from company_dept where company_id in
  110. <foreach item="companyId" collection="array" open="(" separator="," close=")">
  111. #{companyId}
  112. </foreach>
  113. </delete>
  114. <select id="selectDeptListByRoleId" resultType="Integer">
  115. select d.dept_id
  116. from company_dept d
  117. left join company_role_dept rd on d.dept_id = rd.dept_id
  118. where rd.role_id = #{roleId}
  119. <if test="deptCheckStrictly">
  120. and d.dept_id not in (select d.parent_id from company_dept d inner join company_role_dept rd on d.dept_id = rd.dept_id and rd.role_id = #{roleId})
  121. </if>
  122. order by d.parent_id, d.order_num
  123. </select>
  124. <select id="checkDeptExistUser" parameterType="Long" resultType="int">
  125. select count(1) from company_user where dept_id = #{deptId} and del_flag = '0'
  126. </select>
  127. <select id="hasChildByDeptId" parameterType="Long" resultType="int">
  128. select count(1) from company_dept
  129. where del_flag = '0' and parent_id = #{deptId} limit 1
  130. </select>
  131. <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
  132. select count(*) from company_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)
  133. </select>
  134. <update id="updateDeptChildren" parameterType="java.util.List">
  135. update company_dept set ancestors =
  136. <foreach collection="depts" item="item" index="index"
  137. separator=" " open="case dept_id" close="end">
  138. when #{item.deptId} then #{item.ancestors}
  139. </foreach>
  140. where dept_id in
  141. <foreach collection="depts" item="item" index="index"
  142. separator="," open="(" close=")">
  143. #{item.deptId}
  144. </foreach>
  145. </update>
  146. <update id="updateDeptStatus" parameterType="CompanyDept">
  147. update company_dept
  148. <set>
  149. <if test="status != null and status != ''">status = #{status},</if>
  150. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  151. update_time = sysdate()
  152. </set>
  153. where dept_id in (${ancestors})
  154. </update>
  155. <select id="selectChildrenDeptById" parameterType="Long" resultMap="CompanyDeptResult">
  156. select * from company_dept where find_in_set(#{deptId}, ancestors)
  157. </select>
  158. <select id="selectCompanyDeptByIds" resultType="com.fs.company.domain.CompanyDept">
  159. <include refid="selectCompanyDeptVo"/>
  160. where dept_id in
  161. <foreach collection="depts" item="deptId" index="index"
  162. separator="," open="(" close=")">
  163. #{deptId}
  164. </foreach>
  165. </select>
  166. <select id="getCurrentDeptIdDownTreeIds" parameterType="Long" resultType="Long">
  167. WITH RECURSIVE cte AS (
  168. SELECT
  169. *,
  170. 0 AS level
  171. FROM company_dept
  172. WHERE dept_id = #{deptId}
  173. UNION ALL
  174. SELECT
  175. t.*,
  176. cte.level + 1 AS level
  177. FROM company_dept t
  178. INNER JOIN cte
  179. ON t.parent_id = cte.dept_id
  180. )
  181. SELECT dept_id FROM cte ORDER BY level;
  182. </select>
  183. <select id="getDeptFullPathList" parameterType="Long" resultType="com.fs.course.vo.DeptFullPathVO">
  184. WITH RECURSIVE dept_path AS (
  185. SELECT
  186. dept_id,
  187. parent_id,
  188. dept_name,
  189. company_id,
  190. dept_name AS full_path
  191. FROM company_dept
  192. WHERE parent_id = 0
  193. AND company_id = #{companyId}
  194. UNION ALL
  195. SELECT
  196. c.dept_id,
  197. c.parent_id,
  198. c.dept_name,
  199. c.company_id,
  200. CONCAT(d.full_path, '_', c.dept_name) AS full_path
  201. FROM company_dept c
  202. INNER JOIN dept_path d
  203. ON c.parent_id = d.dept_id
  204. WHERE c.company_id = #{companyId}
  205. )
  206. SELECT dept_id, dept_name, full_path
  207. FROM dept_path
  208. ORDER BY full_path
  209. </select>
  210. </mapper>