Browse Source

解除质抵押订单

afa 7 tháng trước cách đây
mục cha
commit
810f865a88

+ 42 - 13
application/api/controller/Pledge.php

@@ -74,22 +74,21 @@ class Pledge extends Api
       /*
       * 质押存储
       */
-      public function create( ProductPledges $productPledges, PledgeLogic $pledgeLogic)
+      public function create(ProductPledges $productPledges, PledgeLogic $pledgeLogic)
       {
         $pledge_id  = $this->request->post('pledge_id', 0, 'intval');
         $order_no   = $this->request->post('order_no/a', '');
         if(empty($pledge_id) || empty($order_no)) $this->error(__("参数有误,无可用产品"));
 
         $pledge = $productPledges::get($pledge_id);
-        if (empty($pledge)) $this->error(__("合成活动不存在"));
-        if (empty($pledge->status) )$this->error(__("合成活动已结束"));
+        if(count(explode(',', $pledge->product_id)) || count($order_no)) $this->error(__("参数有误,无可用产品"));
 
+        if (empty($pledge)) $this->error(__("质抵活动不存在"));
+        if (empty($pledge->status) )$this->error(__("质抵活动已结束"));
         Db::startTrans();
         try {
-          
             // 质抵押订单 
             $pledgeLogic::setPledgeOrder($pledge, $order_no, $this->auth->id);
-
             // 提交事务
             Db::commit();
         } catch (Exception $e) {
@@ -99,23 +98,33 @@ class Pledge extends Api
         }
         $this->success('ok');
       }
-   
-
-    
 
  
       /*
       * 我的茶矿
       */
-      public function teamine(ProductPledges $productPledges, PledgeLogic $pledgeLogic)
+      public function teamine(PledgeLogic $pledgeLogic)
       {
-           
-            Db::startTrans();
             try {
-            
                   // 质抵押订单 
-                  $res = $pledgeLogic::getPledgeOrderList(1275);
+                  $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
+
+            } catch (Exception $e) {
+              
+                  $this->error($e->getMessage(), null, $e->getCode());
+            }
+            $this->success('ok', $res);
+      }
 
+      /*
+      * 收取茶矿
+      */
+      public function collect(PledgeLogic $pledgeLogic)
+      {
+            Db::startTrans();
+            try {
+                  // 质抵押订单 
+                  $res = $pledgeLogic::getPledgeOrderList($this->auth->id);
 
                  // 提交事务
                   Db::commit();
@@ -127,7 +136,27 @@ class Pledge extends Api
             $this->success('ok', $res);
       }
 
+      /*
+      * 解除质押
+      */
+      public function remove(PledgeLogic $pledgeLogic)
+      {
+            $pledge_id  = $this->request->post('pledge_id', 0, 'intval');
+            if(empty($pledge_id)) $this->error(__("参数有误,无可用产品"));
+            Db::startTrans();
+            try {
+                  // 质抵押订单 
+                  $res = $pledgeLogic::setPledgeRemove($pledge_id, $this->auth->id);
 
+                 // 提交事务
+                  Db::commit();
+            } catch (Exception $e) {
+                  // 回滚事务
+                  Db::rollback();
+                  $this->error($e->getMessage(), null, $e->getCode());
+            }
+            $this->success('ok', $res);
+      }
    
  
 }

+ 20 - 0
application/common/logic/PledgeLogic.php

@@ -11,6 +11,7 @@ use app\common\model\ProductPledges;
 
 class PledgeLogic
 {
+
   
     //获取产品信息 user_pledge
     public  static function getByProductIdList(object $list, string $lan = 'zh')
@@ -70,4 +71,23 @@ class PledgeLogic
         return ['total' => $total, 'growth' => $growth, 'list' => $list];   
     }
 
+    //解除质抵押订单
+    public static function setPledgeRemove(int $pledge_id,int $user_id)
+    {
+        $model  = Loader::model('UserPledge');
+        $rows   = $model::where('user_id', $user_id)->where('id', $pledge_id)->where('status', $model::Ongoing)->find();
+        if(empty($rows))  throw new Exception('暂无质押订单');
+        $day   = 86400;
+        $total = 0; //当前累计收益
+ 
+        $time  = time();
+        $reta  = bcdiv($rows->day_num, $day, 2); //天数
+        $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
+        $total = bcmul($reta, $inter, 2) * $rows->num; //累计收益
+        $rows->total_self= bcadd($total, $rows->total_self, 2);
+        $rows->status    = $model::Remove;
+        $rows->last_time = $time;
+        return  $rows->save();   
+    }
+    //
 }

+ 0 - 2
application/common/model/ProductPledges.php

@@ -34,8 +34,6 @@ class ProductPledges extends Model
     const Combin         = 2; //组合
 
 
- 
-
 
     protected static function init()
     {

+ 3 - 3
application/common/model/UserPledge.php

@@ -29,10 +29,10 @@ class UserPledge extends Model
         'update_time_text'
     ];
 
-    //状态: 存储中 0解除
-    const Remove = 0;
+    //状态:1存储中 2解除 0关闭
+    const Close  = 0;
     const Ongoing= 1;
-
+    const Remove = 2;
 
     public static function setPledgeData($user_id, $pledge_id, $product_id, $day_num, $num)
     {