feat(partner): 完善马豆实时动态汇率与精度自适应,支持交易数据清理与汇率重置

这个提交包含在:
chen
2026-09-18 07:59:50 +08:00
父节点 ac8c45bdad
当前提交 7c10864be2
共有 60 个文件被更改,包括 2691 次插入 和 490 次删除

查看文件

@@ -198,7 +198,7 @@ class User extends Common
->where('user_id', $id);
if (!$showSkip) {
$query->where(function ($q) {
$q->where('amount', '>', 0)->whereOr('madow', '>', 0);
$q->where('amount', '<>', 0)->whereOr('madow', '<>', 0);
});
}
$list = $query->order('id', 'desc')->paginate($this->request->param('list_rows', 10));
@@ -312,17 +312,182 @@ class User extends Common
}
$params = $this->request->only(['id', 'type', 'amount', 'memo']);
if (!in_array($params['type'], ['money', 'score'])) {
error_stop('参数错误');
$userId = intval($params['id'] ?? 0);
$type = (string) ($params['type'] ?? 'money');
$amount = trim((string) ($params['amount'] ?? ''));
$memo = trim((string) ($params['memo'] ?? ''));
if (!$userId || !is_numeric($amount) || (float) $amount == 0) {
$this->error('请输入合法的变更数值');
}
$result = Db::transaction(function () use ($params) {
return WalletService::change($params['id'], $params['type'], $params['amount'], 'admin_recharge', [], $params['memo']);
});
if ($result) {
$this->success('充值成功');
$allowedTypes = ['money', 'score', 'madow', 'voucher'];
if (!in_array($type, $allowedTypes)) {
$this->error('不支持的资产类型');
}
$this->error('充值失败');
try {
$result = Db::transaction(function () use ($userId, $type, $amount, $memo) {
$user = UserModel::where('id', $userId)->lock(true)->find();
if (!$user) {
error_stop('会员不存在');
}
$oper = \addons\shopro\library\Operator::get();
$operDesc = ($oper['type'] ?? 'admin') . ($oper['id'] ? '#' . $oper['id'] : '');
if ($type === 'money') {
// 余额充值或扣除 (保留2位小数)
$amount = round((float) $amount, 2);
if ($amount < 0 && bcadd((string) $user->money, (string) $amount, 2) < 0) {
error_stop('当前余额不足,无法扣除');
}
return WalletService::change($userId, 'money', $amount, 'admin_recharge', [], $memo ?: '后台管理调整余额');
}
if ($type === 'score') {
// 积分充值或扣除 (整数)
$amount = intval($amount);
if ($amount == 0) {
error_stop('请输入有效的积分数值');
}
if ($amount < 0 && ($user->score + $amount) < 0) {
error_stop('当前积分不足,无法扣除');
}
return WalletService::change($userId, 'score', $amount, 'admin_recharge', [], $memo ?: '后台管理调整积分');
}
// 对于马豆 (madow) 与兑换券 (voucher),属于合伙人二开资产体系
$agent = \app\admin\model\shopro\commission\Agent::where('user_id', $userId)->lock(true)->find();
if (!$agent) {
// 自动为用户初始化分销商记录,以便承载二开资产体系
$agent = (new \addons\shopro\service\commission\Agent($userId))->createNewAgent();
$agent = \app\admin\model\shopro\commission\Agent::where('user_id', $userId)->lock(true)->find();
if (!$agent) {
$now = time();
\think\Db::name('shopro_commission_agent')->insert([
'user_id' => $userId,
'level' => 1,
'status' => 'normal',
'madow' => '0.00000000',
'voucher_total' => '0.00',
'voucher_released' => '0.00',
'voucher_used' => '0.00',
'createtime' => $now,
'updatetime' => $now,
]);
$agent = \app\admin\model\shopro\commission\Agent::where('user_id', $userId)->lock(true)->find();
}
}
// 生成全局唯一的负数 order_id(防止与商城真实订单冲突,并避免 uk_order_user_type 唯一索引冲突)
$randOrderId = -intval(substr(time(), -6) . mt_rand(100, 999));
$identity = \addons\shopro\service\partner\IdentityService::build($agent);
$role = !empty($identity['identities']) ? end($identity['identities']) : 'taster';
if ($type === 'madow') {
// 马豆充值或扣除 (8位小数精度)
$currentMadow = (string) ($agent->madow ?? '0.00000000');
$amountMadow = function_exists('bcadd') ? bcadd((string) $amount, '0', 8) : number_format((float) $amount, 8, '.', '');
$afterMadow = function_exists('bcadd') ? bcadd($currentMadow, $amountMadow, 8) : ($currentMadow + $amountMadow);
if ((float) $afterMadow < 0) {
error_stop('当前持仓马豆不足,无法扣除');
}
$rate = (new \addons\shopro\service\partner\RateService())->currentRate();
$agent->madow = $afterMadow;
$agent->save();
\addons\shopro\service\partner\PartnerRewardService::write([
'order_id' => $randOrderId,
'buyer_id' => $userId,
'user_id' => $userId,
'role' => $role,
'reward_type' => 'admin_madow',
'amount' => 0,
'madow' => $amountMadow,
'base_amount' => 0,
'rate' => $rate,
'settle_date' => null, // 设为 null,避免与每日均分的 uk_daily_user_type 发生索引冲突
'ext' => [
'formula' => ($amount > 0 ? '管理员充值马豆: +' : '管理员扣除马豆: ') . $amountMadow,
'memo' => $memo ?: ($amount > 0 ? '管理员充值马豆' : '管理员扣除马豆'),
'operator' => $operDesc,
'before' => $currentMadow,
'after' => $afterMadow,
],
'status' => 1,
]);
return true;
}
if ($type === 'voucher') {
// 兑换券充值或扣除 (2位小数精度)
$amountVoucher = round((float) $amount, 2);
$currentReleased = (float) ($agent->voucher_released ?? 0);
$currentUsed = (float) ($agent->voucher_used ?? 0);
$currentTotal = (float) ($agent->voucher_total ?? 0);
$available = max(0, round($currentReleased - $currentUsed, 2));
if ($amountVoucher < 0 && bccomp((string) $available, (string) abs($amountVoucher), 2) < 0) {
error_stop('当前可用兑换券不足,无法扣除');
}
if ($amountVoucher > 0) {
// 充值增加可用兑换券:同时增加 released 与 total
$newReleased = round($currentReleased + $amountVoucher, 2);
$newTotal = round($currentTotal + $amountVoucher, 2);
} else {
// 扣除可用兑换券:减少 released 与 total
$deduct = abs($amountVoucher);
$newReleased = max(0, round($currentReleased - $deduct, 2));
$newTotal = max(0, round($currentTotal - $deduct, 2));
}
$agent->voucher_released = $newReleased;
$agent->voucher_total = $newTotal;
$agent->save();
\addons\shopro\service\partner\PartnerRewardService::write([
'order_id' => $randOrderId,
'buyer_id' => $userId,
'user_id' => $userId,
'role' => $role,
'reward_type' => 'admin_voucher',
'amount' => $amountVoucher,
'madow' => 0,
'base_amount' => abs($amountVoucher),
'rate' => 0,
'settle_date' => null,
'ext' => [
'formula' => ($amountVoucher > 0 ? '管理员充值兑换券: +' : '管理员扣除兑换券: ') . $amountVoucher,
'memo' => $memo ?: ($amountVoucher > 0 ? '管理员充值兑换券' : '管理员扣除兑换券'),
'operator' => $operDesc,
'before_available' => number_format($available, 2, '.', ''),
'after_available' => number_format(max(0, $newReleased - $currentUsed), 2, '.', ''),
],
'status' => 1,
]);
return true;
}
return false;
});
} catch (\think\exception\HttpResponseException $e) {
$data = $e->getResponse()->getData();
$msg = $data ? ($data['msg'] ?? '') : $e->getMessage();
$this->error($msg ?: '操作失败');
} catch (\Exception $e) {
$this->error($e->getMessage() ?: '系统异常');
}
if ($result) {
$this->success('操作成功');
}
$this->error('操作失败');
}