修改行为

This commit is contained in:
邹景立 2021-03-17 10:50:47 +08:00
parent b31c14ff7a
commit 7a9b190eb4
29 changed files with 259 additions and 375 deletions

View File

@ -54,7 +54,7 @@ class OrderClear extends Command
'cancel_datetime' => date('Y-m-d H:i:s'),
'cancel_remark' => '30分钟未完成支付已自动取消',
]);
OrderService::instance()->syncStock($item['order_no']);
OrderService::instance()->stock($item['order_no']);
$this->queue->message($total, $count, "完成取消未支付的订单 {$item['order_no']}", 1);
});
} catch (\Exception $exception) {

View File

@ -2,6 +2,7 @@
namespace app\data\command;
use app\data\service\UserBalanceService;
use app\data\service\UserUpgradeService;
use think\admin\Command;
use think\admin\Exception;
@ -33,7 +34,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']}] 的余额");
UserUpgradeService::instance()->syncBalance($user['id']);
UserBalanceService::instance()->amount($user['id']);
$this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的余额", 1);
}
} catch (\Exception $exception) {

View File

@ -33,7 +33,7 @@ class UserUpgrade 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']}] 的等级");
UserUpgradeService::instance()->syncLevel($user['id']);
UserUpgradeService::instance()->upgrade($user['id']);
$this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的等级", 1);
}
} catch (\Exception $exception) {

View File

@ -4,7 +4,7 @@ namespace app\data\controller;
use app\data\service\OrderService;
use app\data\service\PaymentService;
use app\data\service\TruckService;
use app\data\service\ExpressService;
use app\data\service\UserService;
use think\admin\Controller;
use think\exception\HttpResponseException;
@ -146,7 +146,7 @@ class ShopOrder extends Controller
'code.require' => '快递编号不能为空!',
'number.require' => '配送单号不能为空!',
]);
$this->result = TruckService::instance()->query($data['code'], $data['number']);
$this->result = ExpressService::instance()->query($data['code'], $data['number']);
if (empty($this->result['code'])) $this->error($this->result['info']);
$this->fetch('truck_query');
} catch (HttpResponseException $exception) {
@ -186,7 +186,7 @@ class ShopOrder extends Controller
'cancel_datetime' => date('Y-m-d H:i:s'),
]);
if ($result !== false) {
OrderService::instance()->syncStock($order['order_no']);
OrderService::instance()->stock($order['order_no']);
$this->app->event->trigger('ShopOrderCancel', $order['order_no']);
$this->success('取消未支付的订单成功!');
} else {

View File

@ -2,7 +2,7 @@
namespace app\data\controller;
use app\data\service\TruckService;
use app\data\service\ExpressService;
use think\admin\Controller;
use think\admin\service\SystemService;
use think\exception\HttpResponseException;
@ -97,7 +97,7 @@ class ShopTruckCompany extends Controller
public function synchronize()
{
try {
$result = TruckService::instance()->company();
$result = ExpressService::instance()->company();
if (empty($result['code'])) $this->error($result['info']);
foreach ($result['data'] as $vo) SystemService::instance()->save($this->table, [
'code_1' => $vo['code_1'], 'code_2' => $vo['code_2'],

View File

@ -2,7 +2,7 @@
namespace app\data\controller;
use app\data\service\TruckService;
use app\data\service\ExpressService;
use think\admin\Controller;
use think\admin\extend\CodeExtend;
@ -45,7 +45,7 @@ class ShopTruckTemplate extends Controller
{
if ($this->request->isGet()) {
$this->title = '配送区域管理';
$this->citys = TruckService::instance()->region(3, null);
$this->citys = ExpressService::instance()->region(3, null);
$this->fetch('form_region');
} else {
$data = $this->_vali(['nos.default' => '', 'oks.default' => '']);
@ -91,7 +91,7 @@ class ShopTruckTemplate extends Controller
$data['code'] = CodeExtend::uniqidDate(12, 'T');
}
if ($this->request->isGet()) {
$this->citys = TruckService::instance()->region(2, 1);
$this->citys = ExpressService::instance()->region(2, 1);
}
}

View File

@ -2,6 +2,7 @@
namespace app\data\controller;
use app\data\service\UserBalanceService;
use app\data\service\UserUpgradeService;
use app\data\service\UserService;
use think\admin\Controller;
@ -91,7 +92,7 @@ class UserBalance extends Controller
protected function _form_result(bool $state, array $data)
{
if ($state && isset($data['uid'])) {
UserUpgradeService::instance()->syncBalance($data['uid']);
UserBalanceService::instance()->amount($data['uid']);
}
}
@ -116,7 +117,7 @@ class UserBalance extends Controller
$ids = str2arr(input('id', ''));
$query = $this->app->db->name($this->table);
foreach ($query->whereIn('id', $ids)->cursor() as $vo) {
UserUpgradeService::instance()->syncBalance($vo['uid']);
UserBalanceService::instance()->amount($vo['uid']);
}
}
}

View File

@ -2,6 +2,7 @@
namespace app\data\controller;
use app\data\service\UserRebateService;
use app\data\service\UserUpgradeService;
use think\admin\Controller;
@ -31,7 +32,7 @@ class UserRebate extends Controller
{
$this->title = '用户返利管理';
// 统计所有返利
$this->rebate = UserUpgradeService::instance()->syncRebate(0);
$this->rebate = UserRebateService::instance()->amount(0);
// 创建查询对象
$query = $this->_query($this->table)->equal('type')->like('name,order_no');
// 会员条件查询

View File

@ -2,7 +2,7 @@
namespace app\data\controller;
use app\data\service\RebateCurrentService;
use app\data\service\RebateService;
use think\admin\Controller;
/**
@ -41,7 +41,7 @@ class UserUpgrade extends Controller
foreach ($data as &$vo) {
$vo['rebate_rule'] = str2arr($vo['rebate_rule']);
foreach ($vo['rebate_rule'] as &$v) {
$v = RebateCurrentService::instance()->name($v);
$v = RebateService::instance()->name($v);
}
}
}
@ -77,7 +77,7 @@ class UserUpgrade extends Controller
protected function _form_filter(array &$vo)
{
if ($this->request->isGet()) {
$this->prizes = RebateCurrentService::PRIZES;
$this->prizes = RebateService::PRIZES;
$vo['rebate_rule'] = str2arr($vo['rebate_rule'] ?? '');
} else {
$vo['utime'] = time();

View File

@ -3,7 +3,7 @@
namespace app\data\controller\api;
use app\data\service\GoodsService;
use app\data\service\TruckService;
use app\data\service\ExpressService;
use think\admin\Controller;
/**
@ -56,7 +56,7 @@ class Goods extends Controller
*/
public function getRegion()
{
$this->success('获取区域成功', TruckService::instance()->region(3, 1));
$this->success('获取区域成功', ExpressService::instance()->region(3, 1));
}
}

View File

@ -3,10 +3,10 @@
namespace app\data\controller\api\auth;
use app\data\controller\api\Auth;
use app\data\service\ExpressService;
use app\data\service\GoodsService;
use app\data\service\OrderService;
use app\data\service\PaymentService;
use app\data\service\TruckService;
use app\data\service\UserService;
use think\admin\extend\CodeExtend;
use think\exception\HttpResponseException;
@ -214,7 +214,7 @@ class Order extends Auth
// 根据地址计算运费
$map = ['status' => 1, 'deleted' => 0, 'order_no' => $data['order_no']];
$tCode = $this->app->db->name('ShopOrderItem')->where($map)->column('truck_code');
[$amount, , , $remark] = TruckService::instance()->amount($tCode, $addr['province'], $addr['city'], $tCount);
[$amount, , , $remark] = ExpressService::instance()->amount($tCode, $addr['province'], $addr['city'], $tCount);
$this->success('计算运费成功', ['amount' => $amount, 'remark' => $remark]);
}
@ -243,7 +243,7 @@ class Order extends Auth
// 根据地址计算运费
$map = ['status' => 1, 'deleted' => 0, 'order_no' => $data['order_no']];
$tCodes = $this->app->db->name('ShopOrderItem')->where($map)->column('truck_code');
[$amount, $tCount, $tCode, $remark] = TruckService::instance()->amount($tCodes, $addr['province'], $addr['city'], $tCount);
[$amount, $tCount, $tCode, $remark] = ExpressService::instance()->amount($tCodes, $addr['province'], $addr['city'], $tCount);
// 创建订单发货信息
$express = [
'template_code' => $tCode, 'template_count' => $tCount, 'uid' => $this->uuid,
@ -348,7 +348,7 @@ class Order extends Auth
'cancel_remark' => '用户主动取消订单',
'cancel_datetime' => date('Y-m-d H:i:s'),
]);
if ($result !== false && OrderService::instance()->syncStock($order['order_no'])) {
if ($result !== false && OrderService::instance()->stock($order['order_no'])) {
// 触发订单取消事件
$this->app->event->trigger('ShopOrderCancel', $order['order_no']);
// 返回处理成功数据
@ -449,7 +449,7 @@ class Order extends Auth
{
try {
$data = $this->_vali(['code.require' => '快递不能为空', 'number.require' => '单号不能为空']);
$result = TruckService::instance()->query($data['code'], $data['number']);
$result = ExpressService::instance()->query($data['code'], $data['number']);
empty($result['code']) ? $this->error($result['info']) : $this->success('快递追踪信息', $result);
} catch (HttpResponseException $exception) {
throw $exception;

View File

@ -3,7 +3,7 @@
namespace app\data\controller\api\auth;
use app\data\controller\api\Auth;
use app\data\service\UserUpgradeService;
use app\data\service\UserRebateService;
use think\admin\extend\CodeExtend;
/**
@ -48,7 +48,7 @@ class Rebate extends Auth
]);
$params = sysdata('TransferRule') ?: [];
if (empty($params['transfer_state'])) $this->error('提现功能已经关闭');
[$total, $count] = UserUpgradeService::instance()->syncRebate($this->uuid);
[$total, $count] = UserRebateService::instance()->amount($this->uuid);
if ($total - $count - $data['amount'] < 0) $this->error('可提现金额不足');
if ($data['amount'] < $params['transfer_min']) $this->error("提现不能少于{$params['transfer_min']}");
if ($data['amount'] > $params['transfer_max']) $this->error("提现不能大于{$params['transfer_max']}");
@ -61,7 +61,7 @@ class Rebate extends Auth
'remark' => $data['remark'],
]);
if ($result !== false) {
UserUpgradeService::instance()->syncRebate($this->uuid);
UserRebateService::instance()->amount($this->uuid);
$this->success('提交申请成功');
} else {
$this->error('提交申请失败');

View File

@ -7,11 +7,11 @@ use think\admin\Service;
use think\admin\service\InterfaceService;
/**
* 快递运输数据服务
* Class TruckService
* 快递查询数据服务
* Class ExpressService
* @package app\data\service
*/
class TruckService extends Service
class ExpressService extends Service
{
/**
* 模拟计算快递费用

View File

@ -22,42 +22,20 @@ class OrderService extends Service
/**
* 同步订单关联商品的库存
* @param string $order_no 订单编号
* @param string $orderNo 订单编号
* @return boolean
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncStock(string $order_no): bool
public function stock(string $orderNo): bool
{
$map = ['order_no' => $order_no];
$map = ['order_no' => $orderNo];
$codes = $this->app->db->name('ShopOrderItem')->where($map)->column('goods_code');
foreach (array_unique($codes) as $code) GoodsService::instance()->syncStock($code);
return true;
}
/**
* 刷新用户入会礼包
* @param integer $uid
* @return integer
* @throws \think\db\exception\DbException
*/
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');
$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 $orderNo
@ -66,7 +44,7 @@ class OrderService extends Service
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncUserLevel(string $orderNo): ?array
public function upgrade(string $orderNo): ?array
{
// 目标订单数据
$map = [['order_no', '=', $orderNo], ['status', '>=', 4]];
@ -76,7 +54,7 @@ class OrderService extends Service
$user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find();
if (empty($user)) return null;
// 更新用户购买资格
$entry = $this->syncUserEntry($order['uid']);
$entry = $this->vipEntry($order['uid']);
// 尝试绑定代理用户
if (empty($user['pid1']) && ($order['puid1'] > 0 || $user['pid1'] > 0)) {
$puid1 = $order['puid1'] > 0 ? $order['puid1'] : $user['bid'];
@ -90,10 +68,32 @@ class OrderService extends Service
]);
}
// 重新计算用户等级
UserUpgradeService::instance()->syncLevel($user['id'], $orderNo);
UserUpgradeService::instance()->upgrade($user['id'], $orderNo);
return [$user, $order, $entry];
}
/**
* 刷新用户入会礼包
* @param integer $uid
* @return integer
* @throws \think\db\exception\DbException
*/
private function vipEntry(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 array $data

View File

@ -1,45 +0,0 @@
<?php
namespace app\data\service;
use think\admin\Exception;
use think\admin\Service;
/**
* 月度返利服务
* Class RebateMonthService
* @package app\agent\service
*/
class RebateMonthService extends Service
{
/** @var string */
protected $date;
/** @var array */
protected $user;
/**
* 绑定数据表
* @var string
*/
private $table = 'DataUserRebate';
/**
* 指定用户发放奖励
* @param mixed $uid
* @param array $user
* @throws Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function execute($uid, array $user)
{
if (empty($user)) $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) throw new Exception("指定用户[{$uid}]不存在");
$this->app->log->notice("开始处理用户[{$user['id']}]月度[{$this->date}]返利");
$this->user = $user;
// $this->_prize_02();
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace app\data\service;
use think\admin\Exception;
use think\admin\Service;
/**
* 季度返利服务
* Class RebateQuarterService
* @package app\data\service
*/
class RebateQuarterService extends Service
{
/** @var string */
protected $quarter;
/** @var string */
protected $dateAfter;
/** @var string */
protected $dateStart;
/**
* 发放季度奖励
* @param string $year 年份
* @param string $month 月份
* @return RebateQuarterService
*/
public function build(string $year = '', string $month = ''): RebateQuarterService
{
$year = $year ?: date('Y', '-3 month');
$month = $month ?: date('m', '-3 month');
$this->quarter = ceil(date('n', mktime(0, 0, 0, $month, 1, $year)) / 3);
$this->dateAfter = date('Y-m-t 23:59:59', mktime(0, 0, 0, ($this->quarter - 1) * 3 + 0, 1, date('Y')));
$this->dateStart = date('Y-m-01 00:00:00', mktime(0, 0, 0, ($this->quarter - 2) * 3 + 1, 1, date('Y')));
return $this;
}
/**
* 指定用户发放奖励
* @param mixed $uid
* @param array $user
* @throws Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function execute($uid, $user = [])
{
if (empty($user)) $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) throw new Exception("指定用户[{$uid}]不存在");
$this->app->log->notice("开始处理用户[{$user['id']}]季度[{$this->dateStart} - {$this->dateAfter}]返利");
// $this->_prize_05($user);
}
}

View File

@ -6,11 +6,11 @@ use think\admin\Exception;
use think\admin\Service;
/**
* 实时返利服务
* Class RebateCurrentService
* 系统实时返利服务
* Class RebateService
* @package app\data\service
*/
class RebateCurrentService extends Service
class RebateService extends Service
{
const PRIZE_01 = 'PRIZE01';
const PRIZE_02 = 'PRIZE02';
@ -84,28 +84,6 @@ class RebateCurrentService extends Service
return self::PRIZES[$prize]['name'] ?? $prize;
}
/**
* 确认收货订单处理
* @param string $orderNo
* @return array [status, message]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function confirm(string $orderNo): array
{
$map = ['order_no' => $orderNo, 'status' => 6];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) return [0, '需处理的订单状态异常!'];
$map = [['status', '=', 0], ['order_no', 'like', "{$orderNo}%"]];
$this->app->db->name($this->table)->where($map)->update(['status' => 1]);
if (UserUpgradeService::instance()->syncLevel($order['uid'])) {
return [1, '重新计算用户金额成功!'];
} else {
return [0, '重新计算用户金额失败!'];
}
}
/**
* 执行订单返利处理
* @param string $orderNo
@ -120,8 +98,6 @@ class RebateCurrentService extends Service
$map = ['order_no' => $orderNo, 'payment_status' => 1];
$this->order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($this->order)) throw new Exception('订单不存在');
// 同步订单用户余额
if ($this->order['reward_balance'] > 0) $this->_balance();
// 检查订单参与返利
if ($this->order['amount_total'] <= 0) throw new Exception('订单金额为零');
if ($this->order['rebate_amount'] <= 0) throw new Exception('订单返利为零');
@ -151,25 +127,6 @@ class RebateCurrentService extends Service
}
}
/**
* 来自订单余额充值
* @return array [total, count]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function _balance(): array
{
data_save('DataUserBalance', [
'uid' => $this->order['uid'],
'code' => $this->order['order_no'],
'name' => "订单余额充值",
'remark' => "来自订单{$this->order['order_no']}的余额充值",
'amount' => $this->order['reward_balance'],
], 'code');
return UserUpgradeService::instance()->syncBalance($this->order['uid']);
}
/**
* 用户首推奖励
* @return boolean
@ -199,7 +156,7 @@ class RebateCurrentService extends Service
'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
// 更新用户奖利金额
UserUpgradeService::instance()->syncLevel($this->from1['id']);
UserUpgradeService::instance()->upgrade($this->from1['id']);
}
return true;
}
@ -233,7 +190,7 @@ class RebateCurrentService extends Service
'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
// 更新用户奖利金额
UserUpgradeService::instance()->syncLevel($this->from1['id']);
UserUpgradeService::instance()->upgrade($this->from1['id']);
}
return true;
}
@ -260,7 +217,7 @@ class RebateCurrentService extends Service
'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
// 更新用户奖利金额
UserUpgradeService::instance()->syncLevel($this->from1['id']);
UserUpgradeService::instance()->upgrade($this->from1['id']);
}
return true;
}
@ -286,7 +243,7 @@ class RebateCurrentService extends Service
'uid' => $this->from2['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
// 更新代理奖利金额
UserUpgradeService::instance()->syncLevel($this->from2['id']);
UserUpgradeService::instance()->upgrade($this->from2['id']);
}
return true;
}
@ -359,7 +316,7 @@ class RebateCurrentService extends Service
$this->app->db->name($this->table)->insert(array_merge($map, [
'uid' => $this->from2['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
UserUpgradeService::instance()->syncLevel($this->from2['id']);
UserUpgradeService::instance()->upgrade($this->from2['id']);
}
$prevLevel = $user['vip_code'];
}
@ -421,7 +378,7 @@ class RebateCurrentService extends Service
'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
// 更新用户奖利金额
UserUpgradeService::instance()->syncLevel($this->from1['id']);
UserUpgradeService::instance()->upgrade($this->from1['id']);
}
return true;
}

View File

@ -1,50 +0,0 @@
<?php
namespace app\data\service;
use think\admin\Exception;
use think\admin\Service;
/**
* 年度返利服务
* Class RebateYearService
* @package app\data\service
*/
class RebateYearService extends Service
{
/** @var array */
protected $user;
/** @var string */
protected $year;
/**
* 发放年度奖励
* @param string $year 年份
* @return RebateYearService
*/
public function build(string $year = ''): RebateYearService
{
$this->year = $year ?: date('Y', strtotime('-1 year'));
return $this;
}
/**
* 指定用户发放奖励
* @param mixed $uid
* @param array $user
* @throws Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function execute($uid, $user = [])
{
if (empty($user)) $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) throw new Exception("指定用户[{$uid}]不存在");
$this->app->log->notice("开始处理用户[{$user['id']}]年度[{$this->year}]返利");
$this->user = $user;
// $this->_prize_06();
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace app\data\service;
use think\admin\Service;
/**
* 用户余额数据服务
* Class UserBalanceService
* @package app\data\service
*/
class UserBalanceService extends Service
{
/**
* 同步刷新用户余额
* @param int $uuid 用户UID
* @param array $nots 排除的订单
* @return array [total, count]
* @throws \think\db\exception\DbException
*/
public function amount(int $uuid, array $nots = []): array
{
$total = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and amount>0 and deleted=0")->sum('amount'));
$count = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and amount<0 and deleted=0")->sum('amount'));
if (empty($nots)) {
$this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
} else {
$count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount');
}
return [$total, $count];
}
/**
* 验证订单发放余额
* @param string $orderNo
* @return array [total, count]
* @throws \think\admin\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function confirm(string $orderNo): array
{
$map = [['order_no', '=', $orderNo], ['status', '>=', 4]];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) throw new \think\admin\Exception('需处理的订单状态异常');
data_save('DataUserBalance', [
'uid' => $order['uid'],
'code' => $order['order_no'],
'name' => "订单余额充值",
'remark' => "来自订单{$order['order_no']}的余额充值",
'amount' => $order['reward_balance'],
], 'code');
return $this->amount($order['uid']);
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace app\data\service;
use think\admin\Service;
/**
* 用户返利数据服务
* Class UserRebateService
* @package app\data\service
*/
class UserRebateService extends Service
{
/**
* 同步刷新用户返利
* @param integer $uuid
* @return array [total, count, lock]
* @throws \think\db\exception\DbException
*/
public function amount(int $uuid): array
{
if ($uuid > 0) {
$count = abs($this->app->db->name('DataUserTransfer')->whereRaw("uid='{$uuid}' and status>0")->sum('amount'));
$total = abs($this->app->db->name('DataUserRebate')->whereRaw("uid='{$uuid}' and status=1 and deleted=0")->sum('amount'));
$locks = abs($this->app->db->name('DataUserRebate')->whereRaw("uid='{$uuid}' and status=0 and deleted=0")->sum('amount'));
$this->app->db->name('DataUser')->where(['id' => $uuid])->update([
'rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $locks,
]);
} else {
$count = abs($this->app->db->name('DataUserTransfer')->whereRaw("status>0")->sum('amount'));
$total = abs($this->app->db->name('DataUserRebate')->whereRaw("status=1 and deleted=0")->sum('amount'));
$locks = abs($this->app->db->name('DataUserRebate')->whereRaw("status=0 and deleted=0")->sum('amount'));
}
return [$total, $count, $locks];
}
/**
* 确认收货订单处理
* @param string $orderNo
* @return array [status, message]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function confirm(string $orderNo): array
{
$map = ['order_no' => $orderNo, 'status' => 6];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) return [0, '需处理的订单状态异常!'];
$map = [['status', '=', 0], ['order_no', 'like', "{$orderNo}%"]];
$this->app->db->name('DataUserRebate')->where($map)->update(['status' => 1]);
if (UserUpgradeService::instance()->upgrade($order['uid'])) {
return [1, '重新计算用户金额成功!'];
} else {
return [0, '重新计算用户金额失败!'];
}
}
}

View File

@ -22,27 +22,27 @@ class UserService extends Service
const TYPES = [
// 接口支付配置(不需要的直接注释)
UserService::API_TYPE_WAP => [
self::API_TYPE_WAP => [
'name' => '手机浏览器',
'auth' => 'phone',
],
UserService::API_TYPE_WEB => [
self::API_TYPE_WEB => [
'name' => '电脑浏览器',
'auth' => 'phone',
],
UserService::API_TYPE_WXAPP => [
self::API_TYPE_WXAPP => [
'name' => '微信小程序',
'auth' => 'openid1',
],
UserService::API_TYPE_WECHAT => [
self::API_TYPE_WECHAT => [
'name' => '微信服务号',
'auth' => 'openid2',
],
UserService::API_TYPE_IOSAPP => [
self::API_TYPE_IOSAPP => [
'name' => '苹果APP应用',
'auth' => 'phone',
],
UserService::API_TYPE_ANDROID => [
self::API_TYPE_ANDROID => [
'name' => '安卓APP应用',
'auth' => 'phone',
],

View File

@ -5,7 +5,7 @@ namespace app\data\service;
use think\admin\Service;
/**
* 用户转账服务
* 用户提现数据服务
* Class UserTransferService
* @package app\data\service
*/

View File

@ -25,84 +25,6 @@ class UserUpgradeService extends Service
return $query->where(['status' => 1])->order('number asc')->select()->toArray();
}
/**
* 尝试绑定上级代理
* @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['pids'])) return [0, '已绑定推荐人'];
// 检查代理用户
if (empty($pid)) $pid = $user['pid0'];
if (empty($pid)) return [0, '绑定推荐人不存在'];
if ($uid == $pid) return [0, '推荐人不能是自己'];
$parant = $this->app->db->name('DataUser')->where(['id' => $pid])->find();
if (empty($parant['pids']) || empty($parant['vip_code'])) return [0, '推荐人无推荐资格'];
if (stripos($parant['path'], "-{$uid}-") !== false) return [0, '不能绑定下属'];
// 组装代理数据
$path = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-";
$data = [
'pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1'],
'pids' => $force ? 1 : 0, 'path' => $path, 'layer' => substr_count($path, '-'),
];
// 更新用户代理
if ($this->app->db->name('DataUser')->where(['id' => $uid])->update($data) !== false) {
return [1, '绑定代理成功'];
} else {
return [0, '绑定代理失败'];
}
}
/**
* 同步刷新用户返利
* @param integer $uuid
* @return array [total, count, lock]
* @throws \think\db\exception\DbException
*/
public function syncRebate(int $uuid): array
{
if ($uuid > 0) {
$count = abs($this->app->db->name('DataUserTransfer')->whereRaw("uid='{$uuid}' and status>0")->sum('amount'));
$total = abs($this->app->db->name('DataUserRebate')->whereRaw("uid='{$uuid}' and status=1 and deleted=0")->sum('amount'));
$locks = abs($this->app->db->name('DataUserRebate')->whereRaw("uid='{$uuid}' and status=0 and deleted=0")->sum('amount'));
$this->app->db->name('DataUser')->where(['id' => $uuid])->update([
'rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $locks,
]);
} else {
$count = abs($this->app->db->name('DataUserTransfer')->whereRaw("status>0")->sum('amount'));
$total = abs($this->app->db->name('DataUserRebate')->whereRaw("status=1 and deleted=0")->sum('amount'));
$locks = abs($this->app->db->name('DataUserRebate')->whereRaw("status=0 and deleted=0")->sum('amount'));
}
return [$total, $count, $locks];
}
/**
* 同步刷新用户余额
* @param int $uuid 用户UID
* @param array $nots 排除的订单
* @return array [total, count]
* @throws \think\db\exception\DbException
*/
public function syncBalance(int $uuid, array $nots = []): array
{
$total = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and amount>0 and deleted=0")->sum('amount'));
$count = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and amount<0 and deleted=0")->sum('amount'));
if (empty($nots)) {
$this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
} else {
$count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount');
}
return [$total, $count];
}
/**
* 同步计算用户等级
* @param integer $uid 指定用户UID
@ -113,12 +35,10 @@ class UserUpgradeService extends Service
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncLevel(int $uid, bool $parent = true, ?string $orderNo = null): bool
public function upgrade(int $uid, bool $parent = true, ?string $orderNo = null): bool
{
$user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) return true;
// 刷新用户返利
$this->syncRebate($uid);
// 开始处理等级
[$vipName, $vipCode] = ['普通用户', 0];
// 统计历史数据
@ -177,6 +97,42 @@ class UserUpgradeService extends Service
'uid' => $user['uid'], 'order_no' => $orderNo, 'vip_code_old' => $user['vip_code'], 'vip_code_new' => $vipCode,
]);
}
return ($parent && $user['pid2'] > 0) ? $this->syncLevel($user['pid2'], false) : true;
return ($parent && $user['pid2'] > 0) ? $this->upgrade($user['pid2'], false) : true;
}
/**
* 尝试绑定上级代理
* @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['pids'])) return [0, '已绑定推荐人'];
// 检查代理用户
if (empty($pid)) $pid = $user['pid0'];
if (empty($pid)) return [0, '绑定推荐人不存在'];
if ($uid == $pid) return [0, '推荐人不能是自己'];
$parant = $this->app->db->name('DataUser')->where(['id' => $pid])->find();
if (empty($parant['pids']) || empty($parant['vip_code'])) return [0, '推荐人无推荐资格'];
if (stripos($parant['path'], "-{$uid}-") !== false) return [0, '不能绑定下属'];
// 组装代理数据
$path = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-";
$data = [
'pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1'],
'pids' => $force ? 1 : 0, 'path' => $path, 'layer' => substr_count($path, '-'),
];
// 更新用户代理
if ($this->app->db->name('DataUser')->where(['id' => $uid])->update($data) !== false) {
return [1, '绑定代理成功'];
} else {
return [0, '绑定代理失败'];
}
}
}

View File

@ -36,10 +36,6 @@ class AlipayPaymentService extends PaymentService
'public_key' => $this->_trimCertHeader($this->params['alipay_public_key']),
// 支付宝私钥 (1行填写)
'private_key' => $this->_trimCertHeader($this->params['alipay_private_key']),
// 应用公钥证书(新版资金类接口转 app_cert_sn
# 'app_cert' => '',
// 支付宝根证书(新版资金类接口转 alipay_root_cert_sn
# 'root_cert' => '',
// 支付成功通知地址
'notify_url' => '',
// 网页支付回跳地址

View File

@ -3,7 +3,7 @@
namespace app\data\service\payment;
use app\data\service\PaymentService;
use app\data\service\UserUpgradeService;
use app\data\service\UserBalanceService;
use think\admin\Exception;
use think\admin\extend\CodeExtend;
@ -56,7 +56,7 @@ class BalancePyamentService extends PaymentService
// 创建支付行为
$this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount);
// 扣减用户余额
[$total, $count] = UserUpgradeService::instance()->syncBalance($order['uid'], [$orderNo]);
[$total, $count] = UserBalanceService::instance()->amount($order['uid'], [$orderNo]);
if ($paymentAmount > $total - $count) throw new Exception("可抵扣余额不足");
$this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['payment_balance' => $paymentAmount]);
// 扣除余额金额
@ -70,7 +70,7 @@ class BalancePyamentService extends PaymentService
// 更新支付行为
$this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付');
// 刷新用户余额
UserUpgradeService::instance()->syncBalance($order['uid']);
UserBalanceService::instance()->amount($order['uid']);
return ['info' => '余额支付完成'];
}
}

View File

@ -2,10 +2,12 @@
use app\data\command\OrderClear;
use app\data\command\UserBalance;
use app\data\command\UserUpgrade;
use app\data\command\UserTransfer;
use app\data\command\UserUpgrade;
use app\data\service\OrderService;
use app\data\service\RebateCurrentService;
use app\data\service\RebateService;
use app\data\service\UserBalanceService;
use app\data\service\UserRebateService;
use think\Console;
if (app()->request->isCli()) {
@ -18,14 +20,19 @@ if (app()->request->isCli()) {
} else {
// 注册订单支付处理事件
app()->event->listen('ShopOrderPayment', function ($orderNo) {
app()->log->notice("订单支付事件,订单号:{$orderNo}");
OrderService::instance()->syncUserLevel($orderNo);
RebateCurrentService::instance()->execute($orderNo);
app()->log->notice("订单 {$orderNo} 支付事件,执行用户升级行为");
OrderService::instance()->upgrade($orderNo);
app()->log->notice("订单 {$orderNo} 支付事件,执行用户返利行为");
RebateService::instance()->execute($orderNo);
app()->log->notice("订单 {$orderNo} 支付事件,执行发放余额行为");
UserBalanceService::instance()->confirm($orderNo);
});
// 注册订单确认支付事件
app()->event->listen('ShopOrderConfirm', function ($orderNo) {
app()->log->notice("订单确认事件,订单号:{$orderNo}");
RebateCurrentService::instance()->confirm($orderNo);
app()->log->notice("订单 {$orderNo} 确认事件,执行返利确认行为");
UserRebateService::instance()->confirm($orderNo);
});
}

View File

@ -237,18 +237,18 @@ return array(
'app\\data\\controller\\api\\auth\\News' => $baseDir . '/app/data/controller/api/auth/News.php',
'app\\data\\controller\\api\\auth\\Order' => $baseDir . '/app/data/controller/api/auth/Order.php',
'app\\data\\controller\\api\\auth\\Rebate' => $baseDir . '/app/data/controller/api/auth/Rebate.php',
'app\\data\\service\\ExpressService' => $baseDir . '/app/data/service/ExpressService.php',
'app\\data\\service\\GoodsService' => $baseDir . '/app/data/service/GoodsService.php',
'app\\data\\service\\MessageService' => $baseDir . '/app/data/service/MessageService.php',
'app\\data\\service\\NewsService' => $baseDir . '/app/data/service/NewsService.php',
'app\\data\\service\\OrderService' => $baseDir . '/app/data/service/OrderService.php',
'app\\data\\service\\PaymentService' => $baseDir . '/app/data/service/PaymentService.php',
'app\\data\\service\\RebateCurrentService' => $baseDir . '/app/data/service/RebateCurrentService.php',
'app\\data\\service\\RebateMonthService' => $baseDir . '/app/data/service/RebateMonthService.php',
'app\\data\\service\\RebateQuarterService' => $baseDir . '/app/data/service/RebateQuarterService.php',
'app\\data\\service\\RebateYearService' => $baseDir . '/app/data/service/RebateYearService.php',
'app\\data\\service\\TruckService' => $baseDir . '/app/data/service/TruckService.php',
'app\\data\\service\\RebateService' => $baseDir . '/app/data/service/RebateService.php',
'app\\data\\service\\UserBalanceService' => $baseDir . '/app/data/service/UserBalanceService.php',
'app\\data\\service\\UserRebateService' => $baseDir . '/app/data/service/UserRebateService.php',
'app\\data\\service\\UserService' => $baseDir . '/app/data/service/UserService.php',
'app\\data\\service\\UserTokenService' => $baseDir . '/app/data/service/UserTokenService.php',
'app\\data\\service\\UserTransferService' => $baseDir . '/app/data/service/UserTransferService.php',
'app\\data\\service\\UserUpgradeService' => $baseDir . '/app/data/service/UserUpgradeService.php',
'app\\data\\service\\payment\\AlipayPaymentService' => $baseDir . '/app/data/service/payment/AlipayPaymentService.php',
'app\\data\\service\\payment\\BalancePyamentService' => $baseDir . '/app/data/service/payment/BalancePyamentService.php',

View File

@ -366,18 +366,18 @@ class ComposerStaticInit4f89fd0e0503ccf740f2fa5757825d7b
'app\\data\\controller\\api\\auth\\News' => __DIR__ . '/../..' . '/app/data/controller/api/auth/News.php',
'app\\data\\controller\\api\\auth\\Order' => __DIR__ . '/../..' . '/app/data/controller/api/auth/Order.php',
'app\\data\\controller\\api\\auth\\Rebate' => __DIR__ . '/../..' . '/app/data/controller/api/auth/Rebate.php',
'app\\data\\service\\ExpressService' => __DIR__ . '/../..' . '/app/data/service/ExpressService.php',
'app\\data\\service\\GoodsService' => __DIR__ . '/../..' . '/app/data/service/GoodsService.php',
'app\\data\\service\\MessageService' => __DIR__ . '/../..' . '/app/data/service/MessageService.php',
'app\\data\\service\\NewsService' => __DIR__ . '/../..' . '/app/data/service/NewsService.php',
'app\\data\\service\\OrderService' => __DIR__ . '/../..' . '/app/data/service/OrderService.php',
'app\\data\\service\\PaymentService' => __DIR__ . '/../..' . '/app/data/service/PaymentService.php',
'app\\data\\service\\RebateCurrentService' => __DIR__ . '/../..' . '/app/data/service/RebateCurrentService.php',
'app\\data\\service\\RebateMonthService' => __DIR__ . '/../..' . '/app/data/service/RebateMonthService.php',
'app\\data\\service\\RebateQuarterService' => __DIR__ . '/../..' . '/app/data/service/RebateQuarterService.php',
'app\\data\\service\\RebateYearService' => __DIR__ . '/../..' . '/app/data/service/RebateYearService.php',
'app\\data\\service\\TruckService' => __DIR__ . '/../..' . '/app/data/service/TruckService.php',
'app\\data\\service\\RebateService' => __DIR__ . '/../..' . '/app/data/service/RebateService.php',
'app\\data\\service\\UserBalanceService' => __DIR__ . '/../..' . '/app/data/service/UserBalanceService.php',
'app\\data\\service\\UserRebateService' => __DIR__ . '/../..' . '/app/data/service/UserRebateService.php',
'app\\data\\service\\UserService' => __DIR__ . '/../..' . '/app/data/service/UserService.php',
'app\\data\\service\\UserTokenService' => __DIR__ . '/../..' . '/app/data/service/UserTokenService.php',
'app\\data\\service\\UserTransferService' => __DIR__ . '/../..' . '/app/data/service/UserTransferService.php',
'app\\data\\service\\UserUpgradeService' => __DIR__ . '/../..' . '/app/data/service/UserUpgradeService.php',
'app\\data\\service\\payment\\AlipayPaymentService' => __DIR__ . '/../..' . '/app/data/service/payment/AlipayPaymentService.php',
'app\\data\\service\\payment\\BalancePyamentService' => __DIR__ . '/../..' . '/app/data/service/payment/BalancePyamentService.php',

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php
// This file is automatically generated at:2021-03-16 14:49:04
// This file is automatically generated at:2021-03-17 10:31:58
declare (strict_types = 1);
return array (
0 => 'think\\admin\\Library',