You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.2 KiB
160 lines
5.2 KiB
package com.fr.plugin.xx.theme.sky.web; |
|
|
|
import com.fr.cert.token.lang.Collections; |
|
import com.fr.decision.fun.impl.BaseHttpHandler; |
|
import com.fr.decision.webservice.Response; |
|
import com.fr.decision.webservice.bean.config.StyleConfigBean; |
|
import com.fr.decision.webservice.bean.entry.EntryBean; |
|
import com.fr.decision.webservice.v10.config.ConfigService; |
|
import com.fr.decision.webservice.v10.entry.EntryService; |
|
import com.fr.decision.webservice.v10.login.LoginService; |
|
import com.fr.decision.webservice.v10.user.UserService; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.StringUtils; |
|
import com.fr.third.fasterxml.jackson.databind.ObjectMapper; |
|
import com.fr.third.springframework.beans.BeanUtils; |
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
|
import com.fr.web.utils.WebUtils; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpServletResponse; |
|
import java.util.*; |
|
import java.util.stream.Collectors; |
|
|
|
/** |
|
* @author k |
|
* @Project name finereport10 |
|
* @description: |
|
* @created on 2019/10/27 16:45. |
|
* @email xx.xx@qq.com |
|
* @Modified By |
|
*/ |
|
public class GetAllMenuHttpHandler extends BaseHttpHandler { |
|
public GetAllMenuHttpHandler() { |
|
} |
|
|
|
private static final ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
@Override |
|
public RequestMethod getMethod() { |
|
return RequestMethod.GET; |
|
} |
|
|
|
@Override |
|
public String getPath() { |
|
return "/sky/getAllMenu"; |
|
} |
|
|
|
@Override |
|
public boolean isPublic() { |
|
return true; |
|
} |
|
|
|
@Override |
|
public void handle(HttpServletRequest request, HttpServletResponse response) throws Exception { |
|
EntryBeanModel entryBeanModel = new EntryBeanModel(); |
|
try { |
|
String userName = LoginService.getInstance().getUserNameFromRequest(request); |
|
String userId = UserService.getInstance().getCurrentUserId(userName); |
|
List<EntryBean> entries = EntryService.getInstance().getAllEntries(userId); |
|
entryBeanModel.setChilds(getList(entries)); |
|
entryBeanModel.setId(""); |
|
}catch (Exception e){ |
|
FineLoggerFactory.getLogger().error(e.getMessage(),e); |
|
entryBeanModel.setChilds(new ArrayList<>()); |
|
} |
|
WebUtils.printAsString(response, objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(Response.ok(entryBeanModel))); |
|
} |
|
|
|
private List<EntryBeanModel> getList(List<EntryBean> entries){ |
|
if(Collections.isEmpty(entries)){ |
|
return new ArrayList<>(); |
|
} |
|
EntryBeanModel finalEntryBeanModel = new EntryBeanModel(); |
|
Map<String,EntryBeanModel> entryMap = new HashMap<>(); |
|
Iterator entrisList = entries.iterator(); |
|
EntryBean entryBean; |
|
EntryBeanModel entryBeanModel = null; |
|
// 获取整个顶层目录 |
|
while (entrisList.hasNext()) { |
|
entryBean = (EntryBean) entrisList.next(); |
|
if (entryBean != null && entryBean.getDeviceType() % 2 != 0) { |
|
entryBeanModel = new EntryBeanModel(); |
|
BeanUtils.copyProperties(entryBean, entryBeanModel); |
|
if (entryBean.isIsParent()) { |
|
entryMap.put(entryBean.getId(), entryBeanModel); |
|
} |
|
if ("decision-directory-root".equalsIgnoreCase(entryBeanModel.getId())) { |
|
finalEntryBeanModel = entryBeanModel; |
|
} |
|
} |
|
} |
|
entrisList = entries.iterator(); |
|
while (entrisList.hasNext()) { |
|
entryBean = (EntryBean) entrisList.next(); |
|
if (entryBean != null && entryBean.getDeviceType() % 2 != 0) { |
|
EntryBeanModel beanModel; |
|
if (entryBean.getEntryType() == 3) { |
|
String parentId = entryBean.getpId(); |
|
beanModel = entryMap.get(entryBean.getId()); |
|
if (StringUtils.isNotBlank(parentId) && beanModel != null) { |
|
EntryBeanModel entry = entryMap.get(parentId); |
|
if(entry!=null){ |
|
entry.addChild(beanModel); |
|
} |
|
} |
|
} else { |
|
entryBeanModel = new EntryBeanModel(); |
|
BeanUtils.copyProperties(entryBean, entryBeanModel); |
|
if (StringUtils.isNotBlank(entryBeanModel.getpId())) { |
|
beanModel = entryMap.get(entryBeanModel.getpId()); |
|
if(beanModel != null){ |
|
beanModel.addChild(entryBeanModel); |
|
} |
|
} |
|
} |
|
} |
|
} |
|
List<EntryBeanModel> list = new ArrayList<>(); |
|
finalEntryBeanModel.setChilds(menu(finalEntryBeanModel.getChilds())); |
|
list.add(finalEntryBeanModel); |
|
return list; |
|
} |
|
|
|
private List<EntryBeanModel> menu(List<EntryBeanModel> models) { |
|
List<EntryBeanModel> entryBeanModels = new ArrayList<>(); |
|
try { |
|
models = models.stream().sorted((o1, o2) -> { |
|
if (o1 == null && o2 == null) { |
|
return 0; |
|
} |
|
if (o1 == null) { |
|
return 1; |
|
} |
|
if (o2 == null) { |
|
return -1; |
|
} |
|
return Long.compare(o1.getSortIndex(), o2.getSortIndex()); |
|
}).collect(Collectors.toList()); |
|
} catch (Exception e) { |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
for (EntryBeanModel entryBeanModel : models) { |
|
List<EntryBeanModel> childRoot = new ArrayList<>(); |
|
menuEntry(childRoot, entryBeanModel.getChilds()); |
|
entryBeanModel.setChilds(childRoot); |
|
entryBeanModels.add(entryBeanModel); |
|
} |
|
return entryBeanModels; |
|
} |
|
|
|
private void menuEntry(List<EntryBeanModel> childRoot, List<EntryBeanModel> child) { |
|
for (EntryBeanModel entryBeanModel : child) { |
|
if (!Collections.isEmpty(entryBeanModel.getChilds())) { |
|
menuEntry(childRoot, entryBeanModel.getChilds()); |
|
} |
|
entryBeanModel.setValue(entryBeanModel.getId()); |
|
childRoot.add(entryBeanModel); |
|
} |
|
} |
|
} |