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

+ 22 - 58
application/api/controller/Market.php

@@ -2,7 +2,7 @@
 
 namespace app\api\controller;
 
-
+use app\common\model\LedgerTokenChangeModel;
 use app\common\controller\Api;
 use app\common\model\AnnouncementModel;
 use app\common\model\UserCollect;
@@ -85,80 +85,41 @@ class Market extends Api
     }
 
 
-    
-    //求购
-    public function buying()
-    {
-        $params = $this->request->post();
-        $validate = \think\Loader::validate('Market');
-        if(!$validate->scene('announcement')->check($params)) $this->error($validate->getError());
-
-    }
-
-
 
     //求购列表
     public function buyingList()
     {
-     
         
     }
 
-
-
     /**
-     * 手段向某会员报单算力
+     * 发起求购
      * @return void
-     * @throws \think\db\exception\DataNotFoundException
-     * @throws \think\db\exception\ModelNotFoundException
-     * @throws \think\exception\DbException
      */
-    public function updateOrder()
+    public function setBuying(LedgerWalletModel $ledgerWalletModel, ProductBuying $productBuying, ProductOrder $productOrder)
     {
-        //算力租赁订单处理
-        // 查询兑换比例
-        $usdtToPower      = (new ParametersModel)->getValue('usdtToPowerRate');
-        $usdtToPowerFloat = floatval($usdtToPower);
-        if (is_null($usdtToPower) || $usdtToPowerFloat <= 0) {
-            return '获取USDT兑换算力的比例失败';
-        }
-
-        $orderInfo = (new OfflineRechargeRecordModel())
-            ->where('id', 4)
-            ->find();
-        if(empty($orderInfo)){
-            halt('订单信息不存在');
-        }
-        $uid   = $orderInfo['user_id'];
-        $fee   = $orderInfo['amount'];
-        $power = bcmul($fee, $usdtToPowerFloat, 6); // 该用户兑得的算力
-        // 启动事务
-        Db::startTrans();
-        try {
-            // 更新总算力和账变
-            (new LedgerWalletModel)->changeWalletAccount($uid, Asset::POWER, $power, Action::PowerRentalPower, $orderInfo['id']);
 
-            // 更新服务器算力,不账变
-            (new LedgerWalletModel)->changeWalletOnly($uid, Asset::RENTAL_POWER, $power);
-
-            // 更新自己(有效会员时间)和所有上级的信息(有效直推人数和团队总算力)
-            (new UserModel())->updateForRental($uid, $power);
-
-            // 发放直推USDT收益
-            (new LedgerWalletModel)->sendUsdtProfit($uid, $fee);
+        $params = $this->request->post();
+        $validate = \think\Loader::validate('Market');
+        if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
 
-            // 发放直推算力收益
-            (new LedgerWalletModel)->sendDirectProfit($uid, $power);
+        if($productBuying::getProductBuyingCount($this->auth->id, $params['product_id']) > config('market_buying.max_buying_num')) $this->error('您的求购次数已达上限');
 
-            // 发代数收益
-            (new LedgerWalletModel)->sendGenerateProfit($uid, $fee);
+        $chabao     =  $ledgerWalletModel::getWalletChaBao($this->auth->id);
+        $total      = bcmul($params['min_price'], $params['num'], 6); // 所需茶宝
+        if($chabao < $total) $this->error('您的茶币不足');
 
-            // 发放见点奖
-            (new LedgerWalletModel)->sendRegBonus($uid, $fee);
+        // 启动事务
+        Db::startTrans();
+        try {
+     
+            // 记录订单 
+            $productBuying::setCreateBuying($this->auth->id, $params['product_id'], $params['num'], $params['min_price'], $total);
 
-            // 更新购买(充值)记录
-            (new OfflineRechargeRecordModel())->updateOrderStatus($orderInfo['id'], RechargeStatus::StatusAuthSuccess, 0, $power);
+            // 扣除茶币
+            $ledgerWalletModel->changeWalletAccount($this->auth->id,  Asset::TOKEN, -$total, LedgerTokenChangeModel::BuySellg, $this->auth->id);
 
+            $ledgerWalletModel->changeWalletOnly($this->auth->id, Asset::BUYING, $total); //冻结金额
             // 提交事务
             Db::commit();
         } catch (Exception $e) {
@@ -166,6 +127,9 @@ class Market extends Api
             Db::rollback();
             return $e->getMessage();
         }
+
+        $this->success('ok');
+
     }
 
     //获取配置信息

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

@@ -221,7 +221,7 @@ class Order extends Api
             if(empty($user)) throw new Exception(__("赠送用户不存在"));    
             if($user['id'] == $order_info['user_id']) throw new Exception(__("赠送用户不能是自己"));  
 
-            $chabao = $ledgerWalletModel::getWalletTotalChaBao($this->auth->id);
+            $chabao = $ledgerWalletModel::getWalletChaBao($this->auth->id);
             $fees   = $order_info->gift_fee> 0? bcmul($order_info['price'], bcdiv($order_info->gift_fee, 100, 2), 2): 0;   
             if(bccomp($fees, $chabao, 2) > 0) throw new Exception(__("余额不足请前往充值"), 15001);
            

+ 5 - 5
application/api/validate/Market.php

@@ -13,8 +13,8 @@ class Market extends Validate
       'market_id'     => 'require',
       'product_id'    => 'require',
       'transfer_id'   => 'require',
-      'num'        => 'requireIf:type,2|gt:0',
-      'name'       => 'require',
+      'min_price'     => 'require|number|gt:0',
+      'num'           => 'require|number|gt:0',
       'type'       => 'require|in:1,2',
       'phone'      => 'require|mobile',
       'address'    => 'require',
@@ -29,8 +29,8 @@ class Market extends Validate
       'market_id.require'   => '订单ID有误',
       'product_id.require'  => '产品ID有误', //
       'transfer_id.require' => '参数ID有误',
-      'area_id.require'    => '位置ID有误',
-      'num.require'        => '数量有误',
+      'min_price'           => '价格有误',
+      'num'                 => '数量有误',
       'type.require'       => '参数有误',
       'price.gt'           => '转让金额有误',
       'name.require'       => '姓名不能为空',
@@ -45,7 +45,7 @@ class Market extends Validate
         'collect'      => ['market_id'],
         'announcement' => ['product_id'],
         'transferlock' => ['transfer_id'],
-        'out'  => ['order_id'],
+        'buying'       => ['product_id', 'min_price', 'num'],
         'giv'  => ['order_id', 'address'],
     ];
   

+ 2 - 2
application/common/model/LedgerFrozenChangeModel.php

@@ -16,6 +16,7 @@ class LedgerFrozenChangeModel extends Model
     const Payment           = 3; //转让
     const Receive           = 4; //资产转让收款
     const System            = 5; //系统调整
+ 
 
     public static function getStatusList()
     {
@@ -26,8 +27,7 @@ class LedgerFrozenChangeModel extends Model
             self::Freight => __('物流运费'),
             self::Payment  => __('转账支付'),
             self::Receive => __('转账收款'),
-            self::System => __('系统调整'),
-
+            self::System => __('系统调整')
         ];
     }
 

+ 4 - 2
application/common/model/LedgerTokenChangeModel.php

@@ -25,7 +25,8 @@ class LedgerTokenChangeModel extends Model
      const Community         = 12; //社区津贴
      const Service           = 13; //服务津贴
      const Together          = 14; //共创津贴
-     const Super             = 15;
+     const Super             = 15; //茶宝标记激活
+     const BuySellg          = 16; //求购出售
    
      /*
       * 支付状态
@@ -69,7 +70,8 @@ class LedgerTokenChangeModel extends Model
             self::Community => __('社区津贴'),
             self::Service => __('服务津贴'),
             self::Together => __('共创津贴'),
-            self::Super => __('茶宝标记激活')
+            self::Super => __('茶宝标记激活'),
+            self::BuySellg => __('求购出售')
            ];
     }
    

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

@@ -120,7 +120,7 @@ class LedgerWalletModel extends Model
      * @return string 变动后的金额
      * @throws Exception
      */
-    public function changeWalletOnly(int $uid, string $asset, string $amount ): string
+    public function changeWalletOnly(int $uid, string $asset, string $amount): string
     {
         $available = $this->getWalletTX($uid);
         if (empty($available)) {
@@ -267,113 +267,6 @@ class LedgerWalletModel extends Model
 
  
 
-    /**
-     * 发放见点奖
-     * @param int $uid 服务器算力的用户ID
-     * @param string $power 该租赁得到的算力
-     * @return void
-     * @throws Exception
-     */
-    public function sendRegBonus(int $uid)
-    {
-        $parentIDs = (new UserPathModel())->getAllParentIDs($uid);
-        if (count($parentIDs) == 0) {
-            return;
-        }
-        $userList = (new UserModel())
-            ->field('id,team_level_id')
-            ->where('id', 'in', $parentIDs)
-            ->where('team_level_id', '>', 0)
-            ->order('id desc')
-            ->select();
-        if (empty($userList)) {
-            //没有符合条件的上级
-            return;
-        }
-
-        $team_level_config = DB::table('team_level')
-            ->field('level_id, reg_bonus')
-            ->select();
-        $level_reg_bonus   = [];
-        foreach ($team_level_config as $item) {
-            $level_reg_bonus[$item['level_id']] = $item['reg_bonus'];
-        }
-
-        $now_level = 0;
-        foreach ($userList as $item) {
-            if ($item['team_level_id'] > $now_level && isset($level_reg_bonus[$item['team_level_id']])) {
-                (new LedgerWalletModel())->changeWalletAccount($item['id'], Asset::USDT, $level_reg_bonus[$item['team_level_id']], Action::UsdtRegBonus, $uid);
-                $now_level = $item['team_level_id'];
-            }
-        }
-    }
-
-
-    /**
-     * 发放服务器市场推荐相关收益
-     * @param int $uid
-     * @param string $power
-     * @return void
-     * @throws DbException
-     */
-    public function sendMarketBonus(int $uid, array $server_info)
-    {
-        //直推奖
-        $user = (new UserModel())->get($uid);
-        if (empty($user) || $user['parent_id'] == 0) {
-            return;
-        }
-
-        $parent = (new UserModel())->get($user['parent_id']);
-        if (empty($parent) || $parent['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
-            return;
-        }
-
-        $this->changeWalletAccount($user['parent_id'], Asset::USDT, $server_info['referral_bonus'], Action::ServerReferralBonus, $uid);
-
-        //间推荐奖,向上级的上级发放佣金
-        if ($parent['parent_id'] == 0) {
-            return;
-        }
-        $indirect = (new UserModel())->get($parent['parent_id']);
-        if (empty($indirect) || $indirect['effective_time'] == 0) {//必须参加过算力租赁才能领取该收益
-            return;
-        }
-
-        $this->changeWalletAccount($parent['parent_id'], Asset::USDT, $server_info['indirect_bonus'], Action::ServerIndirectBonus, $uid);
-
-        //社长费用
-        $user_parent_ids = (new UserPathModel())->where('user_id', $uid)->column('parent_id');
-        $parent_users    = (new UserModel())
-            ->field('id,parent_id')
-            ->where('id', 'in', $user_parent_ids)
-            ->where('effective_time', '>', 0)//必须参加过算力租赁才能领取该收益
-     
-            ->order('id desc')
-            ->select();
-        if (empty($parent_users)) {
-            return;
-        }
-
-        //发放社区长费用
-        $info = $parent_users[0];//取第一个
-        $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_bonus'], Action::ServerCommunityBonus, $uid);
-
-        //发放社区长推荐奖
-        if ($info['parent_id'] > 0) {
-            $community_info = (new UserModel())->get($info['parent_id']);
-            if ($community_info['effective_time'] > 0) {
-                $this->changeWalletAccount($info['id'], Asset::USDT, $server_info['community_referral_bonus'], Action::ServerCommunityReferralBonus, $uid);
-            }
-        }
-
-        //系统领导人费用
-        $sys_leader_info_id = 0;//定义待发放收益的系统领导人ID
-     
-        if ($sys_leader_info_id > 0) {
-            $this->changeWalletAccount($sys_leader_info_id, Asset::USDT, $server_info['sys_leader_bonus'], Action::ServerSysLeaderBonus, $uid);
-        }
-    }
 
     public function users()
     {

+ 71 - 3
application/common/model/ProductBuying.php

@@ -9,19 +9,35 @@ class ProductBuying extends Model
 
     protected $name = "product_buying";
 
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+
+    // 定义时间戳字段名
+    protected $createTime = 'create_time';
+    protected $updateTime = 'update_time';
+    protected $deleteTime = false;
+
+    // 追加属性
+    protected $append = [
+        'create_time_text',
+        'update_time_text'
+    ];
+
+
     //1 正常 2 已出售 0 关闭
     const Normal    = 1;
     const SaleOut   = 2;
     const Close     = 0;
 
+
     /*
      * 订单状态
      */
     public $aciton_name = [
         '-1'                => '全部',
-        self::Normal        => '正常',
-        self::SaleOut       => '已出售',
-        self::Close         => '关闭',
+        self::Normal        => '待成交',
+        self::SaleOut       => '已成交',
+        self::Close         => '已取消',
     ];
 
     public static function getProductBuyingMaxPrice(int $productId)
@@ -29,6 +45,58 @@ class ProductBuying extends Model
         return self::where('product_id', $productId)->max('min_price');
     }
 
+    //用户购买套数
+    public static function getProductBuyingCount(int $userId, int $productId)
+    {
+        return self::where('product_id', $productId)->where('user_id', $userId)->where('status', self::Normal)->count();
+    }
+
+    //创建订单
+    public static function setCreateBuying(int $userId, int $productId, int $num, float $minPrice, float $totalPrice)
+    {
 
+        return self::create([
+            'user_id'       => $userId,
+            'product_id'    => $productId,
+            'num'           => $num,
+            'min_price'     => $minPrice,
+            'total_price'   => $totalPrice
+        ]);
+    }
+
+    //获取订单列表
+    public function getBuyingList(int $userId, int $page, int $size, int $status)
+    {
+        $where = [
+            'user_id'   => $userId,
+           'status'    => $status,
+        ];
+        $list = $this->where($where)->page($page, $size)->order('id desc')->select();
+        $count = $this->where($where)->count();
+        return compact('list', 'count');
+    }
+
+    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);
+    }
 
 }

+ 1 - 1
application/common/model/ProductOrder.php

@@ -32,7 +32,7 @@ class ProductOrder extends Model
     const Shipped           = 3;
     const Cancelled         = 4;
     const Closure           = 5;
-    const Freeze            = 6; //冻结
+    const Freeze            = 6; //质押
 
     //类型
     const Popular           = 0; //热销

+ 68 - 0
application/common/model/UserBuying.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace app\common\model;
+
+use EasyWeChat\Kernel\Support\Str;
+use think\Model;
+
+//用户求购出售
+class UserBuying  extends Model
+{
+    protected $name = "user_buying";
+
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'int';
+
+    // 定义时间戳字段名
+    protected $createTime = 'create_time';
+    protected $updateTime = 'update_time';
+    protected $deleteTime = false;
+
+    // 追加属性
+    protected $append = [
+        'create_time_text',
+        'update_time_text'
+    ];
+
+   
+    const DefaultAdders     =0; //默认地址
+    const TakeAdders        =1; //收货地址
+
+    const Waiting           = 0;
+    const Shipped           = 1;
+    const Finish            = 2;
+    /* 
+     *  订单状态  0代发货  1已发货 2完成
+     */
+    public $order_status = [
+        '-1'                    => '全部',
+        self::Waiting           => '待发货',
+        self::Shipped           => '已发货',
+        self::Finish            => '完成'
+    ];
+
+   
+    
+    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);
+    }
+}

+ 1 - 0
application/config.php

@@ -329,5 +329,6 @@ return [
         'min_buy_price'         => 10,  //最小求购价格
         'serve_desc'        => "服务描述",
         'serve_fee'         => 0.045, //服务费
+        'max_buying_count'  => 10, //最大求购套数
     ],
 ];

+ 3 - 3
extend/fast/Asset.php

@@ -20,7 +20,7 @@ class Asset
     const USDT = 'usdt';
 
     /**
-     * 虚拟币
+     * 茶宝
      * @var string
      */
     const TOKEN = 'token';
@@ -38,10 +38,10 @@ class Asset
     const RENTAL_POWER = 'rental_power';
 
     /**
-     * 服务器算力
+     * 求购锁定金额
      * @var string
      */
-    const SERVER_POWER = 'server_power';
+    const BUYING  = 'buying';
 
     /**
      * SMH