afa 6 сар өмнө
parent
commit
31c8831a4e

+ 73 - 32
application/api/controller/Teac.php

@@ -5,9 +5,9 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\api\logic\TeacLogin;
-use app\common\model\ParametersModel;
+use app\common\model\LedgerTeacChangeModel;
 use app\common\model\LedgerTokenChangeModel;
-use app\common\model\UserModel;
+use app\common\model\UserTeac;
 use app\common\model\LedgerWalletModel;
 use fast\Action;
 use fast\Asset;
@@ -20,8 +20,6 @@ use app\common\model\ProductTeac;
 class Teac extends Api
 {
     
-    
-
     /*
      * 求购列表
      */
@@ -50,18 +48,19 @@ class Teac extends Api
         if(config('teac_trade.buy_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.buy_min_num'));
         if(config('teac_trade.buy_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.buy_min_price'));
 
-        $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
-        if($chabao < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包茶宝余额不足');
-        
+        $chabao = bcmul($params['price'], $params['stock'], 2) ;
+        if($ledgerWalletModel->getWalletChaBao($this->auth->id) < $chabao) $this->error('您的钱包茶宝余额不足');
+      
         Db::startTrans();
         try{
             
             //新增求购信息
-            $order_price = $teacLogin::setCreateTrade($this->auth->id, $params['price'],  $params['stock']);
+            $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Buying, $chabao);
 
-            //冻结资产
-            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id);
+            //冻结茶宝
+            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, -$chabao, LedgerTokenChangeModel::Buying, $this->auth->id);
 
+       
             Db::commit();
             $this->success('订单创建成功');
         }catch(Exception $e){
@@ -79,21 +78,19 @@ class Teac extends Api
         $validate = \think\Loader::validate('Teac');
         if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
         
-        if(config('teac_trade.sell_min_num') > $params['price']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
+        if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
         if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
 
-        $teac = $ledgerWalletModel->getWalletTeac($this->auth->id);
-        if($teac < bcmul($params['price'], $params['stock'], 2)) $this->error('您的钱包Teac余额不足');
-        
+        if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
+
         Db::startTrans();
         try{
             
             //新增求购信息
-            $order_price = $teacLogin::setCreateTrade($this->auth->id, $params['price'],  $params['stock'], TeacTrade::Sell);
-
-            //冻结资产
-            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TEAC, -$order_price, LedgerTokenChangeModel::Buying, $this->auth->id);
+            $teacLogin::setCreateTrade($this->auth->id, $params['price'],  $params['stock'], ProductTeac::Sell, $params['stock']);
 
+            //冻结Teac
+            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TEAC, -$params['stock'], LedgerTokenChangeModel::BuySellg, $this->auth->id);
             Db::commit();
             $this->success('订单创建成功');
         }catch(Exception $e){
@@ -105,43 +102,87 @@ class Teac extends Api
 
     /**
      * 出售购买
-     * @return void
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\ModelNotFoundException
-     * @throws \think\exception\DbException
      */
-    public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
+    public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
     {
-      
         $params = $this->request->post();
         $validate = \think\Loader::validate('Teac');
         if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
-        $row = $productTeac::get($params['id']);
-        if(empty($row)) $this->error('订单不存在');
-        if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
+        $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
+        if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
+        if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
+        $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
+        $tatal = bcmul($row['price'], $params['num'], 2);
+        if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
+
         // 启动事务
         Db::startTrans();
         try {
-         
 
+            $fee = config('teac_trade.sell_fee');
+         
+            // 出售购买
+            $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'],  $fee);
 
+            //添加扣除相应茶宝Teac
+            $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
 
-        
+            //修改状态
+            if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
+            $row->frozen -= $params['num'];
+            $row->num += $params['num'];
+            $row->save();
             // 提交事务
             Db::commit();
         } catch (Exception $e) {
             // 回滚事务
-            Db::rollback();
+           Db::rollback();
             return $e->getMessage();
         }
+        $this->success('ok');
     }
 
 
     /**
     * 求购出售
     */
-    public function getBuyOrder()
-    {
+    public function getBuyOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
+    {   
+        $params = $this->request->post();
+        $validate = \think\Loader::validate('Teac');
+        if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
+        $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
+        if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
+        if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
+        $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
+        $tatal = bcmul($row['price'], $params['num'], 2);
+        if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
+
+        // 启动事务
+        Db::startTrans();
+        try {
+
+            $fee = config('teac_trade.sell_fee');
+            $chabao = bcmul($row['price'], $params['num'], 2);
+            // 出售购买
+            $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->frozen -= $chabao;
+            $row->num += $params['num'];
+            $row->save();
+            // 提交事务
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+           Db::rollback();
+            return $e->getMessage();
+        }
+        $this->success('ok');
         $this->success('ok', config('teac_trade'));
     }
 

+ 1 - 1
application/api/controller/User.php

@@ -54,7 +54,7 @@ class User extends Api
             'token'           => '0', // 平台币余额
             'name'            => $user['name'], // 姓名
             'phone'           => $user['phone'], // 手机号
-            'rental_power'    => '0', // 自己购买的算力22
+            'rental_power'    => '0', // 自己购买的算力
             'team_power'      => '0', // 团队总算里
             'balance'         => $ledgerWalletModel::getWalletChaBao($this->auth->id), // 余额
             'rwa_num'         => $user['rwa_num'], // 总茶权

+ 2 - 2
application/api/logic/OrderLogic.php

@@ -2,6 +2,7 @@
 
 namespace app\api\logic;
 
+use app\api\controller\Product;
 use Exception;
 use think\Env;
 use think\Cache;
@@ -41,8 +42,7 @@ class OrderLogic
       //获取产品流通量
       public static function getProductCirculation(int $productId): int
       {     
-         
-            return Loader::model('ProductOrder')::where('product_id', $productId)->where('status', ProductOrder::Paid)->count();
+            return Loader::model('ProductOrder')::where('product_id', $productId)->whereIn('status', [ProductOrder::Paid, ProductOrder::Transferred, ProductOrder::Freeze])->count();
       }
 
 

+ 39 - 39
application/api/logic/TeacLogin.php

@@ -9,9 +9,11 @@ use think\Loader;
 use Exception;
 use finfo;
 use think\Cache;
-use think\Env;
+use fast\Asset;
 use think\Log;
-
+use think\Lang;
+use app\common\model\LedgerTeacChangeModel;
+use app\common\model\LedgerTokenChangeModel;
 /**
  * Teac交易
  */
@@ -22,64 +24,62 @@ class TeacLogin
     /**
      * 发布求购出售信息
     */
-    public static  function setCreateTrade(int $uid, float $price, int $stock, int $typeId = ProductTeac::Buying):float
+    public static  function setCreateTrade(int $uid, float $price, int $stock, int $typeId, float $frozen):object
     {
-        
         $rows = ProductTeac::where('status', ProductTeac::Normal)->find();
         if($rows)  throw new Exception(__("你有未完成的求购订单、不能重复发布"));
 
         //添加订单信息
-        return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, bcmul($price, $stock, 2));
+        return ProductTeac::setUserCreateOrder($uid, $typeId, $price, $stock, $frozen);
     }
 
     /**
-     * 发布出售
-     * @$address  查询地址
-     * @$from_to  是查询付款交易(from),还剩收款交易(to),默认from
+     * 购买出售
+     * @$uid  用户人id
+     * @$fromUid  来源订单id
      */
-    public function setCreateSellOrder(string $from, string $to, int $start_block, int $end_block = 99999999)
+    public static function setCreateSellOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
     {
-        if(empty($from) && empty($to)){
-            return _error('钱包地址不能都为空');
-        }
 
+        $ledgerWalletModel = Loader::model('LedgerWalletModel');
+        $total_price = bcmul($chabao, $num, 2); //总价
+        $fee = bcmul($total_price, $fee, 2); //手续费
+
+        //扣除用户茶宝
+        $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, -$total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
+
+        //添加用户Teac
+        $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTokenChangeModel::BuySellg, $fromUid);
+
+
+        //添加来源茶宝
+        $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
+
+        return true;
     }
 
 
     /**
-     * 获取哈希地址成功状态
-     * @param $orderInfo
-     * @return array|string
+     * 求购出售
+     * @$uid  用户人id
+     * @$fromUid  来源订单id
      */
-    public function getHashStatus($tx_hash):array
+    public static function setCreateBuyingOrder(int $uid, int $fromUid, float $chabao, int $num, float $fee)
     {
-    if (empty($tx_hash)) {
-        return _error('hash值不能为空');
-    }
-
-    $url = "https://api.bscscan.com/api?module=transaction&action=gettxreceiptstatus";
-    $url .= "&apikey=" . $this->bsc_api_key;
-    $url .= "&txhash=" . $tx_hash;
 
-    $body = Http::get($url);
-    if (empty($body)) {
-        return _error('状态api返回内容为空');
-    }
-    // 转成数组
-    $rsArr = json_decode($body, true);
-    if (empty($rsArr) || !is_array($rsArr)) {
-        return _error('状态api返回数据异常,json转换失败');
-    }
+        $ledgerWalletModel = Loader::model('LedgerWalletModel');
+        $total_price = bcmul($chabao, $num, 2); //总价
+        $fee = bcmul($total_price, $fee, 2); //手续费
 
-    if ($rsArr['status'] != '1') {
-        return _error('状态api返回status不为1,当前值为:' . $rsArr['status']);
-    }
+        //添加用户茶宝
+        $ledgerWalletModel->changeWalletAccount($uid, Asset::TOKEN, $total_price, LedgerTokenChangeModel::BuySellg, $fromUid);
 
-    if ($rsArr['result']['status'] != 1) {
-        return _error('状态api返回result中的status不为1,当前值为:' . $rsArr['result']['status']);
-    }
+        //扣除用户Teac
+        $ledgerWalletModel->changeWalletAccount($uid, Asset::TEAC, $num, LedgerTokenChangeModel::BuySellg, $fromUid);
 
-    return _success();
+        //扣除来源茶宝
+        $ledgerWalletModel->changeWalletAccount($fromUid, Asset::TOKEN, bcsub($total_price, $fee, 2), LedgerTokenChangeModel::BuySellg, $uid);
+        return true;
     }
 
       /**

+ 13 - 2
application/common/model/LedgerTeacChangeModel.php

@@ -12,7 +12,10 @@ class LedgerTeacChangeModel extends Model
 
     //0支付 1转让支付 2 转让收款 3 充值 4 提现 5扣除书续费
     const Pledge           = 0;
- 
+    const Sell             = 1;
+    const SellBuy          = 2;
+    const Buying           = 3;
+    const BuySell          = 4;
     /*
      * 支付状态
      * 0未支付 100支付中 200支付成功 400支付失败
@@ -20,6 +23,10 @@ class LedgerTeacChangeModel extends Model
     public $pay_status = [
         '-1'                   => '全部',
         self::Pledge           => '质押存储',
+        self::Sell             => '出售',
+        self::SellBuy          => '出售购买',
+        self::Buying           => '存储',
+        self::BuySell          => '存储出售',
 
     ];
 
@@ -27,7 +34,11 @@ class LedgerTeacChangeModel extends Model
     public static function getStatusList()
     {
         return [
-            self::Pledge => __('质押存储'),
+            self::Pledge    => __('质押存储'),
+            self::Sell      => __('出售'),
+            self::SellBuy   => __('出售购买'),
+            self::Buying    => __('存储'),
+            self::BuySell   => __('存储出售'),
         ];
     }
 

+ 5 - 5
application/common/model/ProductTeac.php

@@ -47,19 +47,19 @@ class ProductTeac extends Model
 
 
     //添加订单信息
-    public static function setUserCreateOrder(int $uid, int $typeId, float $price, int $stock, float $frozen)
+    public static function setUserCreateOrder(int $uid, int $typeId, float $price, int $stock, float $frozen): object
     {
         $total_price = bcmul($price, $stock, 2);
-        self::create([
+        return self::create([
             'user_id' => $uid,
             'type_id' => $typeId,
             'price'   => $price,
             'stock'   => $stock,
             'total_price'=> $total_price,
-            'fee'     => bcmul($total_price, config('market_transfer.serve_fee'), 2),
-            'frozen'  => $frozen,
+            'fee'        => bcmul($total_price, config('market_transfer.serve_fee'), 2),
+            'frozen'     => $frozen,
         ]); 
-        return $total_price;
+      
     }
 
     

+ 14 - 0
application/common/model/UserTeac.php

@@ -24,6 +24,19 @@ class UserTeac extends Model
         'update_time_text'
     ];
 
+    //添加订单记录
+    public static function setUserCreateOrder(int $uid, int $tradeId, int $typeId, int $num, float $price, float $fee)
+    {
+        return self::create([
+            'user_id'       => $uid,
+            'trade_id'      => $tradeId,
+            'type_id'       => $typeId,
+            'num'           => $num,
+            'price'         => $price,
+            'total_price'   => bcmul($price, $num, 2),
+            'fee'           => $fee
+        ]);
+    }
     
     //用户 user_id
     public function users()
@@ -32,6 +45,7 @@ class UserTeac extends Model
     }
 
 
+
     public function getCreateTimeTextAttr($value, $data)
     {
         $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');