afa 7 сар өмнө
parent
commit
fc24092683

+ 3 - 14
application/api/controller/Home.php

@@ -73,21 +73,10 @@ class Home extends Api
     {
         //更新库存
         $productPopular::setUpdateStatus();
-        $resp['product_list']  = $productPopular->with('productsList')->where('product_popular.status', '<', $productPopular::Stop)
-        ->order('weigh desc,start_time asc')->select(); //$this->pageSize)
+        $resp['product_list']  = $productPopular->with('productsList')
+                        ->where('product_popular.status', '<', $productPopular::Stop)
+                        ->order('weigh desc,start_time asc')->select(); //$this->pageSize)
         
-        // 转让 
-        $resp['transfer_list'] = $productTransfer->alias('a')
-            ->join("product_list b", "a.product_id = b.id", "left")
-            ->join("user u", "a.user_id = u.id", "left")
-            ->join("products d", "b.type_id = d.id", "left")
-            ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,price,u.nickname,d.'.$this->lan.'_title as title')
-            ->where('a.status', $productTransfer::Normal)
-            ->limit(2)
-            ->order('a.id DESC')
-            ->select();
-        //抵押
-        $resp['mortgage_list'] = array();
         $this->success('', $resp);
     }
 

+ 14 - 18
application/api/controller/Order.php

@@ -12,6 +12,9 @@ use app\common\model\UserArea;
 use app\common\model\UserModel;
 use app\common\model\LedgerTokenChangeModel;
 use app\common\model\OfflineRechargeRecordModel;
+use app\api\logic\OrderLogic;
+use app\api\logic\MarketLogic;
+use app\common\model\ProductMarket;
 use Exception;
 use fast\Asset;
 use think\Db;
@@ -26,7 +29,7 @@ class Order extends Api
     /**
      * 创建订单
      */
-    public function create(ProductPopular $productPopular, ProductOrder $productOrder, LedgerWalletModel $ledgerWalletModel, UserModel $userModel)
+    public function create(ProductPopular $productPopular, OrderLogic $orderLogic, LedgerWalletModel $ledgerWalletModel, UserModel $userModel)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Order');
@@ -46,12 +49,9 @@ class Order extends Api
             if($order_info->start_time > time()) throw new Exception(__("抢购未开始"));
             if($order_info->stock == 0 || time() >= $order_info->end_time) throw new Exception(__("抢购已结束"));
             
-            //批量地区添加 1选择地区 2未选择地区
-            if($params['type'] == 1)
-                $result = $productOrder::setPopularAreaOrder($areaArr, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id, ProductOrder::Popular);
-            else
-                $result =$productOrder::setPopularNoAreaOrder($areaNum, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id,ProductOrder::Popular);
-            
+            // //批量地区添加 1选择地区 2未选择地区
+            $result = $orderLogic::createOrderByType($params['type'], $areaArr, $params['order_id'], $order_info->price, $params['product_id'], $this->auth->id, $areaNum);
+       
             //余额记录
             $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$totalPrice, LedgerTokenChangeModel::Popular, $this->auth->id);
 
@@ -125,7 +125,7 @@ class Order extends Api
      * 订单寄售转让
      * @return void
      */
-    public function transfer(ProductOrder $productOrder, ProductTransfer $productTransfer)
+    public function transfer(ProductOrder $productOrder, MarketLogic $marketLogic)
     {   
         $params = $this->request->post();
         $validate = \think\Loader::validate('Order');
@@ -138,10 +138,8 @@ class Order extends Api
             if($params['price'] < $order_info->min_transfer_fee || $params['price'] > $order_info->max_transfer_fee) throw new Exception(__("当前订单转让金额为").$order_info->min_transfer_fee.'~'.$order_info->max_transfer_fee);
 
             //转让订单
-            $fee = getConfig('transfer_fee');
-            $feeAmount = bcmul($params['price'], $fee, 2) ;
-            $productTransfer::setTransferOrder($this->auth->id, $order_info['product_id'], $order_info['area_id'], $feeAmount, $params);
-
+            $marketLogic::createTransferOrder($params['price'], $order_info['product_id'], $order_info['area_id'], $order_info['order_no'], $this->auth->id, $params);
+   
             //修改 类型状态
             $order_info->type_id = $productOrder::Transfer;
             $order_info->status  = $productOrder::Transferred;
@@ -193,6 +191,7 @@ class Order extends Api
                 //增加购买Rwa有效
                 $userModel::updateForRwaNum($this->auth->id, $this->auth->parent_id, 1, '+');
             }
+
             //修改原订单状态
             $popular_order->status=$productOrder::Closure;
             $popular_order->save();
@@ -258,7 +257,7 @@ class Order extends Api
 
 
     //取消转让 
-    public function cancel(ProductOrder $productOrder, ProductTransfer $productTransfer)
+    public function cancel(ProductOrder $productOrder, MarketLogic $marketLogic)
     {
         $params = $this->request->post();
         $validate = \think\Loader::validate('Order');
@@ -270,12 +269,9 @@ class Order extends Api
             $order_info = $productOrder->where('id', $params['order_id'])->find();
             if(empty($order_info)) throw new Exception(__("参数有误,无可用产品"));
 
-            //转让列表取消
-            $productTransfer::where('order_id',$params['order_id'])->setField('status', $productTransfer::Stop);
+            //取消转让订单
+            $marketLogic::cancelTransferOrder($order_info['order_id'],  $order_info['user_id'], $order_info);
 
-            //新增记录
-            $productOrder::setCreateOrder($order_info['order_id'], $order_info, $productOrder::Popular, $order_info['user_id'], $order_info['from_user'], getOrderSN('R'.$order_info['order_id']), 0, $order_info->popular_price);
-   
             //修改:类型状态
             $order_info->type_id= $productOrder::Transfer; 
             $order_info->status = $productOrder::Cancelled;

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

@@ -10,7 +10,7 @@ use app\common\model\ProductPledges;
 use Exception;
 use app\common\model\UserModel;
 use think\Db;
-use app\common\logic\PledgeLogic;
+use app\api\logic\PledgeLogic;
 
 
 //质押抵扣

+ 22 - 17
application/api/controller/Product.php

@@ -6,9 +6,11 @@ use think\Db;
 use think\db\Expression;
 use app\common\controller\Api;
 use app\common\model\ProductLists;
+use app\common\model\ProductMarket;
+use app\common\model\ProductOrder;
 use app\common\model\ProductsModel;
 use app\common\model\ProductPopular;
-use app\common\model\ProductTransfer;
+use app\api\logic\OrderLogic;
 
 class Product extends Api
 {
@@ -27,8 +29,7 @@ class Product extends Api
      */
     public function getPopularList(ProductsModel $productsModel, ProductPopular $productPopular, ProductLists $productLists)
     {
-
-        $item = $productsModel->where('status', 1)->column('id,'.$this->lan.'_title as title');
+        $item = $this->getPopularType($productsModel);
         $resp = array();
         foreach ($item as $kk =>$val) {
             $list=  $productLists->where('type_id', $kk)->field('id,thum as img_url,'.$this->lan.'_name as name')->select();
@@ -53,42 +54,46 @@ class Product extends Api
     }
 
 
-   
 
 
     /**
-     * 转让列表
+     * 寄售转让列表
      * sort: 0 默认排序 1价格从高到低 2价格从低到高
      * type_id 分类
      * key_val 商品名称搜索
      */
-    public function getTransferList(ProductTransfer $productTransfer)
+    public function getTransferList(ProductMarket $productMarket, OrderLogic $orderLogic, ProductOrder $productOrder)
     {
         $sort    = $this->request->post('sort/d', '');
         $type_id = $this->request->post('type_id/s', '');
         $key_val = $this->request->post('key_val/s', '');
-        $order = 'a.id DESC';
-        $map   = [];
+        $order = 'a.price ASC';
         if($sort == 1) $order = 'a.price DESC';
-        if($sort == 2) $order = 'a.price ASC';
-        if($type_id > 0) $map['b.type_id'] = ['=', $type_id];
+        $map['a.status'] = $productMarket::Normal;
+        if(!empty($type_id)) $map['b.type_id'] = $type_id;
         if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
-        $list = $productTransfer->alias('a')
+        $list = $productMarket->alias('a')
             ->join("product_list b", "a.product_id = b.id", "left")
-            ->join("user u", "a.user_id = u.id", "left")
-            ->join("products d", "b.type_id = d.id", "left")
-            ->join("product_area dr", "dr.id = a.area_id", "left") //地区
-            ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,price,u.nickname,d.'.$this->lan.'_title as title,dr.address')
-            ->where('a.status', $productTransfer::Normal)
+            ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,a.price,a.product_id,a.type_id')
+            ->where('a.status', $productMarket::Normal)
             ->where($map)
             ->order($order)
             ->paginate($this->pageSize);
+
+        foreach ($list as &$item) {
+            $item['issue']       = $orderLogic::getProductIssue($item->product_id);  //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的   
+            $item['circulation'] = $productOrder::where('status', $productOrder::Paid)->count();  //流通: 所有用户的持有量    
+        }
         $this->success('', $list);
     }
  
 
     
-
+    //获取分类
+    public function getPopularType($productsModel)
+    {
+        return $productsModel->where('status', 1)->column('id,'.$this->lan.'_title as title');
+    }
 
 }
 

+ 89 - 0
application/api/logic/MarketLogic.php

@@ -0,0 +1,89 @@
+<?php
+
+namespace app\api\logic;
+
+use Exception;
+use think\Env;
+use think\Cache;
+use think\Loader ;
+use app\common\model\ProductOrder;
+use app\common\model\ProductLists;
+use app\common\model\ProductTransfer;
+use app\common\model\ProductMarket;
+
+//自由市场
+class MarketLogic
+{
+
+
+      // 添加市场寄售订单
+      public static function createTransferOrder(float $price, int $productId, int $areaId, string $orderNo, int $userId, array $params)
+      {
+            //获取市场价
+            $minPrice = ProductTransfer::getTransferMinPrice($productId);
+            if($price < $minPrice) $minPrice = $price;
+
+            $isUpdate = false;
+            //添加市场
+            $rows = ProductMarket::where('product_id', $productId)->find();
+            if(empty($rows)){
+                  ProductMarket::create([
+                  'product_id' => $productId, 
+                  'type_id'    => ProductLists::getProductTypeById($productId), 
+                  'price'      => $minPrice,'status' => ProductMarket::Normal]);
+            }
+            
+            //更新市场状态
+            if($rows->status != ProductMarket::Normal ){
+                  $rows->status = ProductMarket::Normal;
+                  $isUpdate = true;
+            }
+            //更新最低价
+            if($rows->price != $minPrice){
+                  $rows->price = $minPrice;
+                  $isUpdate = true;
+            }
+            if($isUpdate) $rows->save();
+
+            //添加寄售订单
+            $fee = getConfig('transfer_fee');
+            $feeAmount = bcmul($price, $fee, 2) ;
+            return ProductTransfer::setTransferOrder($userId, $productId, $areaId, $feeAmount, $orderNo, $params);
+      }
+
+
+
+      //取消寄售更新最低价
+      public static function cancelTransferOrder(int $orderId, int $userId, $orderInfo)
+      {
+            //市场
+            $market = ProductMarket::where('product_id', $orderInfo['product_id'])->find();
+
+            //是否全部取消
+            $rows = ProductTransfer::where('product_id', $orderInfo['product_id'])->where('status', ProductTransfer::Normal)->count();
+            if($rows == 1){
+                  $market->status = ProductMarket::Hidden;
+            }else{
+                  //取消最小价格
+                  if($rows->price < $market->price){
+                        $market->price = ProductTransfer::getTransferMinPriceByProduct($orderInfo['product_id'], $rows->price);
+                  }
+            }
+
+            //市场状态
+            $market->save();
+
+            //转让列表取消
+            ProductTransfer::where('order_id',$orderId)->setField('status', ProductTransfer::Stop);
+
+            //新增记录
+            return ProductOrder::setCreateOrder($orderId, $orderInfo, ProductOrder::Popular, $userId, $orderInfo['from_user'], getOrderSN('R'.$orderInfo['order_id']), 0, $orderInfo->popular_price);
+      }
+
+
+
+
+
+
+
+}

+ 42 - 0
application/api/logic/OrderLogic.php

@@ -0,0 +1,42 @@
+<?php
+
+namespace app\api\logic;
+
+use Exception;
+use think\Env;
+use think\Cache;
+use think\Loader ;
+use fast\Asset;
+use app\common\model\ProductOrder;
+use app\common\model\LedgerTeacChangeModel;
+use app\common\model\ProductPopular;
+
+//订单 product_popular
+class OrderLogic
+{
+
+
+      //创建订单
+      public static function createOrderByType(int $type, array $areaArr, int $orderId, int $price, int $productId, int $uid, int $areaNum): bool
+      {
+            if($type == 1){
+                  $result = ProductOrder::setPopularAreaOrder($areaArr, $orderId, $price, $productId, $uid, ProductOrder::Popular);
+            }else{
+                  $result = ProductOrder::setPopularNoAreaOrder($areaNum, $orderId, $price, $productId, $uid,ProductOrder::Popular);   
+            }
+            return $result;
+      }
+
+      //获取产品发行量
+      public static function getProductIssue(int $productId): int
+      {
+            $popularNum = ProductPopular::where('product_id', $productId)->sum('stock - num');
+            
+            $holdNum = ProductOrder::where('product_id', $productId)->where('status', ProductOrder::Paid)->sum('num');
+            return bcadd($popularNum, $holdNum);
+      }
+
+
+
+
+}

+ 5 - 29
application/common/logic/PledgeLogic.php → application/api/logic/PledgeLogic.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace app\common\logic;
+namespace app\api\logic;
 
 use Exception;
 use think\Env;
@@ -11,10 +11,11 @@ use app\common\model\ProductOrder;
 use app\common\model\LedgerTeacChangeModel;
 use app\common\model\ProductPledges;
 
+//质押抵扣
 class PledgeLogic
 {
-
-
+    
+    
     //获取产品信息
     public  static function getByProductIdList(object $list, string $lan = 'zh')
     {
@@ -34,7 +35,6 @@ class PledgeLogic
     {
         $model  = Loader::model('ProductOrder');
         return $model::where('user_id', $user_id)->where('product_id', $product_id)->where('status', $model::Paid)->field('id,order_no')->select();
-      
     }
 
     //添加质抵押订单
@@ -63,6 +63,7 @@ class PledgeLogic
         return $model->whereIn('id', array_column($product, 'id'))->setField('status', $model::Freeze);
     }
 
+
     //获取质抵押订单列表
     //(60*60*24)*(当前时间-最后一次收取时间)
     public static function getPledgeOrderList(int $user_id)//: array
@@ -161,29 +162,4 @@ class PledgeLogic
     }
 
 
-
-
-    //判断请求限制
-    public static function getCheckRequestApi(string $key, int $user_id, int $time = 300)
-    {
-        $timestampsKey = $key.'_'.$user_id;
-        $currentTime = time();
-        $timestamps = Cache::get($timestampsKey, []);
-        if (count($timestamps) >= 5) {
-            // 检查最早的记录是否超过5分钟前
-            $oldestTime = min($timestamps);
-            if ($currentTime - $oldestTime < $time) { // 5分钟内
-                return false;
-            } else {
-                // 移除最早的记录并添加新的时间戳
-                $timestamps = array_diff($timestamps, [$oldestTime]);
-                $timestamps[] = $currentTime;
-            }
-        } else {
-            // 添加新的时间戳
-            $timestamps[] = $currentTime;
-        }
-        Cache::set($timestampsKey, $timestamps, $time); // 设置5分钟过期时间
-        return true;
-    }
 }

+ 0 - 9
application/api/logic/WelfareLoginc.php

@@ -1,6 +1,5 @@
 <?php
 
-
 namespace app\api\logic;
 
 use app\api\controller\Product;
@@ -10,15 +9,9 @@ use app\common\model\UserWelfare;
 use app\common\model\ProductPopular;
 use app\common\model\ProductOrder;
 use app\common\model\UserModel;
-use fast\Action;
-use fast\Asset;
-use fast\MembershipLevel;
 use think\Db;
 use think\Log;
 use think\Exception;
-use think\console\Output;
-use fast\Http;
-
 /**
  *  空投福利
  */
@@ -90,7 +83,6 @@ class WelfareLoginc
      */
     public static function setUserExRwaNum(int $rwa_num, $productId, $rwaProductId, $isArea, $orderId, $price, $mod, $num): int
     {   
-   
         $total = 0; //总数量
         // 使用bcdiv函数进行高精度除法运算,$num除以100,保留两位小数
         $div = bcdiv($num, 100, 2);
@@ -117,7 +109,6 @@ class WelfareLoginc
 
     /**
      * 设置用户产品订单
-     *
      * 该方法根据是否需要区域信息来设置用户的订单信息如果没有指定区域,则调用setPopularNoAreaOrder方法,
      * 否则,首先查询产品关联的区域ID,然后调用setPopularAreaOrder方法设置订单信息
      * @param int $num 订单数量

+ 0 - 13
application/common/model/LedgerThawModel.php

@@ -1,13 +0,0 @@
-<?php
-
-namespace app\common\model;
-
-use think\Model;
-
-class LedgerThawModel extends Model
-{
-
-    protected $name = "ledger_thaw";
-
-
-}

+ 8 - 1
application/common/model/ProductLists.php

@@ -65,12 +65,19 @@ class ProductLists extends Model
         return $this->hasOne('ProductPopular', 'product_id', 'id', [], 'INNER')->order('start_time')->setEagerlyType(0);
     }
     
+    //获取产品分类
+    public static function getProductTypeById(int $productId)
+    {
+        return self::where('id', $productId)->value('type_id');
+    }
+
+
+
     //获取状态
     public function getStatusTextAttr($value, $data){
         $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
     }
         
-
     protected static function init()
     {
         self::afterInsert(function ($row) {

+ 76 - 0
application/common/model/ProductMarket.php

@@ -0,0 +1,76 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+
+class ProductMarket extends Model
+{
+
+    // 表名
+    protected $table = 'product_market';
+
+    
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+
+    // 定义时间戳字段名
+    protected $createTime = 'create_time';
+    protected $updateTime = 'update_time';
+    protected $deleteTime = false;
+
+
+    // 追加属性
+    protected $append = [
+        'create_time_text',
+        'update_time_text'
+    ];
+    
+    //状态 normal','hidden')
+    const Hidden           = 0;
+    const Normal           = 1;
+
+
+
+    //分类表
+    public function products()
+    {
+        return $this->hasOne('ProductsModel', 'id', 'type_id', [], 'LEFT')->setEagerlyType(0);
+    }
+
+
+    //产品
+    public function productlists()
+    {
+        return $this->hasOne('ProductLists', 'id', 'product_id', [], 'LEFT')->setEagerlyType(0);
+    }
+
+
+
+    public function getCreateTimeTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
+        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
+    }
+
+
+    public function getUpdateTimeTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
+        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
+    }
+
+
+    protected function setCreateTimeAttr($value)
+    {
+        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
+    }
+
+    protected function setUpdateTimeAttr($value)
+    {
+        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
+    }
+
+
+}

+ 8 - 1
application/common/model/ProductTransfer.php

@@ -35,7 +35,7 @@ class ProductTransfer extends Model
 
 
     //添加/更新转让订单
-    public static function setTransferOrder($uid, $product_id, $area_id, $feeAmount, $params)
+    public static function setTransferOrder($uid, $product_id, $area_id, $feeAmount, $orderNo, $params)
     {
         $rows = self::where('order_id', $params['order_id'])->find();
         if(empty($rows)){
@@ -46,6 +46,7 @@ class ProductTransfer extends Model
             $order_data['fees']       = $feeAmount;
             $order_data['area_id']    = $area_id;
             $order_data['order_id']   = $params['order_id']; //订单ID
+            $order_data['order_no']    = $orderNo;
             return self::create($order_data);
         }else{
             $rows->price  = $params['price'];
@@ -61,6 +62,12 @@ class ProductTransfer extends Model
     {
         return self::where('product_id', $product_id)->where('status', self::Normal)->min('price');
     }
+
+    //获取最小价格
+    public static function getTransferMinPriceByProduct(int $product_id, float $price)
+    {
+        return self::where('product_id', $product_id)->where('status', self::Normal)->where('price', '>', $price)->min('price');
+    }
     
     //产品
     public function products()