|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.zy.asrs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.asrs.wcs.common.constant.Constants; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Iterator; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Optional; | 
|---|
|  |  |  | import java.util.function.BiConsumer; | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param <R>            parentId的类型 | 
|---|
|  |  |  | * @return List<T> | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static <T, R> List<T> toTreeData(List<T> data, R parentId, | 
|---|
|  |  |  | public static <T, R> List<T> toTreeData(List<T> data, | 
|---|
|  |  |  | R parentId, | 
|---|
|  |  |  | Function<? super T, ? extends R> parentIdMapper, | 
|---|
|  |  |  | Function<? super T, ? extends R> idMapper, | 
|---|
|  |  |  | BiConsumer<T, List<T>> consumer) { | 
|---|
|  |  |  | 
|---|
|  |  |  | if (parentId.equals(dParentId)) { | 
|---|
|  |  |  | R dId = idMapper.apply(d); | 
|---|
|  |  |  | List<T> children = toTreeData(data, dId, parentIdMapper, idMapper, consumer); | 
|---|
|  |  |  | consumer.accept(d, children); | 
|---|
|  |  |  | if(children.size() > 0) { | 
|---|
|  |  |  | consumer.accept(d, children); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | result.add(d); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //    public static <T> List<T> treeRemove(List<T> data, String condition, | 
|---|
|  |  |  | //                                         Function<? super T, ? extends String> fetcher, | 
|---|
|  |  |  | //                                         Function<T, List<T>> childrenGetter, | 
|---|
|  |  |  | //                                         BiConsumer<T, List<T>> childrenSetter) { | 
|---|
|  |  |  | //        List<T> result = new ArrayList<>(); | 
|---|
|  |  |  | //        for (T node : data) { | 
|---|
|  |  |  | //            List<T> children = childrenGetter.apply(node); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            if (children != null && !children.isEmpty()) { | 
|---|
|  |  |  | //                List<T> newChildren = treeRemove(children, condition, fetcher, childrenGetter, childrenSetter); | 
|---|
|  |  |  | //                childrenSetter.accept(node, newChildren); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //            if (fetcher.apply(node).contains(condition)) { | 
|---|
|  |  |  | //                result.add(node); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        return result; | 
|---|
|  |  |  | //    } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static <T> void treeRemove(List<T> list, String condition, Function<? super T, ? extends String> fetcher, Function<T, List<T>> childrenGetter) { | 
|---|
|  |  |  | Iterator<T> iterator = list.iterator(); | 
|---|
|  |  |  | while (iterator.hasNext()) { | 
|---|
|  |  |  | T next = iterator.next(); | 
|---|
|  |  |  | List<T> children = childrenGetter.apply(next); | 
|---|
|  |  |  | if (children != null && !children.isEmpty()) { | 
|---|
|  |  |  | treeRemove(children, condition, fetcher, childrenGetter); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | if (!fetcher.apply(next).contains(condition)) { | 
|---|
|  |  |  | iterator.remove(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void cors(HttpServletResponse response){ | 
|---|
|  |  |  | // 跨域设置 | 
|---|
|  |  |  | response.setHeader("Access-Control-Max-Age", "3600"); | 
|---|
|  |  |  | response.setHeader("Access-Control-Allow-Origin", "*"); | 
|---|
|  |  |  | response.setHeader("Access-Control-Allow-Methods", "*"); | 
|---|
|  |  |  | response.setHeader("Access-Control-Allow-Headers", "*"); | 
|---|
|  |  |  | response.setHeader("Access-Control-Expose-Headers", Constants.TOKEN_HEADER_NAME); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|