blob: 580f9e8313cb19629e4663166a7f7a49ce00722e [file] [log] [blame]
rootcd436562025-05-08 14:09:19 +00001package database;
2
TRM-coding87c24972025-06-07 14:05:29 +08003import java.util.Calendar;
4import java.util.UUID;
root0dbc9812025-05-19 04:41:57 +00005import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
TRM-coding87c24972025-06-07 14:05:29 +08008import java.util.Date;
root0dbc9812025-05-19 04:41:57 +00009
10import javax.persistence.EntityManager;
TRM-coding87c24972025-06-07 14:05:29 +080011import javax.persistence.EntityManagerFactory;
12import javax.persistence.EntityTransaction;
Raverf79fdb62025-06-03 06:02:49 +000013import javax.persistence.PersistenceContext;
root678fd3e2025-06-03 14:20:20 +000014import javax.persistence.Persistence;
root0dbc9812025-05-19 04:41:57 +000015
16import com.querydsl.jpa.impl.JPAQuery;
TRM-coding87c24972025-06-07 14:05:29 +080017import com.querydsl.core.Tuple;
root0dbc9812025-05-19 04:41:57 +000018
19import entity.BegInfo;
20import entity.Notice;
21import entity.Post;
TRM-coding87c24972025-06-07 14:05:29 +080022import entity.PostReply;
root0dbc9812025-05-19 04:41:57 +000023import entity.Profile;
TRM-coding87c24972025-06-07 14:05:29 +080024import entity.QBegInfo;
root0dbc9812025-05-19 04:41:57 +000025import entity.QNotice;
TRM-coding87c24972025-06-07 14:05:29 +080026import entity.QProfile;
root0dbc9812025-05-19 04:41:57 +000027import entity.QSeed;
TRM-coding87c24972025-06-07 14:05:29 +080028import entity.QSubmitSeed;
root0dbc9812025-05-19 04:41:57 +000029import entity.QUser;
30import entity.QUserPT;
31import entity.QUserStar;
TRM-codingcdfe5482025-06-06 17:31:01 +080032import entity.QUserInvite;
TRM-coding87c24972025-06-07 14:05:29 +080033import entity.QUserVotes;
rhj46f62c42025-06-06 23:24:10 +080034import entity.QPost;
root0dbc9812025-05-19 04:41:57 +000035import entity.Seed;
36import entity.User;
37import entity.UserPT;
38import entity.UserStar;
Raverf79fdb62025-06-03 06:02:49 +000039import entity.config;
rhj5b69b7e2025-06-07 01:28:08 +080040import entity.PostReply;
41import entity.QPostReply;
root0dbc9812025-05-19 04:41:57 +000042
TRM-coding87c24972025-06-07 14:05:29 +080043import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
root0dbc9812025-05-19 04:41:57 +000046public class Database1 implements DataManagerInterface {
Raverf79fdb62025-06-03 06:02:49 +000047 @PersistenceContext
48 private final EntityManager entitymanager;
TRM-coding87c24972025-06-07 14:05:29 +080049 private static final Logger logger = LoggerFactory.getLogger(Database1.class);
50 private EntityManagerFactory emf;
root678fd3e2025-06-03 14:20:20 +000051 public Database1() {
Raverf79fdb62025-06-03 06:02:49 +000052 config cfg = new config();
53 Map<String,Object> props = new HashMap<>();
54 props.put("javax.persistence.jdbc.url",
55 "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database);
root678fd3e2025-06-03 14:20:20 +000056 props.put("javax.persistence.jdbc.user", cfg.SqlUsername);
57 props.put("javax.persistence.jdbc.password", cfg.SqlPassword);
Raverf79fdb62025-06-03 06:02:49 +000058 this.entitymanager = Persistence.createEntityManagerFactory("myPersistenceUnit", props).createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +080059 // this.emf=entitymanager;
root678fd3e2025-06-03 14:20:20 +000060 }
TRM-codingcdfe5482025-06-06 17:31:01 +080061 @Override
62 public String LoginUser(User userinfo){
63 try {
64 // 检查传入的参数是否合法
65 if (userinfo == null || userinfo.password == null) {
66 return null;
67 }
68
TRM-codingcdfe5482025-06-06 17:31:01 +080069 boolean hasEmail = userinfo.email != null && !userinfo.email.isEmpty();
70
71 // 如果两个都为空或两个都不为空,返回null
rhj46f62c42025-06-06 23:24:10 +080072 if (!hasEmail) {
TRM-codingcdfe5482025-06-06 17:31:01 +080073 return null;
74 }
75
76 JPAQuery<User> query = new JPAQuery<>(entitymanager);
77 QUser u = QUser.user;
78 User foundUser = null;
79
rhj46f62c42025-06-06 23:24:10 +080080
81 // 通过邮箱和密码查找
82 foundUser = query.select(u)
83 .from(u)
84 .where(u.email.eq(userinfo.email)
85 .and(u.password.eq(userinfo.password)))
86 .fetchOne();
TRM-codingcdfe5482025-06-06 17:31:01 +080087
88 // 如果找到匹配的用户则返回用户ID,否则返回null
89 return foundUser != null ? foundUser.userid : null;
90
91 } catch (Exception e) {
92 e.printStackTrace();
93 return null;
94 }
95 }
root0dbc9812025-05-19 04:41:57 +000096
97 // 返回状态:0 success,1 邮箱重复,2其他原因
98 @Override
Raverf79fdb62025-06-03 06:02:49 +000099 public int RegisterUser(User userinfo){
100 try{
TRM-codingcdfe5482025-06-06 17:31:01 +0800101 // 首先检查该邮箱是否在UserInvite表中被邀请过
102 JPAQuery<String> inviteQuery = new JPAQuery<>(entitymanager);
103 QUserInvite ui = QUserInvite.userInvite;
104 List<String> invitedEmails = inviteQuery.select(ui.inviterEmail).from(ui).fetch();
105
106 // 如果邮箱不在被邀请列表中,拒绝注册
107 if(!invitedEmails.contains(userinfo.email)){
108 return 2; // 未被邀请
109 }
110
111 // 检查邮箱是否已在User表中存在
Raverf79fdb62025-06-03 06:02:49 +0000112 JPAQuery<String> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000113 QUser u = QUser.user;
114 List<String> allEmails = query.select(u.email).from(u).fetch();
115
Raverf79fdb62025-06-03 06:02:49 +0000116 if(allEmails.contains(userinfo.email)){
TRM-codingcdfe5482025-06-06 17:31:01 +0800117 return 1; // 邮箱重复
root0dbc9812025-05-19 04:41:57 +0000118 }
root0dbc9812025-05-19 04:41:57 +0000119
rhj46f62c42025-06-06 23:24:10 +0800120 entitymanager.getTransaction().begin();
121 entitymanager.persist(userinfo);
122 entitymanager.getTransaction().commit();
123 return 0; // 注册成功
TRM-coding93f7be82025-06-06 22:29:02 +0800124
rhj46f62c42025-06-06 23:24:10 +0800125 }catch(Exception e){
126 e.printStackTrace();
127 if (entitymanager.getTransaction().isActive()) {
128 entitymanager.getTransaction().rollback();
129 }
130 return 4;
131 }
TRM-coding93f7be82025-06-06 22:29:02 +0800132
root0dbc9812025-05-19 04:41:57 +0000133 }
134
135 // 返回状态:0 success,1 不存在,2其他原因
136 @Override
Raverf79fdb62025-06-03 06:02:49 +0000137 public int UpdateInformation(User userinfo){
root0dbc9812025-05-19 04:41:57 +0000138 try {
139 if (userinfo.userid == null) {
140 return 2; // userid为null直接返回错误
141 }
rhjc6a4ee02025-06-06 00:45:18 +0800142
143 entitymanager.getTransaction().begin();
144
Raverf79fdb62025-06-03 06:02:49 +0000145 JPAQuery<User> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000146 QUser u = QUser.user;
147 User updateUser = query.select(u).from(u).where(u.userid.eq(userinfo.userid)).fetchOne();
148
Raverf79fdb62025-06-03 06:02:49 +0000149 if(updateUser == null){
rhjc6a4ee02025-06-06 00:45:18 +0800150 entitymanager.getTransaction().rollback();
root0dbc9812025-05-19 04:41:57 +0000151 return 1;
152 }
153 // 只更新需要的字段,避免破坏持久化状态和关联关系
rhjc6a4ee02025-06-06 00:45:18 +0800154 if (userinfo.email != null) updateUser.email = userinfo.email;
155 if (userinfo.username != null) updateUser.username = userinfo.username;
156 if (userinfo.password != null) updateUser.password = userinfo.password;
157 if (userinfo.sex != null) updateUser.sex = userinfo.sex;
158 if (userinfo.school != null) updateUser.school = userinfo.school;
159 if (userinfo.pictureurl != null) updateUser.pictureurl = userinfo.pictureurl;
160 if (userinfo.profile != null) updateUser.profile = userinfo.profile;
root0dbc9812025-05-19 04:41:57 +0000161 updateUser.accountstate = userinfo.accountstate;
162 updateUser.invitetimes = userinfo.invitetimes;
163 // 如有其他字段也一并赋值
Raverf79fdb62025-06-03 06:02:49 +0000164 entitymanager.merge(updateUser);
rhjc6a4ee02025-06-06 00:45:18 +0800165 entitymanager.getTransaction().commit();
root0dbc9812025-05-19 04:41:57 +0000166 return 0;
167 } catch (Exception e) {
168 e.printStackTrace();
rhjc6a4ee02025-06-06 00:45:18 +0800169 if (entitymanager.getTransaction().isActive()) {
170 entitymanager.getTransaction().rollback();
171 }
root0dbc9812025-05-19 04:41:57 +0000172 return 2;
173 }
Raverf79fdb62025-06-03 06:02:49 +0000174
root0dbc9812025-05-19 04:41:57 +0000175 }
176
177 // 返回用户的全部基本信息
178 @Override
Raverf79fdb62025-06-03 06:02:49 +0000179 public User GetInformation(String userid){
180 User user = entitymanager.find(User.class, userid);
181 return user;
root0dbc9812025-05-19 04:41:57 +0000182 }
183
184 //返回用户的全部pt站信息
185 @Override
Raverf79fdb62025-06-03 06:02:49 +0000186 public UserPT GetInformationPT(String userid){
187 UserPT userPT = entitymanager.find(UserPT.class, userid);
188 return userPT;
root0dbc9812025-05-19 04:41:57 +0000189 }
190
191 //返回状态:0 success,1 邮箱重复,2其他原因
192 @Override
Raverf79fdb62025-06-03 06:02:49 +0000193 public int UpdateInformationPT(UserPT userinfo){
194 try{
195 JPAQuery<UserPT> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000196 QUserPT u = QUserPT.userPT;
197 UserPT userPT = query.select(u).from(u).where(u.userid.eq(userinfo.userid)).fetchOne();
Raverf79fdb62025-06-03 06:02:49 +0000198
199 if(userPT == null){
root0dbc9812025-05-19 04:41:57 +0000200 return 1;
201 }
202 userPT = userinfo;
Raverf79fdb62025-06-03 06:02:49 +0000203 entitymanager.merge(userPT);
root0dbc9812025-05-19 04:41:57 +0000204 return 0;
205
Raverf79fdb62025-06-03 06:02:49 +0000206 }catch(Exception e){
root0dbc9812025-05-19 04:41:57 +0000207 e.printStackTrace();
208 return 2;
209 }
210 }
211
212 //返回状态:0 success,1 id重复,2其他原因
213 @Override
Raverf79fdb62025-06-03 06:02:49 +0000214 public int RegisterUserPT(UserPT userinfo){
root0dbc9812025-05-19 04:41:57 +0000215 try {
rhj46f62c42025-06-06 23:24:10 +0800216 entitymanager.getTransaction().begin();
217
Raverf79fdb62025-06-03 06:02:49 +0000218 JPAQuery<UserPT> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000219 QUserPT u = QUserPT.userPT;
220 UserPT checkUserPT = query.select(u).from(u).where(u.userid.eq(userinfo.userid)).fetchOne();
221 if (checkUserPT != null) {
rhj46f62c42025-06-06 23:24:10 +0800222 entitymanager.getTransaction().rollback();
root0dbc9812025-05-19 04:41:57 +0000223 return 1;
224 }
rhj46f62c42025-06-06 23:24:10 +0800225
Raverf79fdb62025-06-03 06:02:49 +0000226 entitymanager.persist(userinfo);
rhj46f62c42025-06-06 23:24:10 +0800227 entitymanager.getTransaction().commit();
root0dbc9812025-05-19 04:41:57 +0000228 return 0;
229 } catch (Exception e) {
230 e.printStackTrace();
rhj46f62c42025-06-06 23:24:10 +0800231 if (entitymanager.getTransaction().isActive()) {
232 entitymanager.getTransaction().rollback();
233 }
root0dbc9812025-05-19 04:41:57 +0000234 return 2;
235 }
236 }
237
238 //返回种子的全部信息
239 @Override
Raverf79fdb62025-06-03 06:02:49 +0000240 public Seed GetSeedInformation(String seedid){
241 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
242 QSeed s = QSeed.seed;
243 Seed seed = query.select(s).from(s).where(s.seedid.eq(seedid)).fetchOne();
244 return seed;
root0dbc9812025-05-19 04:41:57 +0000245 }
246
Raveraae06122025-06-05 08:13:35 +0000247 @Override
248 public Seed[] GetSeedListByTag(String tag){
249 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
250 QSeed s = QSeed.seed;
251 List<Seed> seeds = query.select(s).from(s).where(s.seedtag.eq(tag)).fetch();
252 return seeds.toArray(new Seed[0]);
253 }
254
rhjc6a4ee02025-06-06 00:45:18 +0800255 @Override
256 public Seed[] GetSeedListByUser(String userid){
257 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
258 QSeed s = QSeed.seed;
259 List<Seed> seeds = query.select(s).from(s).where(s.seeduserid.eq(userid)).fetch();
260 return seeds.toArray(new Seed[0]);
261 }
262
263 @Override
264 public int DeleteSeed(String seedid){
265 try {
266 entitymanager.getTransaction().begin();
267 Seed seed = entitymanager.find(Seed.class, seedid);
268 if (seed == null) {
269 entitymanager.getTransaction().rollback();
270 return 1; // 种子不存在
271 }
272 entitymanager.remove(seed);
273 entitymanager.getTransaction().commit();
274 return 0; // 成功删除
275 } catch (Exception e) {
276 e.printStackTrace();
277 if (entitymanager.getTransaction().isActive()) {
278 entitymanager.getTransaction().rollback();
279 }
280 return 2; // 其他错误
281 }
282 }
283
root0dbc9812025-05-19 04:41:57 +0000284 //添加一个新的种子,0成功,其他失败信息待定;
285 @Override
Raverf79fdb62025-06-03 06:02:49 +0000286 public int RegisterSeed(Seed seedinfo){
root0dbc9812025-05-19 04:41:57 +0000287 try {
Raveraae06122025-06-05 08:13:35 +0000288 entitymanager.getTransaction().begin();
Raverf79fdb62025-06-03 06:02:49 +0000289 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000290 QSeed s = QSeed.seed;
291 Seed seed = query.select(s).from(s).where(s.seedid.eq(seedinfo.seedid)).fetchOne();
Raveraae06122025-06-05 08:13:35 +0000292 User user = entitymanager.find(User.class, seedinfo.seeduserid);
293 if (user == null) {
294 entitymanager.getTransaction().rollback();
295 return 2; // 用户不存在
296 }
rhjc6a4ee02025-06-06 00:45:18 +0800297 seedinfo.user = user; // 设置种子的用户关联
root0dbc9812025-05-19 04:41:57 +0000298 if (seed != null) {
Raveraae06122025-06-05 08:13:35 +0000299 entitymanager.getTransaction().rollback();
root0dbc9812025-05-19 04:41:57 +0000300 return 1;
301 }
Raverf79fdb62025-06-03 06:02:49 +0000302 entitymanager.persist(seedinfo);
Raveraae06122025-06-05 08:13:35 +0000303 entitymanager.getTransaction().commit();
root0dbc9812025-05-19 04:41:57 +0000304 return 0;
305 } catch (Exception e) {
306 e.printStackTrace();
Raveraae06122025-06-05 08:13:35 +0000307 if (entitymanager.getTransaction().isActive()) {
308 entitymanager.getTransaction().rollback();
309 }
root0dbc9812025-05-19 04:41:57 +0000310 return 2;
311 }
312 }
313
314 //接收新的种子然后更新其全部属性
315 @Override
Raverf79fdb62025-06-03 06:02:49 +0000316 public int UpdateSeed(Seed seedinfo){
root0dbc9812025-05-19 04:41:57 +0000317 try {
Raverf79fdb62025-06-03 06:02:49 +0000318 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000319 QSeed s = QSeed.seed;
320 Seed seed = query.select(s).from(s).where(s.seedid.eq(seedinfo.seedid)).fetchOne();
321 if (seed == null) {
322 return 1;
323 }
324 seed = seedinfo;
Raverf79fdb62025-06-03 06:02:49 +0000325 entitymanager.merge(seed);
root0dbc9812025-05-19 04:41:57 +0000326 return 0;
327 } catch (Exception e) {
328 e.printStackTrace();
329 return 2;
330 }
331 }
332
333 //传入搜索的关键词或句子,返回搜索到的种子信息(按照公共字符数量排序)
334 @Override
Raverf79fdb62025-06-03 06:02:49 +0000335 public Seed[] SearchSeed(String userQ){
336 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
337 QSeed s = QSeed.seed;
338 List<Seed> seeds = query.select(s).from(s).fetch();
root0dbc9812025-05-19 04:41:57 +0000339
Raverf79fdb62025-06-03 06:02:49 +0000340 if (seeds == null || userQ == null || userQ.trim().isEmpty()) {
root0dbc9812025-05-19 04:41:57 +0000341 return seeds.toArray(new Seed[0]);
342 }
Raverf79fdb62025-06-03 06:02:49 +0000343
344 String processedQuery = userQ.toLowerCase().trim();
345 Map<Seed, Integer> seedCountMap = new HashMap<>();
346 for(Seed seed : seeds){
347 String title = seed.title.toLowerCase().trim();
348 int count = countCommonCharacter(processedQuery, title);
349 seedCountMap.put(seed, count);
350 }
351 seeds.sort((s1, s2) -> {
352 int count1 = seedCountMap.getOrDefault(s1, 0);
353 int count2 = seedCountMap.getOrDefault(s2, 0);
354 return Integer.compare(count2, count1);
355 });
356
357 return seeds.toArray(new Seed[0]);
root0dbc9812025-05-19 04:41:57 +0000358 }
359
360 //计算字符串公共字符数量
Raverf79fdb62025-06-03 06:02:49 +0000361 private int countCommonCharacter(String str1, String str2){
root0dbc9812025-05-19 04:41:57 +0000362 Map<Character, Integer> map1 = new HashMap<>();
363 Map<Character, Integer> map2 = new HashMap<>();
364
Raverf79fdb62025-06-03 06:02:49 +0000365 for(char c : str1.toCharArray()){
root0dbc9812025-05-19 04:41:57 +0000366 if (!Character.isWhitespace(c)) {
367 map1.put(c, map1.getOrDefault(c, 0) + 1);
368 }
369 }
370
Raverf79fdb62025-06-03 06:02:49 +0000371 for(char c : str2.toCharArray()){
root0dbc9812025-05-19 04:41:57 +0000372 if (!Character.isWhitespace(c)) {
373 map2.put(c, map2.getOrDefault(c, 0) + 1);
374 }
375 }
376
377 int res = 0;
Raverf79fdb62025-06-03 06:02:49 +0000378 for(char c : map1.keySet()){
root0dbc9812025-05-19 04:41:57 +0000379 if (map2.containsKey(c)) {
380 res += Math.min(map1.get(c), map2.get(c));
381 }
Raverf79fdb62025-06-03 06:02:49 +0000382
root0dbc9812025-05-19 04:41:57 +0000383 }
384 return res;
385 }
386
387 //返回状态:0 success,1 重复,2其他原因
388 @Override
Raverf79fdb62025-06-03 06:02:49 +0000389 public int AddNotice(Notice notice){
root0dbc9812025-05-19 04:41:57 +0000390 try {
Raverf79fdb62025-06-03 06:02:49 +0000391 JPAQuery<Notice> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000392 QNotice n = QNotice.notice;
393 Notice checkNotice = query.select(n).from(n).where(n.noticeid.eq(notice.noticeid)).fetchOne();
394 if (checkNotice != null) {
395 return 1;
396 }
Raverf79fdb62025-06-03 06:02:49 +0000397
398 entitymanager.persist(notice);
root0dbc9812025-05-19 04:41:57 +0000399 return 0;
400 } catch (Exception e) {
401 e.printStackTrace();
402 return 2;
403 }
Raverf79fdb62025-06-03 06:02:49 +0000404
root0dbc9812025-05-19 04:41:57 +0000405 }
406
407 //返回状态:0 success,1 不存在,2其他原因
408 @Override
Raverf79fdb62025-06-03 06:02:49 +0000409 public boolean UpdateNotice(Notice notice){
root0dbc9812025-05-19 04:41:57 +0000410 try {
Raverf79fdb62025-06-03 06:02:49 +0000411 Notice oldNotice = entitymanager.find(Notice.class, notice.noticeid);
root0dbc9812025-05-19 04:41:57 +0000412 if (oldNotice == null) {
413 return false;
414 }
415 oldNotice = notice;
Raverf79fdb62025-06-03 06:02:49 +0000416 entitymanager.merge(oldNotice);
root0dbc9812025-05-19 04:41:57 +0000417 return true;
418 } catch (Exception e) {
419 e.printStackTrace();
420 return false;
421 }
422 }
423
424 //删除公告,返回状态:0 success,1 不存在,2其他原因
425 @Override
Raverf79fdb62025-06-03 06:02:49 +0000426 public boolean DeleteNotice(String noticeid){
root0dbc9812025-05-19 04:41:57 +0000427 try {
Raverf79fdb62025-06-03 06:02:49 +0000428 Notice notice = entitymanager.find(Notice.class, noticeid);
root0dbc9812025-05-19 04:41:57 +0000429 if (notice == null) {
430 return false;
431 }
Raverf79fdb62025-06-03 06:02:49 +0000432 entitymanager.remove(notice);
root0dbc9812025-05-19 04:41:57 +0000433 return true;
434 } catch (Exception e) {
435 e.printStackTrace();
436 return false;
437 }
Raverf79fdb62025-06-03 06:02:49 +0000438
root0dbc9812025-05-19 04:41:57 +0000439 }
440
441 //获取用户的剩余邀请次数
Raverf79fdb62025-06-03 06:02:49 +0000442 public int GetUserAvailableInviteTimes(String userid){
root0dbc9812025-05-19 04:41:57 +0000443 try {
Raverf79fdb62025-06-03 06:02:49 +0000444 JPAQuery<Integer> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000445 QUser u = QUser.user;
446 int invite_left = query.select(u.invitetimes).from(u).where(u.userid.eq(userid)).fetchOne();
Raverf79fdb62025-06-03 06:02:49 +0000447
root0dbc9812025-05-19 04:41:57 +0000448 return invite_left;
449 } catch (Exception e) {
450 e.printStackTrace();
451 return -1;
452 }
Raverf79fdb62025-06-03 06:02:49 +0000453
root0dbc9812025-05-19 04:41:57 +0000454 }
455
456 //邀请用户,返回状态:0 success,1 剩余次数不足,2,3其他原因
457 @Override
Raverf79fdb62025-06-03 06:02:49 +0000458 public int InviteUser(String inviterid,String inviteemail){
root0dbc9812025-05-19 04:41:57 +0000459 try {
Raverf79fdb62025-06-03 06:02:49 +0000460 User user = entitymanager.find(User.class, inviterid);
root0dbc9812025-05-19 04:41:57 +0000461 if (user == null || !user.email.equals(inviteemail)) {
462 return 3;
463 }
464 if (user.invitetimes <= 0) {
465 return 1;
466 }
467 user.invitetimes -= 1;
Raverf79fdb62025-06-03 06:02:49 +0000468 entitymanager.merge(user);
root0dbc9812025-05-19 04:41:57 +0000469 return 0;
470 } catch (Exception e) {
471 e.printStackTrace();
472 return 2;
473 }
Raverf79fdb62025-06-03 06:02:49 +0000474
root0dbc9812025-05-19 04:41:57 +0000475 }
476
477 //添加一个收藏,返回状态:0 success,1 不存在,2其他原因
478 @Override
Raverf79fdb62025-06-03 06:02:49 +0000479 public boolean AddCollect(String userid,String seedid){
480 JPAQuery<User> query2 = new JPAQuery<>(entitymanager);
481 QUser u2 = QUser.user;
482 User user = query2.select(u2).from(u2).where(u2.userid.eq(userid)).fetchOne();
483 if (user == null) {
root0dbc9812025-05-19 04:41:57 +0000484 return false;
485 }
Raverf79fdb62025-06-03 06:02:49 +0000486 JPAQuery<Seed> query3 = new JPAQuery<>(entitymanager);
487 QSeed p = QSeed.seed;
488 Seed seed = query3.select(p).from(p).where(p.seedid.eq(seedid)).fetchOne();
489 if (seed == null) {
490 return false;
491 }
492 JPAQuery<String> query = new JPAQuery<>(entitymanager);
493 QUserStar u = QUserStar.userStar;
494 List<String> allSeedId = query.select(u.seedid).from(u).where(u.userid.eq(userid)).fetch();
495
496 if (allSeedId.contains(seedid)) {
497 return false;
498 }
499 UserStar userStar = new UserStar();
500 userStar.userid = userid;
501 userStar.seedid = seedid;
502 entitymanager.persist(userStar);
503 return true;
root0dbc9812025-05-19 04:41:57 +0000504 }
505
506 //删除一个收藏,返回状态:0 success,1 不存在,2其他原因
507 @Override
Raverf79fdb62025-06-03 06:02:49 +0000508 public boolean DeleteCollect(String userid,String seedid){
root0dbc9812025-05-19 04:41:57 +0000509 try {
Raverf79fdb62025-06-03 06:02:49 +0000510 JPAQuery<UserStar> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000511 QUserStar u = QUserStar.userStar;
512 UserStar userStar = query.select(u).from(u).where(u.userid.eq(userid).and(u.seedid.eq(seedid))).fetchOne();
513 if (userStar == null) {
514 return true; // 收藏不存在
515 }
Raverf79fdb62025-06-03 06:02:49 +0000516 entitymanager.remove(userStar);
root0dbc9812025-05-19 04:41:57 +0000517 return true;
518 } catch (Exception e) {
519 e.printStackTrace();
520 return false;
521 }
Raverf79fdb62025-06-03 06:02:49 +0000522
root0dbc9812025-05-19 04:41:57 +0000523 }
524
525 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800526 public int AddBegSeed(BegInfo info) {
527 if (info == null || info.begid == null || info.begid.isEmpty()) {
528 logger.warn("Invalid parameter: info is null or begid is empty");
529 return 2;
530 }
531
532 EntityTransaction tx = null;
533
534 try {
535 tx = entitymanager.getTransaction();
536 tx.begin();
537
538 // 检查是否重复
539 BegInfo existingBeg = entitymanager.find(BegInfo.class, info.begid);
540 if (existingBeg != null) {
541 logger.warn("BegSeed with ID {} already exists", info.begid);
542 return 1;
543 }
544
545 // 设置默认值
546 if (info.endtime == null) {
547 // 设置默认14天截止期
548 Calendar calendar = Calendar.getInstance();
549 calendar.add(Calendar.DAY_OF_MONTH, 14);
550 info.endtime = calendar.getTime();
551 }
552 info.hasseed = 0;
553
554 // 保存新的求种信息
555 entitymanager.persist(info);
556 tx.commit();
557
558 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
559 return 0;
560
561 } catch (Exception e) {
562 if (tx != null && tx.isActive()) {
563 tx.rollback();
564 }
565 logger.error("Error adding BegSeed: {}", e.getMessage());
566 return 2;
567 }
root0dbc9812025-05-19 04:41:57 +0000568 }
569
570 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800571 public int UpdateBegSeed(BegInfo info) {
572 if (info == null || info.begid == null || info.begid.isEmpty()) {
573 logger.warn("Invalid parameter: info is null or begid is empty");
574 return 2;
575 }
576
577 EntityTransaction tx = null;
578
579 try {
580 tx = entitymanager.getTransaction();
581 tx.begin();
582
583 // 检查是否存在
584 BegInfo existingBeg = entitymanager.find(BegInfo.class, info.begid);
585 if (existingBeg == null) {
586 logger.warn("BegSeed with ID {} does not exist", info.begid);
587 return 1;
588 }
589
590 // 保持原有值不变的字段
591 info.hasseed = existingBeg.hasseed;
592 if (info.endtime == null) {
593 info.endtime = existingBeg.endtime;
594 }
595
596 // 更新求种信息
597 entitymanager.merge(info);
598 tx.commit();
599
600 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
601 return 0;
602
603 } catch (Exception e) {
604 if (tx != null && tx.isActive()) {
605 tx.rollback();
606 }
607 logger.error("Error updating BegSeed: {}", e.getMessage());
608 return 2;
609 }
root0dbc9812025-05-19 04:41:57 +0000610 }
611
612 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800613 public int DeleteBegSeed(String begid) {
614 if (begid == null || begid.isEmpty()) {
615 logger.warn("Invalid parameter: begid is null or empty");
616 return 2;
617 }
618
619 EntityTransaction tx = null;
620
621 try {
622 tx = entitymanager.getTransaction();
623 tx.begin();
624
625 // 查找要删除的求种信息
626 BegInfo begInfo = entitymanager.find(BegInfo.class, begid);
627 if (begInfo == null) {
628 logger.warn("BegSeed with ID {} does not exist", begid);
629 tx.rollback();
630 return 1;
631 }
632
633 // 删除求种信息
634 entitymanager.remove(begInfo);
635 tx.commit();
636
637 logger.info("Successfully deleted BegSeed with ID: {}", begid);
638 return 0;
639
640 } catch (Exception e) {
641 if (tx != null && tx.isActive()) {
642 tx.rollback();
643 }
644 logger.error("Error deleting BegSeed: {}", e.getMessage());
645 return 2;
646 }
root0dbc9812025-05-19 04:41:57 +0000647 }
648
649 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800650 public int VoteSeed(String begId, String seedId, String userId) {
651 if (begId == null || seedId == null || userId == null ||
652 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
653 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
654 return 2;
655 }
656
657 EntityTransaction tx = null;
658
659 try {
660 tx = entitymanager.getTransaction();
661 tx.begin();
662
663 // 检查求种信息是否存在
664 BegInfo begInfo = entitymanager.find(BegInfo.class, begId);
665 if (begInfo == null) {
666 logger.warn("BegSeed with ID {} does not exist", begId);
667 return 2;
668 }
669
670 // 检查用户是否已投票
671 Long voteCount = new JPAQuery<>(entitymanager)
672 .select(QUserVotes.userVotes.count())
673 .from(QUserVotes.userVotes)
674 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
675 .fetchOne();
676
677 if (voteCount != null && voteCount > 0) {
678 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
679 return 1;
680 }
681
682 // 创建新的投票记录
683 entitymanager.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
684 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
685 .setParameter(1, userId)
686 .setParameter(2, begId)
687 .setParameter(3, seedId)
688 .executeUpdate();
689
690 // 更新SubmitSeed表中的投票数
691 entitymanager.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
692 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
693 .setParameter("begId", begId)
694 .setParameter("seedId", seedId)
695 .executeUpdate();
696
697 tx.commit();
698 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
699 return 0;
700
701 } catch (Exception e) {
702 if (tx != null && tx.isActive()) {
703 tx.rollback();
704 }
705 logger.error("Error voting for seed: {}", e.getMessage());
706 return 2;
707 }
root0dbc9812025-05-19 04:41:57 +0000708 }
709
710 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800711 public int SubmitSeed(String begid, Seed seed) {
712 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
713 logger.warn("Invalid parameters: begid or seed is null or empty");
714 return 2;
715 }
716
717 EntityTransaction tx = null;
718
719 try {
720 tx = entitymanager.getTransaction();
721 tx.begin();
722
723 // 检查求种信息是否存在
724 BegInfo begInfo = entitymanager.find(BegInfo.class, begid);
725 if (begInfo == null) {
726 logger.warn("BegSeed with ID {} does not exist", begid);
727 return 2;
728 }
729
730 // 检查种子是否已提交过
731 QSubmitSeed ss = QSubmitSeed.submitSeed;
732 Long submitCount = new JPAQuery<>(entitymanager)
733 .select(ss.count())
734 .from(ss)
735 .where(ss.begInfo.begid.eq(begid))
736 .where(ss.seed.seedid.eq(seed.seedid))
737 .fetchOne();
738
739 if (submitCount > 0) {
740 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
741 begid);
742 return 1;
743 }
744
745 // 保存种子信息(如果不存在)
746 if (entitymanager.find(Seed.class, seed.seedid) == null) {
747 entitymanager.persist(seed);
748 }
749
750 // 创建提交记录
751 entitymanager.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
752 .setParameter(1, begid)
753 .setParameter(2, seed.seedid)
754 .executeUpdate();
755
756 tx.commit();
757 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
758 return 0;
759
760 } catch (Exception e) {
761 if (tx != null && tx.isActive()) {
762 tx.rollback();
763 }
764 logger.error("Error submitting seed: {}", e.getMessage());
765 return 2;
766 }
root0dbc9812025-05-19 04:41:57 +0000767 }
768
769 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800770 public void SettleBeg() {
771 EntityTransaction tx = null;
772
773 try {
774 tx = entitymanager.getTransaction();
775 tx.begin();
776
777 // 1. 获取所有已过期且未完成的求种信息
778 QBegInfo b = QBegInfo.begInfo;
779 List<BegInfo> expiredBegs = new JPAQuery<>(entitymanager)
780 .select(b)
781 .from(b)
782 .where(b.endtime.loe(new Date())
783 .and(b.hasseed.eq(0)))
784 .fetch();
785
786 for (BegInfo beg : expiredBegs) {
787 // 2. 查找投票最多的提交任务
788 QSubmitSeed ss = QSubmitSeed.submitSeed;
789 Tuple topSubmission = new JPAQuery<>(entitymanager)
790 .select(ss.seed.seedid, ss.votes)
791 .from(ss)
792 .where(ss.begInfo.begid.eq(beg.begid))
793 .orderBy(ss.votes.desc())
794 .limit(1)
795 .fetchOne();
796
797 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
798 String seedId = topSubmission.get(ss.seed.seedid);
799
800 // 3. 获取上传者ID
801 QSeed s = QSeed.seed;
802 String ownerId = new JPAQuery<>(entitymanager)
803 .select(s.seeduserid)
804 .from(s)
805 .where(s.seedid.eq(seedId))
806 .fetchOne();
807
808 // 4. 获取上传者的PT信息并更新魔力值
809 UserPT ownerPT = entitymanager.find(UserPT.class, ownerId);
810 if (ownerPT != null) {
811 // 5. 发放奖励
812 ownerPT.magic += beg.magic;
813 entitymanager.merge(ownerPT);
814
815 // 6. 更新求种状态
816 beg.hasseed = 1;
817 entitymanager.merge(beg);
818
819 logger.info("Reward {} magic points awarded to user {} for beg {}",
820 beg.magic, ownerId, beg.begid);
821 }
822 }
823 }
824
825 tx.commit();
826 logger.info("Successfully settled {} expired beg requests",
827 expiredBegs.size());
828
829 } catch (Exception e) {
830 if (tx != null && tx.isActive()) {
831 tx.rollback();
832 }
833 logger.error("Error settling beg requests: {}", e.getMessage(), e);
834 }
root0dbc9812025-05-19 04:41:57 +0000835 }
836
837 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800838 public int AddPost(Post post) {
839 if (post == null || post.postid == null || post.postid.isEmpty()) {
840 logger.warn("Invalid parameter: post is null or postid is empty");
841 return 2;
842 }
843
844 EntityTransaction tx = null;
845
846 try {
847 tx = entitymanager.getTransaction();
848 tx.begin();
849
850 // 检查是否重复
851 Post existingPost = entitymanager.find(Post.class, post.postid);
852 if (existingPost != null) {
853 logger.warn("Post with ID {} already exists", post.postid);
854 return 1;
855 }
856
857 // 检查用户是否存在
858 User user = entitymanager.find(User.class, post.postuserid);
859 if (user == null) {
860 logger.warn("User with ID {} does not exist", post.postuserid);
861 return 2;
862 }
863
864 // 设置初始值
865 if (post.posttime == null) {
866 post.posttime = new Date();
867 }
868 post.replytime = 0;
869 post.readtime = 0;
870
871 // 保存帖子
872 entitymanager.persist(post);
873 tx.commit();
874
875 logger.info("Successfully added new post with ID: {}", post.postid);
876 return 0;
877
878 } catch (Exception e) {
879 if (tx != null && tx.isActive()) {
880 tx.rollback();
881 }
882 logger.error("Error adding post: {}", e.getMessage());
883 return 2;
884 }
root0dbc9812025-05-19 04:41:57 +0000885 }
886
887 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800888 public int UpdatePost(Post post) {
889 if (post == null || post.postid == null || post.postid.isEmpty()) {
890 logger.warn("Invalid parameter: post is null or postid is empty");
891 return 2;
892 }
893
894 EntityTransaction tx = null;
895
896 try {
897 tx = entitymanager.getTransaction();
898 tx.begin();
899
900 // 检查帖子是否存在
901 Post existingPost = entitymanager.find(Post.class, post.postid);
902 if (existingPost == null) {
903 logger.warn("Post with ID {} does not exist", post.postid);
904 return 1;
905 }
906
907 // 保持原有不可修改的字段
908 post.postuserid = existingPost.postuserid;
909 post.posttime = existingPost.posttime;
910 post.replytime = existingPost.replytime;
911 post.readtime = existingPost.readtime;
912
913 // 更新帖子
914 entitymanager.merge(post);
915 tx.commit();
916
917 logger.info("Successfully updated post with ID: {}", post.postid);
918 return 0;
919
920 } catch (Exception e) {
921 if (tx != null && tx.isActive()) {
922 tx.rollback();
923 }
924 logger.error("Error updating post: {}", e.getMessage());
925 return 2;
926 }
root0dbc9812025-05-19 04:41:57 +0000927 }
928
929 @Override
Raverf79fdb62025-06-03 06:02:49 +0000930 public int DeletePost(String postid){
TRM-coding87c24972025-06-07 14:05:29 +0800931 if (postid == null || postid.isEmpty()) {
932 logger.warn("Invalid parameter: postid is null or empty");
933 return 2;
934 }
935
936 EntityManager em = null;
937 EntityTransaction tx = null;
938
939 try {
940 em = emf.createEntityManager();
941 tx = em.getTransaction();
942 tx.begin();
943
944 // 查找要删除的帖子
945 Post post = em.find(Post.class, postid);
946 if (post == null) {
947 logger.warn("Post with ID {} does not exist", postid);
948 tx.rollback();
949 return 1;
950 }
951
952 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
953 em.remove(post);
954 tx.commit();
955
956 logger.info("Successfully deleted post with ID: {}", postid);
957 return 0;
958
959 } catch (Exception e) {
960 if (tx != null && tx.isActive()) {
961 tx.rollback();
962 }
963 logger.error("Error deleting post: {}", e.getMessage());
964 return 2;
965 } finally {
966 if (em != null) {
967 em.close();
968 }
969 }
root0dbc9812025-05-19 04:41:57 +0000970 }
971
972 @Override
Raverf79fdb62025-06-03 06:02:49 +0000973 public int AddComment(String postid, String userid, String comment){
TRM-coding87c24972025-06-07 14:05:29 +0800974 if (postid == null || postid.isEmpty() ||
975 userid == null || userid.isEmpty() ||
976 comment == null || comment.isEmpty()) {
977 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
978 return 2;
979 }
980
981 EntityManager em = null;
982 EntityTransaction tx = null;
983
984 try {
985 em = emf.createEntityManager();
986 tx = em.getTransaction();
987 tx.begin();
988
989 // 检查帖子是否存在
990 Post post = em.find(Post.class, postid);
991 if (post == null) {
992 logger.warn("Post with ID {} does not exist", postid);
993 return 1;
994 }
995
996 // 检查评论用户是否存在
997 User user = em.find(User.class, userid);
998 if (user == null) {
999 logger.warn("User with ID {} does not exist", userid);
1000 return 1;
1001 }
1002
1003 // 创建新的评论
1004 PostReply reply = new PostReply();
1005 reply.replyid = UUID.randomUUID().toString();
1006 reply.postid = postid;
1007 reply.authorid = userid;
1008 reply.content = comment;
1009 reply.createdAt = new Date();
1010
1011 // 保存评论
1012 em.persist(reply);
1013
1014 // 更新帖子的回复数
1015 post.replytime += 1;
1016 em.merge(post);
1017
1018 tx.commit();
1019
1020 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
1021 userid, postid, reply.replyid);
1022 return 0;
1023
1024 } catch (Exception e) {
1025 if (tx != null && tx.isActive()) {
1026 tx.rollback();
1027 }
1028 logger.error("Error adding comment: {}", e.getMessage());
1029 return 2;
1030 } finally {
1031 if (em != null) {
1032 em.close();
1033 }
1034 }
root0dbc9812025-05-19 04:41:57 +00001035 }
1036
1037 @Override
Raverf79fdb62025-06-03 06:02:49 +00001038 public int DeleteComment(String postid,String commentid){
TRM-coding87c24972025-06-07 14:05:29 +08001039 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
1040 logger.warn("Invalid parameters: postid or commentid is null or empty");
1041 return 2;
1042 }
1043
1044 EntityManager em = null;
1045 EntityTransaction tx = null;
1046
1047 try {
1048 em = emf.createEntityManager();
1049 tx = em.getTransaction();
1050 tx.begin();
1051
1052 // 检查帖子是否存在
1053 Post post = em.find(Post.class, postid);
1054 if (post == null) {
1055 logger.warn("Post with ID {} does not exist", postid);
1056 return 1;
1057 }
1058
1059 // 检查评论是否存在且属于该帖子
1060 PostReply reply = em.find(PostReply.class, commentid);
1061 if (reply == null || !reply.postid.equals(postid)) {
1062 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
1063 return 1;
1064 }
1065
1066 // 删除评论
1067 em.remove(reply);
1068
1069 // 更新帖子的回复数
1070 post.replytime = Math.max(0, post.replytime - 1);
1071 em.merge(post);
1072
1073 tx.commit();
1074
1075 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
1076 return 0;
1077
1078 } catch (Exception e) {
1079 if (tx != null && tx.isActive()) {
1080 tx.rollback();
1081 }
1082 logger.error("Error deleting comment: {}", e.getMessage());
1083 return 2;
1084 } finally {
1085 if (em != null) {
1086 em.close();
1087 }
1088 }
root0dbc9812025-05-19 04:41:57 +00001089 }
1090
1091 @Override
Raverf79fdb62025-06-03 06:02:49 +00001092 public boolean ExchangeMagicToUpload(String userid,int magic)//将魔力值兑换为上传量,返回状态:0 success,1 不存在,2其他原因
root0dbc9812025-05-19 04:41:57 +00001093 {
TRM-coding87c24972025-06-07 14:05:29 +08001094 if (userid == null || userid.isEmpty() || magic <= 0) {
1095 logger.warn("参数无效: userid为空或magic <= 0");
1096 return false;
1097 }
1098
1099 EntityManager em = null;
1100 EntityTransaction tx = null;
1101
1102 try {
1103 em = emf.createEntityManager();
1104 tx = em.getTransaction();
1105 tx.begin();
1106
1107 UserPT userPT = em.find(UserPT.class, userid);
1108 if (userPT == null) {
1109 logger.warn("未找到用户 {} 的PT信息", userid);
1110 return false;
1111 }
1112
1113 if (userPT.magic < magic) {
1114 logger.warn("用户 {} 的魔力值不足", userid);
1115 return false;
1116 }
1117
1118 // 1:1兑换,直接加上魔力值(假设单位是MB)
1119 userPT.magic -= magic;
1120 userPT.upload += magic;
1121
1122 // 更新分享率
1123 if (userPT.download > 0) {
1124 userPT.share = (double) userPT.upload / userPT.download;
1125 }
1126
1127 em.merge(userPT);
1128 tx.commit();
1129
1130 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
1131 return true;
1132
1133 } catch (Exception e) {
1134 if (tx != null && tx.isActive()) {
1135 tx.rollback();
1136 }
1137 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
1138 return false;
1139 } finally {
1140 if (em != null) {
1141 em.close();
1142 }
1143 }
root0dbc9812025-05-19 04:41:57 +00001144 }
Raverf79fdb62025-06-03 06:02:49 +00001145
root0dbc9812025-05-19 04:41:57 +00001146 @Override
Raverf79fdb62025-06-03 06:02:49 +00001147 public boolean ExchangeMagicToDownload(String userid,int magic)
1148 {
TRM-coding87c24972025-06-07 14:05:29 +08001149 if (userid == null || userid.isEmpty() || magic <= 0) {
1150 logger.warn("参数无效: userid为空或magic <= 0");
1151 return false;
1152 }
1153
1154 EntityManager em = null;
1155 EntityTransaction tx = null;
1156
1157 try {
1158 em = emf.createEntityManager();
1159 tx = em.getTransaction();
1160 tx.begin();
1161
1162 UserPT userPT = em.find(UserPT.class, userid);
1163 if (userPT == null) {
1164 logger.warn("未找到用户 {} 的PT信息", userid);
1165 return false;
1166 }
1167
1168 if (userPT.magic < magic) {
1169 logger.warn("用户 {} 的魔力值不足", userid);
1170 return false;
1171 }
1172
1173 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
1174 userPT.magic -= magic;
1175 userPT.download = Math.max(0, userPT.download - magic);
1176
1177 // 更新分享率
1178 if (userPT.download > 0) {
1179 userPT.share = (double) userPT.upload / userPT.download;
1180 }
1181
1182 em.merge(userPT);
1183 tx.commit();
1184
1185 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
1186 return true;
1187
1188 } catch (Exception e) {
1189 if (tx != null && tx.isActive()) {
1190 tx.rollback();
1191 }
1192 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
1193 return false;
1194 } finally {
1195 if (em != null) {
1196 em.close();
1197 }
1198 }
root0dbc9812025-05-19 04:41:57 +00001199 }//将魔力值兑换为下载量,返回状态:0 success,1 不存在,2其他原因
1200
1201 @Override
Raverf79fdb62025-06-03 06:02:49 +00001202 public boolean ExchangeMagicToVip(String userid,int magic){
TRM-coding87c24972025-06-07 14:05:29 +08001203 if (userid == null || userid.isEmpty() || magic <= 0) {
1204 logger.warn("参数无效: userid为空或magic <= 0");
1205 return false;
1206 }
1207
1208 EntityManager em = null;
1209 EntityTransaction tx = null;
1210
1211 try {
1212 em = emf.createEntityManager();
1213 tx = em.getTransaction();
1214 tx.begin();
1215
1216 UserPT userPT = em.find(UserPT.class, userid);
1217 if (userPT == null) {
1218 logger.warn("未找到用户 {} 的PT信息", userid);
1219 return false;
1220 }
1221
1222 if (userPT.magic < magic) {
1223 logger.warn("用户 {} 的魔力值不足", userid);
1224 return false;
1225 }
1226
1227 // 1:1兑换VIP下载次数
1228 userPT.magic -= magic;
1229 userPT.viptime += magic;
1230
1231 em.merge(userPT);
1232 tx.commit();
1233
1234 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
1235 return true;
1236
1237 } catch (Exception e) {
1238 if (tx != null && tx.isActive()) {
1239 tx.rollback();
1240 }
1241 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
1242 return false;
1243 } finally {
1244 if (em != null) {
1245 em.close();
1246 }
1247 }
root0dbc9812025-05-19 04:41:57 +00001248 }
1249 //将魔力值兑换为VIP次数,返回状态:0 success,1 不存在,2其他原因
1250
1251 @Override
Raverf79fdb62025-06-03 06:02:49 +00001252 public boolean UploadTransmitProfile(Profile profile){
TRM-coding87c24972025-06-07 14:05:29 +08001253 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
1254 profile.userid == null || profile.userid.isEmpty()) {
1255 logger.warn("参数无效: profile为空或必要字段为空");
1256 return false;
1257 }
1258
1259 EntityManager em = null;
1260 EntityTransaction tx = null;
1261
1262 try {
1263 em = emf.createEntityManager();
1264 tx = em.getTransaction();
1265 tx.begin();
1266
1267 // 检查用户是否存在
1268 User user = em.find(User.class, profile.userid);
1269 if (user == null) {
1270 logger.warn("用户 {} 不存在", profile.userid);
1271 return false;
1272 }
1273
1274 // 检查是否已存在相同的迁移申请
1275 Profile existingProfile = em.find(Profile.class, profile.profileurl);
1276 if (existingProfile != null) {
1277 logger.warn("迁移申请 {} 已存在", profile.profileurl);
1278 return false;
1279 }
1280
1281 // 设置初始值
1282 profile.exampass = false;
1283 profile.magicgived = "0";
1284 profile.uploadgived = "0";
1285
1286 // 保存迁移申请
1287 em.persist(profile);
1288 tx.commit();
1289
1290 logger.info("成功上传迁移申请 {}", profile.profileurl);
1291 return true;
1292
1293 } catch (Exception e) {
1294 if (tx != null && tx.isActive()) {
1295 tx.rollback();
1296 }
1297 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
1298 return false;
1299 } finally {
1300 if (em != null) {
1301 em.close();
1302 }
1303 }
root0dbc9812025-05-19 04:41:57 +00001304 }
1305
1306 @Override
Raverf79fdb62025-06-03 06:02:49 +00001307 public Profile GetTransmitProfile(String profileid){
TRM-coding87c24972025-06-07 14:05:29 +08001308 if (profileid == null || profileid.isEmpty()) {
1309 logger.warn("参数无效: profileid为空");
1310 return null;
1311 }
1312
1313 EntityManager em = null;
1314
1315 try {
1316 em = emf.createEntityManager();
1317 Profile profile = em.find(Profile.class, profileid);
1318 if (profile == null) {
1319 logger.warn("未找到迁移申请 {}", profileid);
1320 return null;
1321 }
1322
1323 logger.info("成功获取迁移申请 {}", profileid);
1324 return profile;
1325
1326 } catch (Exception e) {
1327 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
1328 return null;
1329 } finally {
1330 if (em != null) {
1331 em.close();
1332 }
1333 }
root0dbc9812025-05-19 04:41:57 +00001334 }
1335 //获取迁移信息
Raverf79fdb62025-06-03 06:02:49 +00001336
root0dbc9812025-05-19 04:41:57 +00001337 @Override
Raverf79fdb62025-06-03 06:02:49 +00001338 public boolean ExamTransmitProfile(String profileid,boolean result){
TRM-coding87c24972025-06-07 14:05:29 +08001339 if (profileid == null || profileid.isEmpty()) {
1340 logger.warn("参数无效: profileid为空");
1341 return false;
1342 }
1343
1344 EntityManager em = null;
1345 EntityTransaction tx = null;
1346
1347 try {
1348 em = emf.createEntityManager();
1349 tx = em.getTransaction();
1350 tx.begin();
1351
1352 // 查找迁移申请
1353 Profile profile = em.find(Profile.class, profileid);
1354 if (profile == null) {
1355 logger.warn("未找到迁移申请 {}", profileid);
1356 return false;
1357 }
1358
1359 // 更新审核状态
1360 profile.exampass = result;
1361
1362 if (result) {
1363 // 如果审核通过,更新用户的PT信息
1364 UserPT userPT = em.find(UserPT.class, profile.userid);
1365 if (userPT != null) {
1366 // 发放魔力值
1367 int magicToGive = Integer.parseInt(profile.magictogive);
1368 userPT.magic += magicToGive;
1369 profile.magicgived = String.valueOf(magicToGive);
1370
1371 // 发放上传量
1372 long uploadToGive = Long.parseLong(profile.uploadtogive);
1373 userPT.upload += uploadToGive;
1374 profile.uploadgived = String.valueOf(uploadToGive);
1375
1376 em.merge(userPT);
1377 }
1378 }
1379
1380 em.merge(profile);
1381 tx.commit();
1382
1383 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1384 return true;
1385
1386 } catch (Exception e) {
1387 if (tx != null && tx.isActive()) {
1388 tx.rollback();
1389 }
1390 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1391 return false;
1392 } finally {
1393 if (em != null) {
1394 em.close();
1395 }
1396 }
root0dbc9812025-05-19 04:41:57 +00001397 }
1398 //审核迁移信息,0成功,1失败
1399 @Override
Raverf79fdb62025-06-03 06:02:49 +00001400 public Profile[] GetTransmitProfileList(){
TRM-coding87c24972025-06-07 14:05:29 +08001401 EntityManager em = null;
1402
1403 try {
1404 em = emf.createEntityManager();
1405
1406 // 获取所有迁移申请
1407 QProfile p = QProfile.profile;
1408 List<Profile> profiles = new JPAQuery<>(em)
1409 .select(p)
1410 .from(p)
1411 .fetch();
1412
1413 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1414 return profiles.toArray(new Profile[0]);
1415
1416 } catch (Exception e) {
1417 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1418 return new Profile[0];
1419 } finally {
1420 if (em != null) {
1421 em.close();
1422 }
1423 }
root0dbc9812025-05-19 04:41:57 +00001424 }
1425 //获取所有迁移信息
1426
rhj46f62c42025-06-06 23:24:10 +08001427 @Override
1428 public Post[] GetPostList() {
1429 JPAQuery<Post> query = new JPAQuery<>(entitymanager);
1430 QPost p = QPost.post;
1431 List<Post> posts = query.select(p).from(p).fetch();
1432 return posts.toArray(new Post[0]);
1433 }
1434
rhj5b69b7e2025-06-07 01:28:08 +08001435 @Override
1436 public Post GetPost(String postid) {
1437 JPAQuery<Post> query = new JPAQuery<>(entitymanager);
1438 QPost p = QPost.post;
1439 Post post = query.select(p).from(p).where(p.postid.eq(postid)).fetchOne();
1440 return post;
1441 }
1442
1443 @Override
1444 public PostReply[] GetPostReplyList(String postid) {
1445 JPAQuery<PostReply> query = new JPAQuery<>(entitymanager);
1446 QPostReply p = QPostReply.postReply;
1447 List<PostReply> replies = query.select(p).from(p).where(p.postid.eq(postid)).fetch();
1448 return replies.toArray(new PostReply[0]);
1449 }
root0dbc9812025-05-19 04:41:57 +00001450}
Raverf79fdb62025-06-03 06:02:49 +00001451