group.rober.base.dict.service.DictAdminService Maven / Gradle / Ivy
The newest version!
package group.rober.base.dict.service;
import group.rober.base.autoconfigure.BaseProperties;
import group.rober.base.dict.model.DictEntry;
import group.rober.base.dict.model.DictItemEntry;
import group.rober.base.dict.service.impl.DictServiceImpl;
import group.rober.base.dict.service.impl.po.DictItemPO;
import group.rober.base.dict.service.impl.po.DictPO;
import group.rober.runtime.kit.*;
import group.rober.runtime.lang.BizException;
import group.rober.runtime.lang.RoberException;
import group.rober.sql.core.DataAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class DictAdminService {
@Autowired
BaseProperties baseProperties;
@Autowired
DataAccessor dataAccessor;
@Autowired
DictServiceImpl dictService;
public List getAllDictList(){
return dictService.getDictList();
// List entries = new ArrayList<>();
// List dictPoList = dataAccessor.selectList(DictPO.class,"select * from FOWK_DICT where 1=1 order by SORT_CODE ASC");
// dictPoList.forEach(dictPO->{
// DictEntry entry = new DictEntry();
// BeanKit.copyProperties(dictPO, entry);
// Map itemMap = dictService.getDictItemMap(dictPO.getCode());
// entry.setItemMap(itemMap);
// entries.add(entry);
// });
//
// return entries;
}
public File getDictDataDirectory(){
String devBaseDir = baseProperties.getDevBaseDir();
String dataFormDataClasspath = baseProperties.getDictDataClasspath();
File workPath = new File(this.getClass().getClassLoader().getResource("").getFile());
String projectRoot = workPath.getParentFile().getParentFile().getParent();
projectRoot = projectRoot.replaceAll("\\\\", "/");//对windows环境下的路径做一下替换
ValidateKit.notBlank(devBaseDir,"开发模式下,请设置显示模板资源文件路径:devBaseDir");
devBaseDir = devBaseDir.replaceAll("\\$\\{projectRoot\\}",projectRoot);
String baseDirPath = FileKit.standardFilePath(devBaseDir);
if(baseDirPath.endsWith("/"))baseDirPath = baseDirPath.substring(0,baseDirPath.length()-1);
File baseDir = new File(baseDirPath);
String relativePath = dataFormDataClasspath.replaceAll("\\.","/");
if(!relativePath.startsWith("/"))relativePath = "/"+relativePath;
return new File(baseDir+relativePath);
}
public void dbTransferFile(File dir){
List entries = getAllDictList();
entries.forEach(entry->{
File curDir = FileKit.getFile(dir,entry.getCategoryCode());
if(!curDir.exists())curDir.mkdirs();
File jsonFile = FileKit.getFile(curDir,entry.getCode()+".json");
if(jsonFile.exists())jsonFile.delete();
try {
FileKit.touchFile(jsonFile);
} catch (IOException e) {
throw new RoberException("创建json文件失败",e);
}
String jsonText = JSONKit.toJsonString(entry,true);
try {
FileKit.write(jsonFile,jsonText, Charset.defaultCharset(),false);
} catch (IOException e) {
throw new RoberException("写入json文件失败",e);
}
});
}
@Transactional
public Integer fileTransferDB() {
Integer result = 0;
// this.deleteExistedDic();
//查出从配置文件加载的数据字典
File dir = this.getDictDataDirectory();
List dicts = this.getDictEntrys(dir);
final Map fileDictMap = new HashMap<>();
dicts.forEach(dict->{
fileDictMap.put(dict.getCode(),dict);
});
//分别计算需要插入,更新,删除的三个列表
List insertDictList = ListKit.newArrayList();
List updateDictList = ListKit.newArrayList();
List deleteDictList = ListKit.newArrayList();
// List dbDictList = dataAccessor.selectList(DictEntry.class,"select * from FOWK_DICT ORDER BY SORT_CODE ASC,CODE ASC ");
List dbDictList = getAllDictList();
//以DB查出来的为基础进行处理
for(DictEntry dbEntry : dbDictList){
DictEntry fileEntry = fileDictMap.get(dbEntry.getCode());
fileDictMap.remove(dbEntry.getCode()) ;//和DB交集之后就把文件中的移除了
// if(dbEntry.getCode().equals("WorkflowTaskStatus")){
// System.out.println();
// }
String fileJsonText = JSONKit.toJsonString(fileEntry);
String dbJsonText = JSONKit.toJsonString(dbEntry);
if(fileEntry == null){ //数据库里有,但是文件里没有,则说明被删除了,则添加到删除列表中去
deleteDictList.add(dbEntry);
}else if(!fileJsonText.equals(dbJsonText)){//当文件计算出来的签名和数据库中不一至时,说明两边有有,应该做更新
updateDictList.add(fileEntry);
}
}
insertDictList.addAll(fileDictMap.values());
//需要插入的
if(!insertDictList.isEmpty()){
//把明细表中的无论有都删除了,后面保存明细时,只需要插入
dataAccessor.execute("delete from FOWK_DICT_ITEM where DICT_CODE IN (:dictCodes)",
MapKit.mapOf("dictCodes",insertDictList.stream().map(DictEntry::getCode).collect(Collectors.toList())));
for (DictEntry entry:insertDictList) {
DictPO dictPO = this.dictEntryConvertDictPO(entry);
List dictItemPOList = this.getDictItemByDictEntry(entry,dictPO);
result += dataAccessor.insert(dictPO);
dataAccessor.insert(dictItemPOList);
}
}
//需要更新的
if(!updateDictList.isEmpty()){
//把明细表中的无论有都删除了,后面保存明细时,只需要插入
dataAccessor.execute("delete from FOWK_DICT_ITEM where DICT_CODE IN (:dictCodes)",
MapKit.mapOf("dictCodes",updateDictList.stream().map(DictEntry::getCode).collect(Collectors.toList())));
for (DictEntry entry:updateDictList) {
DictPO dictPO = this.dictEntryConvertDictPO(entry);
List dictItemPOList = this.getDictItemByDictEntry(entry,dictPO);
result += dataAccessor.update(dictPO);
dataAccessor.insert(dictItemPOList);
}
}
//需要删除的
if(!deleteDictList.isEmpty()){
List deleteDictCodeList = deleteDictList.stream().map(DictEntry::getCode).collect(Collectors.toList());
result += dataAccessor.execute("delete from FOWK_DICT where CODE IN (:dictCodes)",MapKit.mapOf("dictCodes",deleteDictCodeList));
dataAccessor.execute("delete from FOWK_DICT_ITEM where DICT_CODE IN (:dictCodes)",MapKit.mapOf("dictCodes",deleteDictCodeList));
}
return result;
}
private List getDictItemByDictEntry(DictEntry dictEntry, DictPO dictPO) {
List dictItemEntries = dictEntry.getDictItemEntrys();
List dictItemPOList = dictItemEntries.stream().map(itemEntry -> this.dictItemEntryConvertDictItemPO(itemEntry,dictPO.getCode())).collect(Collectors.toList());
return dictItemPOList;
}
private DictItemPO dictItemEntryConvertDictItemPO(DictItemEntry dictItemEntry, String code) {
DictItemPO dictItemPO = new DictItemPO();
BeanKit.copyProperties(dictItemEntry,dictItemPO);
dictItemPO.setDictCode(code);
return dictItemPO;
}
private DictPO dictEntryConvertDictPO(DictEntry dictEntry) {
DictPO dictPO = new DictPO();
BeanKit.copyProperties(dictEntry,dictPO);
return dictPO;
}
private void deleteExistedDic() {
String dictSql = "DELETE FROM FOWK_DICT";
dataAccessor.execute(dictSql);
String dictItemSql = "DELETE FROM FOWK_DICT_ITEM";
dataAccessor.execute(dictItemSql);
}
public List getDictEntrys(File dir) {
List dicts = new ArrayList<>();
if (!dir.exists())
return dicts;
File[] dirs = dir.listFiles();
for (int i = 0; i < dirs.length; i++) {
if (!dirs[i].isDirectory())
continue;
File[] files = dirs[i].listFiles();
for (int fileIndex = 0; fileIndex < files.length; fileIndex++) {
if ("index.builder.json".equals(files[fileIndex].getName()))
continue;
DictEntry dictEntry = this.getDictFromFile(files[fileIndex]);
dicts.add(dictEntry);
}
}
return dicts;
}
public DictEntry getDictFromFile(File file) {
DictEntry dict = null;
if (!file.exists())
return dict;
try {
String content = FileKit.readFileToString(file, Charset.defaultCharset());
dict = JSONKit.jsonToBean(content, DictEntry.class);
} catch (IOException e) {
throw new BizException(e.getMessage());
}
return dict;
}
public void clearDictCache() {
dictService.clearCacheAll();
}
}