176 lines
4.7 KiB
PHP
176 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\user;
|
|
|
|
use app\admin\model\MoneyLogHistory;
|
|
use Throwable;
|
|
use app\admin\model\User;
|
|
use app\admin\model\UserMoneyLog;
|
|
use app\common\controller\Backend;
|
|
|
|
class MoneyLog extends Backend
|
|
{
|
|
/**
|
|
* @var object
|
|
* @phpstan-var UserMoneyLog
|
|
*/
|
|
protected object $model;
|
|
|
|
protected array $withJoinTable = ['user', 'admin', 'bank'];
|
|
protected array $withTable = ['scoreLog'];
|
|
|
|
// 排除字段
|
|
protected string|array $preExcludeFields = ['create_time'];
|
|
|
|
protected string|array $quickSearchField = ['user.username', 'user.nickname'];
|
|
|
|
public function initialize(): void
|
|
{
|
|
parent::initialize();
|
|
$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
|
|
* @throws Throwable
|
|
*/
|
|
public function add(int $userId = 0): void
|
|
{
|
|
$this->error(__('No rows were added'));
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
if (!$data) {
|
|
$this->error(__('Parameter %s can not be empty', ['']));
|
|
}
|
|
$result = false;
|
|
$data = $this->excludeFields($data);
|
|
|
|
if ($data['type'] == 2) {
|
|
$data['money'] = -$data['money'];
|
|
}
|
|
|
|
$this->model->startTrans();
|
|
try {
|
|
$data['created_by'] = $this->auth->getInfo()['id'];
|
|
$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();
|
|
if (!$user) {
|
|
$this->error(__("The user can't find it"));
|
|
}
|
|
$this->success('', [
|
|
'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'));
|
|
}
|
|
|
|
if ($this->request->isPost()) {
|
|
$data = $this->request->post();
|
|
if (!$data) {
|
|
$this->error(__('Parameter %s can not be empty', ['']));
|
|
}
|
|
|
|
$result = false;
|
|
$this->model->startTrans();
|
|
try {
|
|
$result = $row->save($data);
|
|
$this->model->commit();
|
|
} catch (Throwable $e) {
|
|
$this->model->rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result !== false) {
|
|
$this->success(__('Update successful'));
|
|
} else {
|
|
$this->error(__('No rows updated'));
|
|
}
|
|
return;
|
|
}
|
|
$this->success('', [
|
|
'row' => $row
|
|
]);
|
|
}
|
|
|
|
public function logHistory($id): void
|
|
{
|
|
$bindFields = [
|
|
'admin' => ['id','username'],
|
|
];
|
|
$history = MoneyLogHistory::withJoin($bindFields, 'left')
|
|
->where('money_log_id', $id)
|
|
->select();
|
|
if (!empty($history)) {
|
|
foreach ($history as &$item) {
|
|
$item['admin_name'] = $item['admin']['username'] ?? '';
|
|
unset($item['admin']);
|
|
}
|
|
unset($item);
|
|
}
|
|
$this->success('', $history);
|
|
}
|
|
} |