SysDeptMapper.xml 7.8 KB

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