Commit 8caaa4a0 by xmy

feat:模版生成

parent 9d38a381
...@@ -2,51 +2,55 @@ ...@@ -2,51 +2,55 @@
namespace hdll\services\generate; namespace hdll\services\generate;
use mysql_xdevapi\Exception;
use Swoft\Bean\Annotation\Bean; use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Inject; use Swoft\Bean\Annotation\Inject;
use Swoft\Db\Db;
use Swoft\Db\Query;
use Swoft\Devtool\FileGenerator; use Swoft\Devtool\FileGenerator;
use Swoft\Devtool\Model\Data\SchemaData; use Swoft\Devtool\Model\Data\SchemaData;
/** /**
* @Bean()
* Class GenData * Class GenData
* @package hdll\services\generate * @package hdll\services\generate
*/ */
class GenData class GenData
{ {
/**
* @Inject()
* @var SchemaData
*/
private $data;
public function generate($param) public function getColumns($table)
{ {
list($db, $inc, $exc, $path, $driver, $tablePrefix, $fieldPrefix, $tplFile, $tplDir) = $params; $columns = Db::query("describe $table")->getResult();
$tableSchemas = $this->data->getSchemaTableData($driver, $db, $inc, $exc, $tablePrefix); return array_column($columns, 'Field');
foreach ($tableSchemas as $tableSchema) {
$this->generateClass($driver, $db, $tableSchema, $fieldPrefix, $path, $tplFile, $tplDir);
}
} }
public function generateData($driver, $db, $tableSchema, $fieldPrefix, $path, $tplFile, $tplDir) public function generate($params)
{ {
$mappingClass = $tableSchema['mapping']; list($db, $table, $path, $tplFile, $tplDir) = $params;
$config = [ $this->generateData($table, $path, $tplFile, $tplDir);
}
public function generateData($table, $path, $tplFile, $tplDir)
{
$config = [
'tplFilename' => $tplFile, 'tplFilename' => $tplFile,
'tplDir' => $tplDir, 'tplDir' => $tplDir,
'className' => $mappingClass, 'className' => $table,
]; ];
$file = alias($path); $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 = [ $data = [
'tableName' => $tableSchema['name'], 'className' => ucfirst($table),
'namespace' => 'App\\Models\\Data', 'tableName' => $table,
'namespace' => "App\\Models\\$tplFile",
'columns' => $columnSchemas
]; ];
$gen = new FileGenerator($config); $gen = new FileGenerator($config);
$gen->getParser()->setOpenCloseTagChars("{{","}}");
$gen->renderas($file, $data); $gen->renderas($file, $data);
} }
......
<?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);
}
}
<?php <?php
namespace {= namespace}; namespace {{= namespace}};
use App\Common\Db\CQuery; use App\Common\Db\CQuery;
use Swoft\Bean\Annotation\Bean; use Swoft\Bean\Annotation\Bean;
use App\Models\Entity\{= tableName}; use App\Models\Entity\{{= className}};
{= usespace}
/** /**
* @Bean() * @Bean()
* Class {= tableName}Data * Class {{= className}}Data
* @package App\Models\Data * @package App\Models\Data
*/ */
class {= tableName}Data class {{= className}}Data
{ {
public function get($condition, $fields = ['*']) public function get($condition, $fields = ['*'])
{ {
$condition['deleteTime'] = 0; $condition['deleteTime'] = 0;
$data = {= tableName}::findOne($condition, ['fields' => $fields])->getResult(); $data = {{= className}}::findOne($condition, ['fields' => $fields])->getResult();
return $data ? $data->toArray() : []; return $data ? $data->toArray() : [];
} }
public function save($data) public function save($data)
{ {
$image = new {= tableName}(); $image = new {{= className}}();
return $image->fill($data)->save()->getResult(); return $image->fill($data)->save()->getResult();
} }
public function update($id, $data) public function update($id, $data)
{ {
$data['updateTime'] = time(); $data['updateTime'] = time();
return {= tableName}::updateOne($data, ['id' => $id])->getResult(); return {{= className}}::updateOne($data, ['id' => $id])->getResult();
} }
public function del($condition){ public function del($condition){
return {= tableName}::updateOne(['deleteTime'=>time()],$condition)->getResult(); return {{= className}}::updateOne(['deleteTime'=>time()],$condition)->getResult();
} }
public function list($condition, $fields = ['*'], $page = 1, $limit = 15) 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(); $data = CQuery::table({{= className}}::tableName())->condition($condition)->limit($page, $limit)->orderBy('id', 'DESC')->get($fields)->getResult();
return $data ?? []; return $data ?? [];
} }
public function all($condition,$fields=['*']){ public function all($condition,$fields=['*']){
return CQuery::table({= tableName}::tableName())->condition($condition)->get($fields)->getResult(); return CQuery::table({{= className}}::tableName())->condition($condition)->get($fields)->getResult();
} }
} }
<?php <?php
namespace {= namespace}; namespace {{= namespace}};
use App\Exception\BaseException;
use App\Exception\EnumException;
use Swoft\Bean\Annotation\Bean; use Swoft\Bean\Annotation\Bean;
use App\Models\Data\{= tableName}Data; use Swoft\Bean\Annotation\Inject;
{= usespace} use App\Models\Data\{{= className}}Data;
/** /**
* @Bean() * @Bean()
* Class {= tableName}Logic * Class {{= className}}Logic
* @package App\Models\Logic * @package App\Models\Logic
*/ */
class {= tableName}Logic class {{= className}}Logic
{ {
/** /**
* @Inject() * @Inject()
* @var ${= tableName}Data * @var {{= className}}Data
*/ */
private ${= tableName}; private ${{= tableName}};
public function get($id,$storeId) public function get($id,$storeId)
{ {
$data = $this->${= tableName}->get(['id' => $id, 'storeId' => $storeId]); $data = $this->{{= tableName}}->get(['id' => $id, 'storeId' => $storeId]);
if (empty($data)) { return $data;
throw new BaseException(EnumException::E101); }
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();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment