blob: b91e471aba8c8442b0f81974ea4cfe7788481534 [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;
rhj5ebd93c2025-06-07 15:57:28 +080042import entity.UserInvite;
root0dbc9812025-05-19 04:41:57 +000043
TRM-coding87c24972025-06-07 14:05:29 +080044import org.slf4j.Logger;
45import org.slf4j.LoggerFactory;
46
root0dbc9812025-05-19 04:41:57 +000047public class Database1 implements DataManagerInterface {
TRM-coding87c24972025-06-07 14:05:29 +080048 private static final Logger logger = LoggerFactory.getLogger(Database1.class);
49 private EntityManagerFactory emf;
rhj5ebd93c2025-06-07 15:57:28 +080050
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);
rhj5ebd93c2025-06-07 15:57:28 +080058 // 只创建一个 EntityManagerFactory,为每个操作创建新的 EntityManager
59 this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props);
root678fd3e2025-06-03 14:20:20 +000060 }
rhj5ebd93c2025-06-07 15:57:28 +080061
62 // 为每个操作创建新的 EntityManager 以避免线程安全问题
63 private EntityManager createEntityManager() {
64 return emf.createEntityManager();
65 }
66
TRM-codingcdfe5482025-06-06 17:31:01 +080067 @Override
68 public String LoginUser(User userinfo){
69 try {
70 // 检查传入的参数是否合法
71 if (userinfo == null || userinfo.password == null) {
72 return null;
73 }
74
TRM-codingcdfe5482025-06-06 17:31:01 +080075 boolean hasEmail = userinfo.email != null && !userinfo.email.isEmpty();
76
77 // 如果两个都为空或两个都不为空,返回null
rhj46f62c42025-06-06 23:24:10 +080078 if (!hasEmail) {
TRM-codingcdfe5482025-06-06 17:31:01 +080079 return null;
80 }
81
rhj5ebd93c2025-06-07 15:57:28 +080082 EntityManager entitymanager = createEntityManager();
TRM-codingcdfe5482025-06-06 17:31:01 +080083 JPAQuery<User> query = new JPAQuery<>(entitymanager);
84 QUser u = QUser.user;
85 User foundUser = null;
86
rhj46f62c42025-06-06 23:24:10 +080087
88 // 通过邮箱和密码查找
89 foundUser = query.select(u)
90 .from(u)
91 .where(u.email.eq(userinfo.email)
92 .and(u.password.eq(userinfo.password)))
93 .fetchOne();
TRM-codingcdfe5482025-06-06 17:31:01 +080094
95 // 如果找到匹配的用户则返回用户ID,否则返回null
96 return foundUser != null ? foundUser.userid : null;
97
98 } catch (Exception e) {
99 e.printStackTrace();
100 return null;
101 }
102 }
root0dbc9812025-05-19 04:41:57 +0000103
104 // 返回状态:0 success,1 邮箱重复,2其他原因
105 @Override
Raverf79fdb62025-06-03 06:02:49 +0000106 public int RegisterUser(User userinfo){
rhj5ebd93c2025-06-07 15:57:28 +0800107 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000108 try{
TRM-codingcdfe5482025-06-06 17:31:01 +0800109 // 首先检查该邮箱是否在UserInvite表中被邀请过
rhj5ebd93c2025-06-07 15:57:28 +0800110 JPAQuery<UserInvite> inviteQuery = new JPAQuery<>(entitymanager);
TRM-codingcdfe5482025-06-06 17:31:01 +0800111 QUserInvite ui = QUserInvite.userInvite;
rhj5ebd93c2025-06-07 15:57:28 +0800112 List<UserInvite> UserInvites = inviteQuery.select(ui).from(ui).where(ui.inviterEmail.eq(userinfo.email)).fetch();
TRM-codingcdfe5482025-06-06 17:31:01 +0800113
114 // 如果邮箱不在被邀请列表中,拒绝注册
rhj5ebd93c2025-06-07 15:57:28 +0800115 if(UserInvites.isEmpty()){
TRM-codingcdfe5482025-06-06 17:31:01 +0800116 return 2; // 未被邀请
117 }
118
119 // 检查邮箱是否已在User表中存在
Raverf79fdb62025-06-03 06:02:49 +0000120 JPAQuery<String> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000121 QUser u = QUser.user;
122 List<String> allEmails = query.select(u.email).from(u).fetch();
123
Raverf79fdb62025-06-03 06:02:49 +0000124 if(allEmails.contains(userinfo.email)){
TRM-codingcdfe5482025-06-06 17:31:01 +0800125 return 1; // 邮箱重复
root0dbc9812025-05-19 04:41:57 +0000126 }
root0dbc9812025-05-19 04:41:57 +0000127
rhj5ebd93c2025-06-07 15:57:28 +0800128 UserPT userPT = new UserPT();
129 userPT.userid = userinfo.userid;
130 userPT.magic = 0; // 设置默认值
131 userPT.upload = 0; // 设置默认值
132 userPT.download = 0; // 设置默认值
133 userPT.share = 0.0; // 设置默认值
134 userPT.user = userinfo; // 设置关联关系
135 userPT.farmurl = ""; // 设置默认值
136 userPT.viptime = 0; // 设置默认值
137
rhj46f62c42025-06-06 23:24:10 +0800138 entitymanager.getTransaction().begin();
139 entitymanager.persist(userinfo);
rhj5ebd93c2025-06-07 15:57:28 +0800140 entitymanager.persist(userPT);
141 // 删除所有匹配的邀请记录
142 for (UserInvite invite : UserInvites) {
143 entitymanager.remove(invite);
144 }
rhj46f62c42025-06-06 23:24:10 +0800145 entitymanager.getTransaction().commit();
146 return 0; // 注册成功
TRM-coding93f7be82025-06-06 22:29:02 +0800147
rhj46f62c42025-06-06 23:24:10 +0800148 }catch(Exception e){
149 e.printStackTrace();
150 if (entitymanager.getTransaction().isActive()) {
151 entitymanager.getTransaction().rollback();
152 }
153 return 4;
154 }
TRM-coding93f7be82025-06-06 22:29:02 +0800155
root0dbc9812025-05-19 04:41:57 +0000156 }
157
158 // 返回状态:0 success,1 不存在,2其他原因
159 @Override
Raverf79fdb62025-06-03 06:02:49 +0000160 public int UpdateInformation(User userinfo){
rhj5ebd93c2025-06-07 15:57:28 +0800161 EntityManager entitymanager = createEntityManager();
root0dbc9812025-05-19 04:41:57 +0000162 try {
163 if (userinfo.userid == null) {
164 return 2; // userid为null直接返回错误
165 }
rhj5ebd93c2025-06-07 15:57:28 +0800166
rhjc6a4ee02025-06-06 00:45:18 +0800167 entitymanager.getTransaction().begin();
168
Raverf79fdb62025-06-03 06:02:49 +0000169 JPAQuery<User> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000170 QUser u = QUser.user;
171 User updateUser = query.select(u).from(u).where(u.userid.eq(userinfo.userid)).fetchOne();
172
Raverf79fdb62025-06-03 06:02:49 +0000173 if(updateUser == null){
rhjc6a4ee02025-06-06 00:45:18 +0800174 entitymanager.getTransaction().rollback();
root0dbc9812025-05-19 04:41:57 +0000175 return 1;
176 }
177 // 只更新需要的字段,避免破坏持久化状态和关联关系
rhjc6a4ee02025-06-06 00:45:18 +0800178 if (userinfo.email != null) updateUser.email = userinfo.email;
179 if (userinfo.username != null) updateUser.username = userinfo.username;
180 if (userinfo.password != null) updateUser.password = userinfo.password;
181 if (userinfo.sex != null) updateUser.sex = userinfo.sex;
182 if (userinfo.school != null) updateUser.school = userinfo.school;
183 if (userinfo.pictureurl != null) updateUser.pictureurl = userinfo.pictureurl;
184 if (userinfo.profile != null) updateUser.profile = userinfo.profile;
root0dbc9812025-05-19 04:41:57 +0000185 updateUser.accountstate = userinfo.accountstate;
186 updateUser.invitetimes = userinfo.invitetimes;
187 // 如有其他字段也一并赋值
Raverf79fdb62025-06-03 06:02:49 +0000188 entitymanager.merge(updateUser);
rhjc6a4ee02025-06-06 00:45:18 +0800189 entitymanager.getTransaction().commit();
root0dbc9812025-05-19 04:41:57 +0000190 return 0;
191 } catch (Exception e) {
192 e.printStackTrace();
rhjc6a4ee02025-06-06 00:45:18 +0800193 if (entitymanager.getTransaction().isActive()) {
194 entitymanager.getTransaction().rollback();
195 }
root0dbc9812025-05-19 04:41:57 +0000196 return 2;
197 }
Raverf79fdb62025-06-03 06:02:49 +0000198
root0dbc9812025-05-19 04:41:57 +0000199 }
200
201 // 返回用户的全部基本信息
202 @Override
Raverf79fdb62025-06-03 06:02:49 +0000203 public User GetInformation(String userid){
rhj5ebd93c2025-06-07 15:57:28 +0800204 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000205 User user = entitymanager.find(User.class, userid);
206 return user;
root0dbc9812025-05-19 04:41:57 +0000207 }
208
209 //返回用户的全部pt站信息
210 @Override
Raverf79fdb62025-06-03 06:02:49 +0000211 public UserPT GetInformationPT(String userid){
rhj5ebd93c2025-06-07 15:57:28 +0800212 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000213 UserPT userPT = entitymanager.find(UserPT.class, userid);
214 return userPT;
root0dbc9812025-05-19 04:41:57 +0000215 }
216
217 //返回状态:0 success,1 邮箱重复,2其他原因
218 @Override
Raverf79fdb62025-06-03 06:02:49 +0000219 public int UpdateInformationPT(UserPT userinfo){
220 try{
rhj5ebd93c2025-06-07 15:57:28 +0800221 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000222 JPAQuery<UserPT> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000223 QUserPT u = QUserPT.userPT;
224 UserPT userPT = query.select(u).from(u).where(u.userid.eq(userinfo.userid)).fetchOne();
Raverf79fdb62025-06-03 06:02:49 +0000225
226 if(userPT == null){
root0dbc9812025-05-19 04:41:57 +0000227 return 1;
228 }
229 userPT = userinfo;
Raverf79fdb62025-06-03 06:02:49 +0000230 entitymanager.merge(userPT);
root0dbc9812025-05-19 04:41:57 +0000231 return 0;
232
Raverf79fdb62025-06-03 06:02:49 +0000233 }catch(Exception e){
root0dbc9812025-05-19 04:41:57 +0000234 e.printStackTrace();
235 return 2;
236 }
237 }
238
239 //返回状态:0 success,1 id重复,2其他原因
240 @Override
Raverf79fdb62025-06-03 06:02:49 +0000241 public int RegisterUserPT(UserPT userinfo){
rhj5ebd93c2025-06-07 15:57:28 +0800242 EntityManager entitymanager = createEntityManager();
root0dbc9812025-05-19 04:41:57 +0000243 try {
rhj46f62c42025-06-06 23:24:10 +0800244 entitymanager.getTransaction().begin();
245
Raverf79fdb62025-06-03 06:02:49 +0000246 JPAQuery<UserPT> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000247 QUserPT u = QUserPT.userPT;
248 UserPT checkUserPT = query.select(u).from(u).where(u.userid.eq(userinfo.userid)).fetchOne();
249 if (checkUserPT != null) {
rhj46f62c42025-06-06 23:24:10 +0800250 entitymanager.getTransaction().rollback();
root0dbc9812025-05-19 04:41:57 +0000251 return 1;
252 }
rhj46f62c42025-06-06 23:24:10 +0800253
Raverf79fdb62025-06-03 06:02:49 +0000254 entitymanager.persist(userinfo);
rhj46f62c42025-06-06 23:24:10 +0800255 entitymanager.getTransaction().commit();
root0dbc9812025-05-19 04:41:57 +0000256 return 0;
257 } catch (Exception e) {
258 e.printStackTrace();
rhj46f62c42025-06-06 23:24:10 +0800259 if (entitymanager.getTransaction().isActive()) {
260 entitymanager.getTransaction().rollback();
261 }
root0dbc9812025-05-19 04:41:57 +0000262 return 2;
263 }
264 }
265
266 //返回种子的全部信息
267 @Override
Raverf79fdb62025-06-03 06:02:49 +0000268 public Seed GetSeedInformation(String seedid){
rhj5ebd93c2025-06-07 15:57:28 +0800269 EntityManager em = createEntityManager();
270 try {
271 JPAQuery<Seed> query = new JPAQuery<>(em);
272 QSeed s = QSeed.seed;
273 QUser u = QUser.user;
274 // 使用 fetch join 预先加载关联的 user 对象,避免懒加载导致的 ResultSet closed 错误
275 Seed seed = query.select(s).from(s)
276 .leftJoin(s.user, u).fetchJoin()
277 .where(s.seedid.eq(seedid)).fetchOne();
278 return seed;
279 } finally {
280 em.close();
281 }
root0dbc9812025-05-19 04:41:57 +0000282 }
283
Raveraae06122025-06-05 08:13:35 +0000284 @Override
285 public Seed[] GetSeedListByTag(String tag){
rhj5ebd93c2025-06-07 15:57:28 +0800286 EntityManager em = createEntityManager();
287 try {
288 JPAQuery<Seed> query = new JPAQuery<>(em);
289 QSeed s = QSeed.seed;
290 QUser u = QUser.user;
291 // 使用 fetch join 预先加载关联的 user 对象,避免懒加载导致的 ResultSet closed 错误
292 List<Seed> seeds = query.select(s).from(s)
293 .leftJoin(s.user, u).fetchJoin()
294 .where(s.seedtag.eq(tag)).fetch();
295 return seeds.toArray(new Seed[0]);
296 } finally {
297 em.close();
298 }
Raveraae06122025-06-05 08:13:35 +0000299 }
300
rhjc6a4ee02025-06-06 00:45:18 +0800301 @Override
302 public Seed[] GetSeedListByUser(String userid){
rhj5ebd93c2025-06-07 15:57:28 +0800303 EntityManager em = createEntityManager();
304 try {
305 JPAQuery<Seed> query = new JPAQuery<>(em);
306 QSeed s = QSeed.seed;
307 QUser u = QUser.user;
308 // 使用 fetch join 预先加载关联的 user 对象,避免懒加载导致的 ResultSet closed 错误
309 List<Seed> seeds = query.select(s).from(s)
310 .leftJoin(s.user, u).fetchJoin()
311 .where(s.seeduserid.eq(userid)).fetch();
312 return seeds.toArray(new Seed[0]);
313 } finally {
314 em.close();
315 }
rhjc6a4ee02025-06-06 00:45:18 +0800316 }
317
318 @Override
319 public int DeleteSeed(String seedid){
rhj5ebd93c2025-06-07 15:57:28 +0800320 EntityManager entitymanager = createEntityManager();
rhjc6a4ee02025-06-06 00:45:18 +0800321 try {
322 entitymanager.getTransaction().begin();
323 Seed seed = entitymanager.find(Seed.class, seedid);
324 if (seed == null) {
325 entitymanager.getTransaction().rollback();
326 return 1; // 种子不存在
327 }
328 entitymanager.remove(seed);
329 entitymanager.getTransaction().commit();
330 return 0; // 成功删除
331 } catch (Exception e) {
332 e.printStackTrace();
333 if (entitymanager.getTransaction().isActive()) {
334 entitymanager.getTransaction().rollback();
335 }
336 return 2; // 其他错误
337 }
338 }
339
root0dbc9812025-05-19 04:41:57 +0000340 //添加一个新的种子,0成功,其他失败信息待定;
341 @Override
Raverf79fdb62025-06-03 06:02:49 +0000342 public int RegisterSeed(Seed seedinfo){
rhj5ebd93c2025-06-07 15:57:28 +0800343 EntityManager entitymanager = createEntityManager();
root0dbc9812025-05-19 04:41:57 +0000344 try {
Raveraae06122025-06-05 08:13:35 +0000345 entitymanager.getTransaction().begin();
Raverf79fdb62025-06-03 06:02:49 +0000346 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000347 QSeed s = QSeed.seed;
348 Seed seed = query.select(s).from(s).where(s.seedid.eq(seedinfo.seedid)).fetchOne();
Raveraae06122025-06-05 08:13:35 +0000349 User user = entitymanager.find(User.class, seedinfo.seeduserid);
350 if (user == null) {
351 entitymanager.getTransaction().rollback();
352 return 2; // 用户不存在
353 }
rhjc6a4ee02025-06-06 00:45:18 +0800354 seedinfo.user = user; // 设置种子的用户关联
root0dbc9812025-05-19 04:41:57 +0000355 if (seed != null) {
Raveraae06122025-06-05 08:13:35 +0000356 entitymanager.getTransaction().rollback();
root0dbc9812025-05-19 04:41:57 +0000357 return 1;
358 }
Raverf79fdb62025-06-03 06:02:49 +0000359 entitymanager.persist(seedinfo);
Raveraae06122025-06-05 08:13:35 +0000360 entitymanager.getTransaction().commit();
root0dbc9812025-05-19 04:41:57 +0000361 return 0;
362 } catch (Exception e) {
363 e.printStackTrace();
Raveraae06122025-06-05 08:13:35 +0000364 if (entitymanager.getTransaction().isActive()) {
365 entitymanager.getTransaction().rollback();
366 }
root0dbc9812025-05-19 04:41:57 +0000367 return 2;
368 }
369 }
370
371 //接收新的种子然后更新其全部属性
372 @Override
Raverf79fdb62025-06-03 06:02:49 +0000373 public int UpdateSeed(Seed seedinfo){
root0dbc9812025-05-19 04:41:57 +0000374 try {
rhj5ebd93c2025-06-07 15:57:28 +0800375 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000376 JPAQuery<Seed> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000377 QSeed s = QSeed.seed;
378 Seed seed = query.select(s).from(s).where(s.seedid.eq(seedinfo.seedid)).fetchOne();
379 if (seed == null) {
380 return 1;
381 }
382 seed = seedinfo;
Raverf79fdb62025-06-03 06:02:49 +0000383 entitymanager.merge(seed);
root0dbc9812025-05-19 04:41:57 +0000384 return 0;
385 } catch (Exception e) {
386 e.printStackTrace();
387 return 2;
388 }
389 }
390
391 //传入搜索的关键词或句子,返回搜索到的种子信息(按照公共字符数量排序)
392 @Override
Raverf79fdb62025-06-03 06:02:49 +0000393 public Seed[] SearchSeed(String userQ){
rhj5ebd93c2025-06-07 15:57:28 +0800394 EntityManager em = createEntityManager();
395 try {
396 JPAQuery<Seed> query = new JPAQuery<>(em);
397 QSeed s = QSeed.seed;
398 QUser u = QUser.user;
399 // 使用 fetch join 预先加载关联的 user 对象,避免懒加载导致的 ResultSet closed 错误
400 List<Seed> seeds = query.select(s).from(s)
401 .leftJoin(s.user, u).fetchJoin()
402 .fetch();
root0dbc9812025-05-19 04:41:57 +0000403
rhj5ebd93c2025-06-07 15:57:28 +0800404 if (seeds == null || userQ == null || userQ.trim().isEmpty()) {
405 return seeds.toArray(new Seed[0]);
406 }
407
408 String processedQuery = userQ.toLowerCase().trim();
409 Map<Seed, Integer> seedCountMap = new HashMap<>();
410 for(Seed seed : seeds){
411 String title = seed.title.toLowerCase().trim();
412 int count = countCommonCharacter(processedQuery, title);
413 seedCountMap.put(seed, count);
414 }
415 seeds.sort((s1, s2) -> {
416 int count1 = seedCountMap.getOrDefault(s1, 0);
417 int count2 = seedCountMap.getOrDefault(s2, 0);
418 return Integer.compare(count2, count1);
419 });
420
root0dbc9812025-05-19 04:41:57 +0000421 return seeds.toArray(new Seed[0]);
rhj5ebd93c2025-06-07 15:57:28 +0800422 } finally {
423 em.close();
root0dbc9812025-05-19 04:41:57 +0000424 }
root0dbc9812025-05-19 04:41:57 +0000425 }
426
427 //计算字符串公共字符数量
Raverf79fdb62025-06-03 06:02:49 +0000428 private int countCommonCharacter(String str1, String str2){
root0dbc9812025-05-19 04:41:57 +0000429 Map<Character, Integer> map1 = new HashMap<>();
430 Map<Character, Integer> map2 = new HashMap<>();
431
Raverf79fdb62025-06-03 06:02:49 +0000432 for(char c : str1.toCharArray()){
root0dbc9812025-05-19 04:41:57 +0000433 if (!Character.isWhitespace(c)) {
434 map1.put(c, map1.getOrDefault(c, 0) + 1);
435 }
436 }
437
Raverf79fdb62025-06-03 06:02:49 +0000438 for(char c : str2.toCharArray()){
root0dbc9812025-05-19 04:41:57 +0000439 if (!Character.isWhitespace(c)) {
440 map2.put(c, map2.getOrDefault(c, 0) + 1);
441 }
442 }
443
444 int res = 0;
Raverf79fdb62025-06-03 06:02:49 +0000445 for(char c : map1.keySet()){
root0dbc9812025-05-19 04:41:57 +0000446 if (map2.containsKey(c)) {
447 res += Math.min(map1.get(c), map2.get(c));
448 }
Raverf79fdb62025-06-03 06:02:49 +0000449
root0dbc9812025-05-19 04:41:57 +0000450 }
451 return res;
452 }
453
454 //返回状态:0 success,1 重复,2其他原因
455 @Override
Raverf79fdb62025-06-03 06:02:49 +0000456 public int AddNotice(Notice notice){
root0dbc9812025-05-19 04:41:57 +0000457 try {
rhj5ebd93c2025-06-07 15:57:28 +0800458 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000459 JPAQuery<Notice> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000460 QNotice n = QNotice.notice;
461 Notice checkNotice = query.select(n).from(n).where(n.noticeid.eq(notice.noticeid)).fetchOne();
462 if (checkNotice != null) {
463 return 1;
464 }
Raverf79fdb62025-06-03 06:02:49 +0000465
466 entitymanager.persist(notice);
root0dbc9812025-05-19 04:41:57 +0000467 return 0;
468 } catch (Exception e) {
469 e.printStackTrace();
470 return 2;
471 }
Raverf79fdb62025-06-03 06:02:49 +0000472
root0dbc9812025-05-19 04:41:57 +0000473 }
474
475 //返回状态:0 success,1 不存在,2其他原因
476 @Override
Raverf79fdb62025-06-03 06:02:49 +0000477 public boolean UpdateNotice(Notice notice){
root0dbc9812025-05-19 04:41:57 +0000478 try {
rhj5ebd93c2025-06-07 15:57:28 +0800479 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000480 Notice oldNotice = entitymanager.find(Notice.class, notice.noticeid);
root0dbc9812025-05-19 04:41:57 +0000481 if (oldNotice == null) {
482 return false;
483 }
484 oldNotice = notice;
Raverf79fdb62025-06-03 06:02:49 +0000485 entitymanager.merge(oldNotice);
root0dbc9812025-05-19 04:41:57 +0000486 return true;
487 } catch (Exception e) {
488 e.printStackTrace();
489 return false;
490 }
491 }
492
493 //删除公告,返回状态:0 success,1 不存在,2其他原因
494 @Override
Raverf79fdb62025-06-03 06:02:49 +0000495 public boolean DeleteNotice(String noticeid){
root0dbc9812025-05-19 04:41:57 +0000496 try {
rhj5ebd93c2025-06-07 15:57:28 +0800497 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000498 Notice notice = entitymanager.find(Notice.class, noticeid);
root0dbc9812025-05-19 04:41:57 +0000499 if (notice == null) {
500 return false;
501 }
Raverf79fdb62025-06-03 06:02:49 +0000502 entitymanager.remove(notice);
root0dbc9812025-05-19 04:41:57 +0000503 return true;
504 } catch (Exception e) {
505 e.printStackTrace();
506 return false;
507 }
Raverf79fdb62025-06-03 06:02:49 +0000508
root0dbc9812025-05-19 04:41:57 +0000509 }
510
511 //获取用户的剩余邀请次数
Raverf79fdb62025-06-03 06:02:49 +0000512 public int GetUserAvailableInviteTimes(String userid){
root0dbc9812025-05-19 04:41:57 +0000513 try {
rhj5ebd93c2025-06-07 15:57:28 +0800514 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000515 JPAQuery<Integer> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000516 QUser u = QUser.user;
517 int invite_left = query.select(u.invitetimes).from(u).where(u.userid.eq(userid)).fetchOne();
Raverf79fdb62025-06-03 06:02:49 +0000518
root0dbc9812025-05-19 04:41:57 +0000519 return invite_left;
520 } catch (Exception e) {
521 e.printStackTrace();
522 return -1;
523 }
Raverf79fdb62025-06-03 06:02:49 +0000524
root0dbc9812025-05-19 04:41:57 +0000525 }
526
527 //邀请用户,返回状态:0 success,1 剩余次数不足,2,3其他原因
528 @Override
Raverf79fdb62025-06-03 06:02:49 +0000529 public int InviteUser(String inviterid,String inviteemail){
root0dbc9812025-05-19 04:41:57 +0000530 try {
rhj5ebd93c2025-06-07 15:57:28 +0800531 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000532 User user = entitymanager.find(User.class, inviterid);
root0dbc9812025-05-19 04:41:57 +0000533 if (user == null || !user.email.equals(inviteemail)) {
534 return 3;
535 }
536 if (user.invitetimes <= 0) {
537 return 1;
538 }
539 user.invitetimes -= 1;
Raverf79fdb62025-06-03 06:02:49 +0000540 entitymanager.merge(user);
root0dbc9812025-05-19 04:41:57 +0000541 return 0;
542 } catch (Exception e) {
543 e.printStackTrace();
544 return 2;
545 }
Raverf79fdb62025-06-03 06:02:49 +0000546
root0dbc9812025-05-19 04:41:57 +0000547 }
548
549 //添加一个收藏,返回状态:0 success,1 不存在,2其他原因
550 @Override
Raverf79fdb62025-06-03 06:02:49 +0000551 public boolean AddCollect(String userid,String seedid){
rhj5ebd93c2025-06-07 15:57:28 +0800552 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000553 JPAQuery<User> query2 = new JPAQuery<>(entitymanager);
554 QUser u2 = QUser.user;
555 User user = query2.select(u2).from(u2).where(u2.userid.eq(userid)).fetchOne();
556 if (user == null) {
root0dbc9812025-05-19 04:41:57 +0000557 return false;
558 }
Raverf79fdb62025-06-03 06:02:49 +0000559 JPAQuery<Seed> query3 = new JPAQuery<>(entitymanager);
560 QSeed p = QSeed.seed;
561 Seed seed = query3.select(p).from(p).where(p.seedid.eq(seedid)).fetchOne();
562 if (seed == null) {
563 return false;
564 }
565 JPAQuery<String> query = new JPAQuery<>(entitymanager);
566 QUserStar u = QUserStar.userStar;
567 List<String> allSeedId = query.select(u.seedid).from(u).where(u.userid.eq(userid)).fetch();
568
569 if (allSeedId.contains(seedid)) {
570 return false;
571 }
572 UserStar userStar = new UserStar();
573 userStar.userid = userid;
574 userStar.seedid = seedid;
575 entitymanager.persist(userStar);
576 return true;
root0dbc9812025-05-19 04:41:57 +0000577 }
578
579 //删除一个收藏,返回状态:0 success,1 不存在,2其他原因
580 @Override
Raverf79fdb62025-06-03 06:02:49 +0000581 public boolean DeleteCollect(String userid,String seedid){
root0dbc9812025-05-19 04:41:57 +0000582 try {
rhj5ebd93c2025-06-07 15:57:28 +0800583 EntityManager entitymanager = createEntityManager();
Raverf79fdb62025-06-03 06:02:49 +0000584 JPAQuery<UserStar> query = new JPAQuery<>(entitymanager);
root0dbc9812025-05-19 04:41:57 +0000585 QUserStar u = QUserStar.userStar;
586 UserStar userStar = query.select(u).from(u).where(u.userid.eq(userid).and(u.seedid.eq(seedid))).fetchOne();
587 if (userStar == null) {
588 return true; // 收藏不存在
589 }
Raverf79fdb62025-06-03 06:02:49 +0000590 entitymanager.remove(userStar);
root0dbc9812025-05-19 04:41:57 +0000591 return true;
592 } catch (Exception e) {
593 e.printStackTrace();
594 return false;
595 }
Raverf79fdb62025-06-03 06:02:49 +0000596
root0dbc9812025-05-19 04:41:57 +0000597 }
598
599 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800600 public int AddBegSeed(BegInfo info) {
601 if (info == null || info.begid == null || info.begid.isEmpty()) {
602 logger.warn("Invalid parameter: info is null or begid is empty");
603 return 2;
604 }
605
606 EntityTransaction tx = null;
607
608 try {
rhj5ebd93c2025-06-07 15:57:28 +0800609 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800610 tx = entitymanager.getTransaction();
611 tx.begin();
612
613 // 检查是否重复
614 BegInfo existingBeg = entitymanager.find(BegInfo.class, info.begid);
615 if (existingBeg != null) {
616 logger.warn("BegSeed with ID {} already exists", info.begid);
617 return 1;
618 }
619
620 // 设置默认值
621 if (info.endtime == null) {
622 // 设置默认14天截止期
623 Calendar calendar = Calendar.getInstance();
624 calendar.add(Calendar.DAY_OF_MONTH, 14);
625 info.endtime = calendar.getTime();
626 }
627 info.hasseed = 0;
628
629 // 保存新的求种信息
630 entitymanager.persist(info);
631 tx.commit();
632
633 logger.info("Successfully added new BegSeed with ID: {}", info.begid);
634 return 0;
635
636 } catch (Exception e) {
637 if (tx != null && tx.isActive()) {
638 tx.rollback();
639 }
640 logger.error("Error adding BegSeed: {}", e.getMessage());
641 return 2;
642 }
root0dbc9812025-05-19 04:41:57 +0000643 }
644
645 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800646 public int UpdateBegSeed(BegInfo info) {
647 if (info == null || info.begid == null || info.begid.isEmpty()) {
648 logger.warn("Invalid parameter: info is null or begid is empty");
649 return 2;
650 }
651
652 EntityTransaction tx = null;
653
654 try {
rhj5ebd93c2025-06-07 15:57:28 +0800655 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800656 tx = entitymanager.getTransaction();
657 tx.begin();
658
659 // 检查是否存在
660 BegInfo existingBeg = entitymanager.find(BegInfo.class, info.begid);
661 if (existingBeg == null) {
662 logger.warn("BegSeed with ID {} does not exist", info.begid);
663 return 1;
664 }
665
666 // 保持原有值不变的字段
667 info.hasseed = existingBeg.hasseed;
668 if (info.endtime == null) {
669 info.endtime = existingBeg.endtime;
670 }
671
672 // 更新求种信息
673 entitymanager.merge(info);
674 tx.commit();
675
676 logger.info("Successfully updated BegSeed with ID: {}", info.begid);
677 return 0;
678
679 } catch (Exception e) {
680 if (tx != null && tx.isActive()) {
681 tx.rollback();
682 }
683 logger.error("Error updating BegSeed: {}", e.getMessage());
684 return 2;
685 }
root0dbc9812025-05-19 04:41:57 +0000686 }
687
688 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800689 public int DeleteBegSeed(String begid) {
690 if (begid == null || begid.isEmpty()) {
691 logger.warn("Invalid parameter: begid is null or empty");
692 return 2;
693 }
694
695 EntityTransaction tx = null;
696
697 try {
rhj5ebd93c2025-06-07 15:57:28 +0800698 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800699 tx = entitymanager.getTransaction();
700 tx.begin();
701
702 // 查找要删除的求种信息
703 BegInfo begInfo = entitymanager.find(BegInfo.class, begid);
704 if (begInfo == null) {
705 logger.warn("BegSeed with ID {} does not exist", begid);
706 tx.rollback();
707 return 1;
708 }
709
710 // 删除求种信息
711 entitymanager.remove(begInfo);
712 tx.commit();
713
714 logger.info("Successfully deleted BegSeed with ID: {}", begid);
715 return 0;
716
717 } catch (Exception e) {
718 if (tx != null && tx.isActive()) {
719 tx.rollback();
720 }
721 logger.error("Error deleting BegSeed: {}", e.getMessage());
722 return 2;
723 }
root0dbc9812025-05-19 04:41:57 +0000724 }
725
726 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800727 public int VoteSeed(String begId, String seedId, String userId) {
728 if (begId == null || seedId == null || userId == null ||
729 begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) {
730 logger.warn("Invalid parameters: begId, seedId or userId is null or empty");
731 return 2;
732 }
733
734 EntityTransaction tx = null;
735
736 try {
rhj5ebd93c2025-06-07 15:57:28 +0800737 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800738 tx = entitymanager.getTransaction();
739 tx.begin();
740
741 // 检查求种信息是否存在
742 BegInfo begInfo = entitymanager.find(BegInfo.class, begId);
743 if (begInfo == null) {
744 logger.warn("BegSeed with ID {} does not exist", begId);
745 return 2;
746 }
747
748 // 检查用户是否已投票
749 Long voteCount = new JPAQuery<>(entitymanager)
750 .select(QUserVotes.userVotes.count())
751 .from(QUserVotes.userVotes)
752 .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId)))
753 .fetchOne();
754
755 if (voteCount != null && voteCount > 0) {
756 logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId);
757 return 1;
758 }
759
760 // 创建新的投票记录
761 entitymanager.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " +
762 "VALUES (?, ?, ?, CURRENT_TIMESTAMP)")
763 .setParameter(1, userId)
764 .setParameter(2, begId)
765 .setParameter(3, seedId)
766 .executeUpdate();
767
768 // 更新SubmitSeed表中的投票数
769 entitymanager.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " +
770 "WHERE s.id.begId = :begId AND s.id.seedId = :seedId")
771 .setParameter("begId", begId)
772 .setParameter("seedId", seedId)
773 .executeUpdate();
774
775 tx.commit();
776 logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId);
777 return 0;
778
779 } catch (Exception e) {
780 if (tx != null && tx.isActive()) {
781 tx.rollback();
782 }
783 logger.error("Error voting for seed: {}", e.getMessage());
784 return 2;
785 }
root0dbc9812025-05-19 04:41:57 +0000786 }
787
788 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800789 public int SubmitSeed(String begid, Seed seed) {
790 if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) {
791 logger.warn("Invalid parameters: begid or seed is null or empty");
792 return 2;
793 }
794
795 EntityTransaction tx = null;
796
797 try {
rhj5ebd93c2025-06-07 15:57:28 +0800798 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800799 tx = entitymanager.getTransaction();
800 tx.begin();
801
802 // 检查求种信息是否存在
803 BegInfo begInfo = entitymanager.find(BegInfo.class, begid);
804 if (begInfo == null) {
805 logger.warn("BegSeed with ID {} does not exist", begid);
806 return 2;
807 }
808
809 // 检查种子是否已提交过
810 QSubmitSeed ss = QSubmitSeed.submitSeed;
811 Long submitCount = new JPAQuery<>(entitymanager)
812 .select(ss.count())
813 .from(ss)
814 .where(ss.begInfo.begid.eq(begid))
815 .where(ss.seed.seedid.eq(seed.seedid))
816 .fetchOne();
817
818 if (submitCount > 0) {
819 logger.warn("Seed {} has already been submitted for beg {}", seed.seedid,
820 begid);
821 return 1;
822 }
823
824 // 保存种子信息(如果不存在)
825 if (entitymanager.find(Seed.class, seed.seedid) == null) {
826 entitymanager.persist(seed);
827 }
828
829 // 创建提交记录
830 entitymanager.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)")
831 .setParameter(1, begid)
832 .setParameter(2, seed.seedid)
833 .executeUpdate();
834
835 tx.commit();
836 logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid);
837 return 0;
838
839 } catch (Exception e) {
840 if (tx != null && tx.isActive()) {
841 tx.rollback();
842 }
843 logger.error("Error submitting seed: {}", e.getMessage());
844 return 2;
845 }
root0dbc9812025-05-19 04:41:57 +0000846 }
847
848 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800849 public void SettleBeg() {
850 EntityTransaction tx = null;
851
852 try {
rhj5ebd93c2025-06-07 15:57:28 +0800853 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800854 tx = entitymanager.getTransaction();
855 tx.begin();
856
857 // 1. 获取所有已过期且未完成的求种信息
858 QBegInfo b = QBegInfo.begInfo;
859 List<BegInfo> expiredBegs = new JPAQuery<>(entitymanager)
860 .select(b)
861 .from(b)
862 .where(b.endtime.loe(new Date())
863 .and(b.hasseed.eq(0)))
864 .fetch();
865
866 for (BegInfo beg : expiredBegs) {
867 // 2. 查找投票最多的提交任务
868 QSubmitSeed ss = QSubmitSeed.submitSeed;
869 Tuple topSubmission = new JPAQuery<>(entitymanager)
870 .select(ss.seed.seedid, ss.votes)
871 .from(ss)
872 .where(ss.begInfo.begid.eq(beg.begid))
873 .orderBy(ss.votes.desc())
874 .limit(1)
875 .fetchOne();
876
877 if (topSubmission != null && topSubmission.get(ss.votes) > 0) {
878 String seedId = topSubmission.get(ss.seed.seedid);
879
880 // 3. 获取上传者ID
881 QSeed s = QSeed.seed;
882 String ownerId = new JPAQuery<>(entitymanager)
883 .select(s.seeduserid)
884 .from(s)
885 .where(s.seedid.eq(seedId))
886 .fetchOne();
887
888 // 4. 获取上传者的PT信息并更新魔力值
889 UserPT ownerPT = entitymanager.find(UserPT.class, ownerId);
890 if (ownerPT != null) {
891 // 5. 发放奖励
892 ownerPT.magic += beg.magic;
893 entitymanager.merge(ownerPT);
894
895 // 6. 更新求种状态
896 beg.hasseed = 1;
897 entitymanager.merge(beg);
898
899 logger.info("Reward {} magic points awarded to user {} for beg {}",
900 beg.magic, ownerId, beg.begid);
901 }
902 }
903 }
904
905 tx.commit();
906 logger.info("Successfully settled {} expired beg requests",
907 expiredBegs.size());
908
909 } catch (Exception e) {
910 if (tx != null && tx.isActive()) {
911 tx.rollback();
912 }
913 logger.error("Error settling beg requests: {}", e.getMessage(), e);
914 }
root0dbc9812025-05-19 04:41:57 +0000915 }
916
917 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800918 public int AddPost(Post post) {
919 if (post == null || post.postid == null || post.postid.isEmpty()) {
920 logger.warn("Invalid parameter: post is null or postid is empty");
921 return 2;
922 }
923
924 EntityTransaction tx = null;
925
926 try {
rhj5ebd93c2025-06-07 15:57:28 +0800927 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800928 tx = entitymanager.getTransaction();
929 tx.begin();
930
931 // 检查是否重复
932 Post existingPost = entitymanager.find(Post.class, post.postid);
933 if (existingPost != null) {
934 logger.warn("Post with ID {} already exists", post.postid);
935 return 1;
936 }
937
938 // 检查用户是否存在
939 User user = entitymanager.find(User.class, post.postuserid);
940 if (user == null) {
941 logger.warn("User with ID {} does not exist", post.postuserid);
942 return 2;
943 }
944
945 // 设置初始值
946 if (post.posttime == null) {
947 post.posttime = new Date();
948 }
949 post.replytime = 0;
950 post.readtime = 0;
951
952 // 保存帖子
953 entitymanager.persist(post);
954 tx.commit();
955
956 logger.info("Successfully added new post with ID: {}", post.postid);
957 return 0;
958
959 } catch (Exception e) {
960 if (tx != null && tx.isActive()) {
961 tx.rollback();
962 }
963 logger.error("Error adding post: {}", e.getMessage());
964 return 2;
965 }
root0dbc9812025-05-19 04:41:57 +0000966 }
967
968 @Override
TRM-coding87c24972025-06-07 14:05:29 +0800969 public int UpdatePost(Post post) {
970 if (post == null || post.postid == null || post.postid.isEmpty()) {
971 logger.warn("Invalid parameter: post is null or postid is empty");
972 return 2;
973 }
974
975 EntityTransaction tx = null;
976
977 try {
rhj5ebd93c2025-06-07 15:57:28 +0800978 EntityManager entitymanager = createEntityManager();
TRM-coding87c24972025-06-07 14:05:29 +0800979 tx = entitymanager.getTransaction();
980 tx.begin();
981
982 // 检查帖子是否存在
983 Post existingPost = entitymanager.find(Post.class, post.postid);
984 if (existingPost == null) {
985 logger.warn("Post with ID {} does not exist", post.postid);
986 return 1;
987 }
988
989 // 保持原有不可修改的字段
990 post.postuserid = existingPost.postuserid;
991 post.posttime = existingPost.posttime;
992 post.replytime = existingPost.replytime;
993 post.readtime = existingPost.readtime;
994
995 // 更新帖子
996 entitymanager.merge(post);
997 tx.commit();
998
999 logger.info("Successfully updated post with ID: {}", post.postid);
1000 return 0;
1001
1002 } catch (Exception e) {
1003 if (tx != null && tx.isActive()) {
1004 tx.rollback();
1005 }
1006 logger.error("Error updating post: {}", e.getMessage());
1007 return 2;
1008 }
root0dbc9812025-05-19 04:41:57 +00001009 }
1010
1011 @Override
Raverf79fdb62025-06-03 06:02:49 +00001012 public int DeletePost(String postid){
TRM-coding87c24972025-06-07 14:05:29 +08001013 if (postid == null || postid.isEmpty()) {
1014 logger.warn("Invalid parameter: postid is null or empty");
1015 return 2;
1016 }
1017
1018 EntityManager em = null;
1019 EntityTransaction tx = null;
1020
1021 try {
1022 em = emf.createEntityManager();
1023 tx = em.getTransaction();
1024 tx.begin();
1025
1026 // 查找要删除的帖子
1027 Post post = em.find(Post.class, postid);
1028 if (post == null) {
1029 logger.warn("Post with ID {} does not exist", postid);
1030 tx.rollback();
1031 return 1;
1032 }
1033
1034 // 删除帖子(由于设置了级联删除,相关的回复会自动删除)
1035 em.remove(post);
1036 tx.commit();
1037
1038 logger.info("Successfully deleted post with ID: {}", postid);
1039 return 0;
1040
1041 } catch (Exception e) {
1042 if (tx != null && tx.isActive()) {
1043 tx.rollback();
1044 }
1045 logger.error("Error deleting post: {}", e.getMessage());
1046 return 2;
1047 } finally {
1048 if (em != null) {
1049 em.close();
1050 }
1051 }
root0dbc9812025-05-19 04:41:57 +00001052 }
1053
1054 @Override
Raverf79fdb62025-06-03 06:02:49 +00001055 public int AddComment(String postid, String userid, String comment){
TRM-coding87c24972025-06-07 14:05:29 +08001056 if (postid == null || postid.isEmpty() ||
1057 userid == null || userid.isEmpty() ||
1058 comment == null || comment.isEmpty()) {
1059 logger.warn("Invalid parameters: postid, userid or comment is null or empty");
1060 return 2;
1061 }
1062
1063 EntityManager em = null;
1064 EntityTransaction tx = null;
1065
1066 try {
1067 em = emf.createEntityManager();
1068 tx = em.getTransaction();
1069 tx.begin();
1070
1071 // 检查帖子是否存在
1072 Post post = em.find(Post.class, postid);
1073 if (post == null) {
1074 logger.warn("Post with ID {} does not exist", postid);
1075 return 1;
1076 }
1077
1078 // 检查评论用户是否存在
1079 User user = em.find(User.class, userid);
1080 if (user == null) {
1081 logger.warn("User with ID {} does not exist", userid);
1082 return 1;
1083 }
1084
1085 // 创建新的评论
1086 PostReply reply = new PostReply();
1087 reply.replyid = UUID.randomUUID().toString();
1088 reply.postid = postid;
1089 reply.authorid = userid;
1090 reply.content = comment;
1091 reply.createdAt = new Date();
1092
1093 // 保存评论
1094 em.persist(reply);
1095
1096 // 更新帖子的回复数
1097 post.replytime += 1;
1098 em.merge(post);
1099
1100 tx.commit();
1101
1102 logger.info("Successfully added comment by user {} to post {}, reply ID: {}",
1103 userid, postid, reply.replyid);
1104 return 0;
1105
1106 } catch (Exception e) {
1107 if (tx != null && tx.isActive()) {
1108 tx.rollback();
1109 }
1110 logger.error("Error adding comment: {}", e.getMessage());
1111 return 2;
1112 } finally {
1113 if (em != null) {
1114 em.close();
1115 }
1116 }
root0dbc9812025-05-19 04:41:57 +00001117 }
1118
1119 @Override
Raverf79fdb62025-06-03 06:02:49 +00001120 public int DeleteComment(String postid,String commentid){
TRM-coding87c24972025-06-07 14:05:29 +08001121 if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) {
1122 logger.warn("Invalid parameters: postid or commentid is null or empty");
1123 return 2;
1124 }
1125
1126 EntityManager em = null;
1127 EntityTransaction tx = null;
1128
1129 try {
1130 em = emf.createEntityManager();
1131 tx = em.getTransaction();
1132 tx.begin();
1133
1134 // 检查帖子是否存在
1135 Post post = em.find(Post.class, postid);
1136 if (post == null) {
1137 logger.warn("Post with ID {} does not exist", postid);
1138 return 1;
1139 }
1140
1141 // 检查评论是否存在且属于该帖子
1142 PostReply reply = em.find(PostReply.class, commentid);
1143 if (reply == null || !reply.postid.equals(postid)) {
1144 logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid);
1145 return 1;
1146 }
1147
1148 // 删除评论
1149 em.remove(reply);
1150
1151 // 更新帖子的回复数
1152 post.replytime = Math.max(0, post.replytime - 1);
1153 em.merge(post);
1154
1155 tx.commit();
1156
1157 logger.info("Successfully deleted comment {} from post {}", commentid, postid);
1158 return 0;
1159
1160 } catch (Exception e) {
1161 if (tx != null && tx.isActive()) {
1162 tx.rollback();
1163 }
1164 logger.error("Error deleting comment: {}", e.getMessage());
1165 return 2;
1166 } finally {
1167 if (em != null) {
1168 em.close();
1169 }
1170 }
root0dbc9812025-05-19 04:41:57 +00001171 }
1172
1173 @Override
Raverf79fdb62025-06-03 06:02:49 +00001174 public boolean ExchangeMagicToUpload(String userid,int magic)//将魔力值兑换为上传量,返回状态:0 success,1 不存在,2其他原因
root0dbc9812025-05-19 04:41:57 +00001175 {
TRM-coding87c24972025-06-07 14:05:29 +08001176 if (userid == null || userid.isEmpty() || magic <= 0) {
1177 logger.warn("参数无效: userid为空或magic <= 0");
1178 return false;
1179 }
1180
1181 EntityManager em = null;
1182 EntityTransaction tx = null;
1183
1184 try {
1185 em = emf.createEntityManager();
1186 tx = em.getTransaction();
1187 tx.begin();
1188
1189 UserPT userPT = em.find(UserPT.class, userid);
1190 if (userPT == null) {
1191 logger.warn("未找到用户 {} 的PT信息", userid);
1192 return false;
1193 }
1194
1195 if (userPT.magic < magic) {
1196 logger.warn("用户 {} 的魔力值不足", userid);
1197 return false;
1198 }
1199
1200 // 1:1兑换,直接加上魔力值(假设单位是MB)
1201 userPT.magic -= magic;
1202 userPT.upload += magic;
1203
1204 // 更新分享率
1205 if (userPT.download > 0) {
1206 userPT.share = (double) userPT.upload / userPT.download;
1207 }
1208
1209 em.merge(userPT);
1210 tx.commit();
1211
1212 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic);
1213 return true;
1214
1215 } catch (Exception e) {
1216 if (tx != null && tx.isActive()) {
1217 tx.rollback();
1218 }
1219 logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage());
1220 return false;
1221 } finally {
1222 if (em != null) {
1223 em.close();
1224 }
1225 }
root0dbc9812025-05-19 04:41:57 +00001226 }
Raverf79fdb62025-06-03 06:02:49 +00001227
root0dbc9812025-05-19 04:41:57 +00001228 @Override
Raverf79fdb62025-06-03 06:02:49 +00001229 public boolean ExchangeMagicToDownload(String userid,int magic)
1230 {
TRM-coding87c24972025-06-07 14:05:29 +08001231 if (userid == null || userid.isEmpty() || magic <= 0) {
1232 logger.warn("参数无效: userid为空或magic <= 0");
1233 return false;
1234 }
1235
1236 EntityManager em = null;
1237 EntityTransaction tx = null;
1238
1239 try {
1240 em = emf.createEntityManager();
1241 tx = em.getTransaction();
1242 tx.begin();
1243
1244 UserPT userPT = em.find(UserPT.class, userid);
1245 if (userPT == null) {
1246 logger.warn("未找到用户 {} 的PT信息", userid);
1247 return false;
1248 }
1249
1250 if (userPT.magic < magic) {
1251 logger.warn("用户 {} 的魔力值不足", userid);
1252 return false;
1253 }
1254
1255 // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB)
1256 userPT.magic -= magic;
1257 userPT.download = Math.max(0, userPT.download - magic);
1258
1259 // 更新分享率
1260 if (userPT.download > 0) {
1261 userPT.share = (double) userPT.upload / userPT.download;
1262 }
1263
1264 em.merge(userPT);
1265 tx.commit();
1266
1267 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic);
1268 return true;
1269
1270 } catch (Exception e) {
1271 if (tx != null && tx.isActive()) {
1272 tx.rollback();
1273 }
1274 logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage());
1275 return false;
1276 } finally {
1277 if (em != null) {
1278 em.close();
1279 }
1280 }
root0dbc9812025-05-19 04:41:57 +00001281 }//将魔力值兑换为下载量,返回状态:0 success,1 不存在,2其他原因
1282
1283 @Override
Raverf79fdb62025-06-03 06:02:49 +00001284 public boolean ExchangeMagicToVip(String userid,int magic){
TRM-coding87c24972025-06-07 14:05:29 +08001285 if (userid == null || userid.isEmpty() || magic <= 0) {
1286 logger.warn("参数无效: userid为空或magic <= 0");
1287 return false;
1288 }
1289
1290 EntityManager em = null;
1291 EntityTransaction tx = null;
1292
1293 try {
1294 em = emf.createEntityManager();
1295 tx = em.getTransaction();
1296 tx.begin();
1297
1298 UserPT userPT = em.find(UserPT.class, userid);
1299 if (userPT == null) {
1300 logger.warn("未找到用户 {} 的PT信息", userid);
1301 return false;
1302 }
1303
1304 if (userPT.magic < magic) {
1305 logger.warn("用户 {} 的魔力值不足", userid);
1306 return false;
1307 }
1308
1309 // 1:1兑换VIP下载次数
1310 userPT.magic -= magic;
1311 userPT.viptime += magic;
1312
1313 em.merge(userPT);
1314 tx.commit();
1315
1316 logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic);
1317 return true;
1318
1319 } catch (Exception e) {
1320 if (tx != null && tx.isActive()) {
1321 tx.rollback();
1322 }
1323 logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage());
1324 return false;
1325 } finally {
1326 if (em != null) {
1327 em.close();
1328 }
1329 }
root0dbc9812025-05-19 04:41:57 +00001330 }
1331 //将魔力值兑换为VIP次数,返回状态:0 success,1 不存在,2其他原因
1332
1333 @Override
Raverf79fdb62025-06-03 06:02:49 +00001334 public boolean UploadTransmitProfile(Profile profile){
TRM-coding87c24972025-06-07 14:05:29 +08001335 if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() ||
1336 profile.userid == null || profile.userid.isEmpty()) {
1337 logger.warn("参数无效: profile为空或必要字段为空");
1338 return false;
1339 }
1340
1341 EntityManager em = null;
1342 EntityTransaction tx = null;
1343
1344 try {
1345 em = emf.createEntityManager();
1346 tx = em.getTransaction();
1347 tx.begin();
1348
1349 // 检查用户是否存在
1350 User user = em.find(User.class, profile.userid);
1351 if (user == null) {
1352 logger.warn("用户 {} 不存在", profile.userid);
1353 return false;
1354 }
1355
1356 // 检查是否已存在相同的迁移申请
1357 Profile existingProfile = em.find(Profile.class, profile.profileurl);
1358 if (existingProfile != null) {
1359 logger.warn("迁移申请 {} 已存在", profile.profileurl);
1360 return false;
1361 }
1362
1363 // 设置初始值
1364 profile.exampass = false;
1365 profile.magicgived = "0";
1366 profile.uploadgived = "0";
1367
1368 // 保存迁移申请
1369 em.persist(profile);
1370 tx.commit();
1371
1372 logger.info("成功上传迁移申请 {}", profile.profileurl);
1373 return true;
1374
1375 } catch (Exception e) {
1376 if (tx != null && tx.isActive()) {
1377 tx.rollback();
1378 }
1379 logger.error("上传迁移申请时发生错误: {}", e.getMessage());
1380 return false;
1381 } finally {
1382 if (em != null) {
1383 em.close();
1384 }
1385 }
root0dbc9812025-05-19 04:41:57 +00001386 }
1387
1388 @Override
Raverf79fdb62025-06-03 06:02:49 +00001389 public Profile GetTransmitProfile(String profileid){
TRM-coding87c24972025-06-07 14:05:29 +08001390 if (profileid == null || profileid.isEmpty()) {
1391 logger.warn("参数无效: profileid为空");
1392 return null;
1393 }
1394
1395 EntityManager em = null;
1396
1397 try {
1398 em = emf.createEntityManager();
1399 Profile profile = em.find(Profile.class, profileid);
1400 if (profile == null) {
1401 logger.warn("未找到迁移申请 {}", profileid);
1402 return null;
1403 }
1404
1405 logger.info("成功获取迁移申请 {}", profileid);
1406 return profile;
1407
1408 } catch (Exception e) {
1409 logger.error("获取迁移申请时发生错误: {}", e.getMessage());
1410 return null;
1411 } finally {
1412 if (em != null) {
1413 em.close();
1414 }
1415 }
root0dbc9812025-05-19 04:41:57 +00001416 }
1417 //获取迁移信息
Raverf79fdb62025-06-03 06:02:49 +00001418
root0dbc9812025-05-19 04:41:57 +00001419 @Override
Raverf79fdb62025-06-03 06:02:49 +00001420 public boolean ExamTransmitProfile(String profileid,boolean result){
TRM-coding87c24972025-06-07 14:05:29 +08001421 if (profileid == null || profileid.isEmpty()) {
1422 logger.warn("参数无效: profileid为空");
1423 return false;
1424 }
1425
1426 EntityManager em = null;
1427 EntityTransaction tx = null;
1428
1429 try {
1430 em = emf.createEntityManager();
1431 tx = em.getTransaction();
1432 tx.begin();
1433
1434 // 查找迁移申请
1435 Profile profile = em.find(Profile.class, profileid);
1436 if (profile == null) {
1437 logger.warn("未找到迁移申请 {}", profileid);
1438 return false;
1439 }
1440
1441 // 更新审核状态
1442 profile.exampass = result;
1443
1444 if (result) {
1445 // 如果审核通过,更新用户的PT信息
1446 UserPT userPT = em.find(UserPT.class, profile.userid);
1447 if (userPT != null) {
1448 // 发放魔力值
1449 int magicToGive = Integer.parseInt(profile.magictogive);
1450 userPT.magic += magicToGive;
1451 profile.magicgived = String.valueOf(magicToGive);
1452
1453 // 发放上传量
1454 long uploadToGive = Long.parseLong(profile.uploadtogive);
1455 userPT.upload += uploadToGive;
1456 profile.uploadgived = String.valueOf(uploadToGive);
1457
1458 em.merge(userPT);
1459 }
1460 }
1461
1462 em.merge(profile);
1463 tx.commit();
1464
1465 logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result);
1466 return true;
1467
1468 } catch (Exception e) {
1469 if (tx != null && tx.isActive()) {
1470 tx.rollback();
1471 }
1472 logger.error("审核迁移申请时发生错误: {}", e.getMessage());
1473 return false;
1474 } finally {
1475 if (em != null) {
1476 em.close();
1477 }
1478 }
root0dbc9812025-05-19 04:41:57 +00001479 }
1480 //审核迁移信息,0成功,1失败
1481 @Override
Raverf79fdb62025-06-03 06:02:49 +00001482 public Profile[] GetTransmitProfileList(){
TRM-coding87c24972025-06-07 14:05:29 +08001483 EntityManager em = null;
1484
1485 try {
1486 em = emf.createEntityManager();
1487
1488 // 获取所有迁移申请
1489 QProfile p = QProfile.profile;
1490 List<Profile> profiles = new JPAQuery<>(em)
1491 .select(p)
1492 .from(p)
1493 .fetch();
1494
1495 logger.info("成功获取所有迁移申请,共 {} 条", profiles.size());
1496 return profiles.toArray(new Profile[0]);
1497
1498 } catch (Exception e) {
1499 logger.error("获取迁移申请列表时发生错误: {}", e.getMessage());
1500 return new Profile[0];
1501 } finally {
1502 if (em != null) {
1503 em.close();
1504 }
1505 }
root0dbc9812025-05-19 04:41:57 +00001506 }
1507 //获取所有迁移信息
1508
rhj46f62c42025-06-06 23:24:10 +08001509 @Override
1510 public Post[] GetPostList() {
rhj5ebd93c2025-06-07 15:57:28 +08001511 EntityManager entitymanager = createEntityManager();
rhj46f62c42025-06-06 23:24:10 +08001512 JPAQuery<Post> query = new JPAQuery<>(entitymanager);
1513 QPost p = QPost.post;
1514 List<Post> posts = query.select(p).from(p).fetch();
1515 return posts.toArray(new Post[0]);
1516 }
1517
rhj5b69b7e2025-06-07 01:28:08 +08001518 @Override
1519 public Post GetPost(String postid) {
rhj5ebd93c2025-06-07 15:57:28 +08001520 EntityManager entitymanager = createEntityManager();
rhj5b69b7e2025-06-07 01:28:08 +08001521 JPAQuery<Post> query = new JPAQuery<>(entitymanager);
1522 QPost p = QPost.post;
1523 Post post = query.select(p).from(p).where(p.postid.eq(postid)).fetchOne();
1524 return post;
1525 }
1526
1527 @Override
1528 public PostReply[] GetPostReplyList(String postid) {
rhj5ebd93c2025-06-07 15:57:28 +08001529 EntityManager entitymanager = createEntityManager();
rhj5b69b7e2025-06-07 01:28:08 +08001530 JPAQuery<PostReply> query = new JPAQuery<>(entitymanager);
1531 QPostReply p = QPostReply.postReply;
1532 List<PostReply> replies = query.select(p).from(p).where(p.postid.eq(postid)).fetch();
1533 return replies.toArray(new PostReply[0]);
1534 }
rhj5ebd93c2025-06-07 15:57:28 +08001535
1536 @Override
1537 public Post[] SearchPost(String postQ) {
1538 EntityManager entitymanager = createEntityManager();
1539 JPAQuery<Post> query = new JPAQuery<>(entitymanager);
1540 QPost p = QPost.post;
1541 List<Post> posts = query.select(p).from(p).fetch();
1542
1543 if (posts == null || postQ == null || postQ.trim().isEmpty()) {
1544 return posts.toArray(new Post[0]);
1545 }
1546
1547 String processedQuery = postQ.toLowerCase().trim();
1548 Map<Post, Integer> postCountMap = new HashMap<>();
1549 for(Post post : posts){
1550 String title = post.posttitle.toLowerCase().trim();
1551 int count = countCommonCharacter(processedQuery, title);
1552 postCountMap.put(post, count);
1553 }
1554 posts.sort((s1, s2) -> {
1555 int count1 = postCountMap.getOrDefault(s1, 0);
1556 int count2 = postCountMap.getOrDefault(s2, 0);
1557 return Integer.compare(count2, count1);
1558 });
1559
1560 return posts.toArray(new Post[0]);
1561 }
root0dbc9812025-05-19 04:41:57 +00001562}
Raverf79fdb62025-06-03 06:02:49 +00001563