123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- <?php
- namespace app\common\model\base\models;
- /**
- * @title : 模型代码生成服务
- * @desc :
- * @Author : Rock
- * @Date : 2023-04-18 09:42:34
- */
- class ModelServer
- {
- /**
- * @title: 删除模型文件
- * @desc: 描述
- * @param {int} $model_id
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-23 17:09:42
- */
- static public function deleteFile(int $model_id = 0)
- {
- $info = ModelManage::where('id',$model_id)->find();
- $name = $info->name;
- $classname = self::getCamel($info->name);//获取类名
- $dirname = self::parseDirname($info->dirname);// 文件保存位置
- if(!empty($dirname)){
- $path = root_path()."app".DS."common".DS."model".DS.$dirname.DS;
- }else{
- $path = root_path()."app".DS."common".DS."model".DS;
- }
- $filename = $path.$classname.'.php';
- if(file_exists($filename)){
- @unlink($filename);
- }
- }
- /**
- * @title: 生成模型代码
- * @desc: 描述
- * @param {int} $model_id
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-23 17:09:54
- */
- static public function createModelFile(int $model_id=0)
- {
- $info = ModelManage::where('id',$model_id)->find();
- $name = $info->name;
- $pk = ModelManage::pk($name);
- $dirname = self::parseDirname($info->dirname);// 文件保存位置
- $fileTitle = self::createTitle($info->title);//生成文件注释
- $fieldList = ModelFields::where('model_name',$name)->where('status',1)->select()->toArray();//获取字段列表
- $classname = self::getCamel($info->name);//获取类名
- $schema = env('app_debug', false)?'':self::createSchema(array_column($fieldList,'db_type','field'),$pk);// 获取字段数据类型
- $attr = self::createAttr($fieldList,$pk);
- if(!empty($dirname)){
- $path = root_path()."app".DS."common".DS."model".DS.$dirname.DS;
- $namespace = "app\common\model\\".str_replace('/','\\',$dirname);
- }else{
- $path = root_path()."app".DS."common".DS."model".DS;
- $namespace = "app\common\model";
- }
- if(!is_dir($path)){
- mkdir($path,0777,true);
- }
- $filename = $path.$classname.'.php';
- $content = <<<PHP
- <?php
- namespace $namespace;
- $fileTitle
- use app\common\model\Common;
- use think\model\concern\SoftDelete;
- class $classname extends Common
- {
- use SoftDelete;
- protected \$name = "$name";
- $schema
- protected \$pk = "$pk";
- $attr
- }
- PHP;
- FileServer::writeLine($filename,$content);
- }
- /**
- * @title: 格式化dir路径
- * @desc: 描述
- * @param {string} $dirname
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-24 14:46:37
- */
- static public function parseDirname(string $dirname)
- {
- return !empty($dirname)?explode(DS,str_replace(['/','\\'],DS,$dirname))[0]:'';
- }
- /**
- * @title: 根据数据表名生成类名
- * @desc: 描述
- * @param {string} {name} {} {数据表名}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-18 17:09:13
- */
- static public function getCamel(string $name)
- {
- $arr = explode('_',$name);
- $arr = array_filter($arr);
- $classname = "";
- foreach($arr as $item){
- $classname .= ucfirst($item);
- }
- return $classname;
- }
- /**
- * @title: 生成模型注释
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-18 15:13:19
- */
- static public function createTitle(string $title)
- {
- $title = <<<EOF
- /**
- * @title : $title 模型
- * @desc :
- */
- EOF;
- return $title;
- }
- /**
- * @title: 生成模型schema属性
- * @desc: 描述
- * @param {array} $fieldList
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-18 14:58:27
- */
- static public function createSchema(array $fieldList,string $pk)
- {
- $defaultList = [
- $pk=>'int',
- 'create_at'=>'datetime',
- 'update_at'=>'datetime',
- 'delete_at'=>'datetime',
- ];
- $fieldList = array_merge($defaultList,$fieldList);
- $str = "[\n";
- foreach($fieldList as $field=>$dbtype){
- $str.="\t\t'$field'=>'$dbtype',\n";
- }
- $str = trim($str,",\n");
- $str .= "\n ];";
- return 'protected $schema = '.$str;
- }
- /**
- * @title: 生成模型关联的获取器
- * @desc: 描述
- * @param {array} {fieldList} {} {字段列表}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-18 17:29:57
- */
- static public function createAttr(array $fieldList,string $pk)
- {
- $attr = "";
- $append = "protected \$append = [";
- foreach($fieldList as $item){
- $field = $item['field'];
- switch($item['field_type']){
- case 'select':
- case 'radio':
- $camel = self::getCamel($field);
- $arrTxt = self::getTxtByContent($item['content']);
- $attr.="\tstatic public function ".$camel."List()\n";
- $attr.="\t{\n\t\treturn $arrTxt;\n\t}\n";
- $attr .= "\tpublic function get".$camel."TxtAttr(\$value,\$row)\n";
- $attr .= "\t{\n\t\t\$".$camel."List = self::".$camel."List();\n";
- $attr .="\t\treturn \$".$camel."List[\$value ?? \$row['$field']??''] ?? '';\n";
- $attr .= "\t}\n";
- $append.="'".$field."_txt',";
- break;
- case 'selects':
- case 'checkbox':
- $camel = self::getCamel($field);
- $arrTxt = self::getTxtByContent($item['content']);
- $attr .="\tpublic function set".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n";
- $attr .="\t\treturn implode(',',array_filter(\$value));\n";
- $attr .="\t}\n";
- $attr .="\tpublic function get".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n";
- $attr .="\t\treturn array_filter(explode(',',\$value));\n";
- $attr .="\t}\n";
- $attr .="\tstatic public function ".$camel."List()\n";
- $attr .="\t{\n\t\treturn $arrTxt;\n\t}\n";
- $attr .= "\tpublic function get".$camel."TxtAttr(\$value,\$row)\n";
- $attr .= "\t{\n\t\t\$".$camel."List = self::".$camel."List();\n";
- $attr .= "\t\t\$value = explode(',',\$row['$field']??'');\n";
- $attr .= "\t\t\$res=[];\n";
- $attr .= "\t\tforeach(\$value as \$val){\n";
- $attr .= "\t\t\t\$res[] = \$".$camel."List[\$val] ?? '';\n";
- $attr .="\t\t}\n";
- $attr .="\t\treturn array_filter(\$res);\n";
- $attr .= "\t}\n";
- $append.="'".$field."_txt',";
- break;
- case 'switch':
- $camel = self::getCamel($field);
- $extra_rule = $item['extra_rule'];
- $active_text = $extra_rule['active_text'];
- $inactive_text = $extra_rule['inactive_text'];
- $active_value = $extra_rule['active_value'];
- $inactive_value = $extra_rule['inactive_value'];
- $attr.="\tstatic public function ".$camel."List()\n";
- $attr.="\t{\n\t\treturn [$active_value =>'$active_text',$inactive_value=>'$inactive_text'];\n\t}\n";
- $attr .= "\tpublic function get".$camel."TxtAttr(\$value,\$row)\n";
- $attr .= "\t{\n\t\t\$".$camel."List = self::".$camel."List();\n";
- $attr .="\t\treturn \$".$camel."List[\$value ?? \$row['$field']??''] ?? '';\n";
- $attr .= "\t}\n";
- $append.="'".$field."_txt',";
- break;
- case 'image':
- case 'file':
- $camel = self::getCamel($field);
- $attr .="\tpublic function set".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??'';\n";
- $attr .= "\t\treturn !empty(\$value)?str_replace(WEBURL,'',\$value):\$value;\n\t}\n";
- $attr .="\tpublic function get".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??'';\n";
- $attr .= "\t\treturn !empty(\$value)?WEBURL.\$value:'';\n\t}\n";
- break;
- case 'images':
- case 'files':
- $camel = self::getCamel($field);
- $attr .="\tpublic function set".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??[];\n";
- $attr .="\t\treturn str_replace(WEBURL,'',implode(',',\$value));\n\t}\n";
- $attr .="\tpublic function get".$camel."Attr(\$value,\$row)\n\t{\n";
- $attr .="\t\t\$value = \$value??\$row['$field']??'';\n";
- $attr .="\t\t\$value = array_filter(explode(',',\$value));\n";
- $attr .="\t\tforeach(\$value as &\$val){\n\t\t\t\$val =WEBURL.\$val;\n\t\t}\n";
- $attr .="\t\treturn \$value;\n\t}\n";
- break;
- case 'json':
- case 'datetimerange':
- case 'daterange':
- $camel = self::getCamel($field);
- $attr .="\tpublic function set".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??[];\n";
- $attr .="\t\treturn json_encode(\$value);\n\t}\n";
- $attr .="\tpublic function get".$camel."Attr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??null;\n";
- $attr .="\t\treturn !empty(\$value)?json_decode(\$value,true):[];\n\t}\n";
- break;
- case 'relate_single':
- $relationName = $item['relation_model'];
- $relationField = $item['relation_field'];
- $tbName = explode('_',$relationName);
- $lastTbName = end($tbName);
- $functionName = $field == $lastTbName.'_id'?self::getCamel($relationName):self::getCamel($field);
- $relationInfo = ModelManage::where('name',$relationName)->find();
- $namespace = !empty($relationInfo->dirname)?"\app\common\model\\".str_replace('/','\\',self::parseDirname($relationInfo->dirname)):"app\common\model\\";
- $relationClass = $namespace.'\\'.self::getCamel($relationName);
- $attr .="\tpublic function $functionName()\n";
- $attr .="\t{\n\t\treturn \$this->belongsTo($relationClass::class,'$field','$relationField');\n\t}\n";
- break;
- case 'relate_multiple':
- $relationName = $item['relation_model'];
- $relationShow = $item['relation_show'];
- $functionName = self::getCamel($field);
- $relationPk = ModelManage::pk($relationName);
- $relationInfo = ModelManage::where('name',$relationName)->find();
- $namespace = !empty($relationInfo->dirname)?"\app\common\model\\".str_replace('/','\\',self::parseDirname($relationInfo->dirname)):"app\common\model\\";
- $relationClass = $namespace.'\\'.self::getCamel($relationName);
- $attr .="\tpublic function get{$functionName}TxtAttr(\$value,\$row)\n\t";
- $attr .="{\n\t\t\$value = \$value??\$row['$field']??'';\n\t\t";
- $attr .="\$value = array_filter(explode(',',\$value));\n\t\t";
- $attr .="if(!empty(\$value)){\n\t\t\t\$list = $relationClass::where('$relationPk','IN',\$value)->column('$relationShow');\n\t\t\t";
- $attr .="return implode(',',\$list);\n\t\t}else{\n\t\t\treturn '';\n\t\t}\n\t}\n";
- $append.="'".$field."_txt',";
- $attr .="\tpublic function get{$functionName}Attr(\$value,\$row)\n\t";
- $attr .="{\n\t\t\$value = \$value??\$row['$field']??'';\n\t\treturn array_filter(explode(',',\$value));\n\t}\n";
- $attr .="\tpublic function set{$functionName}Attr(\$value,\$row)\n\t";
- $attr .="{\n\t\t return implode(',',\$value);\n\t}\n";
- break;
- case 'relate_self':
- $relationField = $item['relation_field'];
- $functionName = 'Father';
- $attr .="\tpublic function Father()\n";
- $attr .="\t{\n\t\treturn \$this->belongsTo(self::class,'$field','$relationField');\n\t}\n";
- break;
- case 'relate_dic':
- $functionName = self::getCamel($field);
- $relationDicGroup = $item['relation_dic_group'];
- $cacheName = 'DIC'.strtoupper($relationDicGroup).'LIST';
- $attr .="\tpublic function get{$functionName}TxtAttr(\$value,\$row)";
- $attr .="\n\t{\n\t\t\$list = cache('$cacheName')??[];";
- $attr .="\n\t\tif(empty(\$list)){\n\t\t\t\$list = \app\common\model\base\dic\Dic::where('group_code','$relationDicGroup')->where('status',1)->column('title','code');\n\t\t\tcache('$cacheName',\$list,86400);\n\t\t}";
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']===true){
- $attr .="\n\t\t\$value = \$value??\$row['$field']??'';";
- $attr .="\n\t\t\$arr = explode(',',\$value);";
- $attr .="\n\t\t\$TxtArr = [];";
- $attr .="\n\t\tforeach(\$arr as \$item){\n\t\t\tif(isset(\$list[\$item])){\n\t\t\t\t\$TxtArr[] = \$list[\$item];\n\t\t\t}\n\t\t}";
- $attr .= "\n\t\treturn implode(',',\$TxtArr);\n\t}\n";
- $attr .="\tpublic function get{$functionName}Attr(\$value,\$row)\n\t";
- $attr .="{\n\t\t\$value = \$value??\$row['$field']??'';\n\t\treturn array_filter(explode(',',\$value));\n\t}\n";
- $attr .="\tpublic function set{$functionName}Attr(\$value,\$row)\n\t";
- $attr .="{\n\t\t return implode(',',\$value);\n\t}\n";
- }else{
- $attr .="\n\t\t\$value = \$value??\$row['$field']??'';";
- $attr .= "\n\t\treturn isset(\$list[\$value])?\$list[\$value]:'';\n\t}\n";
- }
- $append.="'".$field."_txt',";
- break;
- case 'user_auto':
- $functionName = self::getCamel($field);
- $relationField = $item['relation_field'];
- $relationShow = $item['relation_show'];
- $attr .="\tpublic function get{$functionName}TxtAttr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??0;\n";
- $attr .="\t\treturn \app\common\model\base\user\User::where('$relationField',\$value)->value('$relationShow');\n\t}\n";
- $append.="'".$field."_txt',";
- $attr .="\tpublic function $functionName()\n";
- $attr .="\t{\n\t\treturn \$this->belongsTo(\app\common\model\base\user\User::class,'$field','$relationField');\n\t}\n";
- break;
- case 'org_auto':
- $functionName = self::getCamel($field);
- $relationField = $item['relation_field'];
- $relationShow = $item['relation_show'];
- $attr .="\tpublic function get{$functionName}TxtAttr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$value = \$value??\$row['$field']??0;\n";
- $attr .="\t\treturn \app\common\model\base\org\Org::where('$relationField',\$value)->value('$relationShow');\n\t}\n";
- $append.="'".$field."_txt',";
- $attr .="\tpublic function $functionName()\n";
- $attr .="\t{\n\t\treturn \$this->belongsTo(\app\common\model\base\org\Org::class,'$field','$relationField');\n\t}\n";
- break;
- case 'user_select':
- $relationShow = $item['relation_show'];
- $relationField = $item['relation_field'];
- $functionName = self::getCamel($field);
- $cacheName = "USERLIST";
- $attr .="\tpublic function get{$functionName}TxtAttr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$list = cache('$cacheName')??[];\n";
- $attr .="\t\t\$value = \$value??\$row['$field']??'';\n";
- $attr .="\t\tif(empty(\$list)){\n\t\t\t\$list = \app\common\model\base\user\User::column('name,nickname','user_id');\n\t\t\tcache('$cacheName',\$list,86400);\n\t\t}\n";
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']===true){
- $attr .="\t\t\$arr = array_filter(explode(',',\$value));\n";
- $attr .="\t\t\$TxtArr=[];\n";
- $attr .="\t\tforeach(\$arr as \$item){\n\t\t\t\tif(isset(\$list[\$item])){\n\t\t\t\t\$TxtArr[] = \$list[\$item]['$relationShow'];\n\t\t\t}\n\t\t}\n";
- $attr .="\t\treturn implode(',',\$TxtArr);\n\t}\n";
- $attr .="\tpublic function get{$functionName}Attr(\$value,\$row){\n\t\t\$value = \$value??\$row['$field']??'';\n\t\treturn array_filter(explode(',',\$value));\n\t}\n";
- $attr .="\tpublic function set{$functionName}Attr(\$value,\$row)\n\t";
- $attr .="{\n\t\t return implode(',',\$value);\n\t}\n";
- }else{
- $attr .="\t\treturn isset(\$list[\$value])?\$list[\$value]['$relationShow']:'';\n\t}\n";
- }
- $append.="'".$field."_txt',";
- $attr .="\tpublic function $functionName()\n";
- $attr .="\t{\n\t\treturn \$this->belongsTo(\app\common\model\base\user\User::class,'$field','$relationField');\n\t}\n";
- break;
- case 'org_select':
- $relationShow = $item['relation_show'];
- $relationField = $item['relation_field'];
- $functionName = self::getCamel($field);
- $cacheName = "ORGLIST";
- $attr .="\tpublic function get{$functionName}TxtAttr(\$value,\$row)\n";
- $attr .="\t{\n\t\t\$list = cache('$cacheName')??[];\n";
- $attr .="\t\t\$value = \$value??\$row['$field']??'';\n";
- $attr .="\t\tif(empty(\$list)){\n\t\t\t\$list = \app\common\model\base\org\Org::column('name,short_name','org_id');\n\t\t\t\tcache('$cacheName',\$list,86400);\n\t\t}\n";
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']===true){
- $attr .="\t\t\$arr = array_filter(explode(',',\$value));\n";
- $attr .="\t\t\$TxtArr=[];\n";
- $attr .="\t\tforeach(\$arr as \$item){\n\t\t\tif(isset(\$list[\$item])){\n\t\t\t\t\$TxtArr[] = \$list[\$item]['$relationShow'];\n\t\t\t}\n\t\t}\n";
- $attr .="\t\treturn implode(',',\$TxtArr);\n\t}\n";
- $attr .="\tpublic function get{$functionName}Attr(\$value,\$row)\n\t{\n\t\t\$value = \$value??\$row['$field']??'';\n\t\treturn array_filter(explode(',',\$value));\n\t}\n";
- $attr .="\tpublic function set{$functionName}Attr(\$value,\$row)\n\t";
- $attr .="{\n\t\t return implode(',',\$value);\n\t}\n";
- }else{
- $attr .="\t\treturn isset(\$list[\$value])?\$list[\$value]['$relationShow']:'';\n\t}\n";
- }
- $append.="'".$field."_txt',";
- $attr .="\tpublic function $functionName()\n";
- $attr .="\t{\n\t\treturn \$this->belongsTo(\app\common\model\base\org\Org::class,'$field','$relationField');\n\t}\n";
- break;
- default:
- break;
- }
- }
- $append = rtrim($append,',');
- $append.="];\n";
- return $append.$attr;
- }
- /**
- * @title: 将字段的可选项content内容,重组为可写入文件的文本;
- * @desc: 描述
- * @param {*} $arr
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-18 17:56:37
- */
- static public function getTxtByContent($contentArr)
- {
- if(empty($contentArr)){
- return "";
- }
- $str = "[";
- foreach($contentArr as $content){
- $key = $content['key'];
- $val = $content['value'];
- $str .= "'$key'=>'$val',";
- }
- $str = rtrim($str,',');
- $str .= "]";
- return $str;
- }
- }
|