[色子游戏]玩家-新增玩家钱包操作
This commit is contained in:
@@ -8,8 +8,8 @@ namespace app\dice\logic\player_wallet_record;
|
||||
|
||||
use plugin\saiadmin\basic\think\BaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use app\dice\model\player_wallet_record\DicePlayerWalletRecord;
|
||||
use app\dice\model\player\DicePlayer;
|
||||
|
||||
/**
|
||||
* 玩家钱包流水逻辑层
|
||||
@@ -35,4 +35,57 @@ class DicePlayerWalletRecordLogic extends BaseLogic
|
||||
return parent::add($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员钱包操作(加点/扣点):更新玩家平台币并创建流水记录
|
||||
* @param array $data player_id, type (3=加点 4=扣点), coin (正数), remark (可选)
|
||||
* @param int $adminId 当前管理员 id
|
||||
* @return mixed
|
||||
* @throws ApiException
|
||||
*/
|
||||
public function adminOperate(array $data, int $adminId): mixed
|
||||
{
|
||||
$playerId = (int) ($data['player_id'] ?? 0);
|
||||
$type = (int) ($data['type'] ?? 0);
|
||||
$coin = (float) ($data['coin'] ?? 0);
|
||||
|
||||
if ($playerId <= 0 || !in_array($type, [3, 4], true)) {
|
||||
throw new ApiException('参数错误:需要有效的 player_id 和 type(3=加点,4=扣点)');
|
||||
}
|
||||
if ($coin <= 0) {
|
||||
throw new ApiException('平台币变动必须大于 0');
|
||||
}
|
||||
|
||||
$player = DicePlayer::where('id', $playerId)->find();
|
||||
if (!$player) {
|
||||
throw new ApiException('玩家不存在');
|
||||
}
|
||||
|
||||
$walletBefore = (float) ($player['coin'] ?? 0);
|
||||
if ($type === 4 && $walletBefore < $coin) {
|
||||
throw new ApiException('扣点数量不能大于当前余额');
|
||||
}
|
||||
|
||||
$walletAfter = $type === 3 ? $walletBefore + $coin : $walletBefore - $coin;
|
||||
$remark = trim((string) ($data['remark'] ?? ''));
|
||||
if ($remark === '') {
|
||||
$remark = $type === 3 ? '管理员加点' : '管理员扣点';
|
||||
}
|
||||
|
||||
DicePlayer::where('id', $playerId)->update(['coin' => $walletAfter]);
|
||||
|
||||
$record = [
|
||||
'player_id' => $playerId,
|
||||
'coin' => $type === 3 ? $coin : -$coin,
|
||||
'type' => $type,
|
||||
'wallet_before' => $walletBefore,
|
||||
'wallet_after' => $walletAfter,
|
||||
'remark' => $remark,
|
||||
'user_id' => $adminId,
|
||||
'total_draw_count' => 0,
|
||||
'paid_draw_count' => 0,
|
||||
'free_draw_count' => 0,
|
||||
];
|
||||
|
||||
return $this->model->create($record);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user