package com.product.admin.service;
|
|
import com.product.admin.config.CmnCode;
|
import com.product.admin.config.CmnConst;
|
import com.product.admin.service.idel.ISystemParamSettingService;
|
import com.product.common.io.FileUtils;
|
import com.product.common.lang.StringUtils;
|
import com.product.core.dao.BaseDao;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.exception.BaseException;
|
import com.product.core.service.support.AbstractBaseService;
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
import com.product.core.transfer.Transactional;
|
import com.product.file.config.FileCode;
|
import com.product.module.sys.entity.SystemUser;
|
import com.product.util.BaseUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.io.*;
|
import java.text.SimpleDateFormat;
|
import java.util.Properties;
|
|
/**
|
* Copyright © 6c
|
*
|
* @Date: 2021-08-06 09:34
|
* @Author: 6c
|
* @Description:
|
*/
|
@Service
|
public class SystemParamSettingService extends AbstractBaseService implements ISystemParamSettingService {
|
|
@Autowired
|
public BaseDao baseDao;
|
|
/**
|
* 保存
|
* @param fse
|
*/
|
@Transactional
|
@Override
|
public String saveSetting(FieldSetEntity fse) {
|
SystemUser curUser = SpringMVCContextHolder.getCurrentUser();
|
// 保存数据
|
if (!StringUtils.isEmpty(fse.getString("operate_type"))) {
|
String uuid = fse.getUUID();
|
if (StringUtils.isEmpty(uuid)) {
|
BaseUtil.createCreatorAndCreationTime(curUser, fse);
|
} else {
|
BaseUtil.updatedRegeneratorAndUpdateTime(curUser, fse);
|
}
|
}
|
|
if(null!=fse.getString("uuid")){
|
FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_PARAMS_SETTING, fse.getString("uuid"), false);
|
if (!fs.getString(CmnConst.PARAM_TYPE).equals(fse.getString(CmnConst.PARAM_TYPE))){
|
String type = fs.getString(CmnConst.PARAM_TYPE);
|
String key = fs.getString(CmnConst.PARAM_KEY);
|
syncProperties(type, key, null);
|
}
|
}
|
baseDao.saveFieldSetEntity(fse);
|
String type = fse.getString(CmnConst.PARAM_TYPE);
|
String key = fse.getString(CmnConst.PARAM_KEY);
|
String value = fse.getString(CmnConst.PARAM_KEY_VALUE);
|
syncProperties(type, key, value);
|
|
return fse.getUUID();
|
}
|
|
/**
|
* 删除
|
* @param fse
|
*/
|
@Transactional
|
@Override
|
public void deleteSetting(FieldSetEntity fse) {
|
FieldSetEntity recordFse = baseDao.getFieldSetEntity(fse.getTableName(), fse.getUUID(), false);
|
if (recordFse == null) {
|
return;
|
}
|
|
String type = recordFse.getString(CmnConst.PARAM_TYPE);
|
String key = recordFse.getString(CmnConst.PARAM_KEY);
|
|
syncProperties(type, key, null);
|
|
baseDao.delete(fse.getTableName(), new Object[]{fse.getUUID()});
|
}
|
|
/**
|
* 同步到properties
|
* @param type
|
* @param key
|
* @param value
|
*/
|
private void syncProperties(String type, String key, String value) {
|
String filePath = System.getProperty("user.dir") + "/src/main/resources/application.properties";
|
String sysType = getProperties(filePath).getProperty("spring.profiles.active");
|
String fileName = "";
|
if ("0".equals(type)) {
|
fileName = "application-" + sysType + ".properties";
|
} else if ("1".equals(type)) {
|
fileName = "file.properties";
|
} else if ("2".equals(type)) {
|
fileName = "cache-" + sysType + ".properties";
|
}
|
|
filePath = System.getProperty("user.dir") + "/src/main/resources/" + fileName;
|
String newContent = replaceAimKeyValue(filePath, key, value);
|
|
try {
|
File file = new File(filePath);
|
FileUtils.write(file, newContent, "utf-8");
|
} catch (IOException e) {
|
throw new BaseException(CmnCode.WRITE_FILE_FAIL.getValue(), CmnCode.WRITE_FILE_FAIL.getText(), e);
|
}
|
}
|
|
/**
|
* 替换指定key的value
|
* @param filePath
|
* @param key
|
* @param value
|
* @return 文件内容
|
*/
|
private String replaceAimKeyValue(String filePath, String key, String value) {
|
StringBuffer sb = new StringBuffer();
|
//日常指定读取的文件
|
File file = new File(filePath);
|
if (!file.exists()) {
|
System.out.println("文件不存在");
|
} else {
|
//初始化读取操作的br对象
|
BufferedReader br = null;
|
try {
|
//调用字节流,设置读取文件的编码格式
|
InputStreamReader ir = new InputStreamReader(new FileInputStream(filePath), "utf-8");
|
br = new BufferedReader(ir);
|
|
String rowContent = null;
|
boolean existFlag = false;
|
while ((rowContent = br.readLine()) != null) {
|
rowContent = rowContent.trim();
|
if (!StringUtils.isEmpty(rowContent) && !rowContent.startsWith("#") && rowContent.startsWith(key + "=")) {
|
if (value == null) {
|
// 删除
|
rowContent = null;
|
} else {
|
// 修改
|
rowContent = key + "=" + value;
|
}
|
existFlag = true;
|
}
|
if (rowContent == null) {
|
continue;
|
}
|
sb.append(rowContent).append("\n");
|
}
|
if (!existFlag) {
|
// 新增
|
sb.append(key).append("=").append(value).append("\n");
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
br.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
return sb.toString();
|
}
|
|
/**
|
* 获取指定配置文件中的信息
|
* @return
|
*/
|
private Properties getProperties(String path) {
|
Properties properties = new Properties();
|
try (FileReader reader = new FileReader(path);) {
|
properties.load(reader);
|
return properties;
|
} catch (IOException e) {
|
throw new BaseException(FileCode.LOAD_FTP_PROPERTIES_FAIL.getValue(), FileCode.LOAD_FTP_PROPERTIES_FAIL.getText(), e);
|
}
|
}
|
|
/**
|
* 初始化properties文件内容到数据库
|
*/
|
public void initSetting() {
|
String filePath = System.getProperty("user.dir") + "/src/main/resources/application.properties";
|
String sysType = getProperties(filePath).getProperty("spring.profiles.active");
|
// 基础设置
|
String fileName = "application-" + sysType + ".properties";
|
filePath = System.getProperty("user.dir") + "/src/main/resources/" + fileName;
|
initSinglePropertiesFile(filePath, "0");
|
|
// 文件设置
|
fileName = "file.properties";
|
filePath = System.getProperty("user.dir") + "/src/main/resources/" + fileName;
|
initSinglePropertiesFile(filePath, "1");
|
|
// 缓存设置
|
fileName = "cache-" + sysType + ".properties";
|
filePath = System.getProperty("user.dir") + "/src/main/resources/" + fileName;
|
initSinglePropertiesFile(filePath, "2");
|
}
|
/**
|
* 获取类型列表
|
*/
|
public DataTableEntity getTypeList(){
|
return baseDao.listTable(CmnConst.PRODUCT_SYS_DICT,"is_used=1 and dict_name='product_SYS_param_type'");
|
}
|
/**
|
* 根据类型获取数据
|
*/
|
public DataTableEntity getTypeData(FieldSetEntity fse){
|
return baseDao.listTable(CmnConst.PRODUCT_SYS_PARAMS_SETTING,"param_type=?",new String[]{fse.getString("type")});
|
}
|
/**
|
* 初始化单个文件到数据库
|
* @param path
|
* @param type
|
*/
|
private void initSinglePropertiesFile(String path, String type) {
|
Properties properties = getProperties(path);
|
properties.forEach((k, v) -> {
|
FieldSetEntity fse = new FieldSetEntity();
|
fse.setTableName(CmnConst.PRODUCT_SYS_PARAMS_SETTING);
|
fse.setValue("param_type", type);
|
fse.setValue("param_key", k);
|
fse.setValue("param_key_value", v);
|
fse.setValue("param_name", k);
|
baseDao.saveFieldSetEntity(fse);
|
});
|
}
|
}
|