lizhen_gitee 2 天之前
父節點
當前提交
369b5959e9
共有 2 個文件被更改,包括 42 次插入0 次删除
  1. 41 0
      app/Controller/Api/v1/TestController.php
  2. 1 0
      routes/api/routes.php

+ 41 - 0
app/Controller/Api/v1/TestController.php

@@ -6,6 +6,7 @@ namespace App\Controller\Api\v1;
 
 use App\Controller\AbstractController;
 use App\Utils\AppResult;
+use Hyperf\DbConnection\Db;
 
 class TestController extends AbstractController
 {
@@ -18,5 +19,45 @@ class TestController extends AbstractController
         return AppResult::success('token','testuid_'.rand(100,150100));
     }
 
+    public function test2(){
+        $user_id = 93;$old_jigou_id = 16;$new_jigou_id = 18;
+        $a = $this->bindjigou($user_id,$old_jigou_id,$new_jigou_id);
+        dump($a);
+    }
+
+    public function bindjigou($user_id,$old_jigou_id,$new_jigou_id){
+
+        Db::beginTransaction();
+
+        //用户答题记录,都改成现在的机构id
+        $rs3 = Db::table('user_question_log')->where('user_id',$user_id)->update(['jigou_id'=>$new_jigou_id]);
+        if(!$rs3){
+            Db::rollBack();
+            return false;
+        }
+
+        //重新计算老机构的分
+        $sum_score = Db::table('user_question_log')->where('jigou_id',$old_jigou_id)->where('is_right',1)->sum('score');
+        $rs2 = Db::table('vote_jigou')->where('id',$old_jigou_id)->update(['score'=>$sum_score]);
+        if(!$rs2){
+            Db::rollBack();
+            return false;
+        }
+
+        //重新计算新机构的分
+        $sum_score = Db::table('user_question_log')->where('jigou_id',$new_jigou_id)->where('is_right',1)->sum('score');
+        $rs1 = Db::table('vote_jigou')->where('id',$new_jigou_id)->update(['score'=>$sum_score]);
+        if(!$rs1){
+            Db::rollBack();
+            return false;
+        }
+
+
+        Db::commit();
+        return true;
+
+    }
+
+
 
 }

+ 1 - 0
routes/api/routes.php

@@ -28,6 +28,7 @@ Router::addGroup('/v1', function () {
     Router::addRoute(['GET','POST'],'/demo/test', [DemoController::class, 'test']);
 
     Router::addRoute(['GET','POST'],'/test/getrandtoken', [TestController::class, 'getrandtoken']);
+    Router::addRoute(['GET','POST'],'/test/test2', [TestController::class, 'test2']);
 
    /* Router::post('/order/tailwind_driver', [OrderController::class, 'tailwind_driver']);
     Router::post('/home/index', [HomeController::class, 'index']);*/