许鹏程
2023-05-25 213cc37cbf0b2515a4de56cc1e01813211bad183
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
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);
        });
    }
}