afa 7 сар өмнө
parent
commit
090a8be179

+ 61 - 13
application/api/controller/Market.php

@@ -13,6 +13,7 @@ use app\common\model\UserModel;
 use app\common\model\LedgerWalletModel;
 use app\common\model\ProductTransfer;
 use app\api\logic\MarketLogic;
+use app\common\model\UserBuying;
 use fast\Action;
 use fast\Asset;
 use fast\Http;
@@ -104,21 +105,18 @@ class Market extends Api
         $params = $this->request->post();
         $validate = \think\Loader::validate('Market');
         if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
-
         if($productBuying::getProductBuyingCount($this->auth->id, $params['product_id']) > config('market_buying.max_buying_num')) $this->error('您的求购次数已达上限');
         $chabao     =  $ledgerWalletModel::getWalletChaBao($this->auth->id);
         $total      = bcmul($params['min_price'], $params['num'], 6); // 所需茶宝
         if($chabao < $total) $this->error('您的茶币不足');
-
         // 启动事务
         Db::startTrans();
         try {
-     
             // 记录订单 
             $productBuying::setCreateBuying($this->auth->id, $params['product_id'], $params['num'], $params['min_price'], $total);
 
             // 扣除茶币
-            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, -$total, LedgerTokenChangeModel::BuySellg, $this->auth->id);
+            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, -$total, LedgerTokenChangeModel::Buying, $this->auth->id);
 
             $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, $total); //冻结金额
             // 提交事务
@@ -131,39 +129,89 @@ class Market extends Api
         $this->success('ok');
     }
 
+    /**
+     * 求购详情
+     * @return void
+     */
+    public function getBuyingDetail( ProductBuying $productBuying)
+    {
+        $params = $this->request->post();
+        $validate = \think\Loader::validate('Market');
+        if(!$validate->scene('buying_info')->check($params)) $this->error($validate->getError());
+        $resq = $productBuying::getProductBuyingDetail($params['buying_id'], $this->request->getLan());
+        $this->success('ok', $resq);
+    }
+
 
     //出售求购
-    public function sellBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
+    public function sellBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying, UserBuying $userBuying)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Market');
         if(!$validate->scene('sellbuying')->check($params)) $this->error($validate->getError());
-        $buying = $productBuying::get($params['id']);
+        $buying = $productBuying::get($params['buying_id']);
         if(empty($buying)) $this->error('订单不存在');
         if($buying->user_id!= $this->auth->id) $this->error('无权操作');
         if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
-        $total = bcmul($buying->min_price, $buying->num, 6); // 所需茶宝
-        $chabao =  $ledgerWalletModel::getWalletChaBao($this->auth->id);
-        if($chabao < $total) $this->error('您的茶币不足');
+      
         // 启动事务
+        Db::startTrans();
+        try {
+
+            // 记录出售订单 
+            $chabao =$userBuying::getCreateUserBuying($this->auth->id, $params['buying_id'], $buying->user_id, $buying->min_price);
+
+            // 添加扣除茶币
+            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, $chabao, LedgerTokenChangeModel::BuySellg, $buying->user_id);
+
+            // 扣除冻结金额
+            $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, -$chabao); //解冻金额
+
+            if($buying->num == 1) $buying->status = ProductBuying::SaleOut;
+            $buying->num = $buying->num +1;
+            $buying->save();
+            // 提交事务
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+            Db::rollback();
+            return $e->getMessage();
+        }
+        $this->success('ok');
 
     }
 
     //取消求购
-    public function cancelBuying(ProductBuying $productBuying)
+    public function cancelBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Market');
         if(!$validate->scene('cancelbuying')->check($params)) $this->error($validate->getError());
-        $buying = $productBuying::get($params['id']);
+        $buying = $productBuying::get($params['buying_id']);
         if(empty($buying)) $this->error('订单不存在');
         if($buying->user_id!= $this->auth->id) $this->error('无权操作');
         if($buying->status != ProductBuying::Normal) $this->error('订单已完成');
-        $buying->status = ProductBuying::Close;
-        $buying->save();
 
+        Db::startTrans();
+        try {
+
+            $buying =$ledgerWalletModel::getWalletBuying($this->auth->id);
+            // 添加扣除茶币
+            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, $buying, LedgerTokenChangeModel::BuyCancel, $buying->user_id);
+
+            $buying->status = ProductBuying::Close;
+            $buying->save();
+            // 提交事务
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+            Db::rollback();
+            return $e->getMessage();
+        }
+        $this->success('ok');
     }
 
+
     //获取配置信息
     public function getMarketConfig()
     {

+ 7 - 6
application/api/validate/Market.php

@@ -15,8 +15,8 @@ class Market extends Validate
       'transfer_id'   => 'require',
       'min_price'     => 'require|number|gt:0',
       'num'           => 'require|number|gt:0',
-      'type'       => 'require|in:1,2',
-      'phone'      => 'require|mobile',
+      'buying_id'     => 'require|number|gt:0',
+      'order_id'      => 'require|number',
       'address'    => 'require',
       'price'      => 'require|number|gt:0',
     ];
@@ -31,9 +31,8 @@ class Market extends Validate
       'transfer_id.require' => '参数ID有误',
       'min_price'           => '价格有误',
       'num'                 => '数量有误',
-      'type.require'       => '参数有误',
-      'price.gt'           => '转让金额有误',
-      'name.require'       => '姓名不能为空',
+      'buying_id'           => '参数有误',
+      'order_id'            => '参数有误',
       'phone.mobile'       => '手机号有误',
       'address.require'    => '请填写地址',
     ];
@@ -46,7 +45,9 @@ class Market extends Validate
         'announcement' => ['product_id'],
         'transferlock' => ['transfer_id'],
         'buying'       => ['product_id', 'min_price', 'num'],
-        'giv'  => ['order_id', 'address'],
+        'buying_info'  => ['buying_id'],
+        'sellbuying'   => ['buying_id', 'order_id'],
+        'cancelbuying' => ['buying_id'],
     ];
   
 }

+ 6 - 3
application/common/model/LedgerTokenChangeModel.php

@@ -26,8 +26,9 @@ class LedgerTokenChangeModel extends Model
      const Service           = 13; //服务津贴
      const Together          = 14; //共创津贴
      const Super             = 15; //茶宝标记激活
-     const BuySellg          = 16; //求购出售
-   
+     const Buying            = 16; //求购购买
+     const BuySellg          = 17; //求购出售
+     const BuyCancel         = 18; //求购取消
      /*
       * 支付状态
       * 0未支付 100支付中 200支付成功 400支付失败
@@ -71,7 +72,9 @@ class LedgerTokenChangeModel extends Model
             self::Service => __('服务津贴'),
             self::Together => __('共创津贴'),
             self::Super => __('茶宝标记激活'),
-            self::BuySellg => __('求购出售')
+            self::Buying => __('求购购买'),
+            self::BuySellg => __('求购出售'),
+            self::BuyCancel => __('求购取消'),
            ];
     }
    

+ 5 - 0
application/common/model/LedgerWalletModel.php

@@ -36,6 +36,11 @@ class LedgerWalletModel extends Model
         return self::where('user_id', $userID)->value('frozen');
     }
 
+    public static function getWalletBuying($userID)
+    {
+        return self::where('user_id', $userID)->value('buying');
+    }
+
     public static function getWalletTotalChaBao($userID)
     {
         return self::where('user_id', $userID)->value('token + frozen');

+ 11 - 0
application/common/model/ProductBuying.php

@@ -64,6 +64,17 @@ class ProductBuying extends Model
         ]);
     }
 
+    //求购详情
+    public static function getProductBuyingDetail(int $buyingId, string $lan)
+    {
+        return self::alias('a')
+            ->join('product_list p', 'a.product_id = p.id')
+            ->field('a.*,p.' . $lan . '_name as name,p.thum')
+            ->where('a.id', $buyingId)
+            ->find();
+
+    }
+
     //获取订单列表
     public static function getBuyingList(int $productId)
     {

+ 16 - 1
application/common/model/UserBuying.php

@@ -41,7 +41,22 @@ class UserBuying  extends Model
         self::Finish            => '完成'
     ];
 
-   
+    //出售记录
+    public static function getCreateUserBuying(int $uid, int $orderId, int $fromId, float $amount)
+    {
+        $fee = bcmul(config('app.market_transfer.serve_fee'), $amount, 2);
+        $income = bcsub($amount, $fee, 2);
+        self::create([
+            'user_id'       => $uid,
+            'order_id'      => $orderId,
+            'from_id'       => $fromId,
+            'amount'        => $amount,
+            'fee'           => $fee,
+            'income'        => $income,
+        ]);
+        return $income;
+    }
+
     
     public function getCreateTimeTextAttr($value, $data)
     {