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 |
| 131 | public boolean AddCollect(String userid, String postid) { |
| 132 | return false; |
| 133 | } |
root | 33a7d95 | 2025-05-18 17:24:41 +0000 | [diff] [blame] | 134 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 135 | @Override |
| 136 | public boolean DeleteCollect(String userid, String postid) { |
| 137 | return false; |
| 138 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 139 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 140 | @Override |
| 141 | public int AddBegSeed(BegInfo info) { |
| 142 | if (info == null || info.begid == null || info.begid.isEmpty()) { |
| 143 | logger.warn("Invalid parameter: info is null or begid is empty"); |
| 144 | return 2; |
| 145 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 146 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 147 | EntityManager em = null; |
| 148 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 149 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 150 | try { |
| 151 | em = emf.createEntityManager(); |
| 152 | tx = em.getTransaction(); |
| 153 | tx.begin(); |
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 | // 检查是否重复 |
| 156 | BegInfo existingBeg = em.find(BegInfo.class, info.begid); |
| 157 | if (existingBeg != null) { |
| 158 | logger.warn("BegSeed with ID {} already exists", info.begid); |
| 159 | return 1; |
| 160 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 161 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 162 | // 设置默认值 |
| 163 | if (info.endtime == null) { |
| 164 | // 设置默认14天截止期 |
| 165 | Calendar calendar = Calendar.getInstance(); |
| 166 | calendar.add(Calendar.DAY_OF_MONTH, 14); |
| 167 | info.endtime = calendar.getTime(); |
| 168 | } |
| 169 | info.hasseed = 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 170 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 171 | // 保存新的求种信息 |
| 172 | em.persist(info); |
| 173 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 174 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 175 | logger.info("Successfully added new BegSeed with ID: {}", info.begid); |
| 176 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 177 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 178 | } catch (Exception e) { |
| 179 | if (tx != null && tx.isActive()) { |
| 180 | tx.rollback(); |
| 181 | } |
| 182 | logger.error("Error adding BegSeed: {}", e.getMessage()); |
| 183 | return 2; |
| 184 | } finally { |
| 185 | if (em != null) { |
| 186 | em.close(); |
| 187 | } |
| 188 | } |
| 189 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 190 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 191 | @Override |
| 192 | public int UpdateBegSeed(BegInfo info) { |
| 193 | if (info == null || info.begid == null || info.begid.isEmpty()) { |
| 194 | logger.warn("Invalid parameter: info is null or begid is empty"); |
| 195 | return 2; |
| 196 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 197 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 198 | EntityManager em = null; |
| 199 | EntityTransaction tx = null; |
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 | try { |
| 202 | em = emf.createEntityManager(); |
| 203 | tx = em.getTransaction(); |
| 204 | tx.begin(); |
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 | // 检查是否存在 |
| 207 | BegInfo existingBeg = em.find(BegInfo.class, info.begid); |
| 208 | if (existingBeg == null) { |
| 209 | logger.warn("BegSeed with ID {} does not exist", info.begid); |
| 210 | return 1; |
| 211 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 212 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 213 | // 保持原有值不变的字段 |
| 214 | info.hasseed = existingBeg.hasseed; |
| 215 | if (info.endtime == null) { |
| 216 | info.endtime = existingBeg.endtime; |
| 217 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 218 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 219 | // 更新求种信息 |
| 220 | em.merge(info); |
| 221 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 222 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 223 | logger.info("Successfully updated BegSeed with ID: {}", info.begid); |
| 224 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 225 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 226 | } catch (Exception e) { |
| 227 | if (tx != null && tx.isActive()) { |
| 228 | tx.rollback(); |
| 229 | } |
| 230 | logger.error("Error updating BegSeed: {}", e.getMessage()); |
| 231 | return 2; |
| 232 | } finally { |
| 233 | if (em != null) { |
| 234 | em.close(); |
| 235 | } |
| 236 | } |
| 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 | @Override |
| 240 | public int DeleteBegSeed(String begid) { |
| 241 | if (begid == null || begid.isEmpty()) { |
| 242 | logger.warn("Invalid parameter: begid is null or empty"); |
| 243 | return 2; |
| 244 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 245 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 246 | EntityManager em = null; |
| 247 | EntityTransaction tx = null; |
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 | try { |
| 250 | em = emf.createEntityManager(); |
| 251 | tx = em.getTransaction(); |
| 252 | tx.begin(); |
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 | // 查找要删除的求种信息 |
| 255 | BegInfo begInfo = em.find(BegInfo.class, begid); |
| 256 | if (begInfo == null) { |
| 257 | logger.warn("BegSeed with ID {} does not exist", begid); |
| 258 | tx.rollback(); |
| 259 | return 1; |
| 260 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 261 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 262 | // 删除求种信息 |
| 263 | em.remove(begInfo); |
| 264 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 265 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 266 | logger.info("Successfully deleted BegSeed with ID: {}", begid); |
| 267 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 268 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 269 | } catch (Exception e) { |
| 270 | if (tx != null && tx.isActive()) { |
| 271 | tx.rollback(); |
| 272 | } |
| 273 | logger.error("Error deleting BegSeed: {}", e.getMessage()); |
| 274 | return 2; |
| 275 | } finally { |
| 276 | if (em != null) { |
| 277 | em.close(); |
| 278 | } |
| 279 | } |
| 280 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 281 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 282 | @Override |
| 283 | public int VoteSeed(String begId, String seedId, String userId) { |
| 284 | if (begId == null || seedId == null || userId == null || |
| 285 | begId.isEmpty() || seedId.isEmpty() || userId.isEmpty()) { |
| 286 | logger.warn("Invalid parameters: begId, seedId or userId is null or empty"); |
| 287 | return 2; |
| 288 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 289 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 290 | EntityManager em = null; |
| 291 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 292 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 293 | try { |
| 294 | em = emf.createEntityManager(); |
| 295 | tx = em.getTransaction(); |
| 296 | tx.begin(); |
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 | // 检查求种信息是否存在 |
| 299 | BegInfo begInfo = em.find(BegInfo.class, begId); |
| 300 | if (begInfo == null) { |
| 301 | logger.warn("BegSeed with ID {} does not exist", begId); |
| 302 | return 2; |
| 303 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 304 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 305 | // 检查用户是否已投票 |
| 306 | Long voteCount = new JPAQuery<>(em) |
| 307 | .select(QUserVotes.userVotes.count()) |
| 308 | .from(QUserVotes.userVotes) |
| 309 | .where(QUserVotes.userVotes.id.eq(new entity.UserVotesId(userId, begId, seedId))) |
| 310 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 311 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 312 | if (voteCount != null && voteCount > 0) { |
| 313 | logger.warn("User {} has already voted for seed {} in beg {}", userId, seedId, begId); |
| 314 | return 1; |
| 315 | } |
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 | // 创建新的投票记录 |
| 318 | em.createNativeQuery("INSERT INTO UserVotes (user_id, beg_id, seed_id, created_at) " + |
| 319 | "VALUES (?, ?, ?, CURRENT_TIMESTAMP)") |
| 320 | .setParameter(1, userId) |
| 321 | .setParameter(2, begId) |
| 322 | .setParameter(3, seedId) |
| 323 | .executeUpdate(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 324 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 325 | // 更新SubmitSeed表中的投票数 |
| 326 | em.createQuery("UPDATE SubmitSeed s SET s.votes = s.votes + 1 " + |
| 327 | "WHERE s.id.begId = :begId AND s.id.seedId = :seedId") |
| 328 | .setParameter("begId", begId) |
| 329 | .setParameter("seedId", seedId) |
| 330 | .executeUpdate(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 331 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 332 | tx.commit(); |
| 333 | logger.info("Successfully added vote from user {} for seed {} in beg {}", userId, seedId, begId); |
| 334 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 335 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 336 | } catch (Exception e) { |
| 337 | if (tx != null && tx.isActive()) { |
| 338 | tx.rollback(); |
| 339 | } |
| 340 | logger.error("Error voting for seed: {}", e.getMessage()); |
| 341 | return 2; |
| 342 | } finally { |
| 343 | if (em != null) { |
| 344 | em.close(); |
| 345 | } |
| 346 | } |
| 347 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 348 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 349 | @Override |
| 350 | public int SubmitSeed(String begid, Seed seed) { |
| 351 | if (begid == null || seed == null || begid.isEmpty() || seed.seedid == null) { |
| 352 | logger.warn("Invalid parameters: begid or seed is null or empty"); |
| 353 | return 2; |
| 354 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 355 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 356 | EntityManager em = null; |
| 357 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 358 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 359 | try { |
| 360 | em = emf.createEntityManager(); |
| 361 | tx = em.getTransaction(); |
| 362 | tx.begin(); |
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 | // 检查求种信息是否存在 |
| 365 | BegInfo begInfo = em.find(BegInfo.class, begid); |
| 366 | if (begInfo == null) { |
| 367 | logger.warn("BegSeed with ID {} does not exist", begid); |
| 368 | return 2; |
| 369 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 370 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 371 | // 检查种子是否已提交过 |
| 372 | QSubmitSeed ss = QSubmitSeed.submitSeed; |
| 373 | Long submitCount = new JPAQuery<>(em) |
| 374 | .select(ss.count()) |
| 375 | .from(ss) |
| 376 | .where(ss.begInfo.begid.eq(begid)) |
| 377 | .where(ss.seed.seedid.eq(seed.seedid)) |
| 378 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 379 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 380 | if (submitCount > 0) { |
| 381 | logger.warn("Seed {} has already been submitted for beg {}", seed.seedid, |
| 382 | begid); |
| 383 | return 1; |
| 384 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 385 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 386 | // 保存种子信息(如果不存在) |
| 387 | if (em.find(Seed.class, seed.seedid) == null) { |
| 388 | em.persist(seed); |
| 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 | em.createNativeQuery("INSERT INTO SubmitSeed (beg_id, seed_id, votes) VALUES (?, ?, 0)") |
| 393 | .setParameter(1, begid) |
| 394 | .setParameter(2, seed.seedid) |
| 395 | .executeUpdate(); |
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 | tx.commit(); |
| 398 | logger.info("Successfully submitted seed {} for beg {}", seed.seedid, begid); |
| 399 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 400 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 401 | } catch (Exception e) { |
| 402 | if (tx != null && tx.isActive()) { |
| 403 | tx.rollback(); |
| 404 | } |
| 405 | logger.error("Error submitting seed: {}", e.getMessage()); |
| 406 | return 2; |
| 407 | } finally { |
| 408 | if (em != null) { |
| 409 | em.close(); |
| 410 | } |
| 411 | } |
| 412 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 413 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 414 | @Override |
| 415 | public void SettleBeg() { |
| 416 | EntityManager em = null; |
| 417 | EntityTransaction tx = null; |
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 | try { |
| 420 | em = emf.createEntityManager(); |
| 421 | tx = em.getTransaction(); |
| 422 | tx.begin(); |
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 | // 1. 获取所有已过期且未完成的求种信息 |
| 425 | QBegInfo b = QBegInfo.begInfo; |
| 426 | List<BegInfo> expiredBegs = new JPAQuery<>(em) |
| 427 | .select(b) |
| 428 | .from(b) |
| 429 | .where(b.endtime.loe(new Date()) |
| 430 | .and(b.hasseed.eq(0))) |
| 431 | .fetch(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 432 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 433 | for (BegInfo beg : expiredBegs) { |
| 434 | // 2. 查找投票最多的提交任务 |
| 435 | QSubmitSeed ss = QSubmitSeed.submitSeed; |
| 436 | Tuple topSubmission = new JPAQuery<>(em) |
| 437 | .select(ss.seed.seedid, ss.votes) |
| 438 | .from(ss) |
| 439 | .where(ss.begInfo.begid.eq(beg.begid)) |
| 440 | .orderBy(ss.votes.desc()) |
| 441 | .limit(1) |
| 442 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 443 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 444 | if (topSubmission != null && topSubmission.get(ss.votes) > 0) { |
| 445 | String seedId = topSubmission.get(ss.seed.seedid); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 446 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 447 | // 3. 获取上传者ID |
| 448 | QSeed s = QSeed.seed; |
| 449 | String ownerId = new JPAQuery<>(em) |
| 450 | .select(s.seeduserid) |
| 451 | .from(s) |
| 452 | .where(s.seedid.eq(seedId)) |
| 453 | .fetchOne(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 454 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 455 | // 4. 获取上传者的PT信息并更新魔力值 |
| 456 | UserPT ownerPT = em.find(UserPT.class, ownerId); |
| 457 | if (ownerPT != null) { |
| 458 | // 5. 发放奖励 |
| 459 | ownerPT.magic += beg.magic; |
| 460 | em.merge(ownerPT); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 461 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 462 | // 6. 更新求种状态 |
| 463 | beg.hasseed = 1; |
| 464 | em.merge(beg); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 465 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 466 | logger.info("Reward {} magic points awarded to user {} for beg {}", |
| 467 | beg.magic, ownerId, beg.begid); |
| 468 | } |
| 469 | } |
| 470 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 471 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 472 | tx.commit(); |
| 473 | logger.info("Successfully settled {} expired beg requests", |
| 474 | expiredBegs.size()); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 475 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 476 | } catch (Exception e) { |
| 477 | if (tx != null && tx.isActive()) { |
| 478 | tx.rollback(); |
| 479 | } |
| 480 | logger.error("Error settling beg requests: {}", e.getMessage(), e); |
| 481 | } finally { |
| 482 | if (em != null) { |
| 483 | em.close(); |
| 484 | } |
| 485 | } |
| 486 | } |
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 | @Override |
| 489 | public int AddPost(Post post) { |
| 490 | if (post == null || post.postid == null || post.postid.isEmpty()) { |
| 491 | logger.warn("Invalid parameter: post is null or postid is empty"); |
| 492 | return 2; |
| 493 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 494 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 495 | EntityManager em = null; |
| 496 | EntityTransaction tx = null; |
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 | try { |
| 499 | em = emf.createEntityManager(); |
| 500 | tx = em.getTransaction(); |
| 501 | tx.begin(); |
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 | // 检查是否重复 |
| 504 | Post existingPost = em.find(Post.class, post.postid); |
| 505 | if (existingPost != null) { |
| 506 | logger.warn("Post with ID {} already exists", post.postid); |
| 507 | return 1; |
| 508 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 509 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 510 | // 检查用户是否存在 |
| 511 | User user = em.find(User.class, post.postuserid); |
| 512 | if (user == null) { |
| 513 | logger.warn("User with ID {} does not exist", post.postuserid); |
| 514 | return 2; |
| 515 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 516 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 517 | // 设置初始值 |
| 518 | if (post.posttime == null) { |
| 519 | post.posttime = new Date(); |
| 520 | } |
| 521 | post.replytime = 0; |
| 522 | post.readtime = 0; |
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 | // 保存帖子 |
| 525 | em.persist(post); |
| 526 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 527 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 528 | logger.info("Successfully added new post with ID: {}", post.postid); |
| 529 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 530 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 531 | } catch (Exception e) { |
| 532 | if (tx != null && tx.isActive()) { |
| 533 | tx.rollback(); |
| 534 | } |
| 535 | logger.error("Error adding post: {}", e.getMessage()); |
| 536 | return 2; |
| 537 | } finally { |
| 538 | if (em != null) { |
| 539 | em.close(); |
| 540 | } |
| 541 | } |
| 542 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 543 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 544 | @Override |
| 545 | public int UpdatePost(Post post) { |
| 546 | if (post == null || post.postid == null || post.postid.isEmpty()) { |
| 547 | logger.warn("Invalid parameter: post is null or postid is empty"); |
| 548 | return 2; |
| 549 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 550 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 551 | EntityManager em = null; |
| 552 | EntityTransaction tx = null; |
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 | try { |
| 555 | em = emf.createEntityManager(); |
| 556 | tx = em.getTransaction(); |
| 557 | tx.begin(); |
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 | // 检查帖子是否存在 |
| 560 | Post existingPost = em.find(Post.class, post.postid); |
| 561 | if (existingPost == null) { |
| 562 | logger.warn("Post with ID {} does not exist", post.postid); |
| 563 | return 1; |
| 564 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 565 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 566 | // 保持原有不可修改的字段 |
| 567 | post.postuserid = existingPost.postuserid; |
| 568 | post.posttime = existingPost.posttime; |
| 569 | post.replytime = existingPost.replytime; |
| 570 | post.readtime = existingPost.readtime; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 571 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 572 | // 更新帖子 |
| 573 | em.merge(post); |
| 574 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 575 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 576 | logger.info("Successfully updated post with ID: {}", post.postid); |
| 577 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 578 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 579 | } catch (Exception e) { |
| 580 | if (tx != null && tx.isActive()) { |
| 581 | tx.rollback(); |
| 582 | } |
| 583 | logger.error("Error updating post: {}", e.getMessage()); |
| 584 | return 2; |
| 585 | } finally { |
| 586 | if (em != null) { |
| 587 | em.close(); |
| 588 | } |
| 589 | } |
| 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 | @Override |
| 593 | public int DeletePost(String postid) { |
| 594 | if (postid == null || postid.isEmpty()) { |
| 595 | logger.warn("Invalid parameter: postid is null or empty"); |
| 596 | return 2; |
| 597 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 598 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 599 | EntityManager em = null; |
| 600 | EntityTransaction tx = null; |
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 | try { |
| 603 | em = emf.createEntityManager(); |
| 604 | tx = em.getTransaction(); |
| 605 | tx.begin(); |
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 | // 查找要删除的帖子 |
| 608 | Post post = em.find(Post.class, postid); |
| 609 | if (post == null) { |
| 610 | logger.warn("Post with ID {} does not exist", postid); |
| 611 | tx.rollback(); |
| 612 | return 1; |
| 613 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 614 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 615 | // 删除帖子(由于设置了级联删除,相关的回复会自动删除) |
| 616 | em.remove(post); |
| 617 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 618 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 619 | logger.info("Successfully deleted post with ID: {}", postid); |
| 620 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 621 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 622 | } catch (Exception e) { |
| 623 | if (tx != null && tx.isActive()) { |
| 624 | tx.rollback(); |
| 625 | } |
| 626 | logger.error("Error deleting post: {}", e.getMessage()); |
| 627 | return 2; |
| 628 | } finally { |
| 629 | if (em != null) { |
| 630 | em.close(); |
| 631 | } |
| 632 | } |
| 633 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 634 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 635 | @Override |
| 636 | public int AddComment(String postid, String userid, String comment) { |
| 637 | if (postid == null || postid.isEmpty() || |
| 638 | userid == null || userid.isEmpty() || |
| 639 | comment == null || comment.isEmpty()) { |
| 640 | logger.warn("Invalid parameters: postid, userid or comment is null or empty"); |
| 641 | return 2; |
| 642 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 643 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 644 | EntityManager em = null; |
| 645 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 646 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 647 | try { |
| 648 | em = emf.createEntityManager(); |
| 649 | tx = em.getTransaction(); |
| 650 | tx.begin(); |
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 | // 检查帖子是否存在 |
| 653 | Post post = em.find(Post.class, postid); |
| 654 | if (post == null) { |
| 655 | logger.warn("Post with ID {} does not exist", postid); |
| 656 | return 1; |
| 657 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 658 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 659 | // 检查评论用户是否存在 |
| 660 | User user = em.find(User.class, userid); |
| 661 | if (user == null) { |
| 662 | logger.warn("User with ID {} does not exist", userid); |
| 663 | return 1; |
| 664 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 665 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 666 | // 创建新的回复 |
| 667 | PostReply reply = new PostReply(); |
| 668 | reply.replyid = UUID.randomUUID().toString(); |
| 669 | reply.postid = postid; |
| 670 | reply.content = comment; |
| 671 | reply.authorid = userid; |
| 672 | reply.createdAt = new Date(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 673 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 674 | // 保存回复 |
| 675 | em.persist(reply); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 676 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 677 | // 更新帖子的回复数 |
| 678 | post.replytime += 1; |
| 679 | em.merge(post); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 680 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 681 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 682 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 683 | logger.info("Successfully added comment by user {} to post {}, reply ID: {}", |
| 684 | userid, postid, reply.replyid); |
| 685 | return 0; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 686 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 687 | } catch (Exception e) { |
| 688 | if (tx != null && tx.isActive()) { |
| 689 | tx.rollback(); |
| 690 | } |
| 691 | logger.error("Error adding comment: {}", e.getMessage()); |
| 692 | return 2; |
| 693 | } finally { |
| 694 | if (em != null) { |
| 695 | em.close(); |
| 696 | } |
| 697 | } |
| 698 | } |
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 | @Override |
| 701 | public int DeleteComment(String postid, String commentid) { |
| 702 | if (postid == null || postid.isEmpty() || commentid == null || commentid.isEmpty()) { |
| 703 | logger.warn("Invalid parameters: postid or commentid is null or empty"); |
| 704 | return 2; |
| 705 | } |
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 | EntityManager em = null; |
| 708 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 709 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 710 | try { |
| 711 | em = emf.createEntityManager(); |
| 712 | tx = em.getTransaction(); |
| 713 | tx.begin(); |
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 | // 检查帖子是否存在 |
| 716 | Post post = em.find(Post.class, postid); |
| 717 | if (post == null) { |
| 718 | logger.warn("Post with ID {} does not exist", postid); |
| 719 | return 1; |
| 720 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 721 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 722 | // 检查评论是否存在且属于该帖子 |
| 723 | PostReply reply = em.find(PostReply.class, commentid); |
| 724 | if (reply == null || !reply.postid.equals(postid)) { |
| 725 | logger.warn("Comment {} does not exist or does not belong to post {}", commentid, postid); |
| 726 | return 1; |
| 727 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 728 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 729 | // 删除评论 |
| 730 | em.remove(reply); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 731 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 732 | // 更新帖子的回复数 |
| 733 | post.replytime = Math.max(0, post.replytime - 1); |
| 734 | em.merge(post); |
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 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 737 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 738 | logger.info("Successfully deleted comment {} from post {}", commentid, postid); |
| 739 | return 0; |
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 | } catch (Exception e) { |
| 742 | if (tx != null && tx.isActive()) { |
| 743 | tx.rollback(); |
| 744 | } |
| 745 | logger.error("Error deleting comment: {}", e.getMessage()); |
| 746 | return 2; |
| 747 | } finally { |
| 748 | if (em != null) { |
| 749 | em.close(); |
| 750 | } |
| 751 | } |
| 752 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 753 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 754 | @Override |
| 755 | public boolean ExchangeMagicToUpload(String userid, int magic) { |
| 756 | if (userid == null || userid.isEmpty() || magic <= 0) { |
| 757 | logger.warn("参数无效: userid为空或magic <= 0"); |
| 758 | return false; |
| 759 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 760 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 761 | EntityManager em = null; |
| 762 | EntityTransaction tx = null; |
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 | try { |
| 765 | em = emf.createEntityManager(); |
| 766 | tx = em.getTransaction(); |
| 767 | tx.begin(); |
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 | UserPT userPT = em.find(UserPT.class, userid); |
| 770 | if (userPT == null) { |
| 771 | logger.warn("未找到用户 {} 的PT信息", userid); |
| 772 | return false; |
| 773 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 774 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 775 | if (userPT.magic < magic) { |
| 776 | logger.warn("用户 {} 的魔力值不足", 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 | // 1:1兑换,直接加上魔力值(假设单位是MB) |
| 781 | userPT.magic -= magic; |
| 782 | userPT.upload += magic; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 783 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 784 | // 更新分享率 |
| 785 | if (userPT.download > 0) { |
| 786 | userPT.share = (double) userPT.upload / userPT.download; |
| 787 | } |
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 | em.merge(userPT); |
| 790 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 791 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 792 | logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 上传量", userid, magic, magic); |
| 793 | return true; |
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 | } catch (Exception e) { |
| 796 | if (tx != null && tx.isActive()) { |
| 797 | tx.rollback(); |
| 798 | } |
| 799 | logger.error("魔力值兑换上传量时发生错误: {}", e.getMessage()); |
| 800 | return false; |
| 801 | } finally { |
| 802 | if (em != null) { |
| 803 | em.close(); |
| 804 | } |
| 805 | } |
| 806 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 807 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 808 | @Override |
| 809 | public boolean ExchangeMagicToDownload(String userid, int magic) { |
| 810 | if (userid == null || userid.isEmpty() || magic <= 0) { |
| 811 | logger.warn("参数无效: userid为空或magic <= 0"); |
| 812 | return false; |
| 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 | EntityManager em = null; |
| 816 | EntityTransaction tx = null; |
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 | try { |
| 819 | em = emf.createEntityManager(); |
| 820 | tx = em.getTransaction(); |
| 821 | tx.begin(); |
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 | UserPT userPT = em.find(UserPT.class, userid); |
| 824 | if (userPT == null) { |
| 825 | logger.warn("未找到用户 {} 的PT信息", userid); |
| 826 | return false; |
| 827 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 828 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 829 | if (userPT.magic < magic) { |
| 830 | logger.warn("用户 {} 的魔力值不足", 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 | // 1:1兑换,直接减去魔力值对应的下载量(假设单位是MB) |
| 835 | userPT.magic -= magic; |
| 836 | userPT.download = Math.max(0, userPT.download - magic); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 837 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 838 | // 更新分享率 |
| 839 | if (userPT.download > 0) { |
| 840 | userPT.share = (double) userPT.upload / userPT.download; |
| 841 | } |
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 | em.merge(userPT); |
| 844 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 845 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 846 | logger.info("用户 {} 成功将 {} 点魔力值兑换为 {}MB 下载量减免", userid, magic, magic); |
| 847 | return true; |
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 | } catch (Exception e) { |
| 850 | if (tx != null && tx.isActive()) { |
| 851 | tx.rollback(); |
| 852 | } |
| 853 | logger.error("魔力值兑换下载量时发生错误: {}", e.getMessage()); |
| 854 | return false; |
| 855 | } finally { |
| 856 | if (em != null) { |
| 857 | em.close(); |
| 858 | } |
| 859 | } |
| 860 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 861 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 862 | @Override |
| 863 | public boolean ExchangeMagicToVip(String userid, int magic) { |
| 864 | if (userid == null || userid.isEmpty() || magic <= 0) { |
| 865 | logger.warn("参数无效: userid为空或magic <= 0"); |
| 866 | return false; |
| 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 | EntityManager em = null; |
| 870 | EntityTransaction tx = null; |
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 | try { |
| 873 | em = emf.createEntityManager(); |
| 874 | tx = em.getTransaction(); |
| 875 | tx.begin(); |
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 | UserPT userPT = em.find(UserPT.class, userid); |
| 878 | if (userPT == null) { |
| 879 | logger.warn("未找到用户 {} 的PT信息", userid); |
| 880 | return false; |
| 881 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 882 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 883 | if (userPT.magic < magic) { |
| 884 | logger.warn("用户 {} 的魔力值不足", 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 | // 1:1兑换VIP下载次数 |
| 889 | userPT.magic -= magic; |
| 890 | userPT.viptime += magic; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 891 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 892 | em.merge(userPT); |
| 893 | tx.commit(); |
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 | logger.info("用户 {} 成功将 {} 点魔力值兑换为 {} 次VIP下载次数", userid, magic, magic); |
| 896 | return true; |
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 | } catch (Exception e) { |
| 899 | if (tx != null && tx.isActive()) { |
| 900 | tx.rollback(); |
| 901 | } |
| 902 | logger.error("魔力值兑换VIP下载次数时发生错误: {}", e.getMessage()); |
| 903 | return false; |
| 904 | } finally { |
| 905 | if (em != null) { |
| 906 | em.close(); |
| 907 | } |
| 908 | } |
| 909 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 910 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 911 | @Override |
| 912 | public boolean UploadTransmitProfile(Profile profile) { |
| 913 | if (profile == null || profile.profileurl == null || profile.profileurl.isEmpty() || |
| 914 | profile.userid == null || profile.userid.isEmpty()) { |
| 915 | logger.warn("参数无效: profile为空或必要字段为空"); |
| 916 | return false; |
| 917 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 918 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 919 | EntityManager em = null; |
| 920 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 921 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 922 | try { |
| 923 | em = emf.createEntityManager(); |
| 924 | tx = em.getTransaction(); |
| 925 | tx.begin(); |
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 | // 检查用户是否存在 |
| 928 | User user = em.find(User.class, profile.userid); |
| 929 | if (user == null) { |
| 930 | logger.warn("用户 {} 不存在", profile.userid); |
| 931 | return false; |
| 932 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 933 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 934 | // 检查是否已存在相同的迁移申请 |
| 935 | Profile existingProfile = em.find(Profile.class, profile.profileurl); |
| 936 | if (existingProfile != null) { |
| 937 | logger.warn("迁移申请 {} 已存在", profile.profileurl); |
| 938 | return false; |
| 939 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 940 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 941 | // 设置初始值 |
| 942 | profile.exampass = false; |
| 943 | profile.magicgived = "0"; |
| 944 | profile.uploadgived = "0"; |
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 | em.persist(profile); |
| 948 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 949 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 950 | logger.info("成功上传迁移申请 {}", profile.profileurl); |
| 951 | return true; |
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 | } catch (Exception e) { |
| 954 | if (tx != null && tx.isActive()) { |
| 955 | tx.rollback(); |
| 956 | } |
| 957 | logger.error("上传迁移申请时发生错误: {}", e.getMessage()); |
| 958 | return false; |
| 959 | } finally { |
| 960 | if (em != null) { |
| 961 | em.close(); |
| 962 | } |
| 963 | } |
| 964 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 965 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 966 | @Override |
| 967 | public Profile GetTransmitProfile(String profileid) { |
| 968 | if (profileid == null || profileid.isEmpty()) { |
| 969 | logger.warn("参数无效: profileid为空"); |
| 970 | return null; |
| 971 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 972 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 973 | EntityManager em = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 974 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 975 | try { |
| 976 | em = emf.createEntityManager(); |
| 977 | Profile profile = em.find(Profile.class, profileid); |
| 978 | if (profile == null) { |
| 979 | logger.warn("未找到迁移申请 {}", profileid); |
| 980 | return null; |
| 981 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 982 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 983 | logger.info("成功获取迁移申请 {}", profileid); |
| 984 | return profile; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 985 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 986 | } catch (Exception e) { |
| 987 | logger.error("获取迁移申请时发生错误: {}", e.getMessage()); |
| 988 | return null; |
| 989 | } finally { |
| 990 | if (em != null) { |
| 991 | em.close(); |
| 992 | } |
| 993 | } |
| 994 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 995 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 996 | @Override |
| 997 | public boolean ExamTransmitProfile(String profileid, boolean result) { |
| 998 | if (profileid == null || profileid.isEmpty()) { |
| 999 | logger.warn("参数无效: profileid为空"); |
| 1000 | return false; |
| 1001 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1002 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1003 | EntityManager em = null; |
| 1004 | EntityTransaction tx = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1005 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1006 | try { |
| 1007 | em = emf.createEntityManager(); |
| 1008 | tx = em.getTransaction(); |
| 1009 | tx.begin(); |
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 | // 查找迁移申请 |
| 1012 | Profile profile = em.find(Profile.class, profileid); |
| 1013 | if (profile == null) { |
| 1014 | logger.warn("未找到迁移申请 {}", profileid); |
| 1015 | return false; |
| 1016 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1017 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1018 | // 更新审核状态 |
| 1019 | profile.exampass = result; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1020 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1021 | if (result) { |
| 1022 | // 如果审核通过,更新用户的PT信息 |
| 1023 | UserPT userPT = em.find(UserPT.class, profile.userid); |
| 1024 | if (userPT != null) { |
| 1025 | // 发放魔力值 |
| 1026 | int magicToGive = Integer.parseInt(profile.magictogive); |
| 1027 | userPT.magic += magicToGive; |
| 1028 | profile.magicgived = String.valueOf(magicToGive); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1029 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1030 | // 发放上传量 |
| 1031 | long uploadToGive = Long.parseLong(profile.uploadtogive); |
| 1032 | userPT.upload += uploadToGive; |
| 1033 | profile.uploadgived = String.valueOf(uploadToGive); |
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 | em.merge(userPT); |
| 1036 | } |
| 1037 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1038 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1039 | em.merge(profile); |
| 1040 | tx.commit(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1041 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1042 | logger.info("成功审核迁移申请 {}, 结果: {}", profileid, result); |
| 1043 | return true; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1044 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1045 | } catch (Exception e) { |
| 1046 | if (tx != null && tx.isActive()) { |
| 1047 | tx.rollback(); |
| 1048 | } |
| 1049 | logger.error("审核迁移申请时发生错误: {}", e.getMessage()); |
| 1050 | return false; |
| 1051 | } finally { |
| 1052 | if (em != null) { |
| 1053 | em.close(); |
| 1054 | } |
| 1055 | } |
| 1056 | } |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1057 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1058 | @Override |
| 1059 | public Profile[] GetTransmitProfileList() { |
| 1060 | EntityManager em = null; |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1061 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1062 | try { |
| 1063 | em = emf.createEntityManager(); |
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 | // 获取所有迁移申请 |
| 1066 | QProfile p = QProfile.profile; |
| 1067 | List<Profile> profiles = new JPAQuery<>(em) |
| 1068 | .select(p) |
| 1069 | .from(p) |
| 1070 | .fetch(); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1071 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1072 | logger.info("成功获取所有迁移申请,共 {} 条", profiles.size()); |
| 1073 | return profiles.toArray(new Profile[0]); |
Haotian W | 88568d8 | 2025-05-15 16:15:21 +0000 | [diff] [blame] | 1074 | |
956303669 | 63296f6 | 2025-06-02 21:16:32 +0800 | [diff] [blame^] | 1075 | } catch (Exception e) { |
| 1076 | logger.error("获取迁移申请列表时发生错误: {}", e.getMessage()); |
| 1077 | return new Profile[0]; |
| 1078 | } finally { |
| 1079 | if (em != null) { |
| 1080 | em.close(); |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | } |