Skip to content

Commit 8564b24

Browse files
committed
update 拆分模型层 方法层
1 parent b743d19 commit 8564b24

27 files changed

+327
-260
lines changed

app/ApiJson/ApiJson.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(protected RequestInterface $request, protected strin
1515

1616
public function Query(): array
1717
{
18-
$parse = new Parse($this->request->getBody()->getContents(), $this->method, $this->request->input('tag', ''));
18+
$parse = new Parse(json_decode($this->request->getBody()->getContents(), true), $this->method, $this->request->input('tag', ''));
1919
return array_merge([
2020
'code' => ResponseCode::SUCCESS,
2121
'msg' => ResponseCode::getMessage(ResponseCode::SUCCESS)

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace App\ApiJson\Entity;
44

55
use App\ApiJson\Interface\QueryInterface;
6-
use App\ApiJson\Handle\AbstractMethodHandle;
6+
use App\ApiJson\Handle\AbstractHandle;
77
use App\ApiJson\Handle\FunctionColumnHandle;
88
use App\ApiJson\Handle\FunctionGroupHandle;
99
use App\ApiJson\Handle\FunctionHavingHandle;
@@ -21,7 +21,7 @@ class ConditionEntity
2121
{
2222
/**
2323
* 匹配规则 根据从上自下优先先匹先出
24-
* @var AbstractMethodHandle[]
24+
* @var AbstractHandle[]
2525
*/
2626
protected array $methodRules = [
2727
FunctionColumnHandle::class,

app/ApiJson/Entity/TableEntity.php

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace App\ApiJson\Entity;
44

5-
use App\ApiJson\Interface\QueryInterface;
6-
75
class TableEntity
86
{
97
/** @var ConditionEntity $ConditionEntity */
@@ -12,71 +10,51 @@ class TableEntity
1210
/** @var string $realTableName 真实表名 */
1311
protected string $realTableName;
1412

15-
/** @var array $condition */
16-
protected array $condition;
17-
18-
/** @var QueryInterface $query */
19-
protected QueryInterface $query;
13+
/** @var array $content 表名对应的数据 */
14+
protected array $content;
2015

2116
/**
2217
* @param string $tableName 表名
23-
* @param array $jsonContent json数据
18+
* @param array $jsonContent json源数据
2419
*/
2520
public function __construct(protected string $tableName, protected array $jsonContent)
2621
{
27-
$this->realTableName = $tableName;
28-
$this->condition = $this->getConditionByContent();
29-
}
30-
31-
public function getResult(): array
32-
{
33-
$this->buildQuery();
22+
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
23+
$this->realTableName = $sanitizeTableName;
24+
$this->content = $this->getContentByTableName();
3425
$this->parseConditionEntity();
35-
return $this->formatResult($this->query->all());
3626
}
3727

38-
public function getCount(): int
28+
public function getTableName(): string
3929
{
40-
$this->buildQuery();
41-
$this->parseConditionEntity();
42-
return $this->query->count();
30+
return $this->tableName;
4331
}
4432

45-
public function insert()
33+
public function getRealTableName(): string
4634
{
47-
35+
return $this->realTableName;
4836
}
4937

50-
public function update()
38+
public function getContent(): array
5139
{
52-
40+
return $this->content;
5341
}
5442

55-
protected function formatResult(array $result): array
43+
public function getConditionEntity(): ConditionEntity
5644
{
57-
return $result;
45+
return $this->conditionEntity;
5846
}
5947

60-
protected function buildQuery()
48+
protected function getContentByTableName(): array
6149
{
62-
$this->query = new (config(join('.', [
63-
'dependencies', QueryInterface::class
64-
])))($this->realTableName);
50+
$content = $this->jsonContent[$this->tableName];
51+
if (isset($content[$this->realTableName])) $content = $content[$this->realTableName];
52+
return $content;
6553
}
6654

6755
protected function parseConditionEntity()
6856
{
69-
$entity = new ConditionEntity($this->condition);
57+
$entity = new ConditionEntity($this->content);
7058
$this->conditionEntity = $entity;
7159
}
72-
73-
protected function getConditionByContent(): array
74-
{
75-
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
76-
if (isset($this->jsonContent[$sanitizeTableName])) {
77-
$this->realTableName = $sanitizeTableName;
78-
return $this->jsonContent[$sanitizeTableName];
79-
}
80-
return $this->jsonContent[$this->tableName];
81-
}
8260
}

app/ApiJson/Handle/AbstractMethodHandle.php renamed to app/ApiJson/Handle/AbstractHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use App\ApiJson\Interface\QueryInterface;
66

7-
abstract class AbstractMethodHandle
7+
abstract class AbstractHandle
88
{
99
/** @var string 清洗后的查询key */
1010
protected string $sanitizeKey;

app/ApiJson/Handle/FunctionColumnHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class FunctionColumnHandle extends AbstractMethodHandle
5+
class FunctionColumnHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/FunctionGroupHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class FunctionGroupHandle extends AbstractMethodHandle
5+
class FunctionGroupHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/FunctionHavingHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class FunctionHavingHandle extends AbstractMethodHandle
5+
class FunctionHavingHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/FunctionOrderHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class FunctionOrderHandle extends AbstractMethodHandle
5+
class FunctionOrderHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/WhereBetweenHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereBetweenHandle extends AbstractMethodHandle
5+
class WhereBetweenHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/WhereExistsHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Hyperf\Database\Query\Builder;
66

7-
class WhereExistsHandle extends AbstractMethodHandle
7+
class WhereExistsHandle extends AbstractHandle
88
{
99
protected function validateCondition(): bool
1010
{

app/ApiJson/Handle/WhereHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereHandle extends AbstractMethodHandle
5+
class WhereHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/WhereInHandle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereInHandle extends AbstractMethodHandle
5+
class WhereInHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{
9-
return str_ends_with($this->key, '{}') && is_array($this->validateCondition());
9+
return str_ends_with($this->key, '{}') && is_array($this->value);
1010
}
1111

1212
protected function buildModel()

app/ApiJson/Handle/WhereJsonContainsHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereJsonContainsHandle extends AbstractMethodHandle
5+
class WhereJsonContainsHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/WhereLikeHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereLikeHandle extends AbstractMethodHandle
5+
class WhereLikeHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/WhereRawHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereRawHandle extends AbstractMethodHandle
5+
class WhereRawHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Handle/WhereRegexpHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace App\ApiJson\Handle;
44

5-
class WhereRegexpHandle extends AbstractMethodHandle
5+
class WhereRegexpHandle extends AbstractHandle
66
{
77
protected function validateCondition(): bool
88
{

app/ApiJson/Interface/QueryInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface QueryInterface
88
{
99
public function __construct(string $tableName);
1010

11+
public function setPrimaryKey(string $primaryKey): void;
12+
13+
public function getPrimaryKey(): string;
14+
1115
public function where(string $column, $operator = null, $value = null, string $boolean = 'and'): self;
1216

1317
public function whereIn(string $column, array $values, string $boolean = 'and', $not = false): self;
@@ -32,6 +36,8 @@ public function insertGetId(array $values, $sequence = null): int;
3236

3337
public function update(array $values): bool;
3438

39+
public function delete($id = null): bool;
40+
3541
public function all();
3642

3743
public function getDb();

app/ApiJson/Method/AbstractMethod.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
use App\ApiJson\Entity\TableEntity;
6+
use App\ApiJson\Interface\QueryInterface;
7+
use App\Constants\ResponseCode;
8+
use Hyperf\Contract\ConfigInterface;
9+
use Hyperf\Utils\ApplicationContext;
10+
11+
abstract class AbstractMethod
12+
{
13+
/** @var QueryInterface $query */
14+
protected QueryInterface $query;
15+
16+
public function __construct(protected TableEntity $tableEntity, protected string $method = 'GET')
17+
{
18+
$this->buildQuery();
19+
}
20+
21+
public function handle(): ?array
22+
{
23+
if (!$this->validateCondition()) return null;
24+
return $this->process();
25+
}
26+
27+
protected function buildQuery()
28+
{
29+
30+
$this->query = new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($this->tableEntity->getRealTableName());
31+
}
32+
33+
protected function parseManyResponse(array $ids, bool $isQueryMany = false): array
34+
{
35+
$response = [
36+
'code' => !empty($ids) ? ResponseCode::SUCCESS : ResponseCode::SERVER_ERROR,
37+
'msg' => ResponseCode::getMessage(!empty($ids) ? ResponseCode::SUCCESS : ResponseCode::SERVER_ERROR),
38+
];
39+
if ($isQueryMany) {
40+
$response = array_merge($response, [
41+
'id[]' => $ids,
42+
'count' => count($ids)
43+
]);
44+
} else {
45+
$response['id'] = current($ids) ?: 0;
46+
}
47+
return $response;
48+
}
49+
50+
protected function isQueryMany(): bool
51+
{
52+
return str_ends_with($this->tableEntity->getTableName(), '[]');
53+
}
54+
55+
abstract protected function validateCondition(): bool;
56+
57+
abstract protected function process();
58+
}

app/ApiJson/Method/DeleteMethod.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\ApiJson\Method;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
use Hyperf\Utils\Arr;
7+
8+
class DeleteMethod extends AbstractMethod
9+
{
10+
protected function validateCondition(): bool
11+
{
12+
return $this->method == 'DELETE';
13+
}
14+
15+
protected function process()
16+
{
17+
$ids = [];
18+
19+
$jsonData = $this->tableEntity->getContent();
20+
$queryMany = $this->isQueryMany();
21+
if (isset($jsonData['id'])) {
22+
if (is_array($jsonData['id'])) {
23+
$ids = $jsonData['id'];
24+
$queryMany = true;
25+
} else {
26+
$ids = [$jsonData['id']];
27+
}
28+
} else if (isset($jsonData['id{}'])) {
29+
$ids = $jsonData['id{}']; //得到本次需要删除的ID
30+
$queryMany = true;
31+
}
32+
$deletedIds = [];
33+
foreach ($ids as $id) {
34+
$this->buildQuery();
35+
$this->query->delete($id) && $deletedIds[] = $id; //这里主键应可配置
36+
}
37+
return $this->parseManyResponse($deletedIds, $queryMany);
38+
}
39+
}

0 commit comments

Comments
 (0)