You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
526 lines
13 KiB
526 lines
13 KiB
<?php |
|
/** |
|
* |
|
* Validate.php |
|
* User: ChenLong |
|
* DateTime: 2020/3/17 17:28 |
|
*/ |
|
|
|
|
|
namespace sdModule\makeAdminBasics\makeItem; |
|
|
|
|
|
use sdModule\layuiSearch\generate\TimeRange; |
|
use sdModule\layuiSearch\SearchForm; |
|
use sdModule\makeAdminBasics\Basics; |
|
use sdModule\makeAdminBasics\ConfigHelper; |
|
use sdModule\makeAdminBasics\Make; |
|
|
|
class Controller implements MakeItemInterface |
|
{ |
|
/** |
|
* @var Make |
|
*/ |
|
private $make; |
|
|
|
/** |
|
* @var array 查询的字段 |
|
*/ |
|
private $selectField = []; |
|
|
|
/** |
|
* @var array 列表查询的关联链表字符 |
|
*/ |
|
private $join = []; |
|
|
|
/** |
|
* @var array 字段详情 |
|
*/ |
|
private $fieldInfo = []; |
|
|
|
/** |
|
* @var string 搜索表单的数组有效字符串 |
|
*/ |
|
private $search = ''; |
|
|
|
|
|
/** |
|
* @var array 引入的use类 |
|
*/ |
|
private $use = []; |
|
|
|
/** |
|
* @var array 写入数据页面的数据 |
|
*/ |
|
private $writeData = []; |
|
|
|
/** |
|
* @var string $searchLabel 搜索label是否html |
|
*/ |
|
private $searchLabel = ''; |
|
|
|
private $quickSearch = []; |
|
|
|
/** |
|
* 构造函数 |
|
* MakeItem constructor. |
|
* @param Make $make |
|
*/ |
|
public function __construct(Make $make) |
|
{ |
|
$this->make = $make; |
|
$fieldInfo = (new Basics())->getTableInfo($make->table); |
|
|
|
$this->fieldInfo = array_column($fieldInfo, 'column_type', 'column_name'); |
|
|
|
ConfigHelper::get('search_label') and $this->searchLabel = '->label(true)'; |
|
|
|
$this->selectFieldHandle(); |
|
$this->writeData(); |
|
$this->quickSearch(); |
|
} |
|
|
|
/** |
|
* 创建文件 |
|
* @return mixed |
|
*/ |
|
public function make(): bool |
|
{ |
|
$table = parse_name($this->make->table, 1); |
|
$time = date('Y-m-d H:i'); |
|
$namespace = ConfigHelper::get('namespace.controller'); |
|
|
|
$code = <<<CODE |
|
<?php |
|
/** |
|
* {$this->make->tableComment} |
|
* {$table}.php |
|
* User: ChenLong |
|
* DateTime: {$time} |
|
*/ |
|
|
|
namespace {$namespace}; |
|
|
|
{$this->useCode()} |
|
/** |
|
* Class {$table} |
|
* @package {$namespace}\\{$table} |
|
* @author chenlong <vip_chenlong@163.com> |
|
*/ |
|
class {$table} extends \\app\\common\\controller\\Admin |
|
{ |
|
public \$pageName = "{$this->make->pageName}"; |
|
|
|
{$this->listsCode()} |
|
{$this->addCode()} |
|
{$this->updateCode()} |
|
{$this->searchFormCode()} |
|
{$this->quickSearchCode()} |
|
|
|
} |
|
|
|
CODE; |
|
return $this->make->makeFile(ConfigHelper::get('file_url.controller') . "{$table}.php", $code); |
|
} |
|
|
|
/** |
|
* 引用的代码 |
|
* @return string |
|
*/ |
|
private function useCode() |
|
{ |
|
return implode(Basics::indentAndLineFeed(0, Basics::BEFORE), $this->use); |
|
} |
|
|
|
|
|
/** |
|
* 列表数据的代码函数 |
|
* @return string |
|
*/ |
|
public function listsCode() |
|
{ |
|
$selectField = implode(',', $this->selectField); |
|
|
|
|
|
return <<<CODE |
|
|
|
/** |
|
* 列表数据接口 |
|
* @return array|\\Closure|mixed|string|\\think\\Collection|\\think\\response\\Json |
|
* @throws \\think\\db\\exception\\DataNotFoundException |
|
* @throws \\think\\db\\exception\\DbException |
|
* @throws \\think\\db\\exception\\ModelNotFoundException |
|
* @throws \Exception |
|
*/ |
|
public function listData() |
|
{ |
|
return \$this{$this->getJoin()}->setField('{$selectField}') |
|
->listsRequest(); |
|
} |
|
|
|
CODE; |
|
|
|
} |
|
|
|
/** |
|
* 搜索表单代码 |
|
* @return string |
|
*/ |
|
private function searchFormCode() |
|
{ |
|
$searchData = sprintf('[%s%s%s]', Basics::indentAndLineFeed(3, Basics::BEFORE), $this->search, |
|
Basics::indentAndLineFeed(2, Basics::BEFORE)); |
|
|
|
return <<<CODE |
|
|
|
/** |
|
* 搜索表单生成 |
|
* @return array|mixed |
|
*/ |
|
public function setSearchForm() |
|
{ |
|
return {$searchData}; |
|
} |
|
|
|
CODE; |
|
} |
|
|
|
/** |
|
* @return string |
|
*/ |
|
private function addCode() |
|
{ |
|
if (!$this->writeData) { |
|
return ''; |
|
} |
|
$haveData = Basics::toLegalArrayCode($this->writeData, 3); |
|
return <<<CODE |
|
|
|
/** |
|
* 数据新增页面 |
|
* @return mixed|string |
|
* @throws \Exception |
|
*/ |
|
public function add() |
|
{ |
|
return \$this->fetch(__FUNCTION__, {$haveData}); |
|
} |
|
|
|
CODE; |
|
|
|
} |
|
|
|
/** |
|
* @return string |
|
*/ |
|
private function updateCode() |
|
{ |
|
if (!$this->writeData) { |
|
return ''; |
|
} |
|
$this->updateDataCode(); |
|
|
|
$haveData = Basics::toLegalArrayCode($this->writeData, 3); |
|
|
|
return <<<CODE |
|
|
|
/** |
|
* 数据编辑页面 |
|
* @param int \$id |
|
* @return array|mixed|\\think\\response\\View |
|
* @throws \\think\\db\\exception\\DataNotFoundException |
|
* @throws \\think\\db\\exception\\DbException |
|
* @throws \\think\\db\\exception\\ModelNotFoundException |
|
* @throws \\Exception |
|
*/ |
|
public function edit(\$id = 0) |
|
{ |
|
return \$this->fetch(__FUNCTION__, {$haveData}); |
|
} |
|
|
|
CODE; |
|
|
|
} |
|
|
|
private function quickSearchCode() |
|
{ |
|
if (empty($this->quickSearch)) { |
|
return ''; |
|
} |
|
|
|
$code = Basics::toLegalArrayCode($this->quickSearch, 3); |
|
|
|
return <<<CODE |
|
|
|
/** |
|
* 快捷搜索设置 |
|
* @return array |
|
*/ |
|
public function setQuickSearchField(): array |
|
{ |
|
return {$code}; |
|
} |
|
|
|
|
|
CODE; |
|
|
|
} |
|
|
|
private function writeData() |
|
{ |
|
$softDeletion = ConfigHelper::get('soft_deletion'); |
|
$parent = false; |
|
foreach ($this->make->makeData as $field => $datum) { |
|
if (empty($datum['type']) || empty($datum['join'])) continue; |
|
|
|
if (is_array($datum['join'])) { |
|
$this->writeData[$field . '_data'] = "|{$this->attrFieldGet($field)}|"; |
|
}else{ |
|
|
|
list($table, $joinData) = explode(':', $datum['join']); |
|
list($value, $title) = explode('=', $joinData); |
|
|
|
if (empty($table)) { |
|
$table = $this->make->table; |
|
$parent = true; |
|
} |
|
|
|
if ($softDeletion) { |
|
$namespace = in_array(parse_name($table, 1), ['Administrators', 'Role']) |
|
? 'app\\admin\\model\\System\\' |
|
: 'app\\admin\\model\\'; |
|
$parent === true |
|
? $this->useAdd($namespace . parse_name($table, 1) . ' as I') |
|
: $this->useAdd($namespace . parse_name($table, 1)); |
|
|
|
$model = $parent === true ? 'I' : parse_name($table, 1); |
|
|
|
$this->writeData[parse_name($table)] = "|soft_delete_query({$model}::class)->column('{$title}', '{$value}')|"; |
|
}else{ |
|
$this->writeData[parse_name($table)] = "|" . parse_name($table, 1) . "::column('{$title}', '{$value}')|"; |
|
} |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 修改页面的的数据的code |
|
*/ |
|
private function updateDataCode() |
|
{ |
|
$where = [$this->make->primaryKey => "|\$id|"]; |
|
$where = Basics::toLegalArrayCode($where, 4); |
|
|
|
$this->writeData['data'] = "|soft_delete_sql(\$this->getTableModel())->where({$where})->findOrEmpty()->getData()|"; |
|
} |
|
|
|
|
|
/** |
|
* 查询字段处理 |
|
*/ |
|
private function selectFieldHandle() |
|
{ |
|
foreach ($this->make->makeData as $field => $datum) { |
|
if (empty($datum['show_type'])) { |
|
continue; |
|
} |
|
|
|
$this->searchHandle($field, $datum); |
|
|
|
if (is_array($datum['join']) || empty($datum['join'])) { |
|
$this->selectField[] = "i.{$field}"; |
|
}else if(strpos($datum['join'], ':') !== false |
|
&& strpos($datum['join'], '=') !== false){ |
|
|
|
$joinData = explode(':', $datum['join']); |
|
list($value, $title) = explode('=', $joinData[1]); |
|
|
|
$this->joinTable($joinData[0], $value); |
|
|
|
$table = $joinData[0] ?: $this->make->table; |
|
$table_pre = $table === $this->make->table ? 'parent' : $table; |
|
$this->selectField[] = "{$table}.{$title} {$table_pre}_{$title},i.{$field}"; |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* 多属性字段获取 |
|
* @param $field |
|
* @return string |
|
*/ |
|
private function attrFieldGet($field) |
|
{ |
|
$this->useAdd(implode('\\', [ConfigHelper::get('namespace.model'), parse_name($this->make->table, 1)]) . ' as I'); |
|
$field = parse_name($field, 1); |
|
return "I::get{$field}Sc(false)"; |
|
} |
|
|
|
/** |
|
* @param string $table join 表 |
|
* @param string $field 关联字段 |
|
*/ |
|
private function joinTable($table, $field) |
|
{ |
|
$where = ''; |
|
$parent = false; |
|
|
|
if (empty($table)) { |
|
$table = $this->make->table; |
|
$parent = true; |
|
} |
|
|
|
$joinStr = $parent === true |
|
? sprintf('i.pid = %s.%s %s', $table, $field, $where) |
|
: sprintf('i.%s_id = %1$s.%s %s', $table, $field, $where); |
|
|
|
$this->join[] = [$table, $joinStr, 'left']; |
|
} |
|
|
|
/** |
|
* 获取join字段 |
|
* @return string |
|
*/ |
|
private function getJoin() |
|
{ |
|
if (!$this->join) return ''; |
|
|
|
$joinStr = ''; |
|
|
|
foreach ($this->join as [$table, $where, $type]) { |
|
$joinStr .= Basics::indentAndLineFeed(4, Basics::BEFORE) . "['{$table}', '{$where}', '{$type}'],"; |
|
} |
|
$joinStr .= Basics::indentAndLineFeed(3, Basics::BEFORE); |
|
return sprintf('->setJoin([%s])' . Basics::indentAndLineFeed(3, Basics::BEFORE), $joinStr); |
|
} |
|
|
|
/** |
|
* @param $field |
|
* @param $datum |
|
* @return bool|void |
|
*/ |
|
private function searchHandle($field, $datum) |
|
{ |
|
if ($datum['show_type'] == 'image') return false; |
|
|
|
$this->useAdd(SearchForm::class); |
|
|
|
if (!is_array($datum['join']) && strpos($datum['join'], ':') !== false |
|
&& strpos($datum['join'], '=') !== false) { |
|
$this->search(explode('=', $datum['join'])[1], $datum, explode(':', $datum['join'])[0]); |
|
}else{ |
|
$this->search($field, $datum, 'i'); |
|
} |
|
} |
|
|
|
|
|
/** |
|
* @param $field |
|
* @param $data |
|
* @param string $alias |
|
*/ |
|
private function search($field, $data, $alias = 'i') |
|
{ |
|
if (empty($alias)) $alias = $this->make->table; |
|
|
|
switch (true) { |
|
case is_array($data['join']): |
|
$this->selectSearch($field, $data['label'], $this->attrFieldGet($field), $alias); |
|
break; |
|
case empty($this->fieldInfo[$field]): |
|
$this->TextLike($field, $data['label'], $alias); |
|
break; |
|
case in_array($this->fieldInfo[$field], ['date', 'datetime']): |
|
$this->timeRangeSearch($field, $data['label'], $alias); |
|
break; |
|
case in_array($this->fieldInfo[$field], ['int', 'tinyint', 'smallint' , 'bigint']): |
|
$this->Text($field, $data['label'], $alias); |
|
break; |
|
default: |
|
$this->TextLike($field, $data['label'], $alias); |
|
} |
|
} |
|
|
|
|
|
/** |
|
* @param $field |
|
* @param $placeholder |
|
* @param $alias |
|
*/ |
|
private function timeRangeSearch($field, $placeholder, $alias) |
|
{ |
|
$this->useAdd(TimeRange::class); |
|
|
|
$rangeSign = [ |
|
'date' => 'TimeRange::TYPE_DATE', |
|
'datetime' => 'TimeRange::TYPE_DATETIME' |
|
]; |
|
$this->search .= sprintf('SearchForm::TimeRange("%s", "%s")'.$this->searchLabel.'->html(%s),%s', |
|
"{$alias}.{$field}_~", $placeholder, $rangeSign[$this->fieldInfo[$field]], Basics::indentAndLineFeed(3, Basics::BEFORE)); |
|
} |
|
|
|
/** |
|
* @param $field |
|
* @param $placeholder |
|
* @param $data |
|
* @param $alias |
|
*/ |
|
private function selectSearch($field, $placeholder, $data, $alias) |
|
{ |
|
$this->search .= sprintf('SearchForm::Select("%s", "%s")'.$this->searchLabel.'->html(%s),%s', |
|
"{$alias}.{$field}", $placeholder, $data, Basics::indentAndLineFeed(3, Basics::BEFORE)); |
|
} |
|
|
|
/** |
|
* @param $field |
|
* @param $placeholder |
|
* @param $alias |
|
*/ |
|
private function Text($field, $placeholder, $alias) |
|
{ |
|
$this->search .= sprintf('SearchForm::Text("%s", "%s")'.$this->searchLabel.'->html(),%s', |
|
"{$alias}.{$field}", $placeholder, Basics::indentAndLineFeed(3, Basics::BEFORE)); |
|
} |
|
|
|
/** |
|
* |
|
* @param $field |
|
* @param $placeholder |
|
* @param $alias |
|
*/ |
|
private function TextLike($field, $placeholder, $alias) |
|
{ |
|
$this->Text($field .'%%', $placeholder, $alias); |
|
} |
|
|
|
/** |
|
* @param $useClass |
|
*/ |
|
private function useAdd($useClass) |
|
{ |
|
$use = "use {$useClass};"; |
|
in_array($use, $this->use) or $this->use[] = $use; |
|
} |
|
|
|
|
|
/** |
|
* 快捷搜索的创建 |
|
*/ |
|
private function quickSearch() |
|
{ |
|
foreach ($this->make->makeData as $field => $datum) { |
|
if (empty($datum['quick_search'])) continue; |
|
if (!empty($datum['join']) && is_string($datum['join']) && strpos($datum['join'], ':') !== false){ |
|
$table = explode(':', $datum['join'])[0] ?: $this->make->table; |
|
$field = explode('=', $datum['join'])[1]; |
|
$this->quickSearch[$table . '.' .$field . '%%'] = $datum['label']; |
|
}elseif (in_array($this->fieldInfo[$field], ['int', 'tinyint', 'smallint', 'bigint']) ) { |
|
$this->quickSearch[$field] = $datum['label']; |
|
}else{ |
|
$this->quickSearch[$field . '%%'] = $datum['label']; |
|
} |
|
} |
|
|
|
} |
|
} |
|
|
|
|