123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- <?php
- namespace app\common\model\base\models;
- /**
- * @title : 控制器代码生成服务
- * @desc :
- * @Author : Rock
- * @Date : 2023-04-19 10:05:44
- */
- class ControllerServer
- {
- /**
- * @title: 删除控制器文件
- * @desc: 描述
- * @param {int} $model_id
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-23 17:12:32
- */
- static public function deleteFile(int $model_id)
- {
- $info = ModelManage::where('id',$model_id)->find();
- $name = $info->name;
- $appname = app('http')->getName();// 获取应用名
- $classname = self::getCamel($info->name);//获取类名
- $dirname = self::parseDirname($info->dirname);// 文件保存位置
- if(!empty($dirname)){
- $path = root_path()."app".DS.$appname.DS."controller".DS.$dirname.DS;
- }else{
- $path = root_path()."app".DS.$appname.DS."controller".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:12:04
- */
- static public function createControllerFile(int $model_id,string $username,array $createList=[],string $showTitle='')
- {
- $info = ModelManage::where('id',$model_id)->find();
- $name = $info->name;
- $pk = ModelManage::pk($name);
- $dirname = self::parseDirname($info->dirname);// 文件保存位置
- $fileTitle = self::createTitle($info->title,$username);//生成文件注释
- $fieldList = ModelFields::where('model_name',$name)->where('status',1)->select()->toArray();//获取字段列表
- $classname = self::getCamel($info->name);//获取类名
- $appname = app('http')->getName();// 获取应用名
- $createWhere = self::createQueryWhere($fieldList,$username);//创建查询条件代码
- $getList = self::createGetList($fieldList,$username);//创建获取列表代码
- $getInfo = self::createGetInfo($fieldList,$username,$pk);//创建获取单个信息代码
- $doEdit = self::createDoEdit($fieldList,$username,$pk);//创建新增编辑代码
- $doDelete = self::createDoDelete($username,$pk,$fieldList);// 创建删除代码
- $getOptions = self::createGetOptions($fieldList,$classname,$username);//创建获取可选项代码
- $changeStatus = self::createChangeStatus($fieldList,$pk);//启用/禁用
- $doExport = self::createDoExpport($fieldList,$info->title,$username);//导出
- $getTree = self::createGetTree($fieldList,$username,$pk);
- if(!empty($dirname)){
- $path = root_path()."app".DS.$appname.DS."controller".DS.$dirname.DS;
- $namespace = "app\\$appname\controller\\".str_replace(DS,'\\',$dirname);
- }else{
- $path = root_path()."app".DS.$appname.DS."controller".DS;
- $namespace = "app\\$appname\controller";
- }
- if(!is_dir($path)){
- mkdir($path,0777,true);
- }
- $filename = $path.$classname.'.php';
- $dirNamespace = str_replace(DS,'\\',$dirname);
- $content = <<<CONTROLLER
- <?php
- namespace $namespace;
- $fileTitle
- use app\\$appname\controller\Base;
- use app\common\model\\{$dirNamespace}\\{$classname} as {$classname}Model;
- class $classname extends Base
- {
- protected \$model = null;
- protected \$noNeedLogin = ['getOptions'];
- protected \$noNeedAuth = [];
- public function initialize()
- {
- parent::initialize();
- \$this->model = new {$classname}Model;
- }
- $createWhere
- $getList
- $getTree
- $getInfo
- $doEdit
- $doDelete
- $getOptions
- $changeStatus
- $doExport
- public function __call(\$name,\$arguments)
- {
- return res(2,"方法{\$name}不存在");
- }
- }
- CONTROLLER;
- 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: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-18 15:13:19
- */
- static public function createTitle(string $title,$username)
- {
- $date = date('Y-m-d H:i:s');
- $title = <<<EOF
- /**
- * @title : $title 管理控制器
- * @desc :
- * @Author : $username
- * @Date : $date
- * @icon fa fa-leaf
- */
- EOF;
- return $title;
- }
- /**
- * @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: 描述
- * @param {array} $fieldList
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 10:35:03
- */
- static public function createQueryWhere(array $fieldList,string $username)
- {
- $str = "\tprivate function createWhere()\n\t{\n\t\t\$data = \$this->request->param();\n\t\t\$where = [];\n\t\t\$whereOr = [];\n";
- $keywordList = [];
- foreach($fieldList as $item){
- $field = $item['field'];
- $dbtype = $item['db_type'];
- $defaultValue = $item['default_value'];
- $title = $item['title'];
- $fieldType = $item['field_type'];
- switch($item['search_type']){
- case 'list':
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
- break;
- case 'relation':
- if(in_array($fieldType,['relate_single','relate_self'])){//关联单选,关联自己,关联字典
- $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
- }elseif('relate_dic'==$fieldType){//关联字典
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
- $str .= "\t\tif(!empty(\$data['$field']) && is_array(\$data['$field'])){\n\t\t\tforeach(\$data['$field'] as \$item){\n\t\t\t\t\$whereOr[] = ['$field','find in set',\$item];\n\t\t\t}\n\t\t}\n";
- }else{
- $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
- }
- }elseif('relate_multiple'==$fieldType){//关联多选
- $str .= "\t\tif(!empty(\$data['$field']) && is_array(\$data['$field'])){\n\t\t\tforeach(\$data['$field'] as \$item){\n\t\t\t\t\$whereOr[] = ['$field','find in set',\$item];\n\t\t\t}\n\t\t}\n";
- }elseif('user_auto'==$fieldType || 'org_auto'==$fieldType){//自动用户/自动组织
- $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
- }elseif('user_select'==$fieldType || 'org_select'==$fieldType){
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
- $str .="\t\tif(!empty(\$data['$field']) && is_array(\$data['$field'])){\n\t\t\tforeach(\$data['$field'] as \$item){\n\t\t\t\t\$whereOr[] = ['$field','find in set',\$item];\n\t\t\t}\n\t\t}\n";
- }else{
- $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
- }
- }
- break;
- case 'keyword':
- $keywordList[] = $field;
- break;
- case 'date':
- if('datetime'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$start = date('Y-m-d 00:00:00',strtotime(\$data['$field']));\n\t\t\t\$end = date('Y-m-d 23:59:59',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- }elseif('date'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$date = date('Y-m-d',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','=',\$date];\n\t\t}\n";
- }
- break;
- case 'daterange':
- $str .= "\t\tif(!empty(\$data['{$field}']) && count(\$data['{$field}'])==2){\n\t\t\t\$start = date('Y-m-d 00:00:00',strtotime(\$data['$field'][0]));\n\t\t\t\$end = date('Y-m-d 23:59:59',strtotime(\$data['$field'][1]));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- break;
- case 'datetime':
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$query = date('Y-m-d h:i:s',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','=',\$query];\n\t\t}\n";
- break;
- case 'datetimerange':
- $str .= "\t\tif(!empty(\$data['".$field."']) && count(\$data['".$field."'])==2){\n\t\t\t\$start = date('Y-m-d H:i:s',strtotime(\$data['".$field."'][0]));\n\t\t\t\$end = date('Y-m-d H:i:s',strtotime(\$data['".$field."'][1]));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- break;
- case 'month':
- if('datetime'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$start = date('Y-m-01 00:00:00',strtotime(\$data['$field']));\n\t\t\t\$end = date('Y-m-t 23:59:59',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- }elseif('date'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$start = date('Y-m-01',strtotime(\$data['$field']));\n\t\t\t\$end = date('Y-m-t',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- }
- case 'year':
- if('datetime'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$start = date('Y-01-01 00:00:00',strtotime(\$data['$field']));\n\t\t\t\$end = date('Y-12-31 23:59:59',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- }elseif('date'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$start = date('Y-01-01',strtotime(\$data['$field']));\n\t\t\t\$end = date('Y-12-31',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- }elseif('month'==$fieldType){
- $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$start = date('Y-01',strtotime(\$data['$field']));\n\t\t\t\$end = date('Y-12',strtotime(\$data['$field']));\n\t\t\t\$where[] = ['$field','BETWEEN',[\$start,\$end]];\n\t\t}\n";
- }
- break;
- }
- }
- if(!empty($keywordList)){
- $fields = implode('|',$keywordList);
- $str .= "\t\tif(!empty(\$data['keyword'])){\n\t\t\t\$keyword = \$data['keyword'];\n\t\t\t\$where[] = ['$fields','LIKE',\"%\$keyword%\"];\n\t\t}\n";
- }
- $str.="\t\treturn ['where'=>\$where,'whereOr'=>\$whereOr];\n\t}\n";
- $note = self::createNote('创建统一查询条件',$fieldList,$username);
- return $note.$str;
- }
- /**
- * @title: 创建新增/编辑的注释
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-25 18:22:15
- */
- static public function createEditNote(array $fieldList,string $username,string $pk)
- {
- $date = date('Y-m-d H:i:s');
- $params = "\t * @param {int} {{$pk}} {} {主键ID,不传或传0表示新增,大于0表示修改}\n";
- foreach($fieldList as $item){
- $field = $item['field'];
- $dbtype = $item['db_type'];
- $defaultValue = $item['default_value'];
- $title = $item['title'];
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }
- $note = "/**\n\t * @title:新增/编辑信息 \n\t * @desc: 描述\n$params\t * @return {*}\n\t * @author: $username\n\t * @method: POST\n\t * @Date: $date\n\t */\n";
- return $note;
- }
- /**
- * @title: 创建查询/导出的注释
- * @desc: 描述
- * @param {string} {methodTitles} {} {方法标题}
- * @param {array} {fieldList} {} {字段列表}
- * @param {string} {username} {} {当前登录用户}
- * @param {bool} {page} {} {是否生成页码参数}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-25 17:24:23
- */
- static public function createNote(string $methodTitle,array $fieldList,string $username,bool $page=false)
- {
- $date = date('Y-m-d H:i:s');
- $params = "";
- $keywordList = [];
- $keywordTitles = [];
- foreach($fieldList as $item){
- $field = $item['field'];
- $dbtype = $item['db_type'];
- $defaultValue = $item['default_value'];
- $fieldType = $item['field_type'];
- $title = $item['title'];
- if($item['search_type']=='list'){
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='keyword'){
- $keywordList[] = $field;
- $keywordTitles[] = $title;
- }elseif($item['search_type']=='date'){
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='daterange'){
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='datetime'){
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='datetimerange'){
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='month'){
- $params .="\t * @param {string} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='year'){
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }elseif($item['search_type']=='relation'){
- switch($fieldType){
- case 'relate_single':
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- break;
- case 'relate_multiple':
- $params .="\t * @param {array} {{$field}} {{$defaultValue}} {{$title}}\n";
- break;
- case 'relate_self':
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- break;
- case 'relate_dic':
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
- $params .="\t * @param {array} {{$field}} {{$defaultValue}} {{$title}}\n";
- }else{
- $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
- }
- break;
- case 'user_auto':
- case 'org_auto':
- $params .="\t * @param {int} {{$field}} {{$defaultValue}} {{title}}\n";
- break;
- case 'user_select':
- case 'org_select':
- if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
- $params .="\t * @param {array} {{$field}} {{$defaultValue}} {{$title}}\n";
- }else{
- $params .="\t * @param {int} {{$field}} {{$defaultValue}} {{$title}}\n";
- }
- break;
- }
- }
- }
- if(!empty($keywordList)){
- $titles = implode('|',$keywordTitles);
- $params .="\t * @param {string} {keyword} {} {搜索关键词,可搜索{$titles}}\n";
- }
- if($page){
- $params .="\t * @param {int} {pageNo} {0} {页码}\n";
- $params .="\t * @param {int} {pageSize} {10} {每页数量}\n";
- }
- $note = "/**\n\t * @title:$methodTitle\n\t * @desc: 描述\n$params\t * @return {*}\n\t * @author: $username\n\t * @method: POST\n\t * @Date: $date\n\t */\n";
- return $note;
- }
- /**
- * @title: 创建查询关联属性
- * @desc: 描述
- * @param {*} $fieldList
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-06-27 10:22:13
- */
- static public function createWith($fieldList)
- {
- $arr = [];
- foreach($fieldList as $field){
- if($field['field_type']=='relate_single'){
- $relationName = $field['relation_model'];
- $tbName = explode('_',$relationName);
- $lastTbName = end($tbName);
- $functionName = $field['field'] == $lastTbName.'_id'?self::getCamel($relationName):self::getCamel($field['field']);
- $arr[] = '\''.$functionName.'\'';
- }
- if($field['field_type']=='relate_self'){
- $arr[] = '\'Father\'';
- }
- }
- $str = '[';
- $str .= implode(',',$arr);
- $str .= ']';
- return $str;
- }
- /**
- * @title: 创建查询列表的代码
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 14:42:40
- */
- static public function createGetList($fieldList,$username)
- {
- $with = self::createWith($fieldList);
- $str = "\tpublic function getList(int \$pageNo=0,int \$pageSize=10)\n\t{\n\t\t\$whereAry = \$this->createWhere();\n\t\t";
- $str .= "if(!empty(\$pageNo)){\n\t\t\t\$res = \$this->model->where(\$whereAry['where'])->where(function(\$query)use(\$whereAry){\n\t\t\t\t\$query->whereOr(\$whereAry['whereOr']);\n\t\t\t})->with($with)->paginate(['page'=>\$pageNo,'list_rows'=>\$pageSize]);\n\t\t\treturn pageRes(1,'获取成功',\$res->total(),\$res->items());\n\t\t";
- $str .="}else{\n\t\t\t\$list = \$this->model->where(\$whereAry['where'])->where(function(\$query)use(\$whereAry){\n\t\t\t\t\$query->whereOr(\$whereAry['whereOr']);\n\t\t\t})->with($with)->select();\n\t\t\treturn res(1,'获取成功',\$list);\n\t\t}\n\t}\n";
- $note = self::createNote('查询数据列表',$fieldList,$username,true);
- return $note.$str;
- }
- /**
- * @title: 创建获取树状数据的代码
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-27 10:45:01
- */
- static public function createGetTree(array $fieldList,string $username,string $pk)
- {
- $date = date('Y-m-d H:i:s');
- $defaultLabel = '';
- foreach($fieldList as $item){
- if(false!==strpos($item['field'],'name') || false!==strpos($item['field'],'title') || false!==strpos($item['field'],'label')){
- $defaultLabel = $item['field'];
- break;
- }
- if('string'==$item['field_type']){
- $defaultLabel = $item['field'];
- }
- }
- $hasRelateSelf = false;
- foreach($fieldList as $item){
- if($item['field_type']=='relate_self'){
- $field = $item['field'];
- $defaultVal = in_array($item['db_type'],['int','smallint','bigint','tinyint'])?0:'';
- $str = "\tpublic function getTree(int \$$field=$defaultVal,string \$labelField='$defaultLabel')\n\t{\n\t\t\$fields = ['$pk','$field',\$labelField];\n\t\t\$where = [];\n\t\tif(!empty(\$$field)){\n\t\t\t\$where[] = ['$field','=',\$$field];\n\t\t}\n\t\t\$list = \$this->model->where(\$where)->field(\$fields)->select()->toArray();\n\t\t\$list = array2tree(\$list,'$field','id',\$$field);\n\t\treturn res(1,\"获取成功\",\$list);\n\t}\n";
- $hasRelateSelf = true;
- }
- }
- if(!$hasRelateSelf){
- $str = "\tpublic function getTree(\$labelField='$defaultLabel')\n\t{\n\t\t\$fields = ['$pk',\$labelField];\n\t\t\$where = [];\n\t\t\$list = \$this->model->where(\$where)->field(\$fields)->select();\n\t\treturn res(1,\"获取成功\",\$list);\n\t}\n";
- }
- $note = "/**\n\t * @title:选择器数据\n\t * @desc: 描述\n\t * @return {*}\n\t * @author: $username\n\t * @method: POST\n\t * @Date: $date\n\t */\n";
- return $note.$str;
- }
- /**
- * @title: 创建查询单条信息的代码
- * @desc: 描述
- * @param {*} $username
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-25 18:51:04
- */
- static public function createGetInfo($fieldList,$username,$pk)
- {
- $with = self::createWith($fieldList);
- $date = date('Y-m-d H:i:s');
- $params = "\t * @param {int} {{$pk}} {} {信息ID}\n";
- $str = "\tpublic function getInfo(int \$$pk=0)\n\t{\n\t\t\$info = \$this->model->with($with)->find(\$$pk);\n\t\treturn res(1,'获取成功',\$info);\n\t}\n";
- $note = "/**\n\t * @title:查询信息\n\t * @desc: 描述\n$params\t * @return {*}\n\t * @author: $username\n\t * @method: POST\n\t * @Date: $date\n\t */\n";
- return $note.$str;
- }
- /**
- * @title: 创建新增/编辑的代码
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 14:42:54
- */
- static public function createDoEdit(array $fieldList,string $username,string $pk)
- {
- $note = self::createEditNote($fieldList,$username,$pk);
- $str = "\tpublic function doEdit()\n\t{\n\t\t\$data = \$this->request->param();\n";
- foreach($fieldList as $fieldItem){
- $field = $fieldItem['field'];
- $auto_methods = $fieldItem['auto_methods'];
- if(!empty($auto_methods) && (in_array('ADD',$auto_methods) || in_array('EDIT',$auto_methods))){
- if(in_array('ADD',$auto_methods)){
- $str .="\t\tif(empty(\$data['$pk'])){\n";
- if('user_auto'==$fieldItem['field_type']){
- $str .="\t\t\t\$data['$field'] = \$this->userinfo->user_id;\n";
- }elseif('org_auto'==$fieldItem['field_type']){
- $str .="\t\t\t\$data['$field'] = \$this->userinfo->org_id;\n";
- }
- $str .="\t\t}\n";
- }
- if(in_array('EDIT',$auto_methods)){
- $str .="\t\tif(!empty(\$data['$pk'])){\n";
- if('user_auto'==$fieldItem['field_type']){
- $str .="\t\t\t\$data['$field'] = \$this->userinfo->user_id;\n";
- }elseif('org_auto'==$fieldItem['field_type']){
- $str .="\t\t\t\$data['$field'] = \$this->userinfo->org_id;\n";
- }
- $str .="\t\t}\n";
- }
- }
- }
- $str .="\t\t\$res = \$this->model->replace()->save(\$data);\n\t\treturn res(1,'编辑成功');\n\t}\n";
- return $note.$str;
- }
- /**
- * @title: 创建删除的代码
- * @desc: 描述
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 14:43:08
- */
- static public function createDoDelete($username,$pk,$fieldList)
- {
- $date = date('Y-m-d H:i:s');
- $params = "\t * @param {int} {$pk} {} {信息ID}\n";
- $str = "\tpublic function doDelete(int \$$pk=0)\n\t{\n";
- $str .="\t\t\$info = \$this->model->find(\$$pk);\n";
- $hasAuto = false;
- foreach($fieldList as $fieldItem){
- $field = $fieldItem['field'];
- $auto_methods = $fieldItem['auto_methods'];
- if(!empty($auto_methods) && in_array('DEL',$auto_methods)){
- if('user_auto'==$fieldItem['field_type']){
- $str .="\t\t\$info->$field = \$this->userinfo->user_id;\n";
- }elseif('org_auto'==$fieldItem['field_type']){
- $str .="\t\t\$info->$field = \$this->userinfo->org_id;\n";
- }
- $hasAuto = true;
- }
- }
- if($hasAuto){
- $str .="\t\t\$info->save();\n";
- }
- $str .="\t\t\$info->delete();\n";
- $str .="\t\treturn res(1,'删除成功');\n\t}\n";
- $note = "/**\n\t * @title:删除信息\n\t * @desc: 描述\n$params\t * @return {*}\n\t * @author: $username\n\t * @method: POST\n\t * @Date: $date\n\t */\n";
- return $note.$str;
- }
- /**
- * @title: 创建获取可选项的代码
- * @desc: 描述
- * @param {array} $fieldList
- * @param {string} $classname
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 14:43:20
- */
- static public function createGetOptions(array $fieldList,string $classname,string $username)
- {
- $fieldTxt = "";
- foreach($fieldList as $item){
- if(in_array($item['field_type'],['radio','select','switch','selects','checkbox'])){
- $camel = self::getCamel($item['field']);
- $field = $camel.'List';
- $fieldTxt .= "\t\t\t'$field'=>{$classname}Model::{$field}(),\n";
- }
- }
- $date = date('Y-m-d H:i:s');
- $fieldTxt = rtrim($fieldTxt,",\n");
- $str = "\tpublic function getOptions()\n\t{\n\t\t\$data = [\n".$fieldTxt."\n\t\t];\n\t\treturn res(1,'获取成功',\$data);\n\t}\n";
- $note = "/**\n\t * @title:获取可选项\n\t * @desc: 描述\n\t * @return {*}\n\t * @author: $username\n\t * @method: POST\n\t * @Date: $date\n\t */\n";
- return $note.$str;
- }
- /**
- * @title: 创建启用/禁用的代码
- * @desc: 描述
- * @param {*} $fieldList
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 14:43:35
- */
- static public function createChangeStatus($fieldList,$pk)
- {
- $str = "";
- $hasStatus = false;
- $autoUserListOn = [];
- $autoUserListOff = [];
- $autoOrgListOn = [];
- $autoOrgListOff = [];
- foreach($fieldList as $item){
- if($item['field']=='status' && $item['field_type']=='switch'){
- $hasStatus = true;
- }
- if('user_auto'==$item['field_type']){
- if(in_array('ON',$item['auto_methods'])){
- $autoUserListOn[] = $item['field'];
- }
- if(in_array('OFF',$item['auto_methods'])){
- $autoUserListOff[] = $item['field'];
- }
- }
- if('org_auto'==$item['field_type']){
- if(in_array('ON',$item['auto_methods'])){
- $autoOrgListOn[] = $item['field'];
- }
- if(in_array('OFF',$item['auto_methods'])){
- $autoOrgListOff[] = $item['field'];
- }
- }
- }
- if($hasStatus){
- $str .="public function changeStatus(int \$$pk=0)\n\t{\n";
- $str .="\t\t\$info = \$this->model->find(\$$pk);\n";
- $str .="\t\tif(!\$info){\n\t\t\treturn res(2,\"未找到此记录\");\n\t\t}\n";
- $str .="\t\t\$info->status = abs(3 * \$info->status - 5);\n";
- if(!empty($autoUserListOn) || !empty($autoOrgListOn)){
- $str .="\t\tif(\$info->status==1){\n";
- foreach($autoUserListOn as $field){
- $str .="\t\t\t\$info->$field = \$this->userinfo->user_id;\n";
- }
- foreach($autoOrgListOn as $field){
- $str .="\t\t\t\$info->$field = \$this->userinfo->org_id;\n";
- }
- $str .="\t\t}\n";
- }
- if(!empty($autoUserListOff) || !empty($autoOrgListOff)){
- $str .="\t\tif(\$info->status==2){\n";
- foreach($autoUserListOff as $field){
- $str .= "\t\t\t\$info->$field = \$this->userinfo->user_id;\n";
- }
- foreach($autoOrgListOff as $field){
- $str .="\t\t\t\$info->$field = \$this->userinfo->org_id;\n";
- }
- $str .="\t\t}\n";
- }
- $str .="\t\t\$info->save();\n\t\treturn res(1,\"操作成功\");\n\t}";
- }
- return $str;
- }
- /**
- * @title: 创建导出的代码
- * @desc: 描述
- * @param {*} $fieldList
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-19 14:43:51
- */
- static public function createDoExpport(array $fieldList,string $modelTitle,string $username)
- {
- $with = self::createWith($fieldList);
- $note = self::createNote('导出数据',$fieldList,$username);
- $str = "private \$fieldList = [\n";
- $str .= "\t\t'index'=>'序号',\n";
- foreach($fieldList as $item){
- $field = $item['field'];
- $title = $item['title'];
- $str.="\t\t'$field'=>'$title',\n";
- }
- $fieldList = array_column($fieldList,null,'field');
- $str .= "\t\t'create_at'=>'创建时间',\n";
- $str .= "\t\t'update_at'=>'更新时间',\n";
- $str = rtrim($str,",\n")."\n\t];\n\t";
- $str .=$note;
- $str .="\tpublic function doExport()\n";
- $str .="\t{\n\t\t\$whereAry = \$this->createWhere();\n\t\t\$list = \$this->model->where(\$whereAry['where'])->where(function(\$query)use(\$whereAry){\n\t\t\t\t\$query->whereOr(\$whereAry['whereOr']);\n\t\t\t})->with($with)->select()->toArray();\n";
- $str .="\t\t\$head = array_values(\$this->fieldList);\n";
- $str .="\t\t\$body = [];\n";
- $str .="\t\tforeach(\$list as \$key=>\$item){\n";
- $str .="\t\t\t\$index = \$key + 1;\n";
- $str .="\t\t\tforeach(\$this->fieldList as \$k=>\$v){\n\t\t\t\tif(\$k=='index'){\n\t\t\t\t\t\$body[\$index][] = \$index;\n\t\t\t\t";
- foreach($fieldList as $fieldItem){
- if($fieldItem['field_type']=='relate_single'){
- $relationName = $fieldItem['relation_model'];
- $tbName = explode('_',$relationName);
- $lastTbName = end($tbName);
- $relation = $fieldItem['field']==$lastTbName.'_id'?self::getCamel($relationName):self::getCamel($fieldItem['field']);
- $relationShow = $fieldItem['relation_show'];
- $str .="}elseif(\$k=='{$fieldItem['field']}'){\n\t\t\t\t\t\$body[\$index][]=\$item['$relation']?\$item['$relation']['$relationShow']:'';\n\t\t\t\t";
- }elseif($fieldItem['field_type']=='relate_self'){
- $relationShow = $fieldItem['relation_show'];
- $str .="}elseif(\$k=='{$fieldItem['field']}'){\n\t\t\t\t\t\$body[\$index][]=\$item['Father']?\$item['Father']['$relationShow']:'';\n\t\t\t\t";
- }elseif($fieldItem['field_type']=='json'){
- $str .="}elseif(\$k=='{$fieldItem['field']}'){\n\t\t\t\t\t\$body[\$index][]=\$item[\$k]?json_encode(\$item[\$k]):'';\n\t\t\t\t";
- }
- }
- $str .="}else{\n\t\t\t\t\t";
- $str .="\$body[\$index][] = isset(\$item[\$k.'_txt'])?(is_array(\$item[\$k.'_txt'])?implode(',',\$item[\$k.'_txt']):\$item[\$k.'_txt']):\$item[\$k];\n\t\t\t\t}\n\t\t\t}\n";
- $str .="\t\t}\n";
- $str .="\t\t//创建文件夹\n\t\t\$basepath = 'uploads'.DS.'download'.DS.date('Ymd');\n\t\t\$savepath = public_path().\$basepath;\n\t\tif(!is_dir(\$savepath)){\n\t\t\t@mkdir(\$savepath,0777,true);\n\t\t}\n";
- $str .="\t\t//保存文件\n\t\t\$filename = time().GetRandStr().'.xls';\n\t\t\$path = \$savepath.DS.\$filename;\n\t\t\$export_help = new \\excel\\MultiHeadExcel();\n\t\t\$export_help->export_help(\$body,\$head,\$path);\n";
- $str .="\t\t//返回路径\n\t\t\$returnpath = WEBURL.DS.\$basepath.DS.\$filename;\n\t\tslog(1,'导出了$modelTitle');\n\t\treturn res(1,'获取成功',['url'=>\$returnpath,'name'=>\$filename]);\n";
- $str .="\t}";
- return $str;
- }
- }
|