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