1. 分账资金变动功能

This commit is contained in:
李琦
2026-06-02 08:15:42 +08:00
parent 6a8c4dcdf6
commit 6b7f9e8971
4 changed files with 227 additions and 23 deletions

View File

@@ -0,0 +1,101 @@
<?php
namespace common\services;
use common\models\newDb\AccountAbleChangeLogModel;
use Yii;
class AccountAbleChangeLogService
{
public const ACCOUNT_TABLE_CASH = 'yii_cash_account';
public const ACCOUNT_TABLE_BALANCE = 'xk_account_balance';
public const FIELD_ABLE_CASH = 'able_cash';
public const FIELD_BALANCE = 'balance';
public static function recordWithdrawableChange(
int $userId,
int $userType,
string $accountTable,
string $fieldName,
$before,
$after,
string $sourceType,
string $sourceTable,
int $sourceId = 0,
array $extra = []
): void {
$change = bcsub((string)$after, (string)$before, 6);
if (bccomp($change, '0', 6) === 0) {
return;
}
$time = time();
$model = new AccountAbleChangeLogModel();
$model->user_id = $userId;
$model->user_type = $userType;
$model->account_table = $accountTable;
$model->field_name = $fieldName;
$model->before_amount = $before;
$model->after_amount = $after;
$model->change_amount = $change;
$model->source_type = $sourceType;
$model->source_table = $sourceTable;
$model->source_id = $sourceId;
$model->order_id = (int)($extra['order_id'] ?? 0);
$model->order_type = (int)($extra['order_type'] ?? 0);
$model->order_no = (string)($extra['order_no'] ?? '');
$model->remark = (string)($extra['remark'] ?? '');
$model->created_at = $time;
$model->updated_at = $time;
if (!$model->save(false)) {
Yii::warning('写入 xk_account_able_change_log 失败', __METHOD__);
}
}
public static function logCashAccountAbleChange(
int $userId,
int $userType,
$before,
$after,
string $sourceType,
string $sourceTable,
int $sourceId = 0,
array $extra = []
): void {
self::recordWithdrawableChange(
$userId,
$userType,
self::ACCOUNT_TABLE_CASH,
self::FIELD_ABLE_CASH,
$before,
$after,
$sourceType,
$sourceTable,
$sourceId,
$extra
);
}
public static function logSupplierBalanceChange(
int $userId,
$before,
$after,
string $sourceType,
string $sourceTable,
int $sourceId = 0,
array $extra = []
): void {
self::recordWithdrawableChange(
$userId,
3,
self::ACCOUNT_TABLE_BALANCE,
self::FIELD_BALANCE,
$before,
$after,
$sourceType,
$sourceTable,
$sourceId,
$extra
);
}
}