This commit is contained in:
邹景立 2021-03-12 11:26:41 +08:00
parent edf5b84622
commit 20d899b757
8 changed files with 132 additions and 44 deletions

View File

@ -2,7 +2,7 @@
namespace app\data\command; namespace app\data\command;
use app\data\service\UserService; use app\data\service\UpgradeService;
use think\admin\Command; use think\admin\Command;
use think\admin\Exception; use think\admin\Exception;
use think\console\Input; use think\console\Input;
@ -33,7 +33,7 @@ class UserBalance extends Command
[$total, $count] = [$this->app->db->name('DataUser')->count(), 0]; [$total, $count] = [$this->app->db->name('DataUser')->count(), 0];
foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) { foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) {
$this->queue->message($total, ++$count, "正在计算用户 [{$user['id']}] 的余额"); $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); $this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的余额", 1);
} }
} catch (\Exception $exception) { } catch (\Exception $exception) {

View File

@ -2,6 +2,7 @@
namespace app\data\controller; namespace app\data\controller;
use app\data\service\UpgradeService;
use app\data\service\UserService; use app\data\service\UserService;
use think\admin\Controller; use think\admin\Controller;
use think\admin\extend\CodeExtend; use think\admin\extend\CodeExtend;
@ -90,7 +91,7 @@ class UserBalance extends Controller
protected function _form_result(bool $state, array $data) protected function _form_result(bool $state, array $data)
{ {
if ($state && isset($data['uid'])) { 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', '')); $ids = str2arr(input('id', ''));
$query = $this->app->db->name($this->table); $query = $this->app->db->name($this->table);
foreach ($query->whereIn('id', $ids)->cursor() as $vo) { foreach ($query->whereIn('id', $ids)->cursor() as $vo) {
UserService::instance()->balance($vo['uid']); UpgradeService::instance()->balance($vo['uid']);
} }
} }
} }

View File

@ -3,6 +3,7 @@
namespace app\data\controller\api\auth; namespace app\data\controller\api\auth;
use app\data\controller\api\Auth; use app\data\controller\api\Auth;
use app\data\service\UpgradeService;
use app\data\service\UserService; use app\data\service\UserService;
use think\admin\extend\CodeExtend; use think\admin\extend\CodeExtend;
@ -60,12 +61,12 @@ class Balance extends Auth
$user = $this->app->db->name('DataUser')->where($map)->find(); $user = $this->app->db->name('DataUser')->where($map)->find();
if (empty($user)) $this->error('目标用户不存在!'); 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 ($data['amount'] > $total - $count) $this->error('可转账余额不足!');
// 写入余额转账记录 // 写入余额转账记录
if ($this->app->db->name($this->table)->insert($data) !== false) { if ($this->app->db->name($this->table)->insert($data) !== false) {
UserService::instance()->balance($data['uid']); UpgradeService::instance()->balance($data['uid']);
UserService::instance()->balance($data['from']); UpgradeService::instance()->balance($data['from']);
$this->success('余额转账成功!'); $this->success('余额转账成功!');
} else { } else {
$this->error('余额转账失败!'); $this->error('余额转账失败!');

View File

@ -3,6 +3,7 @@
namespace app\data\controller\api\auth; namespace app\data\controller\api\auth;
use app\data\controller\api\Auth; use app\data\controller\api\Auth;
use app\data\service\UpgradeService;
use app\data\service\UserService; use app\data\service\UserService;
use think\admin\Storage; use think\admin\Storage;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
@ -134,19 +135,11 @@ class Center extends Auth
public function bindFrom() public function bindFrom()
{ {
$data = $this->_vali(['from.require' => '邀请人不能为空']); $data = $this->_vali(['from.require' => '邀请人不能为空']);
if ($data['from'] == $this->uuid) { [$state, $message] = UpgradeService::instance()->bindAgent($this->uuid, $data['from']);
$this->error('邀请人不能是自己', UserService::instance()->total($this->uuid)); if ($state) {
} $this->success($message, 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));
} else { } else {
$this->error('绑定邀请人失败', UserService::instance()->total($this->uuid)); $this->error($message, UserService::instance()->total($this->uuid));
} }
} }
} }

View File

@ -36,6 +36,61 @@ class OrderService extends Service
return true; 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 * @param array $data

View File

@ -11,6 +11,65 @@ use think\admin\Service;
*/ */
class UpgradeService extends 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 * @param integer $uid 指定用户UID

View File

@ -93,27 +93,6 @@ class UserService extends Service
return $this->get($type, $uuid); 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 是否有效 * 检查 TOKEN 是否有效
* @param string $type 接口类型 * @param string $type 接口类型

View File

@ -3,9 +3,9 @@
namespace app\data\service\payment; namespace app\data\service\payment;
use app\data\service\PaymentService; use app\data\service\PaymentService;
use app\data\service\UserService; use app\data\service\UpgradeService;
use think\admin\extend\CodeExtend;
use think\admin\Exception; use think\admin\Exception;
use think\admin\extend\CodeExtend;
/** /**
* 账号余额支付参数处理 * 账号余额支付参数处理
@ -56,13 +56,13 @@ class BalancePyamentService extends PaymentService
// 创建支付行为 // 创建支付行为
$this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount); $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("可抵扣余额不足"); if ($paymentAmount > $total - $used) throw new Exception("可抵扣余额不足");
$this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['amount_balance' => $paymentAmount]); $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['amount_balance' => $paymentAmount]);
// 更新支付行为 // 更新支付行为
$this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付'); $this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付');
// 刷新用户余额 // 刷新用户余额
UserService::instance()->balance($order['uid']); UpgradeService::instance()->balance($order['uid']);
return ['info' => '余额支付完成']; return ['info' => '余额支付完成'];
} }
} }