浏览代码

添加用户存储产品

afa 6 月之前
父节点
当前提交
e2a6f6f975
共有 2 个文件被更改,包括 56 次插入7 次删除
  1. 26 0
      application/api/controller/Pledge.php
  2. 30 7
      application/api/logic/PledgeLogic.php

+ 26 - 0
application/api/controller/Pledge.php

@@ -197,4 +197,30 @@ class Pledge extends Api
             $this->success('ok');
       }
  
+      //添加用户存储产品
+      public function addPledgeProduct(UserPledge $userPledge, PledgeLogic $pledgeLogic, ProductPledges $productPledges)
+      {
+            $order_no  = $this->request->post('order_no/s', '');
+            $pledge_id   = $this->request->post('pledge_id/d', 0);//存储Id
+            if(empty($order_no) || empty($pledge_id)) $this->error(__("参数有误,无可用产品"));  
+            $order_arr= explode(',', $order_no);   
+            $count    = count($order_arr);
+            $pledge = $userPledge::alias('a')->where('a.id', $pledge_id)->join('product_pledge p', 'p.id = a.pledge_id')->field('a.*,p.type_id,p.status as p_status')->find();
+            if (empty($pledge) || $pledge->status != $userPledge::Ongoing ||  empty($pledge->p_status) || $pledge->type_id != $productPledges::Single) $this->error(__("产品质抵活动不存在不存在"));  
+            if(($count + $pledge->num) > config('pledge_single_max')) $this->error(__("产品数量超出限制"));
+            Db::startTrans();
+            try {
+
+                  // 质抵押订单 
+                  $pledgeLogic::setPledgeProductAdd($pledge, $this->auth->id, $count, $order_arr);
+                  // 提交事务
+                  Db::commit();
+            } catch (Exception $e) {
+                  // 回滚事务
+                  Db::rollback();
+                  $this->error($e->getMessage(), null, $e->getCode());
+            }
+            $this->success('ok');
+      }
+
 }

+ 30 - 7
application/api/logic/PledgeLogic.php

@@ -43,11 +43,7 @@ class PledgeLogic
     public static function setPledgeOrder(object $pledge, array $order_no, int $user_id, int $count, string $pay_type, float $price)
     {
         $model  = Loader::model('ProductOrder');
-        $product= $model::alias('a')
-            ->join('product_list b', 'a.product_id = b.id', 'left')
-            ->where('a.user_id', $user_id)
-            ->where('a.status', $model::Paid)
-            ->whereIn('a.order_no', $order_no)->field('a.id,b.zh_name,b.thum,a.order_no')->limit($count)->select();
+        $product=  self::getOrderProductList($user_id, $order_no, $count);
         
         if(empty($product) || count($product) < $count)  throw new Exception('订单不存在');
         $pledge_num = 1;
@@ -144,8 +140,7 @@ class PledgeLogic
     }
 
     
-    //收取质抵押订单列表
-    //(60*60*24)*(当前时间-最后一次收取时间)
+    //收取质抵押订单列表:(60*60*24)*(当前时间-最后一次收取时间)
     public static function getPledgeCollect(int $user_id)
     {   
         $model  = Loader::model('UserPledge');
@@ -192,4 +187,32 @@ class PledgeLogic
         return $pledge->save();
     }
 
+
+    //添加用户存储产品
+    public static function setPledgeProductAdd(object $pledge, int $user_id, int $count, array $order_no)
+    {
+
+        $product = self::getOrderProductList($user_id, $order_no, $count);
+        if(empty($product) || count($product) < $count)  throw new Exception('订单不存在');
+        
+        //修改订单状态
+        Loader::model('ProductOrder')::where('user_id', $user_id)->whereIn('order_no', $order_no)->setField('status', ProductOrder::Paid);
+
+        //添加产品
+        $detail = json_decode($pledge->details, true);
+        $pledge->details = json_encode(array_merge($detail, $product), JSON_UNESCAPED_UNICODE);   
+        $pledge->num =   $pledge->num + $count ; 
+        return $pledge->save();
+    }
+
+
+    //获取订单产品
+    private static function getOrderProductList(int $user_id, array $order_no, int $count)
+    {
+        return Loader::model('ProductOrder')::alias('a')
+            ->join('product_list b', 'a.product_id = b.id', 'left')
+            ->where('a.user_id', $user_id)
+            ->where('a.status', ProductOrder::Paid)
+            ->whereIn('a.order_no', $order_no)->field('a.id,b.zh_name,b.thum,a.order_no')->limit($count)->select();
+    }
 }