SysDeptMapper.xml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.ylx.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="cityCode" column="city_code" />
  12. <result property="cityName" column="city_name" />
  13. <result property="orderNum" column="order_num" />
  14. <result property="leader" column="leader" />
  15. <result property="phone" column="phone" />
  16. <result property="email" column="email" />
  17. <result property="status" column="status" />
  18. <result property="isDelete" column="is_delete" />
  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.parent_id, d.ancestors, d.dept_name,d.city_code,d.city_name, d.order_num, d.leader, d.phone, d.email, d.status, d.is_delete, d.create_by, d.create_time
  27. from sys_dept d
  28. </sql>
  29. <!-- 查询部门列表 -->
  30. <select id="selectDeptList" parameterType="SysDept" resultMap="SysDeptResult">
  31. <include refid="selectDeptVo"/>
  32. where d.is_delete = 0
  33. <if test="deptId != null and deptId != 0">
  34. AND dept_id = #{deptId}
  35. </if>
  36. <if test="parentId != null and parentId != 0">
  37. AND parent_id = #{parentId}
  38. </if>
  39. <if test="deptName != null and deptName != ''">
  40. AND dept_name like concat('%', #{deptName}, '%')
  41. </if>
  42. <if test="status != null and status != ''">
  43. AND status = #{status}
  44. </if>
  45. <!-- 数据范围过滤
  46. ${params.dataScope}-->
  47. order by d.parent_id, d.order_num
  48. </select>
  49. <!-- 分页查询部门列表 -->
  50. <select id="selectDeptPage" resultMap="SysDeptResult">
  51. <include refid="selectDeptVo"/>
  52. where d.is_delete = 0
  53. <if test="dto.deptId != null and dto.deptId != 0">
  54. AND dept_id = #{dto.deptId}
  55. </if>
  56. <if test="dto.deptName != null and dto.deptName != ''">
  57. AND dept_name like concat('%', #{dto.deptName}, '%')
  58. </if>
  59. <if test="dto.status != null and dto.status != ''">
  60. AND status = #{dto.status}
  61. </if>
  62. <if test="dto.startTime != null and dto.startTime != ''">
  63. AND d.create_time &gt;= #{dto.startTime}
  64. </if>
  65. <if test="dto.endTime != null and dto.endTime != ''">
  66. AND d.create_time &lt;= #{dto.endTime}
  67. </if>
  68. order by d.parent_id, d.order_num
  69. </select>
  70. <select id="selectDeptListByRoleId" resultType="Long">
  71. select d.dept_id
  72. from sys_dept d
  73. left join sys_role_dept rd on d.dept_id = rd.dept_id
  74. where rd.role_id = #{roleId}
  75. <if test="deptCheckStrictly">
  76. 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})
  77. </if>
  78. order by d.parent_id, d.order_num
  79. </select>
  80. <select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
  81. select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status,
  82. (select dept_name from sys_dept where dept_id = d.parent_id) parent_name
  83. from sys_dept d
  84. where d.dept_id = #{deptId}
  85. </select>
  86. <select id="checkDeptExistUser" parameterType="Long" resultType="int">
  87. select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'
  88. </select>
  89. <!-- 查询部门是否存在下级部门 -->
  90. <select id="hasChildByDeptId" parameterType="Long" resultType="int">
  91. select count(1) from sys_dept
  92. where is_delete = 0 and parent_id = #{deptId} limit 1
  93. </select>
  94. <select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
  95. select * from sys_dept where find_in_set(#{deptId}, ancestors)
  96. </select>
  97. <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
  98. select count(*) from sys_dept where status = 0 and is_delete = 0 and find_in_set(#{deptId}, ancestors)
  99. </select>
  100. <!-- 校验部门名称是否唯一 -->
  101. <select id="checkDeptNameUnique" resultMap="SysDeptResult">
  102. <include refid="selectDeptVo"/>
  103. where dept_name=#{deptName} and parent_id = #{parentId} and is_delete = 0 limit 1
  104. </select>
  105. <!-- 新增部门 -->
  106. <insert id="insertDept" parameterType="SysDept">
  107. insert into sys_dept(
  108. <if test="deptId != null and deptId != 0">dept_id,</if>
  109. <if test="parentId != null and parentId != 0">parent_id,</if>
  110. <if test="deptName != null and deptName != ''">dept_name,</if>
  111. <if test="cityCode != null and cityCode != ''">city_code,</if>
  112. <if test="cityName != null and cityName != ''">city_name,</if>
  113. <if test="ancestors != null and ancestors != ''">ancestors,</if>
  114. <if test="orderNum != null">order_num,</if>
  115. <if test="leader != null and leader != ''">leader,</if>
  116. <if test="phone != null and phone != ''">phone,</if>
  117. <if test="email != null and email != ''">email,</if>
  118. <if test="status != null">status,</if>
  119. <if test="createBy != null and createBy != ''">create_by,</if>
  120. create_time
  121. )values(
  122. <if test="deptId != null and deptId != 0">#{deptId},</if>
  123. <if test="parentId != null and parentId != 0">#{parentId},</if>
  124. <if test="deptName != null and deptName != ''">#{deptName},</if>
  125. <if test="cityCode != null and cityCode != ''">#{cityCode},</if>
  126. <if test="cityName != null and cityName != ''">#{cityName},</if>
  127. <if test="ancestors != null and ancestors != ''">#{ancestors},</if>
  128. <if test="orderNum != null">#{orderNum},</if>
  129. <if test="leader != null and leader != ''">#{leader},</if>
  130. <if test="phone != null and phone != ''">#{phone},</if>
  131. <if test="email != null and email != ''">#{email},</if>
  132. <if test="status != null">#{status},</if>
  133. <if test="createBy != null and createBy != ''">#{createBy},</if>
  134. sysdate()
  135. )
  136. </insert>
  137. <update id="updateDept" parameterType="SysDept">
  138. update sys_dept
  139. <set>
  140. <if test="parentId != null and parentId != 0">parent_id = #{parentId},</if>
  141. <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
  142. <if test="ancestors != null and ancestors != ''">ancestors = #{ancestors},</if>
  143. <if test="orderNum != null">order_num = #{orderNum},</if>
  144. <if test="leader != null">leader = #{leader},</if>
  145. <if test="phone != null">phone = #{phone},</if>
  146. <if test="email != null">email = #{email},</if>
  147. <if test="status != null and status != ''">status = #{status},</if>
  148. <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
  149. update_time = sysdate()
  150. </set>
  151. where dept_id = #{deptId}
  152. </update>
  153. <update id="updateDeptChildren" parameterType="java.util.List">
  154. update sys_dept set ancestors =
  155. <foreach collection="depts" item="item" index="index"
  156. separator=" " open="case dept_id" close="end">
  157. when #{item.deptId} then #{item.ancestors}
  158. </foreach>
  159. where dept_id in
  160. <foreach collection="depts" item="item" index="index"
  161. separator="," open="(" close=")">
  162. #{item.deptId}
  163. </foreach>
  164. </update>
  165. <update id="updateDeptStatusNormal" parameterType="Long">
  166. update sys_dept set status = '0' where dept_id in
  167. <foreach collection="array" item="deptId" open="(" separator="," close=")">
  168. #{deptId}
  169. </foreach>
  170. </update>
  171. </mapper>