Commit 9d38a381 by xmy

feat:模版生成

parent 171c747c
<?php
namespace hdll\services\generate;
use Swoft\Bean\Annotation\Bean;
use Swoft\Bean\Annotation\Inject;
use Swoft\Devtool\FileGenerator;
use Swoft\Devtool\Model\Data\SchemaData;
/**
* Class GenData
* @package hdll\services\generate
*/
class GenData
{
/**
* @Inject()
* @var SchemaData
*/
private $data;
public function generate($param)
{
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);
}
}
public function generateData($driver, $db, $tableSchema, $fieldPrefix, $path, $tplFile, $tplDir)
{
$mappingClass = $tableSchema['mapping'];
$config = [
'tplFilename' => $tplFile,
'tplDir' => $tplDir,
'className' => $mappingClass,
];
$file = alias($path);
$file .= sprintf('/%s.php', $mappingClass);
$columnSchemas = $this->data->getSchemaColumnsData($driver,$db,$tableSchema['name'],$fieldPrefix);
$data = [
'tableName' => $tableSchema['name'],
'namespace' => 'App\\Models\\Data',
];
$gen = new FileGenerator($config);
$gen->renderas($file, $data);
}
public function generateSave()
{
}
}
<?php
namespace {= namespace};
use App\Common\Db\CQuery;
use Swoft\Bean\Annotation\Bean;
use App\Models\Entity\{= tableName};
{= usespace}
/**
* @Bean()
* Class {= tableName}Data
* @package App\Models\Data
*/
class {= tableName}Data extends Model
{
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();
}
}
<?php
namespace {= namespace};
use App\Common\Db\CQuery;
use Swoft\Bean\Annotation\Bean;
use App\Models\Entity\{= tableName};
{= usespace}
/**
* @Bean()
* Class {= tableName}Data
* @package App\Models\Data
*/
class {= tableName}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();
}
}
<?php
namespace {= namespace};
use Swoft\Bean\Annotation\Bean;
use App\Models\Data\{= tableName}Data;
{= usespace}
/**
* @Bean()
* Class {= tableName}Logic
* @package App\Models\Logic
*/
class {= tableName}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);
}
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