修改订单处理

This commit is contained in:
邹景立 2021-03-12 11:51:39 +08:00
parent 20d899b757
commit 4530ca11c4
4 changed files with 27 additions and 22 deletions

View File

@ -113,7 +113,7 @@ class Center extends Auth
// 查询邀请的朋友 // 查询邀请的朋友
$query = $this->_query($this->table); $query = $this->_query($this->table);
$query->like('nickname|username#nickname')->equal('pid1,id#uid'); $query->like('nickname|username#nickname')->equal('pid1,id#uid');
$query->field('id,pid1,username,nickname,headimg,amount_total,create_at'); $query->field('id,pid0,pid1,pid2,pids,username,nickname,headimg,amount_total,create_at');
$result = $query->where($map)->order('id desc')->page(true, false, false, 15); $result = $query->where($map)->order('id desc')->page(true, false, false, 15);
// 统计当前用户所有下属数 // 统计当前用户所有下属数
$total = $this->app->db->name($this->table)->where($map)->count(); $total = $this->app->db->name($this->table)->where($map)->count();
@ -135,7 +135,7 @@ class Center extends Auth
public function bindFrom() public function bindFrom()
{ {
$data = $this->_vali(['from.require' => '邀请人不能为空']); $data = $this->_vali(['from.require' => '邀请人不能为空']);
[$state, $message] = UpgradeService::instance()->bindAgent($this->uuid, $data['from']); [$state, $message] = UpgradeService::instance()->bindAgent($this->uuid, $data['from'], false);
if ($state) { if ($state) {
$this->success($message, UserService::instance()->total($this->uuid)); $this->success($message, UserService::instance()->total($this->uuid));
} else { } else {

View File

@ -42,7 +42,7 @@ class OrderService extends Service
* @return integer * @return integer
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
*/ */
public function syncUserVipEntry(int $uid): int private function syncUserEntry(int $uid): int
{ {
// 检查是否购买入会礼包 // 检查是否购买入会礼包
$query = $this->app->db->table('shop_order a')->join('shop_order_item b', 'a.order_no=b.order_no'); $query = $this->app->db->table('shop_order a')->join('shop_order_item b', 'a.order_no=b.order_no');
@ -59,21 +59,24 @@ class OrderService extends Service
} }
/** /**
* 根据订单更新用户等级
* @param string $order_no * @param string $order_no
* @return array|null * @return array|null [USER, ORDER, ENTRY]
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @throws \think\db\exception\ModelNotFoundException
*/ */
public function syncUserLevel(string $order_no): ?array public function syncUserLevel(string $order_no): ?array
{ {
// 查询数据 // 目标订单数据
$order = $this->app->db->name('ShopOrder')->where("order_no='{$order_no}' and status>=4")->find(); $map = [['order_no', '=', $order_no], ['status', '>=', 4]];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) return null; if (empty($order)) return null;
// 订单用户数据
$user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find(); $user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find();
if (empty($user)) return null; if (empty($user)) return null;
// 更新用户购买资格 // 更新用户购买资格
$entry = $this->syncUserVipEntry($order['uid']); $entry = $this->syncUserEntry($order['uid']);
// 尝试绑定代理用户 // 尝试绑定代理用户
if (empty($user['pid1']) && ($order['puid1'] > 0 || $user['pid1'] > 0)) { if (empty($user['pid1']) && ($order['puid1'] > 0 || $user['pid1'] > 0)) {
$puid1 = $order['puid1'] > 0 ? $order['puid1'] : $user['bid']; $puid1 = $order['puid1'] > 0 ? $order['puid1'] : $user['bid'];
@ -88,7 +91,7 @@ class OrderService extends Service
} }
// 重新计算用户等级 // 重新计算用户等级
UpgradeService::instance()->syncLevel($user['id']); UpgradeService::instance()->syncLevel($user['id']);
return [$order, $user, $entry]; return [$user, $order, $entry];
} }
/** /**

View File

@ -47,22 +47,21 @@ class UpgradeService extends Service
{ {
$user = $this->app->db->name('DataUser')->where(['id' => $uid])->find(); $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) return [0, '用户查询失败']; if (empty($user)) return [0, '用户查询失败'];
if (!empty($user['pid1'])) return [0, '用户已绑定上级']; if (!empty($user['pids'])) return [0, '已绑定推荐人'];
// 检查代理用户
if (empty($pid)) $pid = $user['pid0']; if (empty($pid)) $pid = $user['pid0'];
if (empty($pid)) return [0, '绑定用户不存在']; if (empty($pid)) return [0, '绑定推荐人不存在'];
if (intval($uid) === intval($pid)) return [0, '推荐人不能是自己']; if ($uid == $pid) return [0, '推荐人不能是自己'];
$parant = $this->app->db->name('DataUser')->where(['id' => $pid])->find(); $parant = $this->app->db->name('DataUser')->where(['id' => $pid])->find();
if (empty($parant['pids']) || empty($parant['vip_number'])) return [0, '推荐人无推荐资格']; if (empty($parant['pids']) || empty($parant['vip_number'])) return [0, '推荐人无推荐资格'];
if (stripos($parant['path'], "-{$uid}-") !== false) return [0, '不能绑定下属'];
if (is_numeric(stripos($parant['path'], "-{$uid}-"))) return [0, '不能绑定下属']; // 组装代理数据
$data = ['pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1']]; $path = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-";
$data['path'] = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-"; $data = [
$data['layer'] = substr_count($data['path'], '-'); 'pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1'],
// 非正式绑定时,不写入 pid1 及 pid2 字段 'pids' => $force ? 1 : 0, 'path' => $path, 'layer' => substr_count($path, '-'),
if (empty($force)) [$data['pid1'], $data['pid2']] = [0, 0]; ];
// 更新用户代理
if ($this->app->db->name('DataUser')->where(['id' => $uid])->update($data) !== false) { if ($this->app->db->name('DataUser')->where(['id' => $uid])->update($data) !== false) {
return [1, '绑定代理成功']; return [1, '绑定代理成功'];
} else { } else {

View File

@ -4,6 +4,8 @@ use app\data\command\OrderClear;
use app\data\command\UserBalance; use app\data\command\UserBalance;
use app\data\command\UserUpgrade; use app\data\command\UserUpgrade;
use app\data\command\UserTransfer; use app\data\command\UserTransfer;
use app\data\service\OrderService;
use app\data\service\RebateCurrentService;
use think\Console; use think\Console;
if (app()->request->isCli()) { if (app()->request->isCli()) {
@ -17,7 +19,8 @@ if (app()->request->isCli()) {
// 注册订单支付处理事件 // 注册订单支付处理事件
app()->event->listen('ShopOrderPayment', function ($orderNo) { app()->event->listen('ShopOrderPayment', function ($orderNo) {
app()->log->notice("订单支付事件,订单号:{$orderNo}"); app()->log->notice("订单支付事件,订单号:{$orderNo}");
\app\data\service\RebateCurrentService::instance()->execute($orderNo); OrderService::instance()->syncUserLevel($orderNo);
RebateCurrentService::instance()->execute($orderNo);
}); });
} }