|
@@ -9,12 +9,10 @@
|
|
|
anc_name,
|
|
|
anc_nick_name,
|
|
|
anc_phone,
|
|
|
-
|
|
|
descendant,
|
|
|
desc_name,
|
|
|
desc_nick_name,
|
|
|
desc_phone,
|
|
|
-
|
|
|
depth, sort)
|
|
|
SELECT ancestor,
|
|
|
anc_name,
|
|
@@ -45,22 +43,25 @@
|
|
|
</insert>
|
|
|
|
|
|
|
|
|
-
|
|
|
<!-- Get the maximum sort value for a given parent ID -->
|
|
|
<select id="getMaxSortByParentId" resultType="int" parameterType="long">
|
|
|
- SELECT IFNULL(MAX(sort), 0) + 1 FROM distri_share_path WHERE ancestor = #{parentId} AND depth = 1
|
|
|
+ SELECT IFNULL(MAX(sort), 0) + 1
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE ancestor = #{parentId}
|
|
|
+ AND depth = 1
|
|
|
</select>
|
|
|
|
|
|
|
|
|
<!-- 删除旧的祖先关系 -->
|
|
|
<delete id="deleteOldAncestors" parameterType="java.lang.Long">
|
|
|
- DELETE FROM distri_share_path
|
|
|
- WHERE descendant IN (
|
|
|
- SELECT descendant FROM distri_share_path WHERE ancestor = #{nodeId}
|
|
|
- )
|
|
|
- AND ancestor NOT IN (
|
|
|
- SELECT descendant FROM distri_share_path WHERE ancestor = #{nodeId}
|
|
|
- );
|
|
|
+ DELETE
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE descendant IN (SELECT descendant
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE ancestor = #{nodeId})
|
|
|
+ AND ancestor NOT IN (SELECT descendant
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE ancestor = #{nodeId});
|
|
|
</delete>
|
|
|
|
|
|
<!-- 添加新的祖先关系 -->
|
|
@@ -68,12 +69,14 @@
|
|
|
INSERT INTO distri_share_path (ancestor, descendant, depth, sort)
|
|
|
SELECT np.ancestor, c.descendant, np.depth + c.depth + 1, 0
|
|
|
FROM distri_share_path AS np
|
|
|
- CROSS JOIN (
|
|
|
- SELECT descendant, depth FROM distri_share_path WHERE ancestor = #{nodeId}
|
|
|
- ) AS c
|
|
|
+ CROSS JOIN (SELECT descendant, depth
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE ancestor = #{nodeId}) AS c
|
|
|
WHERE np.descendant = #{newParentId}
|
|
|
UNION ALL
|
|
|
- SELECT #{nodeId} as ancestor, descendant, depth + 1, 0 FROM distri_share_path WHERE ancestor = #{nodeId};
|
|
|
+ SELECT #{nodeId} as ancestor, descendant, depth + 1, 0
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE ancestor = #{nodeId};
|
|
|
</insert>
|
|
|
|
|
|
|
|
@@ -89,6 +92,36 @@
|
|
|
SELECT a.descendant
|
|
|
FROM distri_share_path a
|
|
|
JOIN distri_share_path b ON a.ancestor = b.ancestor
|
|
|
- WHERE b.descendant = #{descendantId} AND a.sort < b.sort
|
|
|
+ WHERE b.descendant = #{descendantId}
|
|
|
+ AND a.sort < b.sort
|
|
|
+ </select>
|
|
|
+
|
|
|
+
|
|
|
+ <resultMap id="TreeNodeMap" type="cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.TreeNode">
|
|
|
+ <id property="id" column="descendant"/>
|
|
|
+ <result property="name" column="desc_name"/>
|
|
|
+ <collection property="children" ofType="cn.newfeifan.mall.module.distri.controller.admin.sharepath.vo.TreeNode"
|
|
|
+ column="descendant" select="selectDescendants" />
|
|
|
+
|
|
|
+ </resultMap>
|
|
|
+ <!-- <select id="selectTopLevelAncestors" resultMap="TreeNodeMap">
|
|
|
+ SELECT descendant, desc_name
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE depth = 1
|
|
|
+ </select>-->
|
|
|
+
|
|
|
+ <select id="selectDescendants"
|
|
|
+ resultMap="TreeNodeMap">
|
|
|
+ WITH RECURSIVE SubPath AS (
|
|
|
+ SELECT descendant, desc_name, depth
|
|
|
+ FROM distri_share_path
|
|
|
+ WHERE ancestor = #{ancestor}
|
|
|
+ UNION ALL
|
|
|
+ SELECT d.descendant, d.desc_name, d.depth
|
|
|
+ FROM distri_share_path d
|
|
|
+ INNER JOIN SubPath sp ON sp.descendant = d.ancestor
|
|
|
+ )
|
|
|
+ SELECT * FROM SubPath
|
|
|
+
|
|
|
</select>
|
|
|
</mapper>
|