root | cd43656 | 2025-05-08 14:09:19 +0000 | [diff] [blame] | 1 | package database; |
| 2 | |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 3 | import java.util.Calendar; |
| 4 | import java.util.UUID; |
| 5 | import java.util.List; |
rhj | e18c3f7 | 2025-06-08 00:27:01 +0800 | [diff] [blame^] | 6 | import java.util.ArrayList; |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 7 | import java.util.Map; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 8 | import java.util.Date; |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 9 | import java.util.HashMap; |
rhj | e18c3f7 | 2025-06-08 00:27:01 +0800 | [diff] [blame^] | 10 | import java.io.File; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 11 | |
| 12 | import javax.persistence.EntityManager; |
| 13 | import javax.persistence.EntityManagerFactory; |
| 14 | import javax.persistence.EntityTransaction; |
| 15 | import javax.persistence.Persistence; |
| 16 | |
| 17 | import com.querydsl.jpa.impl.JPAQuery; |
| 18 | import com.querydsl.core.Tuple; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 19 | import entity.QBegInfo; |
| 20 | import entity.QUserVotes; |
| 21 | import entity.QSubmitSeed; |
| 22 | import entity.QProfile; |
| 23 | import entity.QSeed; |
| 24 | import entity.Seed; |
| 25 | import entity.User; |
| 26 | import entity.UserPT; |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 27 | import entity.config; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 28 | import entity.Notice; |
| 29 | import entity.BegInfo; |
rhj | e18c3f7 | 2025-06-08 00:27:01 +0800 | [diff] [blame^] | 30 | import entity.BegInfoDetail; |
| 31 | import entity.BegSeedDetail; |
root | ff0769a | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 32 | import entity.Post; |
| 33 | import entity.PostReply; |
| 34 | import entity.Profile; |
rhj | e18c3f7 | 2025-06-08 00:27:01 +0800 | [diff] [blame^] | 35 | import entity.UserStar; |
| 36 | import entity.SeedWithVotes; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 37 | |
| 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
| 40 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 41 | public class Database2 implements DataManagerInterface { |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 42 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 43 | private EntityManagerFactory emf; |
| 44 | private static final Logger logger = LoggerFactory.getLogger(Database2.class); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 45 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 46 | // 构造函数,初始化EntityManagerFactory |
| 47 | public Database2() { |
| 48 | config cfg = new config(); |
| 49 | Map<String, Object> props = new HashMap<>(); |
| 50 | props.put("javax.persistence.jdbc.url", |
| 51 | "jdbc:mysql://" + cfg.SqlURL + "/" + cfg.Database); |
| 52 | props.put("javax.persistence.jdbc.user", cfg.SqlUsername); |
| 53 | props.put("javax.persistence.jdbc.password", cfg.SqlPassword); |
| 54 | this.emf = Persistence.createEntityManagerFactory("myPersistenceUnit", props); |
| 55 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 56 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 57 | public Database2(EntityManagerFactory emf) { |
| 58 | this.emf = emf; |
| 59 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 60 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 61 | @Override |
TRM-coding | cdfe548 | 2025-06-06 17:31:01 +0800 | [diff] [blame] | 62 | public String LoginUser(User userinfo){ |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | @Override |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 67 | public int RegisterUser(User userinfo) { |
| 68 | return 0; |
| 69 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 70 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 71 | @Override |
| 72 | public int UpdateInformation(User userinfo) { |
| 73 | return 0; |
| 74 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 75 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 76 | @Override |
| 77 | public User GetInformation(String userid) { |
| 78 | return null; |
| 79 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 80 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 81 | @Override |
| 82 | public UserPT GetInformationPT(String userid) { |
| 83 | return null; |
| 84 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 85 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 86 | @Override |
| 87 | public int UpdateInformationPT(UserPT userinfo) { |
| 88 | return 0; |
| 89 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 90 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 91 | @Override |
| 92 | public int RegisterUserPT(UserPT userinfo) { |
| 93 | return 0; |
| 94 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 95 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 96 | @Override |
| 97 | public Seed GetSeedInformation(String seedid) { |
| 98 | return null; |
| 99 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 100 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 101 | @Override |
| 102 | public int RegisterSeed(Seed seedinfo) { |
| 103 | return 0; |
| 104 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 105 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 106 | @Override |
| 107 | public int UpdateSeed(Seed seedinfo) { |
| 108 | return 0; |
| 109 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 110 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 111 | @Override |
| 112 | public Seed[] SearchSeed(String userQ) { |
| 113 | return new Seed[0]; |
| 114 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 115 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 116 | @Override |
| 117 | public int AddNotice(Notice notice) { |
| 118 | return 0; |
| 119 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 120 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 121 | @Override |
| 122 | public boolean UpdateNotice(Notice notice) { |
| 123 | return false; |
| 124 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 125 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 126 | @Override |
| 127 | public boolean DeleteNotice(String noticeid) { |
| 128 | return false; |
| 129 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 130 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 131 | @Override |
| 132 | public int GetUserAvailableInviteTimes(String userid) { |
| 133 | return 0; |
| 134 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 135 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 136 | @Override |
| 137 | public int InviteUser(String inviterid, String inviteemail) { |
| 138 | return 0; |
| 139 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 140 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 141 | @Override |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 142 | public Seed[] GetSeedListByTag(String tag) { |
| 143 | return new Seed[0]; |
| 144 | } |
| 145 | |
| 146 | @Override |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 147 | public Seed[] GetSeedListByUser(String userid) { |
| 148 | return new Seed[0]; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public int DeleteSeed(String seedid) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | @Override |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 157 | public boolean AddCollect(String userid, String postid) { |
| 158 | return false; |
| 159 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 160 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 161 | @Override |
| 162 | public boolean DeleteCollect(String userid, String postid) { |
| 163 | return false; |
| 164 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 165 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 166 | @Override |
| 167 | public int AddBegSeed(BegInfo info) { |
| 168 | if (info == null || info.begid == null || info.begid.isEmpty()) { |
| 169 | logger.warn("Invalid parameter: info is null or begid is empty"); |
| 170 | return 2; |
| 171 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 172 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 173 | EntityManager em = null; |
| 174 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 175 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 176 | try { |
| 177 | em = emf.createEntityManager(); |
| 178 | tx = em.getTransaction(); |
| 179 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 180 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 181 | // 检查是否重复 |
| 182 | BegInfo existingBeg = em.find(BegInfo.class, info.begid); |
| 183 | if (existingBeg != null) { |
| 184 | logger.warn("BegSeed with ID {} already exists", info.begid); |
| 185 | return 1; |
| 186 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 187 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 188 | // 设置默认值 |
| 189 | if (info.endtime == null) { |
| 190 | // 设置默认14天截止期 |
| 191 | Calendar calendar = Calendar.getInstance(); |
| 192 | calendar.add(Calendar.DAY_OF_MONTH, 14); |
| 193 | info.endtime = calendar.getTime(); |
| 194 | } |
| 195 | info.hasseed = 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 196 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 197 | // 保存新的求种信息 |
| 198 | em.persist(info); |
| 199 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 200 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 201 | logger.info("Successfully added new BegSeed with ID: {}", info.begid); |
| 202 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 203 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 204 | } catch (Exception e) { |
| 205 | if (tx != null && tx.isActive()) { |
| 206 | tx.rollback(); |
| 207 | } |
| 208 | logger.error("Error adding BegSeed: {}", e.getMessage()); |
| 209 | return 2; |
| 210 | } finally { |
| 211 | if (em != null) { |
| 212 | em.close(); |
| 213 | } |
| 214 | } |
| 215 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 216 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 217 | @Override |
| 218 | public int UpdateBegSeed(BegInfo info) { |
| 219 | if (info == null || info.begid == null || info.begid.isEmpty()) { |
| 220 | logger.warn("Invalid parameter: info is null or begid is empty"); |
| 221 | return 2; |
| 222 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 223 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 224 | EntityManager em = null; |
| 225 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 226 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 227 | try { |
| 228 | em = emf.createEntityManager(); |
| 229 | tx = em.getTransaction(); |
| 230 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 231 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 232 | // 检查是否存在 |
| 233 | BegInfo existingBeg = em.find(BegInfo.class, info.begid); |
| 234 | if (existingBeg == null) { |
| 235 | logger.warn("BegSeed with ID {} does not exist", info.begid); |
| 236 | return 1; |
| 237 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 238 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 239 | // 保持原有值不变的字段 |
| 240 | info.hasseed = existingBeg.hasseed; |
| 241 | if (info.endtime == null) { |
| 242 | info.endtime = existingBeg.endtime; |
| 243 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 244 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 245 | // 更新求种信息 |
| 246 | em.merge(info); |
| 247 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 248 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 249 | logger.info("Successfully updated BegSeed with ID: {}", info.begid); |
| 250 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 251 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 252 | } catch (Exception e) { |
| 253 | if (tx != null && tx.isActive()) { |
| 254 | tx.rollback(); |
| 255 | } |
| 256 | logger.error("Error updating BegSeed: {}", e.getMessage()); |
| 257 | return 2; |
| 258 | } finally { |
| 259 | if (em != null) { |
| 260 | em.close(); |
| 261 | } |
| 262 | } |
| 263 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 264 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 265 | @Override |
| 266 | public int DeleteBegSeed(String begid) { |
| 267 | if (begid == null || begid.isEmpty()) { |
| 268 | logger.warn("Invalid parameter: begid is null or empty"); |
| 269 | return 2; |
| 270 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 271 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 272 | EntityManager em = null; |
| 273 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 274 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 275 | try { |
| 276 | em = emf.createEntityManager(); |
| 277 | tx = em.getTransaction(); |
| 278 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 279 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 280 | // 查找要删除的求种信息 |
| 281 | BegInfo begInfo = em.find(BegInfo.class, begid); |
| 282 | if (begInfo == null) { |
| 283 | logger.warn("BegSeed with ID {} does not exist", begid); |
| 284 | tx.rollback(); |
| 285 | return 1; |
| 286 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 287 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 288 | // 删除求种信息 |
| 289 | em.remove(begInfo); |
| 290 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 291 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 292 | logger.info("Successfully deleted BegSeed with ID: {}", begid); |
| 293 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 294 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 295 | } catch (Exception e) { |
| 296 | if (tx != null && tx.isActive()) { |
| 297 | tx.rollback(); |
| 298 | } |
| 299 | logger.error("Error deleting BegSeed: {}", e.getMessage()); |
| 300 | return 2; |
| 301 | } finally { |
| 302 | if (em != null) { |
| 303 | em.close(); |
| 304 | } |
| 305 | } |
| 306 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 307 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 308 | @Override |
| 309 | public int VoteSeed(String begId, String seedId, String userId) { |
| 310 | if (begId == null || seedId == null || userId == null || |
| 311 | begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) { |
| 312 | logger.warn("Invalid parameters: begId, seedId or userId is null or empty"); |
| 313 | return 2; |
| 314 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 315 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 316 | EntityManager em = null; |
| 317 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 318 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 319 | try { |
| 320 | em = emf.createEntityManager(); |
| 321 | tx = em.getTransaction(); |
| 322 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 323 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 324 | // 检查求种信息是否存在 |
| 325 | BegInfo begInfo = em.find(BegInfo.class, begId); |
| 326 | if (begInfo == null) { |
| 327 | logger.warn("BegSeed with ID {} does not exist", begId); |
| 328 | return 2; |
| 329 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 330 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 331 | // 检查用户是否已投票 |
| 332 | Long voteCount = new JPAQuery<>(em) |
| 333 | .select(QUserVotes.userVotes.count()) |
| 334 | .from(QUserVotes.userVotes) |
| 335 | .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId))) |
| 336 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 337 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 338 | if (voteCount != null && voteCount > 0) { |
| 339 | logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId); |
| 340 | return 1; |
| 341 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 342 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 343 | // 创建新的投票记录 |
| 344 | em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " + |
| 345 | "VALUES (?, ?, ?, CURRENT_TIMESTAMP)") |
| 346 | .setParameter(1, userId) |
| 347 | .setParameter(2, begId) |
| 348 | .setParameter(3, seedId) |
| 349 | .executeUpdate(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 350 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 351 | // 更新SubmitSeed表中的投票数 |
| 352 | em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " + |
| 353 | "WHERE s.id.begId = :begId AND s.id.seedId = :seedId") |
| 354 | .setParameter("begId", begId) |
| 355 | .setParameter("seedId", seedId) |
| 356 | .executeUpdate(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 357 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 358 | tx.commit(); |
| 359 | logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId); |
| 360 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 361 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 362 | } catch (Exception e) { |
| 363 | if (tx != null && tx.isActive()) { |
| 364 | tx.rollback(); |
| 365 | } |
| 366 | logger.error("Error voting for seed: {}", e.getMessage()); |
| 367 | return 2; |
| 368 | } finally { |
| 369 | if (em != null) { |
| 370 | em.close(); |
| 371 | } |
| 372 | } |
| 373 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 374 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 375 | @Override |
| 376 | public int SubmitSeed(String begid, Seed seed) { |
| 377 | if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) { |
| 378 | logger.warn("Invalid parameters: begid or seed is null or empty"); |
| 379 | return 2; |
| 380 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 381 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 382 | EntityManager em = null; |
| 383 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 384 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 385 | try { |
| 386 | em = emf.createEntityManager(); |
| 387 | tx = em.getTransaction(); |
| 388 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 389 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 390 | // 检查求种信息是否存在 |
| 391 | BegInfo begInfo = em.find(BegInfo.class, begid); |
| 392 | if (begInfo == null) { |
| 393 | logger.warn("BegSeed with ID {} does not exist", begid); |
| 394 | return 2; |
| 395 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 396 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 397 | // 检查种子是否已提交过 |
| 398 | QSubmitSeed ss = QSubmitSeed.submitSeed; |
| 399 | Long submitCount = new JPAQuery<>(em) |
| 400 | .select(ss.count()) |
| 401 | .from(ss) |
| 402 | .where(ss.begInfo.begid.eq(begid)) |
| 403 | .where(ss.seed.seedid.eq(seed.seedid)) |
| 404 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 405 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 406 | if (submitCount > 0) { |
| 407 | logger.warn("Seed {} has already been submitted for beg {}", seed.seedid, |
| 408 | begid); |
| 409 | return 1; |
| 410 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 411 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 412 | // 保存种子信息(如果不存在) |
| 413 | if (em.find(Seed.class, seed.seedid) == null) { |
| 414 | em.persist(seed); |
| 415 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 416 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 417 | // 创建提交记录 |
| 418 | em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)") |
| 419 | .setParameter(1, begid) |
| 420 | .setParameter(2, seed.seedid) |
| 421 | .executeUpdate(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 422 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 423 | tx.commit(); |
| 424 | logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid); |
| 425 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 426 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 427 | } catch (Exception e) { |
| 428 | if (tx != null && tx.isActive()) { |
| 429 | tx.rollback(); |
| 430 | } |
| 431 | logger.error("Error submitting seed: {}", e.getMessage()); |
| 432 | return 2; |
| 433 | } finally { |
| 434 | if (em != null) { |
| 435 | em.close(); |
| 436 | } |
| 437 | } |
| 438 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 439 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 440 | @Override |
| 441 | public void SettleBeg() { |
| 442 | EntityManager em = null; |
| 443 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 444 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 445 | try { |
| 446 | em = emf.createEntityManager(); |
| 447 | tx = em.getTransaction(); |
| 448 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 449 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 450 | // 1. 获取所有已过期且未完成的求种信息 |
| 451 | QBegInfo b = QBegInfo.begInfo; |
| 452 | List<BegInfo> expiredBegs = new JPAQuery<>(em) |
| 453 | .select(b) |
| 454 | .from(b) |
| 455 | .where(b.endtime.loe(new Date()) |
| 456 | .and(b.hasseed.eq(0))) |
| 457 | .fetch(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 458 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 459 | for (BegInfo beg : expiredBegs) { |
| 460 | // 2. 查找投票最多的提交任务 |
| 461 | QSubmitSeed ss = QSubmitSeed.submitSeed; |
| 462 | Tuple topSubmission = new JPAQuery<>(em) |
| 463 | .select(ss.seed.seedid, ss.votes) |
| 464 | .from(ss) |
| 465 | .where(ss.begInfo.begid.eq(beg.begid)) |
| 466 | .orderBy(ss.votes.desc()) |
| 467 | .limit(1) |
| 468 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 469 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 470 | if (topSubmission != null && topSubmission.get(ss.votes) > 0) { |
| 471 | String seedId = topSubmission.get(ss.seed.seedid); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 472 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 473 | // 3. 获取上传者ID |
| 474 | QSeed s = QSeed.seed; |
| 475 | String ownerId = new JPAQuery<>(em) |
| 476 | .select(s.seeduserid) |
| 477 | .from(s) |
| 478 | .where(s.seedid.eq(seedId)) |
| 479 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 480 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 481 | // 4. 获取上传者的PT信息并更新魔力值 |
| 482 | UserPT ownerPT = em.find(UserPT.class, ownerId); |
| 483 | if (ownerPT != null) { |
| 484 | // 5. 发放奖励 |
| 485 | ownerPT.magic += beg.magic; |
| 486 | em.merge(ownerPT); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 487 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 488 | // 6. 更新求种状态 |
| 489 | beg.hasseed = 1; |
| 490 | em.merge(beg); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 491 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 492 | logger.info("Reward {} magic points awarded to user {} for beg {}", |
| 493 | beg.magic, ownerId, beg.begid); |
| 494 | } |
| 495 | } |
| 496 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 497 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 498 | tx.commit(); |
| 499 | logger.info("Successfully settled {} expired beg requests", |
| 500 | expiredBegs.size()); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 501 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 502 | } catch (Exception e) { |
| 503 | if (tx != null && tx.isActive()) { |
| 504 | tx.rollback(); |
| 505 | } |
| 506 | logger.error("Error settling beg requests: {}", e.getMessage(), e); |
| 507 | } finally { |
| 508 | if (em != null) { |
| 509 | em.close(); |
| 510 | } |
| 511 | } |
| 512 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 513 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 514 | @Override |
| 515 | public int AddPost(Post post) { |
| 516 | if (post == null || post.postid == null || post.postid.isEmpty()) { |
| 517 | logger.warn("Invalid parameter: post is null or postid is empty"); |
| 518 | return 2; |
| 519 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 520 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 521 | EntityManager em = null; |
| 522 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 523 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 524 | try { |
| 525 | em = emf.createEntityManager(); |
| 526 | tx = em.getTransaction(); |
| 527 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 528 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 529 | // 检查是否重复 |
| 530 | Post existingPost = em.find(Post.class, post.postid); |
| 531 | if (existingPost != null) { |
| 532 | logger.warn("Post with ID {} already exists", post.postid); |
| 533 | return 1; |
| 534 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 535 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 536 | // 检查用户是否存在 |
| 537 | User user = em.find(User.class, post.postuserid); |
| 538 | if (user == null) { |
| 539 | logger.warn("User with ID {} does not exist", post.postuserid); |
| 540 | return 2; |
| 541 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 542 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 543 | // 设置初始值 |
| 544 | if (post.posttime == null) { |
| 545 | post.posttime = new Date(); |
| 546 | } |
| 547 | post.replytime = 0; |
| 548 | post.readtime = 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 549 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 550 | // 保存帖子 |
| 551 | em.persist(post); |
| 552 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 553 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 554 | logger.info("Successfully added new post with ID: {}", post.postid); |
| 555 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 556 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 557 | } catch (Exception e) { |
| 558 | if (tx != null && tx.isActive()) { |
| 559 | tx.rollback(); |
| 560 | } |
| 561 | logger.error("Error adding post: {}", e.getMessage()); |
| 562 | return 2; |
| 563 | } finally { |
| 564 | if (em != null) { |
| 565 | em.close(); |
| 566 | } |
| 567 | } |
| 568 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 569 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 570 | @Override |
| 571 | public int UpdatePost(Post post) { |
| 572 | if (post == null || post.postid == null || post.postid.isEmpty()) { |
| 573 | logger.warn("Invalid parameter: post is null or postid is empty"); |
| 574 | return 2; |
| 575 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 576 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 577 | EntityManager em = null; |
| 578 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 579 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 580 | try { |
| 581 | em = emf.createEntityManager(); |
| 582 | tx = em.getTransaction(); |
| 583 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 584 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 585 | // 检查帖子是否存在 |
| 586 | Post existingPost = em.find(Post.class, post.postid); |
| 587 | if (existingPost == null) { |
| 588 | logger.warn("Post with ID {} does not exist", post.postid); |
| 589 | return 1; |
| 590 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 591 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 592 | // 保持原有不可修改的字段 |
| 593 | post.postuserid = existingPost.postuserid; |
| 594 | post.posttime = existingPost.posttime; |
| 595 | post.replytime = existingPost.replytime; |
| 596 | post.readtime = existingPost.readtime; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 597 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 598 | // 更新帖子 |
| 599 | em.merge(post); |
| 600 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 601 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 602 | logger.info("Successfully updated post with ID: {}", post.postid); |
| 603 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 604 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 605 | } catch (Exception e) { |
| 606 | if (tx != null && tx.isActive()) { |
| 607 | tx.rollback(); |
| 608 | } |
| 609 | logger.error("Error updating post: {}", e.getMessage()); |
| 610 | return 2; |
| 611 | } finally { |
| 612 | if (em != null) { |
| 613 | em.close(); |
| 614 | } |
| 615 | } |
| 616 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 617 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 618 | @Override |
| 619 | public int DeletePost(String postid) { |
| 620 | if (postid == null || postid.isEmpty()) { |
| 621 | logger.warn("Invalid parameter: postid is null or empty"); |
| 622 | return 2; |
| 623 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 624 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 625 | EntityManager em = null; |
| 626 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 627 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 628 | try { |
| 629 | em = emf.createEntityManager(); |
| 630 | tx = em.getTransaction(); |
| 631 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 632 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 633 | // 查找要删除的帖子 |
| 634 | Post post = em.find(Post.class, postid); |
| 635 | if (post == null) { |
| 636 | logger.warn("Post with ID {} does not exist", postid); |
| 637 | tx.rollback(); |
| 638 | return 1; |
| 639 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 640 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 641 | // 删除帖子(由于设置了级联删除,相关的回复会自动删除) |
| 642 | em.remove(post); |
| 643 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 644 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 645 | logger.info("Successfully deleted post with ID: {}", postid); |
| 646 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 647 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 648 | } catch (Exception e) { |
| 649 | if (tx != null && tx.isActive()) { |
| 650 | tx.rollback(); |
| 651 | } |
| 652 | logger.error("Error deleting post: {}", e.getMessage()); |
| 653 | return 2; |
| 654 | } finally { |
| 655 | if (em != null) { |
| 656 | em.close(); |
| 657 | } |
| 658 | } |
| 659 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 660 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 661 | @Override |
| 662 | public int AddComment(String postid, String userid, String comment) { |
| 663 | if (postid == null || postid.isEmpty() || |
| 664 | userid == null || userid.isEmpty() || |
| 665 | comment == null || comment.isEmpty()) { |
| 666 | logger.warn("Invalid parameters: postid, userid or comment is null or empty"); |
| 667 | return 2; |
| 668 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 669 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 670 | EntityManager em = null; |
| 671 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 672 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 673 | try { |
| 674 | em = emf.createEntityManager(); |
| 675 | tx = em.getTransaction(); |
| 676 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 677 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 678 | // 检查帖子是否存在 |
| 679 | Post post = em.find(Post.class, postid); |
| 680 | if (post == null) { |
| 681 | logger.warn("Post with ID {} does not exist", postid); |
| 682 | return 1; |
| 683 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 684 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 685 | // 检查评论用户是否存在 |
| 686 | User user = em.find(User.class, userid); |
| 687 | if (user == null) { |
| 688 | logger.warn("User with ID {} does not exist", userid); |
| 689 | return 1; |
| 690 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 691 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 692 | // 创建新的回复 |
| 693 | PostReply reply = new PostReply(); |
| 694 | reply.replyid = UUID.randomUUID().toString(); |
| 695 | reply.postid = postid; |
| 696 | reply.content = comment; |
| 697 | reply.authorid = userid; |
| 698 | reply.createdAt = new Date(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 699 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 700 | // 保存回复 |
| 701 | em.persist(reply); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 702 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 703 | // 更新帖子的回复数 |
| 704 | post.replytime += 1; |
| 705 | em.merge(post); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 706 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 707 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 708 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 709 | logger.info("Successfully added comment by user {} to post {}, reply ID: {}", |
| 710 | userid, postid, reply.replyid); |
| 711 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 712 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 713 | } catch (Exception e) { |
| 714 | if (tx != null && tx.isActive()) { |
| 715 | tx.rollback(); |
| 716 | } |
| 717 | logger.error("Error adding comment: {}", e.getMessage()); |
| 718 | return 2; |
| 719 | } finally { |
| 720 | if (em != null) { |
| 721 | em.close(); |
| 722 | } |
| 723 | } |
| 724 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 725 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 726 | @Override |
| 727 | public int DeleteComment(String postid, String commentid) { |
| 728 | if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) { |
| 729 | logger.warn("Invalid parameters: postid or commentid is null or empty"); |
| 730 | return 2; |
| 731 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 732 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 733 | EntityManager em = null; |
| 734 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 735 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 736 | try { |
| 737 | em = emf.createEntityManager(); |
| 738 | tx = em.getTransaction(); |
| 739 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 740 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 741 | // 检查帖子是否存在 |
| 742 | Post post = em.find(Post.class, postid); |
| 743 | if (post == null) { |
| 744 | logger.warn("Post with ID {} does not exist", postid); |
| 745 | return 1; |
| 746 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 747 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 748 | // 检查评论是否存在且属于该帖子 |
| 749 | PostReply reply = em.find(PostReply.class, commentid); |
| 750 | if (reply == null || !reply.postid.equals(postid)) { |
| 751 | logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid); |
| 752 | return 1; |
| 753 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 754 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 755 | // 删除评论 |
| 756 | em.remove(reply); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 757 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 758 | // 更新帖子的回复数 |
| 759 | post.replytime = Math.max(0, post.replytime - 1); |
| 760 | em.merge(post); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 761 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 762 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 763 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 764 | logger.info("Successfully deleted comment {} from post {}", commentid, postid); |
| 765 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 766 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 767 | } catch (Exception e) { |
| 768 | if (tx != null && tx.isActive()) { |
| 769 | tx.rollback(); |
| 770 | } |
| 771 | logger.error("Error deleting comment: {}", e.getMessage()); |
| 772 | return 2; |
| 773 | } finally { |
| 774 | if (em != null) { |
| 775 | em.close(); |
| 776 | } |
| 777 | } |
| 778 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 779 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 780 | @Override |
| 781 | public boolean ExchangeMagicToUpload(String userid, int magic) { |
| 782 | if (userid == null || userid.isEmpty() || magic <= 0) { |
| 783 | logger.warn("参数无效: userid为空或magic <= 0"); |
| 784 | return false; |
| 785 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 786 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 787 | EntityManager em = null; |
| 788 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 789 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 790 | try { |
| 791 | em = emf.createEntityManager(); |
| 792 | tx = em.getTransaction(); |
| 793 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 794 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 795 | UserPT userPT = em.find(UserPT.class, userid); |
| 796 | if (userPT == null) { |
| 797 | logger.warn("未找到用户 {} 的PT信息", userid); |
| 798 | return false; |
| 799 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 800 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 801 | if (userPT.magic < magic) { |
| 802 | logger.warn("用户 {} 的魔力值不足", userid); |
| 803 | return false; |
| 804 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 805 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 806 | // 1:1兑换,直接加上魔力值(假设单位是MB) |
| 807 | userPT.magic -= magic; |
| 808 | userPT.upload += magic; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 809 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 810 | // 更新分享率 |
| 811 | if (userPT.download > 0) { |
| 812 | userPT.share = (double) userPT.upload / userPT.download; |
| 813 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 814 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 815 | em.merge(userPT); |
| 816 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 817 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 818 | logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic); |
| 819 | return true; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 820 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 821 | } catch (Exception e) { |
| 822 | if (tx != null && tx.isActive()) { |
| 823 | tx.rollback(); |
| 824 | } |
| 825 | logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage()); |
| 826 | return false; |
| 827 | } finally { |
| 828 | if (em != null) { |
| 829 | em.close(); |
| 830 | } |
| 831 | } |
| 832 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 833 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 834 | @Override |
| 835 | public boolean ExchangeMagicToDownload(String userid, int magic) { |
| 836 | if (userid == null || userid.isEmpty() || magic <= 0) { |
| 837 | logger.warn("参数无效: userid为空或magic <= 0"); |
| 838 | return false; |
| 839 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 840 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 841 | EntityManager em = null; |
| 842 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 843 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 844 | try { |
| 845 | em = emf.createEntityManager(); |
| 846 | tx = em.getTransaction(); |
| 847 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 848 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 849 | UserPT userPT = em.find(UserPT.class, userid); |
| 850 | if (userPT == null) { |
| 851 | logger.warn("未找到用户 {} 的PT信息", userid); |
| 852 | return false; |
| 853 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 854 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 855 | if (userPT.magic < magic) { |
| 856 | logger.warn("用户 {} 的魔力值不足", userid); |
| 857 | return false; |
| 858 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 859 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 860 | // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB) |
| 861 | userPT.magic -= magic; |
| 862 | userPT.download = Math.max(0, userPT.download - magic); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 863 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 864 | // 更新分享率 |
| 865 | if (userPT.download > 0) { |
| 866 | userPT.share = (double) userPT.upload / userPT.download; |
| 867 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 868 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 869 | em.merge(userPT); |
| 870 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 871 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 872 | logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic); |
| 873 | return true; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 874 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 875 | } catch (Exception e) { |
| 876 | if (tx != null && tx.isActive()) { |
| 877 | tx.rollback(); |
| 878 | } |
| 879 | logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage()); |
| 880 | return false; |
| 881 | } finally { |
| 882 | if (em != null) { |
| 883 | em.close(); |
| 884 | } |
| 885 | } |
| 886 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 887 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 888 | @Override |
| 889 | public boolean ExchangeMagicToVip(String userid, int magic) { |
| 890 | if (userid == null || userid.isEmpty() || magic <= 0) { |
| 891 | logger.warn("参数无效: userid为空或magic <= 0"); |
| 892 | return false; |
| 893 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 894 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 895 | EntityManager em = null; |
| 896 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 897 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 898 | try { |
| 899 | em = emf.createEntityManager(); |
| 900 | tx = em.getTransaction(); |
| 901 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 902 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 903 | UserPT userPT = em.find(UserPT.class, userid); |
| 904 | if (userPT == null) { |
| 905 | logger.warn("未找到用户 {} 的PT信息", userid); |
| 906 | return false; |
| 907 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 908 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 909 | if (userPT.magic < magic) { |
| 910 | logger.warn("用户 {} 的魔力值不足", userid); |
| 911 | return false; |
| 912 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 913 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 914 | // 1:1兑换VIP下载次数 |
| 915 | userPT.magic -= magic; |
| 916 | userPT.viptime += magic; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 917 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 918 | em.merge(userPT); |
| 919 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 920 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 921 | logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic); |
| 922 | return true; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 923 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 924 | } catch (Exception e) { |
| 925 | if (tx != null && tx.isActive()) { |
| 926 | tx.rollback(); |
| 927 | } |
| 928 | logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage()); |
| 929 | return false; |
| 930 | } finally { |
| 931 | if (em != null) { |
| 932 | em.close(); |
| 933 | } |
| 934 | } |
| 935 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 936 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 937 | @Override |
| 938 | public boolean UploadTransmitProfile(Profile profile) { |
| 939 | if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() || |
| 940 | profile.userid == null || profile.userid.isEmpty()) { |
| 941 | logger.warn("参数无效: profile为空或必要字段为空"); |
| 942 | return false; |
| 943 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 944 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 945 | EntityManager em = null; |
| 946 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 947 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 948 | try { |
| 949 | em = emf.createEntityManager(); |
| 950 | tx = em.getTransaction(); |
| 951 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 952 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 953 | // 检查用户是否存在 |
| 954 | User user = em.find(User.class, profile.userid); |
| 955 | if (user == null) { |
| 956 | logger.warn("用户 {} 不存在", profile.userid); |
| 957 | return false; |
| 958 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 959 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 960 | // 检查是否已存在相同的迁移申请 |
| 961 | Profile existingProfile = em.find(Profile.class, profile.profileurl); |
| 962 | if (existingProfile != null) { |
| 963 | logger.warn("迁移申请 {} 已存在", profile.profileurl); |
| 964 | return false; |
| 965 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 966 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 967 | // 设置初始值 |
| 968 | profile.exampass = false; |
| 969 | profile.magicgived = "0"; |
| 970 | profile.uploadgived = "0"; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 971 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 972 | // 保存迁移申请 |
| 973 | em.persist(profile); |
| 974 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 975 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 976 | logger.info("成功上传迁移申请 {}", profile.profileurl); |
| 977 | return true; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 978 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 979 | } catch (Exception e) { |
| 980 | if (tx != null && tx.isActive()) { |
| 981 | tx.rollback(); |
| 982 | } |
| 983 | logger.error("上传迁移申请时发生错误: {}", e.getMessage()); |
| 984 | return false; |
| 985 | } finally { |
| 986 | if (em != null) { |
| 987 | em.close(); |
| 988 | } |
| 989 | } |
| 990 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 991 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 992 | @Override |
| 993 | public Profile GetTransmitProfile(String profileid) { |
| 994 | if (profileid == null || profileid.isEmpty()) { |
| 995 | logger.warn("参数无效: profileid为空"); |
| 996 | return null; |
| 997 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 998 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 999 | EntityManager em = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1000 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1001 | try { |
| 1002 | em = emf.createEntityManager(); |
| 1003 | Profile profile = em.find(Profile.class, profileid); |
| 1004 | if (profile == null) { |
| 1005 | logger.warn("未找到迁移申请 {}", profileid); |
| 1006 | return null; |
| 1007 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1008 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1009 | logger.info("成功获取迁移申请 {}", profileid); |
| 1010 | return profile; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1011 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1012 | } catch (Exception e) { |
| 1013 | logger.error("获取迁移申请时发生错误: {}", e.getMessage()); |
| 1014 | return null; |
| 1015 | } finally { |
| 1016 | if (em != null) { |
| 1017 | em.close(); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1021 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1022 | @Override |
| 1023 | public boolean ExamTransmitProfile(String profileid, boolean result) { |
| 1024 | if (profileid == null || profileid.isEmpty()) { |
| 1025 | logger.warn("参数无效: profileid为空"); |
| 1026 | return false; |
| 1027 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1028 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1029 | EntityManager em = null; |
| 1030 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1031 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1032 | try { |
| 1033 | em = emf.createEntityManager(); |
| 1034 | tx = em.getTransaction(); |
| 1035 | tx.begin(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1036 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1037 | // 查找迁移申请 |
| 1038 | Profile profile = em.find(Profile.class, profileid); |
| 1039 | if (profile == null) { |
| 1040 | logger.warn("未找到迁移申请 {}", profileid); |
| 1041 | return false; |
| 1042 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1043 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1044 | // 更新审核状态 |
| 1045 | profile.exampass = result; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1046 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1047 | if (result) { |
| 1048 | // 如果审核通过,更新用户的PT信息 |
| 1049 | UserPT userPT = em.find(UserPT.class, profile.userid); |
| 1050 | if (userPT != null) { |
| 1051 | // 发放魔力值 |
| 1052 | int magicToGive = Integer.parseInt(profile.magictogive); |
| 1053 | userPT.magic += magicToGive; |
| 1054 | profile.magicgived = String.valueOf(magicToGive); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1055 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1056 | // 发放上传量 |
| 1057 | long uploadToGive = Long.parseLong(profile.uploadtogive); |
| 1058 | userPT.upload += uploadToGive; |
| 1059 | profile.uploadgived = String.valueOf(uploadToGive); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1060 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1061 | em.merge(userPT); |
| 1062 | } |
| 1063 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1064 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1065 | em.merge(profile); |
| 1066 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1067 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1068 | logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result); |
| 1069 | return true; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1070 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1071 | } catch (Exception e) { |
| 1072 | if (tx != null && tx.isActive()) { |
| 1073 | tx.rollback(); |
| 1074 | } |
| 1075 | logger.error("审核迁移申请时发生错误: {}", e.getMessage()); |
| 1076 | return false; |
| 1077 | } finally { |
| 1078 | if (em != null) { |
| 1079 | em.close(); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1083 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1084 | @Override |
| 1085 | public Profile[] GetTransmitProfileList() { |
| 1086 | EntityManager em = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1087 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1088 | try { |
| 1089 | em = emf.createEntityManager(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1090 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1091 | // 获取所有迁移申请 |
| 1092 | QProfile p = QProfile.profile; |
| 1093 | List<Profile> profiles = new JPAQuery<>(em) |
| 1094 | .select(p) |
| 1095 | .from(p) |
| 1096 | .fetch(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1097 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1098 | logger.info("成功获取所有迁移申请,共 {} 条", profiles.size()); |
| 1099 | return profiles.toArray(new Profile[0]); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1100 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1101 | } catch (Exception e) { |
| 1102 | logger.error("获取迁移申请列表时发生错误: {}", e.getMessage()); |
| 1103 | return new Profile[0]; |
| 1104 | } finally { |
| 1105 | if (em != null) { |
| 1106 | em.close(); |
| 1107 | } |
| 1108 | } |
| 1109 | } |
rhj | 46f62c4 | 2025-06-06 23:24:10 +0800 | [diff] [blame] | 1110 | |
| 1111 | @Override |
| 1112 | public Post[] GetPostList(){ |
| 1113 | return null; |
| 1114 | } |
rhj | 5b69b7e | 2025-06-07 01:28:08 +0800 | [diff] [blame] | 1115 | |
| 1116 | @Override |
| 1117 | public Post GetPost(String postid) { |
| 1118 | return null; |
| 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | public PostReply[] GetPostReplyList(String postid) { |
| 1123 | return null; |
| 1124 | } |
rhj | 5ebd93c | 2025-06-07 15:57:28 +0800 | [diff] [blame] | 1125 | |
| 1126 | @Override |
| 1127 | public Post[] SearchPost(String userQ) { |
| 1128 | return null; |
| 1129 | } |
rhj | e18c3f7 | 2025-06-08 00:27:01 +0800 | [diff] [blame^] | 1130 | |
| 1131 | @Override |
| 1132 | public int CheckAdmin(String userid) { |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | @Override |
| 1137 | public int InviteNewUser(String inviterid, String invitedemail) { |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | @Override |
| 1142 | public UserStar[] GetUserStarList(String userid) { |
| 1143 | return new UserStar[0]; |
| 1144 | } |
| 1145 | |
| 1146 | @Override |
| 1147 | public int UploadMigration(String userid, File file){ |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | @Override |
| 1152 | public BegSeedDetail[] GetBegList() { |
| 1153 | EntityManager em = null; |
| 1154 | try { |
| 1155 | em = emf.createEntityManager(); |
| 1156 | |
| 1157 | JPAQuery<BegInfo> query = new JPAQuery<>(em); |
| 1158 | QBegInfo b = QBegInfo.begInfo; |
| 1159 | List<BegInfo> begList = query.select(b).from(b).fetch(); |
| 1160 | |
| 1161 | List<BegSeedDetail> begSeedDetailList = new ArrayList<>(); |
| 1162 | for (BegInfo begInfo : begList) { |
| 1163 | // 查询对应的BegInfo表的Info字段 |
| 1164 | BegInfoDetail begInfoDetail = em.find(BegInfoDetail.class, begInfo.begid); |
| 1165 | // 构造BegSeedDetail对象 |
| 1166 | BegSeedDetail begSeedDetail = new BegSeedDetail(begInfo, begInfoDetail); |
| 1167 | begSeedDetailList.add(begSeedDetail); |
| 1168 | } |
| 1169 | |
| 1170 | return begSeedDetailList.toArray(new BegSeedDetail[0]); |
| 1171 | } catch (Exception e) { |
| 1172 | logger.error("Error getting BegList: {}", e.getMessage(), e); |
| 1173 | return new BegSeedDetail[0]; |
| 1174 | } finally { |
| 1175 | if (em != null) { |
| 1176 | em.close(); |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | @Override |
| 1182 | public BegInfo GetBegDetail(String begid) { |
| 1183 | return null; |
| 1184 | } |
| 1185 | |
| 1186 | @Override |
| 1187 | public BegSeedDetail GetBegSeedDetail(String begid) { |
| 1188 | if (begid == null || begid.isEmpty()) { |
| 1189 | return null; |
| 1190 | } |
| 1191 | |
| 1192 | EntityManager em = null; |
| 1193 | try { |
| 1194 | em = emf.createEntityManager(); |
| 1195 | |
| 1196 | // 查询 BegSeed 表数据 |
| 1197 | JPAQuery<BegInfo> begQuery = new JPAQuery<>(em); |
| 1198 | QBegInfo b = QBegInfo.begInfo; |
| 1199 | BegInfo begInfo = begQuery.select(b).from(b).where(b.begid.eq(begid)).fetchOne(); |
| 1200 | |
| 1201 | if (begInfo == null) { |
| 1202 | return null; |
| 1203 | } |
| 1204 | |
| 1205 | // 查询 BegInfo 表的 Info 字段 |
| 1206 | BegInfoDetail begInfoDetail = em.find(BegInfoDetail.class, begid); |
| 1207 | |
| 1208 | // 构造返回对象 |
| 1209 | return new BegSeedDetail(begInfo, begInfoDetail); |
| 1210 | } catch (Exception e) { |
| 1211 | logger.error("Error getting BegSeedDetail: {}", e.getMessage(), e); |
| 1212 | return null; |
| 1213 | } finally { |
| 1214 | if (em != null) { |
| 1215 | em.close(); |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | @Override |
| 1221 | public SeedWithVotes[] GetBegSeedListWithVotes(String begid) { |
| 1222 | return new SeedWithVotes[0]; |
| 1223 | } |
| 1224 | |
| 1225 | @Override |
| 1226 | public int SubmitBegSeed(String begid, String seedid, String userid) { |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | @Override |
| 1231 | public int createBagSeed(BegInfo begInfo, String userid, String info) { |
| 1232 | return 0; |
| 1233 | } |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame] | 1234 | } |