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 = <<'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; } }