123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="cn.newfeifan.mall.module.distri.dal.mysql.sharepath.SharePathMapper">
- <!-- 增加节点 -->
- <insert id="addNode">
- INSERT INTO distri_share_path (ancestor,
- anc_name,
- anc_nick_name,
- anc_phone,
- descendant,
- desc_name,
- desc_nick_name,
- desc_phone,
- depth, sort)
- SELECT ancestor,
- anc_name,
- anc_nick_name,
- anc_phone,
- #{newDescendantId},
- #{newDescendantName},
- #{newDescendantNickName},
- #{newDescendantMobile},
- depth + 1,
- (SELECT IFNULL(MAX(sort), 0) + 1 FROM distri_share_path WHERE ancestor = #{parentId} AND depth = 1 and deleted = 0)
- FROM distri_share_path
- WHERE descendant = #{parentId} and deleted = 0
- UNION ALL
- SELECT #{parentId},
- #{parentName},
- #{parentNickName},
- #{parentMobile},
- #{newDescendantId},
- #{newDescendantName},
- #{newDescendantNickName},
- #{newDescendantMobile},
- 1,
- (SELECT IFNULL(MAX(sort), 0) + 1 FROM distri_share_path WHERE ancestor = #{parentId} AND depth = 1 and deleted = 0);
- </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>
- <!-- 删除旧的祖先关系 -->
- <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>
- <!-- 添加新的祖先关系 -->
- <insert id="addNewAncestors" parameterType="map">
- 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
- WHERE np.descendant = #{newParentId}
- UNION ALL
- SELECT #{nodeId} as ancestor, descendant, depth + 1, 0
- FROM distri_share_path
- WHERE ancestor = #{nodeId};
- </insert>
- <!-- 查询某节点的所有后代节点 -->
- <select id="findAllDescendants" resultType="long">
- SELECT descendant
- FROM distri_share_path
- WHERE ancestor = #{ancestorId}
- </select>
- <!-- 查询排序号小于当前节点的兄弟节点 -->
- <select id="findLessSortedSiblings" resultType="long">
- 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
- </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="selectDescendants"
- resultMap="TreeNodeMap">
- WITH RECURSIVE SubPath AS (
- SELECT
- descendant,
- desc_name,
- depth,
- 1 AS current_depth
- FROM
- distri_share_path
- WHERE
- ancestor = #{ancestor}
- AND depth = 1
- UNION ALL
- SELECT
- d.descendant,
- d.desc_name,
- d.depth,
- sp.current_depth + 1
- FROM
- distri_share_path d
- JOIN
- SubPath sp ON sp.descendant = d.ancestor
- WHERE
- sp.current_depth <![CDATA[<]]> #{maxDepth}
- )
- SELECT
- descendant,
- desc_name,
- depth
- FROM
- SubPath
- WHERE
- current_depth <![CDATA[<=]]> #{maxDepth};
- </select>
- <select id="selectListByVisitor"
- resultType="cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO">
- select path.* from distri_share_path path
- left join member_user user on user.id = path.descendant
- where depth = 1 and ancestor = #{ancestor} and user.visitor = #{visitor} and path.deleted = 0
- </select>
- </mapper>
|