|
|
@@ -1,465 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace App\Services;
|
|
|
-use App\Services\BaseService;
|
|
|
-use App\Services\UserService;
|
|
|
-use App\Services\ProductService;
|
|
|
-use App\Models\Order;
|
|
|
-use Illuminate\Support\Str;
|
|
|
-use Illuminate\Support\Facades\DB;
|
|
|
-
|
|
|
-class OrderService extends BaseService
|
|
|
-{
|
|
|
-
|
|
|
- public static $MODEL= Order::class;
|
|
|
-
|
|
|
- const STATUS_YES = 1;
|
|
|
- const STATUS_NOT = 0;
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 获取查询条件
|
|
|
- * @param {array} $search 查询内容
|
|
|
- * @return {array}
|
|
|
- */
|
|
|
- public static function getWhere(array $search = []): array
|
|
|
- {
|
|
|
- $where = [];
|
|
|
- if (isset($search['id']) && $search['id'] !== '') {
|
|
|
- $where[] = ['id', '=', $search['id']];
|
|
|
- }
|
|
|
- if (isset($search['user_id']) && $search['user_id'] !== '') {
|
|
|
- $where[] = ['user_id', '=', $search['user_id']];
|
|
|
- }
|
|
|
- if (isset($search['order_no']) && $search['order_no'] !== '') {
|
|
|
- $where[] = ['order_no', '=', $search['order_no']];
|
|
|
- }
|
|
|
- if (isset($search['status']) && $search['status'] !== '') {
|
|
|
- $where[] = ['status', '=', $search['status']];
|
|
|
- }
|
|
|
- if (isset($search['order_date']) && $search['order_date'] !== '') {
|
|
|
- $where[] = ['order_date', '=', $search['order_date']];
|
|
|
- }
|
|
|
- if (isset($search['paid_date']) && $search['paid_date'] !== '') {
|
|
|
- $where[] = ['paid_date', '=', $search['paid_date']];
|
|
|
- }
|
|
|
- if (isset($search['product_id']) && $search['product_id'] !== '') {
|
|
|
- $where[] = ['product_id', '=', $search['product_id']];
|
|
|
- }
|
|
|
- return $where;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 查询单条数据
|
|
|
- * @param array $search
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static function findOne(array $search)
|
|
|
- {
|
|
|
- return static::model()::where(static::getWhere($search))->first();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 查询所有数据
|
|
|
- * @param array $search
|
|
|
- * @return \Illuminate\Database\Eloquent\Collection
|
|
|
- */
|
|
|
- public static function findAll(array $search = [])
|
|
|
- {
|
|
|
- return static::model()::where(static::getWhere($search))->get();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 分页查询
|
|
|
- * @param array $search
|
|
|
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
|
|
|
- */
|
|
|
- public static function paginate(array $search = [])
|
|
|
- {
|
|
|
-
|
|
|
- $limit = isset($search['limit']) ? $search['limit'] : 15;
|
|
|
- $paginator = static::model()::where(static::getWhere($search))
|
|
|
- ->orderBy('id', 'desc')
|
|
|
- ->paginate($limit);
|
|
|
- return ['total' => $paginator->total(), 'data' => $paginator->items()];
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * @description:
|
|
|
- * @param {*} $params
|
|
|
- * @return {*}
|
|
|
- */
|
|
|
- public static function submit($params = [])
|
|
|
- {
|
|
|
- $result = false;
|
|
|
- $msg['code'] = self::NOT;
|
|
|
- $msg['msg'] = '';
|
|
|
-
|
|
|
- // 2. 判断是否是更新
|
|
|
- if (!empty($params['id'])) {
|
|
|
- // 更新
|
|
|
- $info = self::findOne(['id'=>$params['id']] );
|
|
|
- if (!$info) {
|
|
|
- $msg['msg'] = '数据不存在!';
|
|
|
- }else{
|
|
|
- $result = $info->update($params);
|
|
|
- $id = $params['id'];
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
- // 创建
|
|
|
- $result = $info = self::model()::create($params);
|
|
|
- $id = $result->id;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if($result){
|
|
|
- $msg['code'] = self::YES;
|
|
|
- $msg['msg'] = '设置成功';
|
|
|
- }else{
|
|
|
- $msg['msg'] = empty($msg['msg']) ?'操作失败':$msg['msg'];
|
|
|
- }
|
|
|
-
|
|
|
- return $msg;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 分配订单
|
|
|
- * @param {*} $user_id 用户ID
|
|
|
- * @return {*}
|
|
|
- */
|
|
|
- public static function assignOrder($user_id)
|
|
|
- {
|
|
|
- $result = [];
|
|
|
- $result['code'] = self::NOT;
|
|
|
- $result['data'] = [];
|
|
|
-
|
|
|
- $today = date('Y-m-d');
|
|
|
- // // 查找个人待处理订单
|
|
|
- // $pendingOrder = self::findOne(['user_id' => $user_id,'status' => self::STATUS_NOT]);
|
|
|
-
|
|
|
- // 用户信息
|
|
|
- $userInfo = UserService::findOne(['id' => $user_id]);
|
|
|
- $balance = $userInfo->balance; // 余额
|
|
|
-
|
|
|
- $productPriceMax = config_val('product_price_max'); // 商品最高价
|
|
|
-
|
|
|
- $maxOrderQuantity = $userInfo->membership->quantity; // 用户每日可完单限制
|
|
|
- if($maxOrderQuantity > 0){
|
|
|
- // 今日完单数
|
|
|
- $todayCompleteQuantity = static::model()::where(self::getWhere(['user_id' => $user_id ,'status' => self::STATUS_YES]))->count();
|
|
|
- if($todayCompleteQuantity >= $maxOrderQuantity){
|
|
|
- if($userInfo->membership_level_code =='silver'){
|
|
|
- $result['msg'] = '恭喜您,今日订单已完成!可在升级黄金会员后继续新一轮订单';
|
|
|
- }else{
|
|
|
- $result['msg'] = '恭喜您,今日订单已完成!可在升级钻石会员后继续新一轮订单';
|
|
|
- }
|
|
|
- return $result;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $minPrice = 0; // 最低消费
|
|
|
-
|
|
|
- // 已完成数量
|
|
|
- $completeQuantity = static::model()::where(self::getWhere(['user_id' => $user_id ,'status' => self::STATUS_YES]))->count();
|
|
|
- // 达到卡单数
|
|
|
- if(($completeQuantity + 1) == $userInfo->blocked_order){
|
|
|
- $minPrice = bcadd($balance, $userInfo->blocked_amount, 4); // 计算最低消息
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- $product_commission_min = config_val('product_commission_min'); // 商品佣金最低
|
|
|
- $product_commission_max = config_val('product_commission_max'); // 商品佣金最高
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // 已下单的商品id集合
|
|
|
- // $productIds = static::model()::where(self::getWhere(['user_id' => $user_id]))->pluck('product_id');
|
|
|
- $productIds = [];
|
|
|
-
|
|
|
- // 随机的商品信息
|
|
|
- $randomProduct = ProductService::getRandom($productIds,$balance,$minPrice);
|
|
|
- if(empty($randomProduct)){
|
|
|
- $result['msg'] = '暂无适合的商品,请稍后再试';
|
|
|
- return $result;
|
|
|
- }
|
|
|
-
|
|
|
- $productPrice = $randomProduct->price; // 商品价格
|
|
|
-
|
|
|
- if($minPrice > 0){
|
|
|
- $price = $minPrice;
|
|
|
- }else{
|
|
|
- if($balance < $productPrice){
|
|
|
- $price = bcadd($productPrice,rand(10,50),2);
|
|
|
- }else{
|
|
|
- $priceMin = bcmul($balance,0.3,2); // 最低价
|
|
|
- if($balance > 10000){
|
|
|
- $priceMin = 1000;
|
|
|
- }
|
|
|
- if($priceMin < $productPrice){
|
|
|
- $priceMin = $productPrice;
|
|
|
- }
|
|
|
- $priceMax = bcmul($balance,0.7,2); // 最高价
|
|
|
- if($priceMax < $productPrice){
|
|
|
- $priceMax = $productPrice;
|
|
|
- }
|
|
|
- if($priceMax > $productPriceMax){
|
|
|
- $priceMax = $productPriceMax;
|
|
|
- }
|
|
|
- $price = random_float($priceMin,$priceMax,4);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- $price = retain_number_format($price);
|
|
|
-
|
|
|
- $commission = bcmul($price,random_float($product_commission_min,$product_commission_max,4),4);
|
|
|
-
|
|
|
- if($minPrice > 0){
|
|
|
- $commission = $userInfo->blocked_profit; // 卡單默認佣金
|
|
|
- }
|
|
|
- $commission = retain_number_format($commission);
|
|
|
-
|
|
|
- $data = [];
|
|
|
- $data['order_no'] = self::generalOrderNo($user_id); // 订单号
|
|
|
- $data['user_id'] = $user_id; // 用户
|
|
|
- $data['product_id'] = $randomProduct->id; // 商品ID
|
|
|
- $data['price'] = $price; // 商品价格
|
|
|
- $data['commission'] = $commission; // 商品佣金
|
|
|
- $data['product_snapshot'] = json_encode($randomProduct,JSON_UNESCAPED_UNICODE); // 商品快照
|
|
|
- $data['status'] = self::STATUS_NOT; // 订单待处理
|
|
|
- $data['order_date'] = date('Y-m-d'); // 订单日期
|
|
|
- // $data['paid_date'] = date('Y-m-d'); // 完单日期
|
|
|
-
|
|
|
- $order = static::model()::create($data);
|
|
|
-
|
|
|
- if($order){
|
|
|
- $result['code'] = self::YES;
|
|
|
- $result['msg'] = '订单已生成,请尽快支付';
|
|
|
- $result['data'] = $order;
|
|
|
- }else{
|
|
|
- $result['msg'] = '分配订单失败,请稍后再试';
|
|
|
- }
|
|
|
- return $result;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // 生成随机的订单号
|
|
|
- public static function generalOrderNo($user_id = 0)
|
|
|
- {
|
|
|
- $orderNo = 'ORD' . now()->format('YmdHis') . Str::random(6).'_'.$user_id;
|
|
|
- return $orderNo;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 订单支付
|
|
|
- * @param {*} $orderNo 订单号
|
|
|
- * @param {*} $userId 用户ID
|
|
|
- * @return {*}
|
|
|
- */
|
|
|
- public static function paymentOrder($orderNo,$userId)
|
|
|
- {
|
|
|
- $result = [];
|
|
|
- $result['code'] = self::NOT;
|
|
|
- $orderInfo = self::findOne(['order_no' => $orderNo ,'user_id' => $userId]);
|
|
|
- if(!$orderInfo){
|
|
|
- $result['msg'] = '未找到相关订单数据';
|
|
|
- return $result;
|
|
|
- }
|
|
|
-
|
|
|
- if($orderInfo->status == self::STATUS_YES){
|
|
|
- $result['msg'] = '订单已支付,请勿重复提交';
|
|
|
- return $result;
|
|
|
- }
|
|
|
-
|
|
|
- $userInfo = UserService::findOne(['id' => $userId]);
|
|
|
-
|
|
|
- $balance = $userInfo->balance; // 余额
|
|
|
- $chaMoney = bcsub($orderInfo->price,$balance,2);
|
|
|
- if($balance < $orderInfo->price){
|
|
|
- $result['msg'] = lang('当前账户余额不足,完成此订单还差{money},请先完成充值');
|
|
|
- $result['msg'] = str_replace('{money}',$chaMoney,$result['msg']);
|
|
|
- $result['money'] = $chaMoney;
|
|
|
- return $result;
|
|
|
- }
|
|
|
- // $afterBalance = bcsub($balance,$orderInfo->price,4);
|
|
|
- if(bcsub($balance,$orderInfo->price,4) < 0){
|
|
|
- $result['msg'] = lang('当前账户余额不足,完成此订单还差{money},请先完成充值');
|
|
|
- $result['msg'] = str_replace('{money}',$chaMoney,$result['msg']);
|
|
|
- $result['money'] = $chaMoney;
|
|
|
- return $result;
|
|
|
- }
|
|
|
-
|
|
|
- // 能支付成功直接返佣
|
|
|
- $afterBalance = bcadd($balance,$orderInfo->commission,4);
|
|
|
-
|
|
|
- DB::beginTransaction();
|
|
|
- try {
|
|
|
- // 更新用户余额
|
|
|
- $userInfo->balance = $afterBalance;
|
|
|
- $userInfo->save();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- $orderInfo->status = self::STATUS_YES;
|
|
|
- $orderInfo->paid_date = date('Y-m-d');
|
|
|
- $orderInfo->save();
|
|
|
-
|
|
|
- // 支付
|
|
|
- $log = UserBalanceLogService::addLog(
|
|
|
- $userId,
|
|
|
- '佣金',
|
|
|
- $balance,
|
|
|
- $orderInfo->commission,
|
|
|
- '订单:'.$orderNo
|
|
|
- );
|
|
|
-
|
|
|
- // 60单发奖励
|
|
|
- if(static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES]))->count() == 60){
|
|
|
- $profit = rand(config_val('bonus_min'),config_val('bonus_max'));
|
|
|
- // 一级返
|
|
|
- if($userInfo->inviter_id_1){
|
|
|
- self::inviterBonus($userInfo->inviter_id_1,$profit,$orderNo);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if($log['code'] == self::NOT){
|
|
|
- DB::rollBack();
|
|
|
- return $log;
|
|
|
- }
|
|
|
-
|
|
|
- // // 一级返
|
|
|
- // if($userInfo->inviter_id_1){
|
|
|
- // $percentage = config_val('inviter_commission_1');
|
|
|
- // self::inviterCommission($userInfo->inviter_id_1,$orderInfo->commission,$percentage,$orderNo);
|
|
|
- // }
|
|
|
- // // 二级返
|
|
|
- // if($userInfo->inviter_id_2){
|
|
|
- // $percentage = config_val('inviter_commission_2');
|
|
|
- // self::inviterCommission($userInfo->inviter_id_2,$orderInfo->commission,$percentage,$orderNo);
|
|
|
- // }
|
|
|
- // // 三级返
|
|
|
- // if($userInfo->inviter_id_3){
|
|
|
- // $percentage = config_val('inviter_commission_3');
|
|
|
- // self::inviterCommission($userInfo->inviter_id_3,$orderInfo->commission,$percentage,$orderNo);
|
|
|
- // }
|
|
|
-
|
|
|
- DB::commit();
|
|
|
-
|
|
|
- $msg['code'] = self::YES;
|
|
|
- $msg['msg'] = '刷单成功';
|
|
|
- return $msg;
|
|
|
-
|
|
|
- } catch (\Exception $e) {
|
|
|
- DB::rollBack();
|
|
|
-
|
|
|
- $msg['msg'] = $e->getMessage();
|
|
|
- return $msg;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 分佣
|
|
|
- * @param {*} $user_id 用户ID
|
|
|
- * @param {*} $amount 分佣金额
|
|
|
- * @param {*} $percentage 比例
|
|
|
- * @return {*}
|
|
|
- */
|
|
|
- public static function inviterCommission($userId, $amount, $percentage, $orderNo)
|
|
|
- {
|
|
|
- $userInfo = UserService::findOne(['id' => $userId]);
|
|
|
- if($userInfo){
|
|
|
- $balance = $userInfo->balance;
|
|
|
-
|
|
|
- // 使用高精度乘法,设置合适的精度(比如4位小数)
|
|
|
- $changeAmount = bcmul($amount, $percentage, 8); // 中间计算使用较高精度
|
|
|
-
|
|
|
- // 如果需要最终结果保留4位小数
|
|
|
- $changeAmount = bcadd($changeAmount, '0', 4);
|
|
|
-
|
|
|
- // 支付
|
|
|
- UserBalanceLogService::addLog(
|
|
|
- $userId,
|
|
|
- '分佣',
|
|
|
- $balance,
|
|
|
- $changeAmount,
|
|
|
- '订单:'.$orderNo
|
|
|
- );
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description:
|
|
|
- * @param {*} $user_id 用户ID
|
|
|
- * @param {*} $profit 奖金
|
|
|
- * @param {*} $orderNo 订单
|
|
|
- * @return {*}
|
|
|
- */
|
|
|
- public static function inviterBonus($userId, $profit, $orderNo)
|
|
|
- {
|
|
|
- $userInfo = UserService::findOne(['id' => $userId]);
|
|
|
- if($userInfo){
|
|
|
- $balance = $userInfo->balance;
|
|
|
-
|
|
|
- $changeAmount = $profit;
|
|
|
-
|
|
|
- // 支付
|
|
|
- UserBalanceLogService::addLog(
|
|
|
- $userId,
|
|
|
- '代理奖金',
|
|
|
- $balance,
|
|
|
- $changeAmount,
|
|
|
- '订单:'.$orderNo
|
|
|
- );
|
|
|
- $userInfo->balance = bcadd($balance,$profit,2);
|
|
|
- $userInfo->save();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * @description: 统计订单数据
|
|
|
- * @param {*} $user_id
|
|
|
- * @return {*}
|
|
|
- */
|
|
|
- public static function statOrder($userId)
|
|
|
- {
|
|
|
- $result = [];
|
|
|
- $userInfo = UserService::findOne(['id' => $userId]);
|
|
|
- $result['balance'] = $userInfo->balance??0;
|
|
|
- $result['membership_name'] = $userInfo->membership_name??'';
|
|
|
- $result['membership_level_code'] = $userInfo->membership_level_code??'';
|
|
|
- // $result['userInfo'] = $userInfo;
|
|
|
-
|
|
|
- $today = date('Y-m-d');
|
|
|
- $yesterday = date('Y-m-d',(time() - 86400));
|
|
|
-
|
|
|
- // 总收益
|
|
|
- $result['total_profit'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES]))->sum('commission');
|
|
|
-
|
|
|
- // 今日收益
|
|
|
- $result['today_profit'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES ,'order_date' => $today]))->sum('commission');
|
|
|
-
|
|
|
- // 昨日收益
|
|
|
- $result['yesterday_profit'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES ,'order_date' => $yesterday]))->sum('commission');
|
|
|
-
|
|
|
- // 总单数
|
|
|
- $result['total_count'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES]))->count();
|
|
|
-
|
|
|
- // 未完成
|
|
|
- $result['total_pending_count'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_NOT]))->count();
|
|
|
-
|
|
|
- // 今日单数
|
|
|
- $result['today_count'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES ,'order_date' => $today]))->count();
|
|
|
-
|
|
|
- // 昨日单数
|
|
|
- $result['yesterday_count'] = static::model()::where(self::getWhere(['user_id' => $userId ,'status' => self::STATUS_YES ,'order_date' => $yesterday]))->count();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return $result;
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
-}
|