增加积分调整日志
This commit is contained in:
71
app/admin/controller/mall/PointsLog.php
Normal file
71
app/admin/controller/mall/PointsLog.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\mall;
|
||||
|
||||
use app\admin\model\MallPointsLog;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\model\MallUserAsset;
|
||||
use support\Response;
|
||||
use Webman\Http\Request;
|
||||
|
||||
class PointsLog extends Backend
|
||||
{
|
||||
protected ?object $model = null;
|
||||
|
||||
protected array $withJoinTable = ['userAsset', 'admin'];
|
||||
protected array|string $preExcludeFields = ['id', 'before', 'after', 'memo', 'admin_id', 'create_time'];
|
||||
protected array|string $quickSearchField = [
|
||||
'userAsset.playx_user_id',
|
||||
'userAsset.username',
|
||||
'userAsset.phone',
|
||||
];
|
||||
protected bool $autoFillAdminId = true;
|
||||
|
||||
protected function initController(Request $request): ?Response
|
||||
{
|
||||
$this->model = new MallPointsLog();
|
||||
return null;
|
||||
}
|
||||
|
||||
public function add(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
if ($request->method() === 'POST') {
|
||||
return $this->_add();
|
||||
}
|
||||
|
||||
$userAssetId = $request->get('userAssetId', $request->post('userAssetId', 0));
|
||||
$userAsset = MallUserAsset::where('id', $userAssetId)->find();
|
||||
if (!$userAsset) {
|
||||
return $this->error(__('User asset not found'));
|
||||
}
|
||||
|
||||
return $this->success('', ['userAsset' => $userAsset]);
|
||||
}
|
||||
|
||||
public function edit(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->error(__('Points log cannot be modified'));
|
||||
}
|
||||
|
||||
public function del(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->error(__('Points log cannot be deleted'));
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,14 @@ class UserAsset extends Backend
|
||||
*/
|
||||
protected ?object $model = null;
|
||||
|
||||
protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];
|
||||
protected array|string $preExcludeFields = [
|
||||
'id',
|
||||
'locked_points',
|
||||
'available_points',
|
||||
'today_claimed',
|
||||
'create_time',
|
||||
'update_time',
|
||||
];
|
||||
|
||||
protected string|array $quickSearchField = [
|
||||
'playx_user_id',
|
||||
|
||||
13
app/admin/lang/en/mall/pointslog.php
Normal file
13
app/admin/lang/en/mall/pointslog.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'User asset' => 'User asset',
|
||||
'Points adjustment' => 'Points adjustment',
|
||||
'User asset not found' => 'User asset not found',
|
||||
'Points adjustment must be a non-zero integer' => 'Points adjustment must be a non-zero integer',
|
||||
'Insufficient available points' => 'Insufficient available points',
|
||||
'Administrator added %s points' => 'Administrator added %s points',
|
||||
'Administrator deducted %s points' => 'Administrator deducted %s points',
|
||||
'Points log cannot be modified' => 'Points log cannot be modified',
|
||||
'Points log cannot be deleted' => 'Points log cannot be deleted',
|
||||
];
|
||||
13
app/admin/lang/zh-cn/mall/pointslog.php
Normal file
13
app/admin/lang/zh-cn/mall/pointslog.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'User asset' => '用户资产',
|
||||
'Points adjustment' => '调整积分',
|
||||
'User asset not found' => '用户资产不存在',
|
||||
'Points adjustment must be a non-zero integer' => '调整积分必须为非零整数',
|
||||
'Insufficient available points' => '可用积分不足',
|
||||
'Administrator added %s points' => '管理员增加积分 %s',
|
||||
'Administrator deducted %s points' => '管理员扣除积分 %s',
|
||||
'Points log cannot be modified' => '积分流水不允许修改',
|
||||
'Points log cannot be deleted' => '积分流水不允许删除',
|
||||
];
|
||||
61
app/admin/model/MallPointsLog.php
Normal file
61
app/admin/model/MallPointsLog.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\model;
|
||||
|
||||
use app\common\model\MallUserAsset;
|
||||
use think\model\relation\BelongsTo;
|
||||
|
||||
class MallPointsLog extends \app\common\model\MallPointsLog
|
||||
{
|
||||
public static function onBeforeInsert($model): void
|
||||
{
|
||||
$userAssetId = filter_var($model->user_asset_id, FILTER_VALIDATE_INT);
|
||||
if ($userAssetId === false || $userAssetId <= 0) {
|
||||
throw new \InvalidArgumentException(__('User asset not found'));
|
||||
}
|
||||
|
||||
$scoreValue = filter_var($model->score_value, FILTER_VALIDATE_INT);
|
||||
if ($scoreValue === false || $scoreValue === 0) {
|
||||
throw new \InvalidArgumentException(__('Points adjustment must be a non-zero integer'));
|
||||
}
|
||||
|
||||
$userAsset = MallUserAsset::where('id', $userAssetId)->lock(true)->find();
|
||||
if (!$userAsset) {
|
||||
throw new \RuntimeException(__('User asset not found'));
|
||||
}
|
||||
|
||||
$before = $userAsset->available_points;
|
||||
$after = $before + $scoreValue;
|
||||
if ($after < 0) {
|
||||
throw new \RuntimeException(__('Insufficient available points'));
|
||||
}
|
||||
|
||||
$model->user_asset_id = $userAssetId;
|
||||
$model->score_value = $scoreValue;
|
||||
$model->before = $before;
|
||||
$model->after = $after;
|
||||
$model->memo = $scoreValue > 0
|
||||
? __('Administrator added %s points', [abs($scoreValue)])
|
||||
: __('Administrator deducted %s points', [abs($scoreValue)]);
|
||||
|
||||
$userAsset->available_points = $after;
|
||||
$userAsset->save();
|
||||
}
|
||||
|
||||
public static function onBeforeDelete(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public function userAsset(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(MallUserAsset::class, 'user_asset_id');
|
||||
}
|
||||
|
||||
public function admin(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Admin::class, 'admin_id');
|
||||
}
|
||||
}
|
||||
30
app/admin/validate/MallPointsLog.php
Normal file
30
app/admin/validate/MallPointsLog.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class MallPointsLog extends Validate
|
||||
{
|
||||
protected $failException = true;
|
||||
|
||||
protected $rule = [
|
||||
'user_asset_id' => 'require|integer|gt:0',
|
||||
'score_value' => 'require|integer|notIn:0',
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'add' => ['user_asset_id', 'score_value'],
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->field = [
|
||||
'user_asset_id' => __('User asset'),
|
||||
'score_value' => __('Points adjustment'),
|
||||
];
|
||||
parent::__construct();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user