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