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