Commit 8caaa4a0 by xmy

feat:模版生成

parent 9d38a381
......@@ -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 generate($param)
public function getColumns($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 generateData($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);
}
......
<?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
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();
}
}
<?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();
}
}
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