From 20d899b7578c631777f273fa7135c4ee1da2002b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Fri, 12 Mar 2021 11:26:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/command/UserBalance.php | 4 +- app/data/controller/UserBalance.php | 5 +- app/data/controller/api/auth/Balance.php | 7 ++- app/data/controller/api/auth/Center.php | 17 ++---- app/data/service/OrderService.php | 55 +++++++++++++++++ app/data/service/UpgradeService.php | 59 +++++++++++++++++++ app/data/service/UserService.php | 21 ------- .../service/payment/BalancePyamentService.php | 8 +-- 8 files changed, 132 insertions(+), 44 deletions(-) diff --git a/app/data/command/UserBalance.php b/app/data/command/UserBalance.php index a041596fc..8e3f01aa6 100644 --- a/app/data/command/UserBalance.php +++ b/app/data/command/UserBalance.php @@ -2,7 +2,7 @@ namespace app\data\command; -use app\data\service\UserService; +use app\data\service\UpgradeService; use think\admin\Command; use think\admin\Exception; use think\console\Input; @@ -33,7 +33,7 @@ class UserBalance extends Command [$total, $count] = [$this->app->db->name('DataUser')->count(), 0]; foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) { $this->queue->message($total, ++$count, "正在计算用户 [{$user['id']}] 的余额"); - UserService::instance()->balance($user['id']); + UpgradeService::instance()->balance($user['id']); $this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的余额", 1); } } catch (\Exception $exception) { diff --git a/app/data/controller/UserBalance.php b/app/data/controller/UserBalance.php index 71fbf4252..0489ab8fb 100644 --- a/app/data/controller/UserBalance.php +++ b/app/data/controller/UserBalance.php @@ -2,6 +2,7 @@ namespace app\data\controller; +use app\data\service\UpgradeService; use app\data\service\UserService; use think\admin\Controller; use think\admin\extend\CodeExtend; @@ -90,7 +91,7 @@ class UserBalance extends Controller protected function _form_result(bool $state, array $data) { if ($state && isset($data['uid'])) { - UserService::instance()->balance($data['uid']); + UpgradeService::instance()->balance($data['uid']); } } @@ -115,7 +116,7 @@ class UserBalance extends Controller $ids = str2arr(input('id', '')); $query = $this->app->db->name($this->table); foreach ($query->whereIn('id', $ids)->cursor() as $vo) { - UserService::instance()->balance($vo['uid']); + UpgradeService::instance()->balance($vo['uid']); } } } diff --git a/app/data/controller/api/auth/Balance.php b/app/data/controller/api/auth/Balance.php index 8b563af54..84a6c2ed9 100644 --- a/app/data/controller/api/auth/Balance.php +++ b/app/data/controller/api/auth/Balance.php @@ -3,6 +3,7 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; +use app\data\service\UpgradeService; use app\data\service\UserService; use think\admin\extend\CodeExtend; @@ -60,12 +61,12 @@ class Balance extends Auth $user = $this->app->db->name('DataUser')->where($map)->find(); if (empty($user)) $this->error('目标用户不存在!'); // 检测余额否有足够 - [$total, $count] = UserService::instance()->balance($this->uuid); + [$total, $count] = UpgradeService::instance()->balance($this->uuid); if ($data['amount'] > $total - $count) $this->error('可转账余额不足!'); // 写入余额转账记录 if ($this->app->db->name($this->table)->insert($data) !== false) { - UserService::instance()->balance($data['uid']); - UserService::instance()->balance($data['from']); + UpgradeService::instance()->balance($data['uid']); + UpgradeService::instance()->balance($data['from']); $this->success('余额转账成功!'); } else { $this->error('余额转账失败!'); diff --git a/app/data/controller/api/auth/Center.php b/app/data/controller/api/auth/Center.php index b002f43fc..be4dcb5b7 100644 --- a/app/data/controller/api/auth/Center.php +++ b/app/data/controller/api/auth/Center.php @@ -3,6 +3,7 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; +use app\data\service\UpgradeService; use app\data\service\UserService; use think\admin\Storage; use think\exception\HttpResponseException; @@ -134,19 +135,11 @@ class Center extends Auth public function bindFrom() { $data = $this->_vali(['from.require' => '邀请人不能为空']); - if ($data['from'] == $this->uuid) { - $this->error('邀请人不能是自己', UserService::instance()->total($this->uuid)); - } - $fromer = $this->app->db->name($this->table)->where(['id' => $data['from']])->find(); - if (empty($fromer)) $this->error('邀请人状态异常', UserService::instance()->get($this->type, $this->uuid)); - if ($this->user['pid1'] > 0) $this->error('已绑定了邀请人', UserService::instance()->total($this->uuid)); - if (is_numeric(stripos($fromer['path'], "-{$this->uuid}-"))) $this->error('不能绑定下属'); - $data['path'] = rtrim($fromer['path'] ?: '-', '-') . "-{$fromer['id']}-"; - [$data['pid2'], $data['layer']] = [$fromer['pid1'] ?? 0, substr_count($data['path'], '-')]; - if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) { - $this->success('绑定邀请人成功', UserService::instance()->total($this->uuid)); + [$state, $message] = UpgradeService::instance()->bindAgent($this->uuid, $data['from']); + if ($state) { + $this->success($message, UserService::instance()->total($this->uuid)); } else { - $this->error('绑定邀请人失败', UserService::instance()->total($this->uuid)); + $this->error($message, UserService::instance()->total($this->uuid)); } } } \ No newline at end of file diff --git a/app/data/service/OrderService.php b/app/data/service/OrderService.php index bf57ff0c0..137c2b2ce 100644 --- a/app/data/service/OrderService.php +++ b/app/data/service/OrderService.php @@ -36,6 +36,61 @@ class OrderService extends Service return true; } + /** + * 刷新用户入会礼包 + * @param integer $uid + * @return integer + * @throws \think\db\exception\DbException + */ + public function syncUserVipEntry(int $uid): int + { + // 检查是否购买入会礼包 + $query = $this->app->db->table('shop_order a')->join('shop_order_item b', 'a.order_no=b.order_no'); + $count = $query->where("a.uid={$uid} and a.status>=4 and a.payment_status=1 and b.vip_entry>0")->count(); + $buyVipEntry = $count > 0 ? 1 : 0; + // 查询用户最后支付时间 + $buyLastMap = [['uid', '=', $uid], ['status', '>=', 4], ['payment_status', '=', 1]]; + $buyLastDate = $this->app->db->name('ShopOrder')->where($buyLastMap)->max('payment_datetime'); + // 更新用户支付信息 + $this->app->db->name('DataUser')->where(['id' => $uid])->update([ + 'buy_vip_entry' => $buyVipEntry, 'buy_last_date' => $buyLastDate, + ]); + return $buyVipEntry; + } + + /** + * @param string $order_no + * @return array|null + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function syncUserLevel(string $order_no): ?array + { + // 查询数据 + $order = $this->app->db->name('ShopOrder')->where("order_no='{$order_no}' and status>=4")->find(); + if (empty($order)) return null; + $user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find(); + if (empty($user)) return null; + // 更新用户购买资格 + $entry = $this->syncUserVipEntry($order['uid']); + // 尝试绑定代理用户 + if (empty($user['pid1']) && ($order['puid1'] > 0 || $user['pid1'] > 0)) { + $puid1 = $order['puid1'] > 0 ? $order['puid1'] : $user['bid']; + UpgradeService::instance()->bindAgent($user['id'], $puid1); + } + // 重置用户信息并绑定订单 + $user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find(); + if ($user['pid1'] > 0) { + $this->app->db->name('ShopOrder')->where(['order_no' => $order_no])->update([ + 'puid1' => $user['pid1'], 'puid2' => $user['pid2'], + ]); + } + // 重新计算用户等级 + UpgradeService::instance()->syncLevel($user['id']); + return [$order, $user, $entry]; + } + /** * 绑定订单详情数据 * @param array $data diff --git a/app/data/service/UpgradeService.php b/app/data/service/UpgradeService.php index b5f1d103b..a5a62523c 100644 --- a/app/data/service/UpgradeService.php +++ b/app/data/service/UpgradeService.php @@ -11,6 +11,65 @@ use think\admin\Service; */ class UpgradeService extends Service { + + /** + * 同步刷新用户余额 + * @param int $uuid 用户UID + * @param array $nots 排除的订单 + * @return array [total,count] + * @throws \think\db\exception\DbException + */ + public function balance(int $uuid, array $nots = []): array + { + $total = $this->app->db->name('DataUserBalance')->where(['uid' => $uuid, 'deleted' => 0])->sum('amount'); + $total += $this->app->db->name('DataUserBalanceTransfer')->where(['uid' => $uuid, 'deleted' => 0])->sum('amount'); + $count = $this->app->db->name('DataUserBalanceTransfer')->where(['from' => $uuid, 'deleted' => 0])->sum('amount'); + if (empty($nots)) { + $count += $this->app->db->name('ShopOrder')->whereRaw("uid={$uuid} and status>1")->sum('amount_balance'); + $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]); + } else { + $count += $this->app->db->name('ShopOrder')->whereRaw("uid={$uuid} and status>1")->whereNotIn('order_no', $nots)->sum('amount_balance'); + } + return [$total, $count]; + } + + /** + * 尝试绑定上级代理 + * @param integer $uid 用户UID + * @param integer $pid 代理UID + * @param boolean $force 正式绑定 + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function bindAgent(int $uid, int $pid = 0, bool $force = true): array + { + $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find(); + if (empty($user)) return [0, '用户查询失败']; + if (!empty($user['pid1'])) return [0, '用户已绑定上级']; + + if (empty($pid)) $pid = $user['pid0']; + if (empty($pid)) return [0, '绑定用户不存在']; + if (intval($uid) === intval($pid)) return [0, '推荐人不能是自己']; + + $parant = $this->app->db->name('DataUser')->where(['id' => $pid])->find(); + if (empty($parant['pids']) || empty($parant['vip_number'])) return [0, '推荐人无推荐资格']; + + if (is_numeric(stripos($parant['path'], "-{$uid}-"))) return [0, '不能绑定下属']; + $data = ['pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1']]; + $data['path'] = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-"; + $data['layer'] = substr_count($data['path'], '-'); + // 非正式绑定时,不写入 pid1 及 pid2 字段 + if (empty($force)) [$data['pid1'], $data['pid2']] = [0, 0]; + + if ($this->app->db->name('DataUser')->where(['id' => $uid])->update($data) !== false) { + return [1, '绑定代理成功']; + } else { + return [0, '绑定代理失败']; + } + } + /** * 同步计算用户级别 * @param integer $uid 指定用户UID diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index e06747925..7f34cdeef 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -93,27 +93,6 @@ class UserService extends Service return $this->get($type, $uuid); } - /** - * 同步刷新用户余额 - * @param int $uuid 用户UID - * @param array $nots 排除的订单 - * @return array [total,count] - * @throws \think\db\exception\DbException - */ - public function balance(int $uuid, array $nots = []): array - { - $total = $this->app->db->name('DataUserBalance')->where(['uid' => $uuid, 'deleted' => 0])->sum('amount'); - $total += $this->app->db->name('DataUserBalanceTransfer')->where(['uid' => $uuid, 'deleted' => 0])->sum('amount'); - $count = $this->app->db->name('DataUserBalanceTransfer')->where(['from' => $uuid, 'deleted' => 0])->sum('amount'); - if (empty($nots)) { - $count += $this->app->db->name('ShopOrder')->whereRaw("uid={$uuid} and status>1")->sum('amount_balance'); - $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]); - } else { - $count += $this->app->db->name('ShopOrder')->whereRaw("uid={$uuid} and status>1")->whereNotIn('order_no', $nots)->sum('amount_balance'); - } - return [$total, $count]; - } - /** * 检查 TOKEN 是否有效 * @param string $type 接口类型 diff --git a/app/data/service/payment/BalancePyamentService.php b/app/data/service/payment/BalancePyamentService.php index 5bb41fa98..bbf849369 100644 --- a/app/data/service/payment/BalancePyamentService.php +++ b/app/data/service/payment/BalancePyamentService.php @@ -3,9 +3,9 @@ namespace app\data\service\payment; use app\data\service\PaymentService; -use app\data\service\UserService; -use think\admin\extend\CodeExtend; +use app\data\service\UpgradeService; use think\admin\Exception; +use think\admin\extend\CodeExtend; /** * 账号余额支付参数处理 @@ -56,13 +56,13 @@ class BalancePyamentService extends PaymentService // 创建支付行为 $this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount); // 扣减用户余额 - [$total, $used] = UserService::instance()->balance($order['uid'], [$orderNo]); + [$total, $used] = UpgradeService::instance()->balance($order['uid'], [$orderNo]); if ($paymentAmount > $total - $used) throw new Exception("可抵扣余额不足"); $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['amount_balance' => $paymentAmount]); // 更新支付行为 $this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付'); // 刷新用户余额 - UserService::instance()->balance($order['uid']); + UpgradeService::instance()->balance($order['uid']); return ['info' => '余额支付完成']; } } \ No newline at end of file