SharePathMapper.xml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="cn.newfeifan.mall.module.distri.dal.mysql.sharepath.SharePathMapper">
  4. <!-- 增加节点 -->
  5. <insert id="addNode">
  6. INSERT INTO distri_share_path (ancestor,
  7. anc_name,
  8. anc_nick_name,
  9. anc_phone,
  10. descendant,
  11. desc_name,
  12. desc_nick_name,
  13. desc_phone,
  14. depth, sort)
  15. SELECT ancestor,
  16. anc_name,
  17. anc_nick_name,
  18. anc_phone,
  19. #{newDescendantId},
  20. #{newDescendantName},
  21. #{newDescendantNickName},
  22. #{newDescendantMobile},
  23. depth + 1,
  24. (SELECT IFNULL(MAX(sort), 0) + 1 FROM distri_share_path WHERE ancestor = #{parentId} AND depth = 1 and deleted = 0)
  25. FROM distri_share_path
  26. WHERE descendant = #{parentId} and deleted = 0
  27. UNION ALL
  28. SELECT #{parentId},
  29. #{parentName},
  30. #{parentNickName},
  31. #{parentMobile},
  32. #{newDescendantId},
  33. #{newDescendantName},
  34. #{newDescendantNickName},
  35. #{newDescendantMobile},
  36. 1,
  37. (SELECT IFNULL(MAX(sort), 0) + 1 FROM distri_share_path WHERE ancestor = #{parentId} AND depth = 1 and deleted = 0);
  38. </insert>
  39. <!-- Get the maximum sort value for a given parent ID -->
  40. <select id="getMaxSortByParentId" resultType="int" parameterType="long">
  41. SELECT IFNULL(MAX(sort), 0) + 1
  42. FROM distri_share_path
  43. WHERE ancestor = #{parentId}
  44. AND depth = 1
  45. </select>
  46. <!-- 删除旧的祖先关系 -->
  47. <delete id="deleteOldAncestors" parameterType="java.lang.Long">
  48. DELETE
  49. FROM distri_share_path
  50. WHERE descendant IN (SELECT descendant
  51. FROM distri_share_path
  52. WHERE ancestor = #{nodeId})
  53. AND ancestor NOT IN (SELECT descendant
  54. FROM distri_share_path
  55. WHERE ancestor = #{nodeId});
  56. </delete>
  57. <!-- 添加新的祖先关系 -->
  58. <insert id="addNewAncestors" parameterType="map">
  59. INSERT INTO distri_share_path (ancestor, descendant, depth, sort)
  60. SELECT np.ancestor, c.descendant, np.depth + c.depth + 1, 0
  61. FROM distri_share_path AS np
  62. CROSS JOIN (SELECT descendant, depth
  63. FROM distri_share_path
  64. WHERE ancestor = #{nodeId}) AS c
  65. WHERE np.descendant = #{newParentId}
  66. UNION ALL
  67. SELECT #{nodeId} as ancestor, descendant, depth + 1, 0
  68. FROM distri_share_path
  69. WHERE ancestor = #{nodeId};
  70. </insert>
  71. <!-- 查询某节点的所有后代节点 -->
  72. <select id="findAllDescendants" resultType="long">
  73. SELECT descendant
  74. FROM distri_share_path
  75. WHERE ancestor = #{ancestorId}
  76. </select>
  77. <!-- 查询排序号小于当前节点的兄弟节点 -->
  78. <select id="findLessSortedSiblings" resultType="long">
  79. SELECT a.descendant
  80. FROM distri_share_path a
  81. JOIN distri_share_path b ON a.ancestor = b.ancestor
  82. WHERE b.descendant = #{descendantId}
  83. AND a.sort &gt; b.sort
  84. </select>
  85. <resultMap id="TreeNodeMap" type="cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.TreeNode">
  86. <id property="id" column="descendant"/>
  87. <result property="name" column="desc_name"/>
  88. <collection property="children" ofType="cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.TreeNode"
  89. column="descendant" select="selectDescendants" />
  90. </resultMap>
  91. <select id="selectDescendants"
  92. resultMap="TreeNodeMap">
  93. WITH RECURSIVE SubPath AS (
  94. SELECT
  95. descendant,
  96. desc_name,
  97. depth,
  98. 1 AS current_depth
  99. FROM
  100. distri_share_path
  101. WHERE
  102. ancestor = #{ancestor}
  103. AND depth = 1
  104. UNION ALL
  105. SELECT
  106. d.descendant,
  107. d.desc_name,
  108. d.depth,
  109. sp.current_depth + 1
  110. FROM
  111. distri_share_path d
  112. JOIN
  113. SubPath sp ON sp.descendant = d.ancestor
  114. WHERE
  115. sp.current_depth <![CDATA[<]]> #{maxDepth}
  116. )
  117. SELECT
  118. descendant,
  119. desc_name,
  120. depth
  121. FROM
  122. SubPath
  123. WHERE
  124. current_depth <![CDATA[<=]]> #{maxDepth};
  125. </select>
  126. <select id="selectListByVisitor"
  127. resultType="cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO">
  128. select path.* from distri_share_path path
  129. left join member_user user on user.id = path.descendant
  130. where depth = 1 and ancestor = #{ancestor} and user.visitor = #{visitor} and path.deleted = 0
  131. </select>
  132. </mapper>