Browse Source

更新余额

afa 6 months ago
parent
commit
da2eaa5b41

+ 48 - 9
application/api/controller/Teac.php

@@ -27,12 +27,10 @@ class Teac extends Api
     {
         $type_id = $this->request->post('type_id/d', 0);
         if(!in_array($type_id, [ProductTeac::Buying, ProductTeac::Sell])) $this->error('类型错误');
-           
         $list = $productTeac->with('users')
              ->where('product_teac.status', ProductTeac::Normal)->where('type_id', $type_id)
              ->order('create_time desc')
              ->paginate($this->pageSize);
-    
         $this->success('ok', $list);
     }
 
@@ -142,10 +140,8 @@ class Teac extends Api
     }
 
 
-    /**
-    * 求购出售
-    */
-    public function getBuyOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
+    // 求购出售
+    public function setBuyOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
     {   
         $params = $this->request->post();
         $validate = \think\Loader::validate('Teac');
@@ -154,10 +150,9 @@ class Teac extends Api
         if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
         if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
         if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
-        $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
+        $chabao = $ledgerWalletModel->getWalletChaBao(1044);
         $tatal = bcmul($row['price'], $params['num'], 2);
         if($chabao < $tatal) $this->error('您的钱包茶宝余额不足');
-
         // 启动事务
         Db::startTrans();
         try {
@@ -169,7 +164,7 @@ class Teac extends Api
 
             //添加扣除相应茶宝Teac
             $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
-
+          
             //修改状态
             if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
             $row->frozen -= $chabao;
@@ -186,6 +181,50 @@ class Teac extends Api
     }
 
 
+    // 我的交易列表
+    public function getUserTeacList(ProductTeac $productTeac)
+    {
+        $type_id = $this->request->post('type_id/d', 0);
+        if(!in_array($type_id, [ProductTeac::Buying, ProductTeac::Sell])) $this->error('类型错误');
+        $list = $productTeac::where('user_id', $this->auth->id)
+             ->where('status', ProductTeac::Normal)->where('type_id', $type_id)
+             ->order('create_time desc')
+             ->paginate($this->pageSize);
+        $this->success('ok', $list);
+    }
+
+    //取消交易
+    public function cancelTrade(ProductTeac $productTeac, UserTeac $userTeac)
+    {
+        $id = $this->request->post('id/d', 0);
+        $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
+        if(empty($row)) $this->error('订单不存在');
+        if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
+        Db::startTrans();
+        try {
+
+      
+            // 出售购买
+            $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'],  $fee);
+
+            //添加扣除相应茶宝Teac
+            $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
+
+            //修改状态
+            if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
+  
+            $row->status = ProductTeac::Complete;
+            $row->save();
+            // 提交事务
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+           Db::rollback();
+            return $e->getMessage();
+        }
+        $this->success('ok');
+    }
+
     /**
     * 获取配置
     */

+ 3 - 1
application/api/logic/TeacLogin.php

@@ -77,11 +77,13 @@ class TeacLogin
         //添加用户茶宝
         $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $fromUid);
 
+     
         //扣除用户Teac
         $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, -$num, LedgerTeacChangeModel::BuySell, $fromUid);
-
+     
         //添加Teac
         $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TEAC, $num, LedgerTeacChangeModel::Buying, $uid);
+
         return true;
     }
 

+ 1 - 13
application/common/model/LedgerWalletModel.php

@@ -84,18 +84,12 @@ class LedgerWalletModel extends Model
         $frozen = ''; //冻结金额
         // 账变资产模型
         switch ($asset) {
-            case Asset::USDT:
-                $changeModel = new LedgerUsdtChangeModel();
-                break;
             case Asset::TOKEN:
                 $changeModel = new LedgerTokenChangeModel();
                 break;
             case Asset::TEAC:
                 $changeModel = new LedgerTeacChangeModel();
                 break;
-            case Asset::SMH:
-                $changeModel = new LedgerSmhChangeModel();
-                break;
             case Asset::FROZEN:
                 //扣掉赠送手续费
                 $changeModel = new LedgerFrozenChangeModel();
@@ -103,10 +97,8 @@ class LedgerWalletModel extends Model
             default:
                 throw new Exception('币种错误:' . $asset);
         }
-
         // 更新钱包余额
         $newAmount = $this->changeWalletOnly($uid, $asset, $amount);
-
         // 创建账变记录
         $insertRs = $changeModel->insert([
             'user_id'        => $uid,
@@ -116,7 +108,6 @@ class LedgerWalletModel extends Model
             'create_time'    => time(),
             'action'         => $action
         ]);
-
         if (empty($insertRs)) {
             throw new Exception('创建' . $asset . '账变记录失败');
         }
@@ -141,17 +132,14 @@ class LedgerWalletModel extends Model
         if (bccomp($amount, '0', 6) == 0) {
             throw new Exception('金额变动为0');
         }
-
         // 余额不足的判断
         if ($amount < 0 && $available[$asset] < -$amount) {
             throw new Exception(Asset::getAssetName($asset) . '余额不足');
         }
-
         $newAmount    = bcadd($available[$asset], $amount, 6); // 新余额
         $walletUpdate = [$asset => $newAmount, 'update_time' => time()];
-        
         // 更新余额
-        $changeRs = $this->save($walletUpdate, ['user_id' => $uid]);
+        $changeRs = $this->update($walletUpdate, ['user_id' => $uid]);
         if (empty($changeRs)) {
             throw new Exception('更新' . $asset . '余额失败');
         }