<?php
namespace app\admin\command;
use fast\Form;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Exception;
use think\exception\ErrorException;
use think\exception\PDOException;
use think\Lang;
use think\Loader;
class Crud extends Command
{
protected $stubList = [];
protected $internalKeywords = [
'abstract',
'and',
'array',
'as',
'break',
'callable',
'case',
'catch',
'class',
'clone',
'const',
'continue',
'declare',
'default',
'die',
'do',
'echo',
'else',
'elseif',
'empty',
'enddeclare',
'endfor',
'endforeach',
'endif',
'endswitch',
'endwhile',
'eval',
'exit',
'extends',
'final',
'for',
'foreach',
'function',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'isset',
'list',
'namespace',
'new',
'or',
'print',
'private',
'protected',
'public',
'require',
'require_once',
'return',
'static',
'switch',
'throw',
'trait',
'try',
'unset',
'use',
'var',
'while',
'xor'
];
/**
* 受保护的系统表, crud不会生效
*/
protected $systemTables = [
'admin',
'admin_log',
'auth_group',
'auth_group_access',
'auth_rule',
'attachment',
'config',
'category',
'ems',
'sms',
'user',
'user_group',
'user_rule',
'user_score_log',
'user_token',
];
/**
* Selectpage搜索字段关联
*/
protected $fieldSelectpageMap = [
'nickname' => ['user_id', 'user_ids', 'admin_id', 'admin_ids']
];
/**
* Enum类型识别为单选框的结尾字符,默认会识别为单选下拉列表
*/
protected $enumRadioSuffix = ['data', 'state', 'status'];
/**
* Set类型识别为复选框的结尾字符,默认会识别为多选下拉列表
*/
protected $setCheckboxSuffix = ['data', 'state', 'status'];
/**
* Int类型识别为日期时间的结尾字符,默认会识别为日期文本框
*/
protected $intDateSuffix = ['time'];
/**
* 开关后缀
*/
protected $switchSuffix = ['switch'];
/**
* 富文本后缀
*/
protected $editorSuffix = ['content'];
/**
* 城市后缀
*/
protected $citySuffix = ['city'];
/**
* 时间区间后缀
*/
protected $rangeSuffix = ['range'];
/**
* JSON后缀
*/
protected $jsonSuffix = ['json'];
/**
* 标签后缀
*/
protected $tagSuffix = ['tag', 'tags'];
/**
* Selectpage对应的后缀
*/
protected $selectpageSuffix = ['_id', '_ids'];
/**
* Selectpage多选对应的后缀
*/
protected $selectpagesSuffix = ['_ids'];
/**
* 以指定字符结尾的字段格式化函数
*/
protected $fieldFormatterSuffix = [
'status' => ['type' => ['varchar', 'enum'], 'name' => 'status'],
'icon' => 'icon',
'flag' => 'flag',
'url' => 'url',
'image' => 'image',
'images' => 'images',
'file' => 'file',
'files' => 'files',
'avatar' => 'image',
'switch' => 'toggle',
'tag' => 'flag',
'tags' => 'flag',
'time' => ['type' => ['int', 'bigint', 'timestamp'], 'name' => 'datetime'],
];
/**
* 识别为图片字段
*/
protected $imageField = ['image', 'images', 'avatar', 'avatars'];
/**
* 识别为文件字段
*/
protected $fileField = ['file', 'files'];
/**
* 保留字段
*/
protected $reservedField = ['admin_id'];
/**
* 排除字段
*/
protected $ignoreFields = [];
/**
* 排序字段
*/
protected $sortField = 'weigh';
/**
* 筛选字段
* @var string
*/
protected $headingFilterField = 'status';
/**
* 添加时间字段
* @var string
*/
protected $createTimeField = 'createtime';
/**
* 更新时间字段
* @var string
*/
protected $updateTimeField = 'updatetime';
/**
* 软删除时间字段
* @var string
*/
protected $deleteTimeField = 'deletetime';
/**
* 编辑器的Class
*/
protected $editorClass = 'editor';
/**
* langList的key最长字节数
*/
protected $fieldMaxLen = 0;
protected function configure()
{
$this
->setName('crud')
->addOption('table', 't', Option::VALUE_REQUIRED, 'table name without prefix', null)
->addOption('controller', 'c', Option::VALUE_OPTIONAL, 'controller name', null)
->addOption('model', 'm', Option::VALUE_OPTIONAL, 'model name', null)
->addOption('fields', 'i', Option::VALUE_OPTIONAL, 'model visible fields', null)
->addOption('force', 'f', Option::VALUE_OPTIONAL, 'force override or force delete,without tips', null)
->addOption('local', 'l', Option::VALUE_OPTIONAL, 'local model', 1)
->addOption('import', 'a', Option::VALUE_OPTIONAL, 'enable import function', 0)
->addOption('relation', 'r', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table name without prefix', null)
->addOption('relationmodel', 'e', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation model name', null)
->addOption('relationforeignkey', 'k', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation foreign key', null)
->addOption('relationprimarykey', 'p', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation primary key', null)
->addOption('relationfields', 's', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table fields', null)
->addOption('relationmode', 'o', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table mode,hasone/belongsto/hasmany', null)
->addOption('relationcontroller', 'w', Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'relation table controller,only work at hasmany mode', null)
->addOption('delete', 'd', Option::VALUE_OPTIONAL, 'delete all files generated by CRUD', null)
->addOption('menu', 'u', Option::VALUE_OPTIONAL, 'create menu when CRUD completed', null)
->addOption('setcheckboxsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate checkbox component with suffix', null)
->addOption('enumradiosuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate radio component with suffix', null)
->addOption('imagefield', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate image component with suffix', null)
->addOption('filefield', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate file component with suffix', null)
->addOption('intdatesuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate date component with suffix', null)
->addOption('switchsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate switch component with suffix', null)
->addOption('citysuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_IS_ARRAY, 'automatically generate citypicker component with suffix', null)
->addOption('jsonsuffix', null, Option::VALUE_OPTIONAL | Option::VALUE_I