|
@@ -7,6 +7,7 @@ use App\Tools\Result;
|
|
use App\Model\GlobalTable;
|
|
use App\Model\GlobalTable;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\DbConnection\Db;
|
|
use App\Model\GlobalTableField;
|
|
use App\Model\GlobalTableField;
|
|
|
|
+use App\Model\GlobalTableFieldType;
|
|
|
|
|
|
#[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
#[RpcService(name: "FormService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
|
class FormService implements FormServiceInterface
|
|
class FormService implements FormServiceInterface
|
|
@@ -61,13 +62,20 @@ class FormService implements FormServiceInterface
|
|
try {
|
|
try {
|
|
// 构建查询
|
|
// 构建查询
|
|
$query = GlobalTable::query()
|
|
$query = GlobalTable::query()
|
|
- ->when(!empty($data['website_id']), fn($q) => $q->where('website_id', $data['website_id']))
|
|
|
|
- ->when(!empty($data['name']), fn($q) => $q->where('name', 'like', '%' . $data['name'] . '%'));
|
|
|
|
|
|
+ ->when(!empty($data['website_id']), function($q) use ($data) {
|
|
|
|
+ return $q->where('website_id', $data['website_id']);
|
|
|
|
+ })
|
|
|
|
+ ->when(!empty($data['name']), function($q) use ($data) {
|
|
|
|
+ return $q->where('name', 'like', '%' . $data['name'] . '%');
|
|
|
|
+ });
|
|
|
|
|
|
// 分页参数
|
|
// 分页参数
|
|
$page = (int)($data['page'] ?? 1);
|
|
$page = (int)($data['page'] ?? 1);
|
|
$pageSize = (int)($data['pageSize'] ?? 10);
|
|
$pageSize = (int)($data['pageSize'] ?? 10);
|
|
|
|
|
|
|
|
+ // 先获取总数
|
|
|
|
+ $total = $query->count();
|
|
|
|
+
|
|
// 获取数据
|
|
// 获取数据
|
|
$list = $query->orderBy('updated_at', 'desc')
|
|
$list = $query->orderBy('updated_at', 'desc')
|
|
->offset(($page - 1) * $pageSize)
|
|
->offset(($page - 1) * $pageSize)
|
|
@@ -78,7 +86,7 @@ class FormService implements FormServiceInterface
|
|
if ($list->isEmpty()) {
|
|
if ($list->isEmpty()) {
|
|
return Result::success([
|
|
return Result::success([
|
|
'list' => [],
|
|
'list' => [],
|
|
- 'total' => 0,
|
|
|
|
|
|
+ 'total' => $total,
|
|
'page' => $page,
|
|
'page' => $page,
|
|
'pageSize' => $pageSize
|
|
'pageSize' => $pageSize
|
|
]);
|
|
]);
|
|
@@ -96,7 +104,7 @@ class FormService implements FormServiceInterface
|
|
$item->website_name = $websites[$item->website_id] ?? '';
|
|
$item->website_name = $websites[$item->website_id] ?? '';
|
|
return $item;
|
|
return $item;
|
|
}),
|
|
}),
|
|
- 'total' => $query->count(),
|
|
|
|
|
|
+ 'total' => $total,
|
|
'page' => $page,
|
|
'page' => $page,
|
|
'pageSize' => $pageSize
|
|
'pageSize' => $pageSize
|
|
]);
|
|
]);
|
|
@@ -122,7 +130,6 @@ class FormService implements FormServiceInterface
|
|
*/
|
|
*/
|
|
public function upGlobalTable(array $data): array
|
|
public function upGlobalTable(array $data): array
|
|
{
|
|
{
|
|
- var_dump($data);
|
|
|
|
$globalTable = GlobalTable::where('id',$data['id'])->first();
|
|
$globalTable = GlobalTable::where('id',$data['id'])->first();
|
|
if(empty($globalTable)){
|
|
if(empty($globalTable)){
|
|
return Result::error('表单不存在');
|
|
return Result::error('表单不存在');
|
|
@@ -158,7 +165,7 @@ class FormService implements FormServiceInterface
|
|
*/
|
|
*/
|
|
public function getGlobalTableFieldList(array $data): array
|
|
public function getGlobalTableFieldList(array $data): array
|
|
{
|
|
{
|
|
- $globalTableField = GlobalTableField::where('table_id',$data['table_id'])->orderBy("sort","asc")->get();
|
|
|
|
|
|
+ $globalTableField = GlobalTableField::where('table_id',$data['id'])->orderBy("sort","asc")->get();
|
|
return Result::success($globalTableField);
|
|
return Result::success($globalTableField);
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
@@ -188,8 +195,21 @@ class FormService implements FormServiceInterface
|
|
if(!empty($globalTableField)){
|
|
if(!empty($globalTableField)){
|
|
return Result::error('字段已存在');
|
|
return Result::error('字段已存在');
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // 检查表中是否已存在该列
|
|
|
|
+ try {
|
|
|
|
+ $columns = Db::connection('global')->select("SHOW COLUMNS FROM `{$globalTable->table}`");
|
|
|
|
+ $columnNames = array_column($columns, 'Field');
|
|
|
|
+ if (in_array($data['field_name'], $columnNames)) {
|
|
|
|
+ return Result::error('字段名已存在于数据表中');
|
|
|
|
+ }
|
|
|
|
+ } catch (\Throwable $e) {
|
|
|
|
+ return Result::error('检查字段是否存在时出错:' . $e->getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
|
|
//给global库的表添加字段
|
|
//给global库的表添加字段
|
|
- Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` ADD COLUMN `{$data['field_name']}` {$data['field_type']}({$data['length']})");
|
|
|
|
|
|
+ Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` ADD COLUMN `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
|
|
//给GlobalTableField表添加数据
|
|
//给GlobalTableField表添加数据
|
|
GlobalTableField::create($data);
|
|
GlobalTableField::create($data);
|
|
return Result::success('添加成功');
|
|
return Result::success('添加成功');
|
|
@@ -211,8 +231,9 @@ class FormService implements FormServiceInterface
|
|
if(!empty($globalTableField) && $globalTableField->id != $data['id']){
|
|
if(!empty($globalTableField) && $globalTableField->id != $data['id']){
|
|
return Result::error('字段已存在');
|
|
return Result::error('字段已存在');
|
|
}
|
|
}
|
|
|
|
+ $globalTableFieldTypeInfo = GlobalTableFieldType::where('id',$data['field_type'])->first();
|
|
//给global库的表修改字段
|
|
//给global库的表修改字段
|
|
- Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` CHANGE COLUMN `{$data['field_name']}` {$data['field_type']}({$data['length']})");
|
|
|
|
|
|
+ Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` CHANGE COLUMN `{$data['field_name']}` `{$data['field_name']}` {$globalTableFieldTypeInfo['field_type']}({$data['length']}) COMMENT '{$data['title']}'");
|
|
//给GlobalTableField表修改数据
|
|
//给GlobalTableField表修改数据
|
|
GlobalTableField::where('id',$data['id'])->update($data);
|
|
GlobalTableField::where('id',$data['id'])->update($data);
|
|
return Result::success('修改成功');
|
|
return Result::success('修改成功');
|
|
@@ -225,12 +246,16 @@ class FormService implements FormServiceInterface
|
|
*/
|
|
*/
|
|
public function delGlobalTableField(array $data): array
|
|
public function delGlobalTableField(array $data): array
|
|
{
|
|
{
|
|
- $globalTable = GlobalTable::where('id',$data['table_id'])->first();
|
|
|
|
|
|
+ $tableFieldInfo = GlobalTableField::where('id',$data['id'])->first();
|
|
|
|
+ if(empty($tableFieldInfo)){
|
|
|
|
+ return Result::error('字段不存在');
|
|
|
|
+ }
|
|
|
|
+ $globalTable = GlobalTable::where('id',$tableFieldInfo['table_id'])->first();
|
|
if(empty($globalTable)){
|
|
if(empty($globalTable)){
|
|
return Result::error('表单不存在');
|
|
return Result::error('表单不存在');
|
|
}
|
|
}
|
|
//给global库的表删除字段
|
|
//给global库的表删除字段
|
|
- Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$data['field_name']}`");
|
|
|
|
|
|
+ Db::connection('global')->statement("ALTER TABLE `{$globalTable->table}` DROP COLUMN `{$tableFieldInfo['field_name']}`");
|
|
//给GlobalTableField表删除数据
|
|
//给GlobalTableField表删除数据
|
|
GlobalTableField::where('id',$data['id'])->delete();
|
|
GlobalTableField::where('id',$data['id'])->delete();
|
|
return Result::success('删除成功');
|
|
return Result::success('删除成功');
|