促销接口实现

Change-Id: I140945a50a8ee32042aaf8018b336afd86f5fbac
diff --git a/src/main/java/api/ApiController.java b/src/main/java/api/ApiController.java
index 7566108..42bc7e3 100644
--- a/src/main/java/api/ApiController.java
+++ b/src/main/java/api/ApiController.java
@@ -8,6 +8,7 @@
 import java.text.ParseException;
 
 import javax.annotation.PostConstruct;
+import javax.persistence.criteria.CriteriaBuilder.In;
 
 import org.springframework.core.io.FileSystemResource;
 import org.springframework.core.io.Resource;
@@ -41,6 +42,8 @@
 import entity.BegInfoDetail;
 import entity.UserStar;
 import entity.SeedWithVotes;
+import entity.SeedPromotion;
+import entity.SeedWithPromotionDTO;
 
 import java.util.UUID;
 
@@ -86,7 +89,6 @@
             seed.seedsize = "1GB";
             seed.seedtag = tag;
             seed.url = "http://example.com/torrent"; // 示例URL
-            System.out.println("seed is null? " + (seed == null));
             int ret = db1.RegisterSeed(seed);
             if (ret != 0) {
                 // 如果注册种子失败,返回错误状态
@@ -126,7 +128,7 @@
         @RequestParam("tag") String tag
     ) {
         try {
-            Seed[] seeds = db1.GetSeedListByTag(tag);
+            SeedWithPromotionDTO[] seeds = db1.GetSeedListByTag(tag);
             if (seeds == null) {
                 return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子
             }
@@ -408,7 +410,6 @@
         @RequestBody String requestBody
     ) {
         try {
-            System.out.println("Register request body: " + requestBody);
             // 解析 JSON 数据
             com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
             
@@ -453,7 +454,6 @@
             
             // 调用数据库注册方法
             int ret = db1.RegisterUser(user);
-            System.out.println("Register result: " + ret);
             
             // // 如果注册成功,还需要创建对应的 UserPT 记录
             // if (ret == 0) {
@@ -993,19 +993,22 @@
             // 解析 JSON 数据
             com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
             com.fasterxml.jackson.databind.JsonNode migrationidNode = jsonNode.get("migration_id");
-            if (migrationidNode == null) {
+            com.fasterxml.jackson.databind.JsonNode grantedUploadedNode = jsonNode.get("granted_uploaded");
+            if (migrationidNode == null || grantedUploadedNode == null) {
                 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
             }
             String migrationid = migrationidNode.asText();
-            System.out.println("Migration ID: " + migrationid);
+            Integer grantedUploaded = grantedUploadedNode != null ? grantedUploadedNode.asInt() : null;
+            if (grantedUploaded == null || grantedUploaded < 0) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 上传量必须为非负整数
+            }
             
             // 添加参数验证
             if (migrationid == null || migrationid.trim().isEmpty()) {
                 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
             }
             
-            boolean ret = db1.ExamTransmitProfile(migrationid, true);
-            System.out.println("Migration approval result: " + ret);
+            boolean ret = db1.ExamTransmitProfile(migrationid, true, grantedUploaded);
             if (ret) {
                 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
             } else {
@@ -1039,7 +1042,7 @@
                 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
             }
             
-            boolean ret = db1.ExamTransmitProfile(migrationid, false);
+            boolean ret = db1.ExamTransmitProfile(migrationid, false, 0);
             if (ret) {
                 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
             } else {
@@ -1100,7 +1103,6 @@
         @RequestParam("file") MultipartFile file
     ) {
         try {
-            System.out.println("Submit appeal request: userid=" + userid + ", content=" + content);
             // 先进行参数验证
             if (userid == null || userid.trim().isEmpty() || 
                 content == null || content.trim().isEmpty() ||
@@ -1109,16 +1111,13 @@
             }
             
             if (file.getSize() > 100 * 1024 * 1024) {
-                System.out.println("File size exceeds limit: " + file.getSize() + " bytes");
                 return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
             }
             
-            System.out.println("Submit appeal request: userid=" + userid + ", content=" + content);
             File tempFile = File.createTempFile(userid, file.getOriginalFilename());
             file.transferTo(tempFile);
             
             int ret = cheat.SubmitAppeal(userid, content, tempFile);
-            System.out.println("Submit appeal result: " + ret);
             if (ret == 0) {
                 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
             } else if (ret == 1) {
@@ -1127,7 +1126,6 @@
                 return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
             }
         } catch (org.springframework.web.multipart.MaxUploadSizeExceededException e) {
-            System.out.println("File upload size exceeded: " + e.getMessage());
             return new ResponseEntity<>(3, HttpStatus.PAYLOAD_TOO_LARGE); // 返回 3 表示文件过大
         } catch (JsonProcessingException e) {
             e.printStackTrace();
@@ -1190,7 +1188,6 @@
             } else if( "vip_downloads".equals(exchangeType) ) {
                 ret = db1.ExchangeMagicToVip(userid, magic);
             } else {
-                System.out.println("Invalid exchange type: " + exchangeType);
                 return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 返回 2 表示交换类型错误
             }
             if (!ret) {
@@ -1311,12 +1308,14 @@
     @CrossOrigin(origins = "*", allowedHeaders = "*")
     public ResponseEntity<Integer> migrateAccount(
         @RequestParam("userid") String userid,
-        @RequestParam("file") MultipartFile file
+        @RequestParam("file") MultipartFile file,
+        @RequestParam("uploadtogive") String uploadtogive
     ) {
         try {
             // 先进行参数验证
             if (userid == null || userid.trim().isEmpty() || 
-                file == null || file.isEmpty()) {
+                file == null || file.isEmpty() ||
+                uploadtogive == null || uploadtogive.trim().isEmpty()) {
                 return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST);
             }
             
@@ -1327,7 +1326,7 @@
             File tempFile = File.createTempFile(userid, file.getOriginalFilename());
             file.transferTo(tempFile);
 
-            int ret = db1.UploadMigration(userid, tempFile);
+            int ret = db1.UploadMigration(userid, tempFile, uploadtogive);
             if (ret == 0) {
                 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
             } else if (ret == 1) {
@@ -1364,7 +1363,6 @@
         @RequestParam("begid") String begid
     ) {
         try {
-            System.out.println("getBegSeedDetail called with begid: " + begid);
             if (begid == null || begid.trim().isEmpty()) {
                 return new ResponseEntity<>("", errorHeaders, HttpStatus.BAD_REQUEST); // 返回 400 表示参数不完整
             }
@@ -1415,7 +1413,6 @@
         @RequestBody String requestBody
     ) {
         try {
-            System.out.println("submitSeed called with requestBody: " + requestBody);
             com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
             com.fasterxml.jackson.databind.JsonNode begidNode = jsonNode.get("begid");
             com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid");
@@ -1436,7 +1433,6 @@
             }
 
             int ret = db1.SubmitBegSeed(begid, seedid, userid);
-            System.out.println("submitSeed result: " + ret);
             if (ret == 0) {
                 return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
             } else if (ret == 1) {
@@ -1545,4 +1541,95 @@
             return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
         }
     }
+
+    @Override
+    public ResponseEntity<String> getAllSeeds(){
+        try {
+            Seed[] allSeeds = db1.getAllSeeds();
+            if (allSeeds == null) {
+                return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子列表
+            }
+            String json = mapper.writeValueAsString(allSeeds);
+            return new ResponseEntity<>(json, headers, HttpStatus.OK);
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+            return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @Override
+    public ResponseEntity<String> getAllSeedPromotions() {
+        try {
+            SeedPromotion[] promotions = db1.getAllSeedPromotions();
+            if (promotions == null) {
+                return new ResponseEntity<>("[]", errorHeaders, HttpStatus.NOT_FOUND); // 返回 404 表示未找到种子推广
+            }
+            String json = mapper.writeValueAsString(promotions);
+            return new ResponseEntity<>(json, headers, HttpStatus.OK);
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+            return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 500 表示处理失败
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
+
+    @Override
+    @CrossOrigin(origins = "*", allowedHeaders = "*")
+    public ResponseEntity<Integer> createSeedPromotion(
+        @RequestBody String requestBody
+    ) {
+        try {
+            com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
+            com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seed_id");
+            com.fasterxml.jackson.databind.JsonNode startTimeNode = jsonNode.get("start_time");
+            com.fasterxml.jackson.databind.JsonNode endTimeNode = jsonNode.get("end_time");
+            com.fasterxml.jackson.databind.JsonNode discountNode = jsonNode.get("discount");
+
+            if (seedidNode == null || startTimeNode == null || endTimeNode == null || discountNode == null) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
+            }
+
+            String seedid = seedidNode.asText();
+            Date startTime;
+            Date endTime;
+            Integer discount = discountNode.asInt();
+            if (discount == null || discount < 0) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 折扣参数错误
+            }
+            try {
+                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
+                String startTimeStr = startTimeNode.asText();
+                startTime = dateFormat.parse(startTimeStr);
+                String endTimeStr = endTimeNode.asText();
+                endTime = dateFormat.parse(endTimeStr);
+            } catch (ParseException e) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 日期格式错误
+            }
+
+            if (seedid == null || seedid.trim().isEmpty() ||
+                startTime == null || endTime == null) {
+                return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整
+            }
+
+            int ret = db1.createSeedPromotion(seedid, startTime, endTime, discount);
+            if (ret == 0) {
+                return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功
+            } else if (ret == 1) {
+                return new ResponseEntity<>(2, HttpStatus.CONFLICT); // 返回 1 表示创建失败或其他错误
+            } else {
+                return new ResponseEntity<>(3, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败
+            }
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+            return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败
+        } catch (Exception e) {
+            e.printStackTrace();
+            return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR);
+        }
+    }
 }
\ No newline at end of file