张世豪
15 小时以前 a541fbdc8812337de120aad3792a2033a5dd7afe
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package user;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
 
/**
 * 用户属性管理类
 * 负责初始化加载user.properties中的属性值,并提供获取和修改功能
 */
public class Usrdell {
    private static final String PROPERTIES_FILE = "user.properties";
    private static Properties userProperties;
    
    // 属性键名常量
    private static final String KEY_EMAIL = "email";
    private static final String KEY_LANGUAGE = "language";
    private static final String KEY_LAST_LOGIN_TIME = "lastLoginTime";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_REGISTRATION_TIME = "registrationTime";
    private static final String KEY_STATUS = "status";
    private static final String KEY_USER_ID = "userId";
    private static final String KEY_USER_NAME = "userName";
    private static final String KEY_REMEMBER_PASSWORD = "rememberPassword";
 
    /**
     * 初始化方法:从user.properties文件加载所有属性值到内存
     */
    public static void initialize() {
        synchronized (Usrdell.class) {
            userProperties = new Properties();
            try (FileInputStream input = new FileInputStream(PROPERTIES_FILE)) {
                userProperties.load(input);
                System.out.println("用户属性初始化成功!");
            } catch (IOException e) {
                System.err.println("初始化失败,文件未找到或读取错误: " + e.getMessage());
                // 如果文件不存在,创建默认属性
                userProperties = new Properties();
            }
            
            // 确保rememberPassword属性存在,如果不存在则设置为默认值"0"
            if (!userProperties.containsKey(KEY_REMEMBER_PASSWORD)) {
                userProperties.setProperty(KEY_REMEMBER_PASSWORD, "0");
                // 保存到文件
                try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
                    userProperties.store(output, "Updated User Properties");
                } catch (IOException e) {
                    System.err.println("保存默认rememberPassword属性失败: " + e.getMessage());
                }
            }
        }
    }
 
    /**
     * 初始化并获取所有属性值,返回Usr对象
     * @return 包含所有属性值的Usr对象
     */
    public static Usr initializeAndGetAll() {
        if (userProperties == null) {
            initialize();
        }
        
        Usr usr = new Usr();
        usr.setEmail(getProperty(KEY_EMAIL));
        usr.setLanguage(getProperty(KEY_LANGUAGE));
        usr.setLastLoginTime(getProperty(KEY_LAST_LOGIN_TIME));
        usr.setPassword(getProperty(KEY_PASSWORD));
        usr.setRegistrationTime(getProperty(KEY_REGISTRATION_TIME));
        usr.setStatus(getProperty(KEY_STATUS));
        usr.setUserId(getProperty(KEY_USER_ID));
        usr.setUserName(getProperty(KEY_USER_NAME));
        usr.setRememberPassword(getProperty(KEY_REMEMBER_PASSWORD));
        
        return usr;
    }
 
    /**
     * 获取指定属性的值
     * @param key 属性键名
     * @return 属性值,如果不存在返回null
     */
    public static String getProperty(String key) {
        if (userProperties == null) {
            initialize();
        }
        String value = userProperties.getProperty(key);
        if (value == null || value.trim().isEmpty() || "-1".equals(value.trim())) {
            return null;
        }
        return value.trim();
    }
 
    /**
     * 更新属性值并保存到文件
     * @param key 属性键名
     * @param value 属性值
     */
    public static void updateProperty(String key, String value) {
        synchronized (Usrdell.class) {
            if (userProperties == null) {
                initialize();
            }
            
            if (value == null || value.trim().isEmpty()) {
                userProperties.setProperty(key, "-1");
            } else {
                userProperties.setProperty(key, value.trim());
            }
            
            try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
                userProperties.store(output, "Updated User Properties");
            } catch (IOException e) {
                System.err.println("更新失败,文件写入错误: " + e.getMessage());
            }
        }
    }
 
    /**
     * 获取用户邮箱
     * @return 用户邮箱
     */
    public static String getEmail() {
        return getProperty(KEY_EMAIL);
    }
 
    /**
     * 设置用户邮箱
     * @param email 用户邮箱
     */
    public static void setEmail(String email) {
        updateProperty(KEY_EMAIL, email);
    }
 
    /**
     * 获取用户邮箱
     * @return 用户邮箱
     */
    public static String getUserEmail() {
        return getEmail();
    }
 
    /**
     * 获取语言设置
     * @return 语言设置
     */
    public static String getLanguage() {
        return getProperty(KEY_LANGUAGE);
    }
 
    /**
     * 设置语言设置
     * @param language 语言设置
     */
    public static void setLanguage(String language) {
        updateProperty(KEY_LANGUAGE, language);
    }
 
    /**
     * 获取最后登录时间
     * @return 最后登录时间
     */
    public static String getLastLoginTime() {
        return getProperty(KEY_LAST_LOGIN_TIME);
    }
 
    /**
     * 设置最后登录时间
     * @param lastLoginTime 最后登录时间
     */
    public static void setLastLoginTime(String lastLoginTime) {
        updateProperty(KEY_LAST_LOGIN_TIME, lastLoginTime);
    }
 
    /**
     * 获取密码
     * @return 密码
     */
    public static String getPassword() {
        return getProperty(KEY_PASSWORD);
    }
 
    /**
     * 设置密码
     * @param password 密码
     */
    public static void setPassword(String password) {
        updateProperty(KEY_PASSWORD, password);
    }
 
    /**
     * 获取注册时间
     * @return 注册时间
     */
    public static String getRegistrationTime() {
        return getProperty(KEY_REGISTRATION_TIME);
    }
 
    /**
     * 设置注册时间
     * @param registrationTime 注册时间
     */
    public static void setRegistrationTime(String registrationTime) {
        updateProperty(KEY_REGISTRATION_TIME, registrationTime);
    }
 
    /**
     * 获取用户状态
     * @return 用户状态
     */
    public static String getStatus() {
        return getProperty(KEY_STATUS);
    }
 
    /**
     * 设置用户状态
     * @param status 用户状态
     */
    public static void setStatus(String status) {
        updateProperty(KEY_STATUS, status);
    }
 
    /**
     * 获取用户ID
     * @return 用户ID
     */
    public static String getUserId() {
        return getProperty(KEY_USER_ID);
    }
 
    /**
     * 设置用户ID
     * @param userId 用户ID
     */
    public static void setUserId(String userId) {
        updateProperty(KEY_USER_ID, userId);
    }
 
    /**
     * 获取用户名
     * @return 用户名
     */
    public static String getUserName() {
        return getProperty(KEY_USER_NAME);
    }
 
    /**
     * 设置用户名
     * @param userName 用户名
     */
    public static void setUserName(String userName) {
        updateProperty(KEY_USER_NAME, userName);
    }
 
    /**
     * 获取是否记住登录密码
     * @return 是否记住登录密码(1表示记住,0表示不记住),如果未设置则返回"0"
     */
    public static String getRememberPassword() {
        String value = getProperty(KEY_REMEMBER_PASSWORD);
        return value != null ? value : "0"; // 默认返回"0"表示不记住
    }
 
    /**
     * 设置是否记住登录密码
     * @param rememberPassword 是否记住登录密码(1表示记住,0表示不记住)
     */
    public static void setRememberPassword(String rememberPassword) {
        updateProperty(KEY_REMEMBER_PASSWORD, rememberPassword);
    }
 
    /**
     * 批量更新用户属性
     * @param usr 包含要更新属性值的Usr对象
     */
    public static void updateAll(Usr usr) {
        if (usr == null) {
            return;
        }
        
        synchronized (Usrdell.class) {
            if (userProperties == null) {
                initialize();
            }
            
            if (usr.getEmail() != null) {
                userProperties.setProperty(KEY_EMAIL, usr.getEmail());
            }
            if (usr.getLanguage() != null) {
                userProperties.setProperty(KEY_LANGUAGE, usr.getLanguage());
            }
            if (usr.getLastLoginTime() != null) {
                userProperties.setProperty(KEY_LAST_LOGIN_TIME, usr.getLastLoginTime());
            }
            if (usr.getPassword() != null) {
                userProperties.setProperty(KEY_PASSWORD, usr.getPassword());
            }
            if (usr.getRegistrationTime() != null) {
                userProperties.setProperty(KEY_REGISTRATION_TIME, usr.getRegistrationTime());
            }
            if (usr.getStatus() != null) {
                userProperties.setProperty(KEY_STATUS, usr.getStatus());
            }
            if (usr.getUserId() != null) {
                userProperties.setProperty(KEY_USER_ID, usr.getUserId());
            }
            if (usr.getUserName() != null) {
                userProperties.setProperty(KEY_USER_NAME, usr.getUserName());
            }
            if (usr.getRememberPassword() != null) {
                userProperties.setProperty(KEY_REMEMBER_PASSWORD, usr.getRememberPassword());
            }
            
            try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
                userProperties.store(output, "Updated User Properties");
            } catch (IOException e) {
                System.err.println("批量更新失败,文件写入错误: " + e.getMessage());
            }
        }
    }
}