<?php
namespace App\Controllers\Admin;
use App\Controllers\AdminController;
use App\Models\Bought;
use App\Models\Shop;
use App\Services\AdminDataMasker;
use App\Services\AdminPermission;
use App\Utils\DatatablesHelper;
use App\Utils\SafeDatatables as Datatables;
class ShopController extends AdminController
{
public function index($request, $response, $args)
{
$columns = ['op' => '操作', 'id' => 'ID', 'name' => '套餐名称', 'host_id' => '宿主机 ID',
'cpu_cores' => 'CPU 核数', 'memory_mib' => '内存 MiB', 'bandwidth_mbps' => '带宽 Mbps',
'disk_gib' => '磁盘 GiB', 'price' => '价格', 'status' => '状态', 'stock' => '库存', 'sort' => '排序'];
return $this->view()->assign('table_config', [
'total_column' => $columns, 'default_show_column' => array_keys($columns), 'ajax_url' => 'shop/ajax',
])->display('admin/shop/index.tpl');
}
public function create($request, $response, $args)
{
return $this->view()->display('admin/shop/create.tpl');
}
public function add($request, $response, $args)
{
return $this->savePlan($request, $response, new Shop());
}
public function edit($request, $response, $args)
{
$shop = Shop::find((int) $args['id']);
if ($shop === null) {
return $response->withStatus(404);
}
return $this->view()->assign('shop', $shop)->display('admin/shop/edit.tpl');
}
public function update($request, $response, $args)
{
$shop = Shop::find((int) $args['id']);
if ($shop === null) {
return $response->withJson(['ret' => 0, 'msg' => '套餐不存在'], 404);
}
return $this->savePlan($request, $response, $shop);
}
private function savePlan($request, $response, Shop $shop)
{
if (!AdminPermission::isSuperAdmin()) {
return $response->withJson(['ret' => 0, 'msg' => '只有管理员可以修改套餐'], 403);
}
try {
$input = [];
foreach (['name', 'host_id', 'price', 'cpu_cores', 'memory_mib', 'bandwidth_mbps', 'disk_gib', 'status', 'stock', 'sort'] as $field) {
$default = $shop->exists ? $shop->$field : null;
$input[$field] = $request->getParam($field, $default);
}
$values = Shop::validatePlan($input);
if ($values['host_id'] !== null & & !\App\Models\CloudHost::where('id', $values['host_id'])->where('enabled', 1)->exists()) {
throw new \InvalidArgumentException('宿主机不存在或已禁用');
}
foreach ($values as $field => $value) {
$shop->$field = $value;
}
if (!$shop->save()) {
throw new \RuntimeException('套餐保存失败');
}
} catch (\InvalidArgumentException $exception) {
return $response->withJson(['ret' => 0, 'msg' => $exception->getMessage()], 422);
} catch (\Throwable $exception) {
error_log('Plan save failed: ' . get_class($exception));
return $response->withJson(['ret' => 0, 'msg' => '套餐保存失败'], 500);
}
return $response->withJson(['ret' => 1, 'msg' => '套餐已保存']);
}
public function deleteGet($request, $response, $args)
{
if (!AdminPermission::isSuperAdmin()) {
return $response->withJson(['ret' => 0, 'msg' => '只有管理员可以下架套餐'], 403);
}
$shop = Shop::find((int) $request->getParam('id', 0));
if ($shop === null) {
return $response->withJson(['ret' => 0, 'msg' => '套餐不存在'], 404);
}
$shop->status = 0;
$shop->save();
return $response->withJson(['ret' => 1, 'msg' => '套餐已下架']);
}
public function ajax_shop($request, $response, $args)
{
$table = new Datatables(new DatatablesHelper());
$table->query('SELECT id AS op,id,name,host_id,cpu_cores,memory_mib,bandwidth_mbps,disk_gib,price,status,stock,sort FROM shop');
$table->edit('op', static function ($data) {
if (!AdminPermission::isSuperAdmin()) {
return '< span class = "text-muted" > 只读< / span > ';
}
$id = (int) $data['id'];
return '< a class = "btn btn-brand" href = "/admin/shop/' . $id . '/edit" > 编辑< / a > ';
});
$table->edit('name', static function ($data) {
return htmlspecialchars((string) $data['name'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
});
$table->edit('status', static function ($data) {
return (int) $data['status'] === 1 ? '上架' : '下架';
});
$response->getBody()->write($table->generate());
return $response;
}
public function bought($request, $response, $args)
{
$columns = ['op' => '操作', 'id' => 'ID', 'content' => '套餐',
'price' => '成交金额', 'user_id' => '用户 ID', 'user_name' => '用户名', 'datetime' => '购买时间'];
return $this->view()->assign('table_config', [
'total_column' => $columns, 'default_show_column' => array_keys($columns), 'ajax_url' => 'bought/ajax',
])->display('admin/shop/bought.tpl');
}
public function deleteBoughtGet($request, $response, $args)
{
return $response->withJson(['ret' => 0, 'msg' => '虚拟化服务尚未接入,暂不能变更实例订单'], 409);
}
public function ajax_bought($request, $response, $args)
{
$table = new Datatables(new DatatablesHelper());
$table->query('SELECT bought.id AS op,bought.id,shop.name AS content,bought.price,user.id AS user_id,user.user_name,bought.datetime FROM bought LEFT JOIN user ON bought.userid=user.id LEFT JOIN shop ON bought.shopid=shop.id');
$table->edit('op', static function ($data) {
return '< span class = "text-muted" > 历史记录< / span > ';
});
$table->edit('user_name', static function ($data) {
return AdminDataMasker::account($data['user_name'] ?? '');
});
$table->edit('content', static function ($data) {
return htmlspecialchars((string) ($data['content'] ?? '套餐已删除'), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
});
$table->edit('datetime', static function ($data) {
return date('Y-m-d H:i:s', (int) $data['datetime']);
});
$response->getBody()->write($table->generate());
return $response;
}
}