Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
services
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tencent
services
Commits
8caaa4a0
Commit
8caaa4a0
authored
Oct 16, 2019
by
xmy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:模版生成
parent
9d38a381
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
246 additions
and
102 deletions
+246
-102
generate/GenData.php
+25
-21
generate/template/controller.stub
+115
-0
generate/template/data.stub
+36
-37
generate/template/logic.stub
+70
-44
No files found.
generate/GenData.php
View file @
8caaa4a0
...
...
@@ -2,51 +2,55 @@
namespace
hdll\services\generate
;
use
mysql_xdevapi\Exception
;
use
Swoft\Bean\Annotation\Bean
;
use
Swoft\Bean\Annotation\Inject
;
use
Swoft\Db\Db
;
use
Swoft\Db\Query
;
use
Swoft\Devtool\FileGenerator
;
use
Swoft\Devtool\Model\Data\SchemaData
;
/**
* @Bean()
* Class GenData
* @package hdll\services\generate
*/
class
GenData
{
/**
* @Inject()
* @var SchemaData
*/
private
$data
;
public
function
ge
nerate
(
$param
)
public
function
ge
tColumns
(
$table
)
{
list
(
$db
,
$inc
,
$exc
,
$path
,
$driver
,
$tablePrefix
,
$fieldPrefix
,
$tplFile
,
$tplDir
)
=
$params
;
$tableSchemas
=
$this
->
data
->
getSchemaTableData
(
$driver
,
$db
,
$inc
,
$exc
,
$tablePrefix
);
foreach
(
$tableSchemas
as
$tableSchema
)
{
$this
->
generateClass
(
$driver
,
$db
,
$tableSchema
,
$fieldPrefix
,
$path
,
$tplFile
,
$tplDir
);
}
$columns
=
Db
::
query
(
"describe
$table
"
)
->
getResult
();
return
array_column
(
$columns
,
'Field'
);
}
public
function
generate
Data
(
$driver
,
$db
,
$tableSchema
,
$fieldPrefix
,
$path
,
$tplFile
,
$tplDir
)
public
function
generate
(
$params
)
{
$mappingClass
=
$tableSchema
[
'mapping'
];
$config
=
[
list
(
$db
,
$table
,
$path
,
$tplFile
,
$tplDir
)
=
$params
;
$this
->
generateData
(
$table
,
$path
,
$tplFile
,
$tplDir
);
}
public
function
generateData
(
$table
,
$path
,
$tplFile
,
$tplDir
)
{
$config
=
[
'tplFilename'
=>
$tplFile
,
'tplDir'
=>
$tplDir
,
'className'
=>
$mappingClass
,
'tplDir'
=>
$tplDir
,
'className'
=>
$table
,
];
$file
=
alias
(
$path
);
$file
.=
sprintf
(
'/%s
.php'
,
$mappingClass
);
$file
.=
sprintf
(
'/%s
%s.php'
,
ucfirst
(
$table
),
$tplFile
);
$columnSchemas
=
$this
->
data
->
getSchemaColumnsData
(
$driver
,
$db
,
$tableSchema
[
'name'
],
$fieldPrefix
);
$columnSchemas
=
$this
->
getColumns
(
$table
);
$data
=
[
'tableName'
=>
$tableSchema
[
'name'
],
'namespace'
=>
'App\\Models\\Data'
,
'className'
=>
ucfirst
(
$table
),
'tableName'
=>
$table
,
'namespace'
=>
"App
\\
Models
\\
$tplFile
"
,
'columns'
=>
$columnSchemas
];
$gen
=
new
FileGenerator
(
$config
);
$gen
=
new
FileGenerator
(
$config
);
$gen
->
getParser
()
->
setOpenCloseTagChars
(
"
{
{","}
}
"
);
$gen
->
renderas
(
$file
,
$data
);
}
...
...
generate/template/controller.stub
0 → 100644
View file @
8caaa4a0
<?php
namespace App\Controllers\Api;
use App\Middlewares\UserMiddleware;
use App\Models\Logic\{{= className}}Logic;
use App\Models\Validate\{{= className}}Validate;
use Hdll\Services\Auth\Enum\AuthEnum;
use Hdll\Services\Common\Bean\Annotation\Auth;
use Hdll\Services\Common\Controller\CommonController;
use Hdll\Services\Common\Entity\User;
use Swoft\Bean\Annotation\Inject;
use Swoft\Http\Message\Bean\Annotation\Middleware;
use Swoft\Http\Message\Bean\Annotation\Middlewares;
use Swoft\Http\Server\Bean\Annotation\Controller;
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
use Swoft\Http\Server\Bean\Annotation\RequestMethod;
/**
* @Controller(prefix="/v1/lottery")
* @Auth(scope=AuthEnum::SCOPE_BUYER)
* @Middlewares({
* @Middleware(UserMiddleware::class)
* })
* Class {{= className}}Controller
* @package App\Controllers\Api
*/
class {{= className}}Controller extends CommonController
{
/**
* @Inject()
* @var {{= className}}Logic
*/
private ${{= tableName}};
/**
* @Inject()
* @var User
*/
private $user;
/**
* @Inject()
* @var {{= className}}Validate
*/
private $validate;
/**
* @Auth(scope=AuthEnum::SCOPE_BUYER, option=">=")
* @RequestMapping(route="info", method=RequestMethod::GET)
* @return array
*/
public function list()
{
$type = request()->input('type');
$page = request()->input('page', 1);
$limit = request()->input('limit', 15);
$data = $this->{{= tableName}}->list($this->user->getStoreId(), $type, $page, $limit);
return $this->success($data);
}
/**
* @Auth(scope=AuthEnum::SCOPE_SELLER)
* @RequestMapping(route="info", method=RequestMethod::POST)
* @return array
* @throws \App\Exception\BaseException
*/
public function save()
{
$param = request()->json();
$this->validate->check($param);
$data = $this->{{= tableName}}->save($this->user->getStoreId(), $param);
return $this->success($data);
}
/**
* @Auth(scope=AuthEnum::SCOPE_SELLER)
* @RequestMapping(route="info", method=RequestMethod::PUT)
* @return array
* @throws \App\Exception\BaseException
*/
public function update()
{
$param = request()->json();
$this->validate->check($param);
$data = $this->{{= tableName}}->update($this->user->getStoreId(), $param);
return $this->success($data);
}
/**
* @Auth(scope=AuthEnum::SCOPE_SELLER)
* @RequestMapping(route="info/{id}", method=RequestMethod::DELETE)
* @param $id
* @return array
*/
public function del($id)
{
$data = $this->{{= tableName}}->del($id, $this->user->getStoreId());
return $this->success($data);
}
/**
* @RequestMapping(route="info/{id}", method=RequestMethod::GET)
* @param $id
* @return array
* @throws \App\Exception\BaseException
*/
public function get($id)
{
$data = $this->{{= tableName}}->get($id, $this->user->getStoreId());
return $this->success($data);
}
}
generate/template/data.stub
View file @
8caaa4a0
<?php
namespace {
= namespace
};
namespace {
{= namespace}
};
use App\Common\Db\CQuery;
use Swoft\Bean\Annotation\Bean;
use App\Models\Entity\{= tableName};
{= usespace}
use App\Models\Entity\{{= className}};
/**
* @Bean()
* Class {
= tableName
}Data
* Class {
{= className}
}Data
* @package App\Models\Data
*/
class {
= tableName
}Data
class {
{= className}
}Data
{
public function get($condition, $fields = ['*'])
{
$condition['deleteTime'] = 0;
$data = {= tableName
}::findOne($condition, ['fields' => $fields])->getResult();
return $data ? $data->toArray() : [];
}
public function save($data)
{
$image = new {= tableName
}();
return $image->fill($data)->save()->getResult();
}
public function update($id, $data)
{
$data['updateTime'] = time();
return {= tableName
}::updateOne($data, ['id' => $id])->getResult();
}
public function del($condition){
return {= tableName
}::updateOne(['deleteTime'=>time()],$condition)->getResult();
}
public function list($condition, $fields = ['*'], $page = 1, $limit = 15)
{
$data = CQuery::table({= tableName
}::tableName())->condition($condition)->limit($page, $limit)->orderBy('id', 'DESC')->get($fields)->getResult();
return $data ?? [];
}
public function all($condition,$fields=['*']){
return CQuery::table({= tableName
}::tableName())->condition($condition)->get($fields)->getResult();
}
public function get($condition, $fields = ['*'])
{
$condition['deleteTime'] = 0;
$data = {{= className}
}::findOne($condition, ['fields' => $fields])->getResult();
return $data ? $data->toArray() : [];
}
public function save($data)
{
$image = new {{= className}
}();
return $image->fill($data)->save()->getResult();
}
public function update($id, $data)
{
$data['updateTime'] = time();
return {{= className}
}::updateOne($data, ['id' => $id])->getResult();
}
public function del($condition){
return {{= className}
}::updateOne(['deleteTime'=>time()],$condition)->getResult();
}
public function list($condition, $fields = ['*'], $page = 1, $limit = 15)
{
$data = CQuery::table({{= className}
}::tableName())->condition($condition)->limit($page, $limit)->orderBy('id', 'DESC')->get($fields)->getResult();
return $data ?? [];
}
public function all($condition,$fields=['*']){
return CQuery::table({{= className}
}::tableName())->condition($condition)->get($fields)->getResult();
}
}
generate/template/logic.stub
View file @
8caaa4a0
<?php
namespace {
= namespace
};
namespace {
{= namespace}
};
use App\Exception\BaseException;
use App\Exception\EnumException;
use Swoft\Bean\Annotation\Bean;
use
App\Models\Data\{= tableName}Data
;
{= usespace}
use
Swoft\Bean\Annotation\Inject
;
use App\Models\Data\{{= className}}Data;
/**
* @Bean()
* Class {
= tableName
}Logic
* Class {
{= className}
}Logic
* @package App\Models\Logic
*/
class {
= tableName
}Logic
class {
{= className}
}Logic
{
/**
* @Inject()
* @var ${= tableName}Data
*/
private ${= tableName};
public function get($id,$storeId)
{
$data = $this->${= tableName}->get(['id' => $id, 'storeId' => $storeId]);
if (empty($data)) {
throw new BaseException(EnumException::E101);
/**
* @Inject()
* @var {{= className}}Data
*/
private ${{= tableName}};
public function get($id,$storeId)
{
$data = $this->{{= tableName}}->get(['id' => $id, 'storeId' => $storeId]);
return $data;
}
public function del($id,$storeId){
$data = $this->{{= tableName}}->del(['id' => $id, 'storeId' => $storeId]);
if (empty($data)) {
throw new BaseException(EnumException::E004);
}
return $data;
}
public function list($storeId, $type, $page, $limit)
{
$condition['storeId'] = $storeId;
$list = $this->{{= tableName}}->list($condition, ['*'], $page, $limit);
return $list;
}
public function save($storeId,$param)
{
$data = [
{{for v in columns}}
{{if v == "id"}}
{{elseif v == "createTime"}}
{{elseif v == "updateTime"}}
{{elseif v == "deleteTime"}}
{{elseif v == "storeId"}}
'storeId' => $storeId,
{{else}}
'{{= v}}' => $param['{{= v}}'],
{{/if}}
{{/for}}
];
arrFilter($data);
return $this->{{= tableName}}->save($data);
}
public function update($storeId, $param)
{
$data = [
{{for v in columns}}
{{if v == "id"}}
{{elseif v == "storeId"}}
{{elseif v == "createTime"}}
{{elseif v == "updateTime"}}
{{elseif v == "deleteTime"}}
{{else}}
'{{= v}}' => $param['{{= v}}'],
{{/if}}
{{/for}}
];
arrFilter($data);
return $this->{{= tableName}}->update(['id' => $param['id'],'storeId' => $storeId],$data);
}
return $data;
}
public function save($data)
{
$image = new {= tableName}();
return $image->fill($data)->save()->getResult();
}
public function update($id, $data)
{
$data['updateTime'] = time();
return {= tableName}::updateOne($data, ['id' => $id])->getResult();
}
public function del($condition){
return {= tableName}::updateOne(['deleteTime'=>time()],$condition)->getResult();
}
public function list($condition, $fields = ['*'], $page = 1, $limit = 15)
{
$data = CQuery::table({= tableName}::tableName())->condition($condition)->limit($page, $limit)->orderBy('id', 'DESC')->get($fields)->getResult();
return $data ?? [];
}
public function all($condition,$fields=['*']){
return CQuery::table({= tableName}::tableName())->condition($condition)->get($fields)->getResult();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment