Commit 62c03f2d LN

增加配置保存功能

1 个父辈 a913fac9
...@@ -198,7 +198,7 @@ public class DataInitManager { ...@@ -198,7 +198,7 @@ public class DataInitManager {
//物料管理:元器件管理,条形码管理 //物料管理:元器件管理,条形码管理
Menu pMenuWl = Menu.CreatePMenu("物料管理", 30, "materiel ", 2, "materiel"); Menu pMenuWl = Menu.CreatePMenu("物料管理", 30, "materiel ", 2, "BOM");
Menu menucom = new Menu(new ArrayList<Menu>(), 1, "component:list", "元器件", 1, "componentParts", "parts/component/index", "", 0, "server"); Menu menucom = new Menu(new ArrayList<Menu>(), 1, "component:list", "元器件", 1, "componentParts", "parts/component/index", "", 0, "server");
Menu menubarcode = new Menu(new ArrayList<Menu>(), 1, "barcode:list", "条形码", 1, "barcode", "barcode/barcode/index", "", 0, "chart"); Menu menubarcode = new Menu(new ArrayList<Menu>(), 1, "barcode:list", "条形码", 1, "barcode", "barcode/barcode/index", "", 0, "chart");
menus.addAll(createMenus(pMenuWl, menucom, menubarcode)); menus.addAll(createMenus(pMenuWl, menucom, menubarcode));
......
package com.neotel.smfcore.common.utils;
import com.neotel.smfcore.SmfCoreApplication;
import com.neotel.smfcore.security.rest.UserController;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.util.ClassUtils;
import org.yaml.snakeyaml.Yaml;
import java.net.URL;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class YmlUpdateUtil {
public static void updateYamlFile(Map<String, Object> updateMap) {
try {
String src = "src/main/resources/application.yml";
URL url = ClassUtils.getDefaultClassLoader().getResource("");
src = url.getPath() + "config/application.yml";
if (!FileUtil.exist(src)) {
log.info("未找到文件:" + src);
return;
}
Yaml yaml = new Yaml();
FileWriter fileWriter = null;
//层级map变量 Map<String, Object> springMap, dataSourceMap, resultMap, helperDialect;
Map<String, Object> resultMap;
try {
//读取yaml文件,默认返回根目录结构
resultMap = (Map<String, Object>) yaml.load(new FileInputStream(new File(src)));
// //get出spring节点数据
// springMap = (Map<String, Object>) resultMap.get("spring");
// //get出数据库节点数据
// dataSourceMap = (Map<String, Object>) springMap.get("datasource");
String msg = "";
for (String key : updateMap.keySet()
) {
Object value = updateMap.get(key);
if(StringUtils.isEmpty(value.toString())){
continue;
}
key= key.replace('.',',');
String[] keyarray= key.split(",");
if(keyarray!=null){
Map<String,Object> currMap=resultMap;
int index=0;
for (String mapKey :
keyarray) {
if(index==(keyarray.length-1)){
currMap=updateMapValue(mapKey,value,currMap);
}else{
currMap=updateMapMap(mapKey,currMap);
}
index++;
}
}
msg += key + "=" + value + ",";
}
//字符输出
fileWriter = new FileWriter(new File(src));
//用yaml方法把map结构格式化为yaml文件结构
String writeStr=yaml.dumpAsMap(resultMap);
fileWriter.write(writeStr);
//刷新
fileWriter.flush();
//关闭流
fileWriter.close();
log.info("更改yml配置:" + msg);
} catch (Exception e) {
e.printStackTrace();
log.error("updateYamlFile error :"+e.toString());
}
} catch (Exception e) {
e.printStackTrace();
log.error("updateYamlFile error :"+e.toString());
}
}
private static Map<String,Object> updateMapMap(String key, Map<String,Object> map ){
if (!map.containsKey(key)) {
map.put(key,new HashMap<String,Object>());
}
return (Map<String,Object>)map.get(key);
}
private static Map<String,Object> updateMapValue(String key,Object value,Map<String,Object> map ){
if (map.containsKey(key)) {
map.remove(key);
}
map.put(key, value);
return map;
}
}
...@@ -48,8 +48,9 @@ public class BarcodeManagerImpl implements IBarcodeManager { ...@@ -48,8 +48,9 @@ public class BarcodeManagerImpl implements IBarcodeManager {
@Override @Override
public PageData<Barcode> findByPage(Query query, Pageable pageable) { public PageData<Barcode> findByPage(Query query, Pageable pageable) {
int totalCount = barcodeDao.countByQuery(query);
List<Barcode> barcodes= barcodeDao.findByQuery(query,pageable); List<Barcode> barcodes= barcodeDao.findByQuery(query,pageable);
return new PageData(barcodes,barcodes.size()); return new PageData(barcodes,totalCount);
} }
@Override @Override
......
...@@ -5,6 +5,8 @@ import com.google.common.collect.Lists; ...@@ -5,6 +5,8 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.neotel.smfcore.common.exception.ValidateException; import com.neotel.smfcore.common.exception.ValidateException;
import com.neotel.smfcore.common.utils.Constants; import com.neotel.smfcore.common.utils.Constants;
import com.neotel.smfcore.common.utils.StringUtils;
import com.neotel.smfcore.common.utils.YmlUpdateUtil;
import com.neotel.smfcore.core.barcode.service.po.Barcode; import com.neotel.smfcore.core.barcode.service.po.Barcode;
import com.neotel.smfcore.core.barcode.utils.CodeResolve; import com.neotel.smfcore.core.barcode.utils.CodeResolve;
import com.neotel.smfcore.core.storage.bean.InventoryItem; import com.neotel.smfcore.core.storage.bean.InventoryItem;
...@@ -103,6 +105,28 @@ public class DataCache { ...@@ -103,6 +105,28 @@ public class DataCache {
List<String> ruleList=(List<String>)value; List<String> ruleList=(List<String>)value;
codeResolve.updateBarcodeRuleList(ruleList); codeResolve.updateBarcodeRuleList(ruleList);
} }
log.info("updateCache ["+cacheKey+"]=["+value+"]");
}
public String GetConfigCache(String cacheKey ,String configKey,String configValue) {
if (StringUtils.isEmpty(configKey) || StringUtils.isEmpty(cacheKey)) {
return configValue;
}
if (StringUtils.isEmpty(configValue)) {
String cacheValue = getCache(cacheKey);
if (cacheValue == null || StringUtils.isEmpty(cacheValue)) {
return configValue;
}
configValue = cacheValue;
Map<String, Object> updateMap = new HashMap<>();
updateMap.put(configKey, configValue);
YmlUpdateUtil.updateYamlFile(updateMap);
} else {
updateCache(cacheKey, configValue);
}
return configValue;
} }
/** /**
......
...@@ -2,9 +2,11 @@ package com.neotel.smfcore.hella.tcp; ...@@ -2,9 +2,11 @@ package com.neotel.smfcore.hella.tcp;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.neotel.smfcore.common.bean.ResultBean; import com.neotel.smfcore.common.bean.ResultBean;
import com.neotel.smfcore.common.utils.StringUtils;
import com.neotel.smfcore.core.device.util.DataCache; import com.neotel.smfcore.core.device.util.DataCache;
import com.neotel.smfcore.core.system.service.po.Settings; import com.neotel.smfcore.core.system.service.po.Settings;
import com.neotel.smfcore.hella.handler.HellaServiceHandler; import com.neotel.smfcore.hella.handler.HellaServiceHandler;
import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
...@@ -52,11 +54,16 @@ public class HellaTcpClient { ...@@ -52,11 +54,16 @@ public class HellaTcpClient {
@Autowired @Autowired
private HellaServiceHandler hellaServiceHandler; private HellaServiceHandler hellaServiceHandler;
@Autowired
private DataCache dataCache;
private static IoSession theSession; private static IoSession theSession;
@PostConstruct @PostConstruct
public void init(){ public void init(){
host=dataCache.GetConfigCache("hella.host","hella.host",host);
port=Integer.parseInt(dataCache.GetConfigCache("hella.port","hella.port",port+"")) ;
updateServerInfo(host, port); updateServerInfo(host, port);
start(); start();
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!