自动生成CURD导出生成关联查询

This commit is contained in:
2022-07-14 10:19:56 +08:00
parent 065851fcb1
commit cf6d0f9814
5 changed files with 43 additions and 7 deletions

View File

@@ -1060,6 +1060,7 @@ class BuildCurdService
$controllerFile = "{$this->rootDir}app{$this->DS}admin{$this->DS}controller{$this->DS}{$this->controllerFilename}.php"; $controllerFile = "{$this->rootDir}app{$this->DS}admin{$this->DS}controller{$this->DS}{$this->controllerFilename}.php";
if (empty($this->relationArray)) { if (empty($this->relationArray)) {
$controllerIndexMethod = ''; $controllerIndexMethod = '';
$controllerExportMethod = '';
} else { } else {
$relationCode = ''; $relationCode = '';
foreach ($this->relationArray as $key => $val) { foreach ($this->relationArray as $key => $val) {
@@ -1072,6 +1073,12 @@ class BuildCurdService
'relationIndexMethod' => $relationCode, 'relationIndexMethod' => $relationCode,
] ]
); );
$controllerExportMethod = $this->replaceTemplate(
$this->getTemplate("controller{$this->DS}exportMethod"),
[
'relationIndexMethod' => trim($relationCode),
]
);
} }
$selectList = ''; $selectList = '';
@@ -1091,6 +1098,7 @@ class BuildCurdService
'controllerAnnotation' => $this->tableComment, 'controllerAnnotation' => $this->tableComment,
'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}", 'modelFilename' => "\app\admin\model\\{$modelFilenameExtend}",
'indexMethod' => $controllerIndexMethod, 'indexMethod' => $controllerIndexMethod,
'exportMethod' => $controllerExportMethod,
'selectList' => $selectList, 'selectList' => $selectList,
] ]
); );

View File

@@ -24,4 +24,7 @@ class {{controllerName}} extends AdminController
} }
{{indexMethod}} {{indexMethod}}
{{exportMethod}}
} }

View File

@@ -0,0 +1,25 @@
/**
* @NodeAnotation(title="导出")
*/
public function export()
{
list($page, $limit, $where) = $this->buildTableParames();
$this->model = $this->model{{relationIndexMethod}};
$fields = $this->request->param('fields', '{}', null);
$image_fields = $this->request->param('image_fields', '{}', null);
$select_fields = $this->request->param('select_fields', '{}', null);
$date_fields = $this->request->param('date_fields', '{}', null);
$fields = json_decode($fields, true);
$image_fields = json_decode($image_fields, true);
$select_fields = json_decode($select_fields, true);
$date_fields = json_decode($date_fields, true);
$content = \app\common\tools\ExcelTools::exportModel($this->model, $where, $fields, $image_fields, $select_fields, $date_fields);
return download($content, $this->model->getName() . date('YmdHis') . '.xlsx', true);
}

View File

@@ -12,10 +12,10 @@ use think\console\Output;
class Version extends Command class Version extends Command
{ {
const VERSION = 'v2.0.3'; const VERSION = 'v2.0.4';
const COMMENT = [ const COMMENT = [
'优化导出功能,兼容自定义参数、兼容关联查询用法', '自动生成CURD导出生成关联查询',
]; ];
protected function configure() protected function configure()

View File

@@ -73,7 +73,7 @@ class ExcelTools
} else if (array_key_exists($field_key, $select_fields)) { } else if (array_key_exists($field_key, $select_fields)) {
// 需要设置选项 // 需要设置选项
$cel = $select_fields[$field_key][$value]; $cel = $select_fields[$field_key][$value] ?? '';
} else if (in_array($field_key, $date_fields)) { } else if (in_array($field_key, $date_fields)) {
if (empty($value)) { if (empty($value)) {
$cel = ''; $cel = '';