@Override
public ServerResponse<List<WebAppDownMenu>> getWebAppDownMenuList(String position) {
// 1.根据position查找所有下载菜单
List<WebAppDownMenu> menuList = webAppDownMenuMapper.selectCategoryByPosition(position);
// 1.1 list<menuList>转换为map<type,menu>
Map<String, WebAppDownMenu> type2menu =new HashMap<String,WebAppDownMenu>();
for(WebAppDownMenu menu : menuList){
type2menu.put(menu.getType(),menu);
}
// 2.遍历一遍结果,得到总的type列表[1,2,3...]、category列表[1,10]以及category下的type列表
// key是type,value是categoryId
Map<String,String> type2category = new HashMap<String,String>();
// key是categoryId,value是<展示位置, type>
Map<String, Map<String, String>> categoryMap = new TreeMap<String, Map<String, String>>();
// key是categoryId,value是categoryName
Map<String, String> category2name = new HashMap<String, String>();
for(WebAppDownMenu menu : menuList){
type2category.put( menu.getType(), menu.getCategoryId() );
// 先用categoryId占位
categoryMap.put(menu.getCategoryId() , new HashMap<String, String>() );
category2name.put(menu.getCategoryId(), menu.getCategoryName() );
}
List<String> typeList = new ArrayList<String>();
for(Map.Entry<String, String> entry : type2category.entrySet()){
String type = entry.getKey();
String categoryId = entry.getValue();
typeList.add(type);
categoryMap.get(categoryId).put( type, type );
}
//3、用总的type列表[1,2,3,..]查询软件详情web_appdown_control,形成map<type, appInfo>
String typeStr = new String();
String ignoreTypeStr = new String();
for (String type : typeList) {
if (typeStr.length() > 0) {
typeStr +=",";
}
typeStr += "'" + type + "'";
}
// 打印typeStr
logger.info("typeStr:" + typeStr);
Map<String, Object> tmpmap = new HashMap<String, Object>();
tmpmap.put("type", typeStr);
List<WebAppDownControl> webAppDownControls = webAppDownControlMapper.getPresentVersionEntityByType(tmpmap);
// list<app>转换为map<type, app>
Map<String, WebAppDownControl> type2app = new HashMap<String, WebAppDownControl>();
for (WebAppDownControl webAppDownControl : webAppDownControls) {
type2app.put(webAppDownControl.getType(), webAppDownControl);
String jsonList = webAppDownControl.getDownInfoJson();
JSONArray jsonArray = JSON.parseArray(jsonList);
//downInfo字段,json解析后的接收对象list声明
List<AppDownInfo> downInfos = new ArrayList<>();
for(int j = 0; j < jsonArray.size();j++){
JSONObject jsonObject = jsonArray.getJSONObject(j);
//获取json串中的key和value
String appType = jsonObject.getString("type");
String appUrl = jsonObject.getString("url");
//实例化json解析后的接收对象AppDownInfo
AppDownInfo appDownInfo = new AppDownInfo();
appDownInfo.setType(appType);
appDownInfo.setUrl(appUrl);
//每封装一个AppDownInfo对象后就add进一个list里
downInfos.add(appDownInfo);
}
webAppDownControl.setDownInfo(downInfos);
}
// 4、组合返回
List<WebAppDownMenu> rspList = new ArrayList<>();
for(Map.Entry<String, Map<String, String> > entry : categoryMap.entrySet()){
typeStr = "";
ignoreTypeStr = "";
String categoryId = entry.getKey();
String categoryName =category2name.get(categoryId);
Map<String, String> type2display = entry.getValue();
if(type2display == null){
logger.info("categoryId =" + categoryId + ";categoryName =" + categoryName +"type2display is null");
continue;
}
WebAppDownMenu menus = new WebAppDownMenu();
menus.setPosition(position);
menus.setCategoryId(categoryId);
menus.setCategoryName(category2name.get(categoryId));
List<WebAppDownControl> controlList = new ArrayList<>();
for(Map.Entry<String, String> e : type2display.entrySet()){
String type = e.getKey();
WebAppDownControl control = type2app.get(type);
if(control != null){
controlList.add(type2app.get(type));
typeStr += type + ",";
}else{
ignoreTypeStr += type + ",";
}
}
// 打印categoryId和对应的typeStr以及ignoreTypeStr
logger.info("categoryId:" + categoryId + ";typeStr:" + typeStr + ";ignoreTypeStr:" + ignoreTypeStr);
menus.setAppList(controlList);
rspList.add(menus);
}
return ServerResponse.createBySuccess(rspList);
}
05-21
894