余额变动修改

This commit is contained in:
2026-04-22 16:38:50 +08:00
parent ad74accfcc
commit 95684c784e
12 changed files with 306 additions and 32 deletions

View File

@@ -2,6 +2,7 @@
namespace app\admin\controller\user;
use app\common\service\Jk8Services;
use Throwable;
use app\admin\model\User;
use app\admin\model\UserMoneyLog;
@@ -15,19 +16,63 @@ class MoneyLog extends Backend
*/
protected object $model;
protected array $withJoinTable = ['user'];
protected array $withJoinTable = ['user', 'admin'];
protected array $withTable = ['scoreLog'];
// 排除字段
protected string|array $preExcludeFields = ['create_time'];
protected string|array $quickSearchField = ['user.username', 'user.nickname'];
protected $jk8Services;
public function initialize(): void
{
parent::initialize();
$this->jk8Services = app(Jk8Services::class);
$this->model = new UserMoneyLog();
}
/**
* 验证逻辑
*/
protected function validateModelData(array $data, string $scene = ''): void
{
if (!$this->modelValidate) return;
$validateClass = str_replace("\\model\\", "\\validate\\", get_class($this->model));
if (class_exists($validateClass)) {
$validate = new $validateClass();
if ($scene) $validate->scene($scene);
$validate->check($data);
}
}
/**
* 查看
* @throws Throwable
*/
public function index(): void
{
if ($this->request->param('select')) {
$this->select();
}
list($where, $alias, $limit, $order) = $this->queryBuilder();
$res = $this->model
->withJoin($this->withJoinTable, $this->withJoinType)
->with($this->withTable)
->alias($alias)
->where($where)
->order($order)
->paginate($limit);
$this->success('', [
'list' => $res->items(),
'total' => $res->total(),
'remark' => get_route_remark(),
]);
}
/**
* 添加
* @param int $userId
@@ -36,7 +81,45 @@ class MoneyLog extends Backend
public function add(int $userId = 0): void
{
if ($this->request->isPost()) {
parent::add();
$data = $this->request->post();
if (!$data) {
$this->error(__('Parameter %s can not be empty', ['']));
}
$result = false;
$data = $this->excludeFields($data);
$user = User::where('id', $data['user_id'])->find();
if ($data['type'] == 2) {
$data['money'] = -$data['money'];
}
if ($user->money + $data['money'] < 0) {
$this->error(__('Insufficient Credit'));
}
try {
// 模型验证
$this->validateModelData($data, 'add');
$transactionId = $this->jk8Services->setScore($user['jk_username'], $data['money']);
} catch (Throwable $e) {
$this->error($e->getMessage());
}
$this->model->startTrans();
try {
$data['transaction_id'] = $transactionId;
$data['created_by'] = $this->auth->getInfo()['id'];
// $data['type'] = $data['money'] > 0 ? 1 : 2;
$result = $this->model->save($data);
$this->model->commit();
} catch (Throwable $e) {
$this->model->rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success(__('Added successfully'));
} else {
$this->error(__('No rows were added'));
}
}
$user = User::where('id', $userId)->find();
@@ -47,4 +130,28 @@ class MoneyLog extends Backend
'user' => $user
]);
}
/**
* 编辑
* @throws Throwable
*/
public function edit(): void
{
$pk = $this->model->getPk();
$id = $this->request->param($pk);
$row = $this->model->find($id);
if (!$row) {
$this->error(__('Record not found'));
}
$user = User::where('id', $row['user_id'])->find();
if ($this->request->isPost()) {
parent::edit();
return;
}
$row['username'] = $user['username'];
$row['nickname'] = $user['nickname'];
$this->success('', [
'row' => $row
]);
}
}