¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.jeecg.modules.dnc.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.modules.dnc.entity.ProductMix; |
| | | import org.jeecg.modules.dnc.mapper.ProductMixMapper; |
| | | import org.jeecg.modules.dnc.service.IPermissionStreamNewService; |
| | | import org.jeecg.modules.dnc.service.IProductMixService; |
| | | import org.jeecg.modules.dnc.utils.TreeBuilder; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.*; |
| | | |
| | | @Service |
| | | public class ProductMixServiceImpl extends ServiceImpl<ProductMixMapper, ProductMix> implements IProductMixService { |
| | | |
| | | @Autowired |
| | | private IPermissionStreamNewService permissionStreamNewService; |
| | | |
| | | /** |
| | | * é»è®¤ç»ææ æ¥è¯¢ |
| | | * @return |
| | | */ |
| | | @Override |
| | | public List<ProductMix> getTree() { |
| | | LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); |
| | | List<ProductMix> rawData = new ArrayList<>(); |
| | | if (loginUser.getUsername().equals("admin")) { |
| | | //ä¸éè¦æéè¿æ»¤ |
| | | rawData=permissionStreamNewService.loadProductMixAll(loginUser.getId()); |
| | | }else{ |
| | | //éè¦æéè¿æ»¤ |
| | | String productIds = loginUser.getProductionIds(); |
| | | if (productIds != null && !productIds.isEmpty()) { |
| | | List<String> productIdList = Arrays.asList(productIds.split(",")); |
| | | rawData = permissionStreamNewService.loadProductMix(loginUser.getId(),productIdList); |
| | | } |
| | | } |
| | | TreeBuilder builder = new TreeBuilder(); |
| | | TreeBuilder.CleanResult cleanResult = builder.preprocessData(rawData); |
| | | List<ProductMix> sorted = builder.topologicalSort( |
| | | cleanResult.getValidNodes(), |
| | | cleanResult.getNodeMap() |
| | | ); |
| | | List<ProductMix> result =builder.assembleTree(sorted, cleanResult.getNodeMap()); |
| | | result.sort(Comparator.comparing(ProductMix::getCreateTime, Comparator.nullsLast(Date::compareTo))); |
| | | return result; |
| | | } |
| | | |
| | | @Override |
| | | public List<ProductMix> getParentList(String id) { |
| | | List<ProductMix> parentList = new ArrayList<>(); |
| | | // 1. æ ¹æ®IDæ¥è¯¢å½åèç¹ |
| | | ProductMix current = this.getById(id); |
| | | if (current == null) { |
| | | return parentList; // èç¹ä¸å卿¶è¿å空å表 |
| | | } |
| | | // 2. ä»å½åèç¹å¼å§å䏿¥æ¾ç¶èç¹ |
| | | Long parentId = current.getParentId(); |
| | | while ( parentId != 0L) { |
| | | ProductMix parent = this.getById(parentId.toString()); |
| | | if (parent == null) { |
| | | break; |
| | | } |
| | | parentList.add(parent); |
| | | parentId = parent.getParentId(); |
| | | } |
| | | |
| | | return parentList; |
| | | } |
| | | |
| | | @Override |
| | | public List<ProductMix> getChildrenList(String id) { |
| | | List<ProductMix> childrenList = new ArrayList<>(); |
| | | ProductMix current = this.getById(id); |
| | | if (current == null) { |
| | | return childrenList; |
| | | } |
| | | |
| | | // 使ç¨éåè¿è¡BFS |
| | | Queue<ProductMix> queue = new LinkedList<>(); |
| | | queue.add(current); // å å
¥å½åèç¹ä½ä¸ºèµ·ç¹ |
| | | |
| | | // è®°å½å·²è®¿é®èç¹çIDï¼é¿å
循ç¯å¼ç¨ |
| | | Set<String> visited = new HashSet<>(); |
| | | visited.add(id); // èµ·å§èç¹å·²è®¿é® |
| | | |
| | | while (!queue.isEmpty()) { |
| | | ProductMix node = queue.poll(); |
| | | // è·³è¿èµ·å§èç¹ï¼å³ä¼ å
¥çèç¹ï¼ï¼ä¸å å
¥ç»æå表 |
| | | if (!node.getId().toString().equals(id)) { |
| | | childrenList.add(node); |
| | | } |
| | | |
| | | // æ¥è¯¢å½åèç¹çç´æ¥åèç¹ |
| | | List<ProductMix> directChildren = this.lambdaQuery().eq(ProductMix::getParentId, node.getId()).list(); |
| | | if (directChildren != null && !directChildren.isEmpty()) { |
| | | for (ProductMix child : directChildren) { |
| | | String childId = child.getId().toString(); |
| | | // å¦æè¯¥åèç¹è¿æªè®¿é®è¿ |
| | | if (!visited.contains(childId)) { |
| | | visited.add(childId); |
| | | queue.add(child); |
| | | } |
| | | // å¦å忽ç¥ï¼é¿å
循ç¯å¼ç¨å¯¼è´çæ»å¾ªç¯ |
| | | } |
| | | } |
| | | } |
| | | return childrenList; |
| | | } |
| | | |
| | | } |