shuhao 6 ay önce
ebeveyn
işleme
e882fb35e1

+ 3 - 3
admin/src/main/java/com/flyer/foster/controller/app/AppFosterRecordController.java

@@ -2,6 +2,7 @@ package com.flyer.foster.controller.app;
 
 import com.flyer.foster.dto.app.FosterRecordAddDTO;
 import com.flyer.foster.entity.FosterRecord;
+import com.flyer.foster.pojo.StpAppUtil;
 import com.flyer.foster.service.IFosterRecordService;
 import com.flyer.util.R;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,10 +28,9 @@ public class AppFosterRecordController {
      * 领养记录列表
      * @return
      */
-    @GetMapping("/list/{gardenId}/")
+    @GetMapping("/list/{gardenId}")
     public R list(@PathVariable Integer gardenId) {
-//        Integer appUserId = (Integer)StpAppUtil.getLoginId();
-        Integer appUserId = 4;
+        String appUserId = StpAppUtil.getLoginId().toString();
         List<FosterRecord> res = iFosterRecordService.lambdaQuery().
                 eq(FosterRecord::getAppUserId, appUserId)
                 .eq(FosterRecord::getGardenId, gardenId)

+ 20 - 5
admin/src/main/java/com/flyer/foster/controller/app/AppTreeController.java

@@ -1,9 +1,11 @@
 package com.flyer.foster.controller.app;
 
+import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
 import com.flyer.foster.dto.TreeAddDTO;
 import com.flyer.foster.dto.TreeUpdateDTO;
 import com.flyer.foster.entity.FosterRecord;
 import com.flyer.foster.entity.Tree;
+import com.flyer.foster.service.IFosterRecordService;
 import com.flyer.foster.service.ITreeService;
 import com.flyer.util.R;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -11,6 +13,7 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 树
@@ -24,6 +27,9 @@ public class AppTreeController {
     @Autowired
     private ITreeService iTreeService;
 
+    @Autowired
+    private IFosterRecordService iFosterRecordService;
+
     /**
      * 根据gardenId获取详情
      * @param gardenId
@@ -38,10 +44,19 @@ public class AppTreeController {
      * 返回区域随机的一棵树
      * @return
      */
-    @GetMapping("/getRandomTree/{areaId}")
-    public R getRandomTree(@PathVariable Integer areaId){
-        List<Tree> list = iTreeService.lambdaQuery().eq(Tree::getAreaId, areaId).list();
-
-        return R.ok().result(null);
+    @GetMapping("/getRandomTree/{gardenId}")
+    public R getRandomTree(@PathVariable Integer gardenId){
+        List<FosterRecord> records = iFosterRecordService.lambdaQuery().eq(FosterRecord::getGardenId, gardenId).list();
+        //已认养的树
+        List<Integer> fosteredIds = records.stream().map(FosterRecord::getTreeId).collect(Collectors.toList());
+        LambdaQueryChainWrapper<Tree> queryChainWrapper = iTreeService.lambdaQuery().eq(Tree::getGardenId, gardenId);
+        if(fosteredIds.size() > 0){
+            queryChainWrapper = queryChainWrapper.notIn(Tree::getId, fosteredIds);
+        }
+        List<Tree> list = queryChainWrapper.list();
+        if(list.size() > 0){
+            return R.ok().result(list.get(0));
+        }
+        return R.error().message("没有可领养的树!");
     }
 }