ControllerServer.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. namespace app\common\model\base\models;
  3. /**
  4. * @title : 控制器代码生成服务
  5. * @desc :
  6. * @Author : Rock
  7. * @Date : 2023-04-19 10:05:44
  8. */
  9. class ControllerServer
  10. {
  11. /**
  12. * @title: 删除控制器文件
  13. * @desc: 描述
  14. * @param {int} $model_id
  15. * @return {*}
  16. * @author: Rock
  17. * @method: POST
  18. * @Date: 2023-04-23 17:12:32
  19. */
  20. static public function deleteFile(int $model_id)
  21. {
  22. $info = ModelManage::where('id',$model_id)->find();
  23. $name = $info->name;
  24. $appname = app('http')->getName();// 获取应用名
  25. $classname = self::getCamel($info->name);//获取类名
  26. $dirname = self::parseDirname($info->dirname);// 文件保存位置
  27. if(!empty($dirname)){
  28. $path = root_path()."app".DS.$appname.DS."controller".DS.$dirname.DS;
  29. }else{
  30. $path = root_path()."app".DS.$appname.DS."controller".DS;
  31. }
  32. $filename = $path.$classname.'.php';
  33. if(file_exists($filename)){
  34. @unlink($filename);
  35. }
  36. }
  37. /**
  38. * @title: 生成控制器文件
  39. * @desc: 描述
  40. * @param {int} $model_id
  41. * @return {*}
  42. * @author: Rock
  43. * @method: POST
  44. * @Date: 2023-04-23 17:12:04
  45. */
  46. static public function createControllerFile(int $model_id,string $username,array $createList=[],string $showTitle='')
  47. {
  48. $info = ModelManage::where('id',$model_id)->find();
  49. $name = $info->name;
  50. $pk = ModelManage::pk($name);
  51. $dirname = self::parseDirname($info->dirname);// 文件保存位置
  52. $fileTitle = self::createTitle($info->title,$username);//生成文件注释
  53. $fieldList = ModelFields::where('model_name',$name)->where('status',1)->select()->toArray();//获取字段列表
  54. $classname = self::getCamel($info->name);//获取类名
  55. $appname = app('http')->getName();// 获取应用名
  56. $createWhere = self::createQueryWhere($fieldList,$username);//创建查询条件代码
  57. $getList = self::createGetList($fieldList,$username);//创建获取列表代码
  58. $getInfo = self::createGetInfo($fieldList,$username,$pk);//创建获取单个信息代码
  59. $doEdit = self::createDoEdit($fieldList,$username,$pk);//创建新增编辑代码
  60. $doDelete = self::createDoDelete($username,$pk,$fieldList);// 创建删除代码
  61. $getOptions = self::createGetOptions($fieldList,$classname,$username);//创建获取可选项代码
  62. $changeStatus = self::createChangeStatus($fieldList,$pk);//启用/禁用
  63. $doExport = self::createDoExpport($fieldList,$info->title,$username);//导出
  64. $getTree = self::createGetTree($fieldList,$username,$pk);
  65. if(!empty($dirname)){
  66. $path = root_path()."app".DS.$appname.DS."controller".DS.$dirname.DS;
  67. $namespace = "app\\$appname\controller\\".str_replace(DS,'\\',$dirname);
  68. }else{
  69. $path = root_path()."app".DS.$appname.DS."controller".DS;
  70. $namespace = "app\\$appname\controller";
  71. }
  72. if(!is_dir($path)){
  73. mkdir($path,0777,true);
  74. }
  75. $filename = $path.$classname.'.php';
  76. $dirNamespace = str_replace(DS,'\\',$dirname);
  77. $content = <<<CONTROLLER
  78. <?php
  79. namespace $namespace;
  80. $fileTitle
  81. use app\\$appname\controller\Base;
  82. use app\common\model\\{$dirNamespace}\\{$classname} as {$classname}Model;
  83. class $classname extends Base
  84. {
  85. protected \$model = null;
  86. protected \$noNeedLogin = ['getOptions'];
  87. protected \$noNeedAuth = [];
  88. public function initialize()
  89. {
  90. parent::initialize();
  91. \$this->model = new {$classname}Model;
  92. }
  93. $createWhere
  94. $getList
  95. $getTree
  96. $getInfo
  97. $doEdit
  98. $doDelete
  99. $getOptions
  100. $changeStatus
  101. $doExport
  102. public function __call(\$name,\$arguments)
  103. {
  104. return res(2,"方法{\$name}不存在");
  105. }
  106. }
  107. CONTROLLER;
  108. FileServer::writeLine($filename,$content);
  109. }
  110. /**
  111. * @title: 格式化dir路径
  112. * @desc: 描述
  113. * @param {string} $dirname
  114. * @return {*}
  115. * @author: Rock
  116. * @method: POST
  117. * @Date: 2023-04-24 14:46:37
  118. */
  119. static public function parseDirname(string $dirname)
  120. {
  121. return !empty($dirname)?explode(DS,str_replace(['/','\\'],DS,$dirname))[0]:'';
  122. }
  123. /**
  124. * @title: 生成控制器注释
  125. * @desc: 描述
  126. * @return {*}
  127. * @author: Rock
  128. * @method: POST
  129. * @Date: 2023-04-18 15:13:19
  130. */
  131. static public function createTitle(string $title,$username)
  132. {
  133. $date = date('Y-m-d H:i:s');
  134. $title = <<<EOF
  135. /**
  136. * @title : $title 管理控制器
  137. * @desc :
  138. * @Author : $username
  139. * @Date : $date
  140. * @icon fa fa-leaf
  141. */
  142. EOF;
  143. return $title;
  144. }
  145. /**
  146. * @title: 根据数据表名生成类名
  147. * @desc: 描述
  148. * @param {string} {name} {} {数据表名}
  149. * @return {*}
  150. * @author: Rock
  151. * @method: POST
  152. * @Date: 2023-04-18 17:09:13
  153. */
  154. static public function getCamel(string $name)
  155. {
  156. $arr = explode('_',$name);
  157. $arr = array_filter($arr);
  158. $classname = "";
  159. foreach($arr as $item){
  160. $classname .= ucfirst($item);
  161. }
  162. return $classname;
  163. }
  164. /**
  165. * @title: 创建统一查询条件的代码
  166. * @desc: 描述
  167. * @param {array} $fieldList
  168. * @return {*}
  169. * @author: Rock
  170. * @method: POST
  171. * @Date: 2023-04-19 10:35:03
  172. */
  173. static public function createQueryWhere(array $fieldList,string $username)
  174. {
  175. $str = "\tprivate function createWhere()\n\t{\n\t\t\$data = \$this->request->param();\n\t\t\$where = [];\n\t\t\$whereOr = [];\n";
  176. $keywordList = [];
  177. foreach($fieldList as $item){
  178. $field = $item['field'];
  179. $dbtype = $item['db_type'];
  180. $defaultValue = $item['default_value'];
  181. $title = $item['title'];
  182. $fieldType = $item['field_type'];
  183. switch($item['search_type']){
  184. case 'list':
  185. $str .="\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
  186. break;
  187. case 'relation':
  188. if(in_array($fieldType,['relate_single','relate_self'])){//关联单选,关联自己,关联字典
  189. $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
  190. }elseif('relate_dic'==$fieldType){//关联字典
  191. if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
  192. $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";
  193. }else{
  194. $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
  195. }
  196. }elseif('relate_multiple'==$fieldType){//关联多选
  197. $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";
  198. }elseif('user_auto'==$fieldType || 'org_auto'==$fieldType){//自动用户/自动组织
  199. $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
  200. }elseif('user_select'==$fieldType || 'org_select'==$fieldType){
  201. if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
  202. $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";
  203. }else{
  204. $str .= "\t\tif(!empty(\$data['$field'])){\n\t\t\t\$where[] = ['$field','=',\$data['$field']];\n\t\t}\n";
  205. }
  206. }
  207. break;
  208. case 'keyword':
  209. $keywordList[] = $field;
  210. break;
  211. case 'date':
  212. if('datetime'==$fieldType){
  213. $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";
  214. }elseif('date'==$fieldType){
  215. $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";
  216. }
  217. break;
  218. case 'daterange':
  219. $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";
  220. break;
  221. case 'datetime':
  222. $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";
  223. break;
  224. case 'datetimerange':
  225. $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";
  226. break;
  227. case 'month':
  228. if('datetime'==$fieldType){
  229. $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";
  230. }elseif('date'==$fieldType){
  231. $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";
  232. }
  233. case 'year':
  234. if('datetime'==$fieldType){
  235. $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";
  236. }elseif('date'==$fieldType){
  237. $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";
  238. }elseif('month'==$fieldType){
  239. $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";
  240. }
  241. break;
  242. }
  243. }
  244. if(!empty($keywordList)){
  245. $fields = implode('|',$keywordList);
  246. $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";
  247. }
  248. $str.="\t\treturn ['where'=>\$where,'whereOr'=>\$whereOr];\n\t}\n";
  249. $note = self::createNote('创建统一查询条件',$fieldList,$username);
  250. return $note.$str;
  251. }
  252. /**
  253. * @title: 创建新增/编辑的注释
  254. * @desc: 描述
  255. * @return {*}
  256. * @author: Rock
  257. * @method: POST
  258. * @Date: 2023-04-25 18:22:15
  259. */
  260. static public function createEditNote(array $fieldList,string $username,string $pk)
  261. {
  262. $date = date('Y-m-d H:i:s');
  263. $params = "\t * @param {int} {{$pk}} {} {主键ID,不传或传0表示新增,大于0表示修改}\n";
  264. foreach($fieldList as $item){
  265. $field = $item['field'];
  266. $dbtype = $item['db_type'];
  267. $defaultValue = $item['default_value'];
  268. $title = $item['title'];
  269. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  270. }
  271. $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";
  272. return $note;
  273. }
  274. /**
  275. * @title: 创建查询/导出的注释
  276. * @desc: 描述
  277. * @param {string} {methodTitles} {} {方法标题}
  278. * @param {array} {fieldList} {} {字段列表}
  279. * @param {string} {username} {} {当前登录用户}
  280. * @param {bool} {page} {} {是否生成页码参数}
  281. * @return {*}
  282. * @author: Rock
  283. * @method: POST
  284. * @Date: 2023-04-25 17:24:23
  285. */
  286. static public function createNote(string $methodTitle,array $fieldList,string $username,bool $page=false)
  287. {
  288. $date = date('Y-m-d H:i:s');
  289. $params = "";
  290. $keywordList = [];
  291. $keywordTitles = [];
  292. foreach($fieldList as $item){
  293. $field = $item['field'];
  294. $dbtype = $item['db_type'];
  295. $defaultValue = $item['default_value'];
  296. $fieldType = $item['field_type'];
  297. $title = $item['title'];
  298. if($item['search_type']=='list'){
  299. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  300. }elseif($item['search_type']=='keyword'){
  301. $keywordList[] = $field;
  302. $keywordTitles[] = $title;
  303. }elseif($item['search_type']=='date'){
  304. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  305. }elseif($item['search_type']=='daterange'){
  306. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  307. }elseif($item['search_type']=='datetime'){
  308. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  309. }elseif($item['search_type']=='datetimerange'){
  310. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  311. }elseif($item['search_type']=='month'){
  312. $params .="\t * @param {string} {{$field}} {{$defaultValue}} {{$title}}\n";
  313. }elseif($item['search_type']=='year'){
  314. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  315. }elseif($item['search_type']=='relation'){
  316. switch($fieldType){
  317. case 'relate_single':
  318. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  319. break;
  320. case 'relate_multiple':
  321. $params .="\t * @param {array} {{$field}} {{$defaultValue}} {{$title}}\n";
  322. break;
  323. case 'relate_self':
  324. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  325. break;
  326. case 'relate_dic':
  327. if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
  328. $params .="\t * @param {array} {{$field}} {{$defaultValue}} {{$title}}\n";
  329. }else{
  330. $params .="\t * @param {{$dbtype}} {{$field}} {{$defaultValue}} {{$title}}\n";
  331. }
  332. break;
  333. case 'user_auto':
  334. case 'org_auto':
  335. $params .="\t * @param {int} {{$field}} {{$defaultValue}} {{title}}\n";
  336. break;
  337. case 'user_select':
  338. case 'org_select':
  339. if(isset($item['extra_rule']['multiple']) && $item['extra_rule']['multiple']==true){
  340. $params .="\t * @param {array} {{$field}} {{$defaultValue}} {{$title}}\n";
  341. }else{
  342. $params .="\t * @param {int} {{$field}} {{$defaultValue}} {{$title}}\n";
  343. }
  344. break;
  345. }
  346. }
  347. }
  348. if(!empty($keywordList)){
  349. $titles = implode('|',$keywordTitles);
  350. $params .="\t * @param {string} {keyword} {} {搜索关键词,可搜索{$titles}}\n";
  351. }
  352. if($page){
  353. $params .="\t * @param {int} {pageNo} {0} {页码}\n";
  354. $params .="\t * @param {int} {pageSize} {10} {每页数量}\n";
  355. }
  356. $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";
  357. return $note;
  358. }
  359. /**
  360. * @title: 创建查询关联属性
  361. * @desc: 描述
  362. * @param {*} $fieldList
  363. * @return {*}
  364. * @author: Rock
  365. * @method: POST
  366. * @Date: 2023-06-27 10:22:13
  367. */
  368. static public function createWith($fieldList)
  369. {
  370. $arr = [];
  371. foreach($fieldList as $field){
  372. if($field['field_type']=='relate_single'){
  373. $relationName = $field['relation_model'];
  374. $tbName = explode('_',$relationName);
  375. $lastTbName = end($tbName);
  376. $functionName = $field['field'] == $lastTbName.'_id'?self::getCamel($relationName):self::getCamel($field['field']);
  377. $arr[] = '\''.$functionName.'\'';
  378. }
  379. if($field['field_type']=='relate_self'){
  380. $arr[] = '\'Father\'';
  381. }
  382. }
  383. $str = '[';
  384. $str .= implode(',',$arr);
  385. $str .= ']';
  386. return $str;
  387. }
  388. /**
  389. * @title: 创建查询列表的代码
  390. * @desc: 描述
  391. * @return {*}
  392. * @author: Rock
  393. * @method: POST
  394. * @Date: 2023-04-19 14:42:40
  395. */
  396. static public function createGetList($fieldList,$username)
  397. {
  398. $with = self::createWith($fieldList);
  399. $str = "\tpublic function getList(int \$pageNo=0,int \$pageSize=10)\n\t{\n\t\t\$whereAry = \$this->createWhere();\n\t\t";
  400. $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";
  401. $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";
  402. $note = self::createNote('查询数据列表',$fieldList,$username,true);
  403. return $note.$str;
  404. }
  405. /**
  406. * @title: 创建获取树状数据的代码
  407. * @desc: 描述
  408. * @return {*}
  409. * @author: Rock
  410. * @method: POST
  411. * @Date: 2023-04-27 10:45:01
  412. */
  413. static public function createGetTree(array $fieldList,string $username,string $pk)
  414. {
  415. $date = date('Y-m-d H:i:s');
  416. $defaultLabel = '';
  417. foreach($fieldList as $item){
  418. if(false!==strpos($item['field'],'name') || false!==strpos($item['field'],'title') || false!==strpos($item['field'],'label')){
  419. $defaultLabel = $item['field'];
  420. break;
  421. }
  422. if('string'==$item['field_type']){
  423. $defaultLabel = $item['field'];
  424. }
  425. }
  426. $hasRelateSelf = false;
  427. foreach($fieldList as $item){
  428. if($item['field_type']=='relate_self'){
  429. $field = $item['field'];
  430. $defaultVal = in_array($item['db_type'],['int','smallint','bigint','tinyint'])?0:'';
  431. $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";
  432. $hasRelateSelf = true;
  433. }
  434. }
  435. if(!$hasRelateSelf){
  436. $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";
  437. }
  438. $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";
  439. return $note.$str;
  440. }
  441. /**
  442. * @title: 创建查询单条信息的代码
  443. * @desc: 描述
  444. * @param {*} $username
  445. * @return {*}
  446. * @author: Rock
  447. * @method: POST
  448. * @Date: 2023-04-25 18:51:04
  449. */
  450. static public function createGetInfo($fieldList,$username,$pk)
  451. {
  452. $with = self::createWith($fieldList);
  453. $date = date('Y-m-d H:i:s');
  454. $params = "\t * @param {int} {{$pk}} {} {信息ID}\n";
  455. $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";
  456. $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";
  457. return $note.$str;
  458. }
  459. /**
  460. * @title: 创建新增/编辑的代码
  461. * @desc: 描述
  462. * @return {*}
  463. * @author: Rock
  464. * @method: POST
  465. * @Date: 2023-04-19 14:42:54
  466. */
  467. static public function createDoEdit(array $fieldList,string $username,string $pk)
  468. {
  469. $note = self::createEditNote($fieldList,$username,$pk);
  470. $str = "\tpublic function doEdit()\n\t{\n\t\t\$data = \$this->request->param();\n";
  471. foreach($fieldList as $fieldItem){
  472. $field = $fieldItem['field'];
  473. $auto_methods = $fieldItem['auto_methods'];
  474. if(!empty($auto_methods) && (in_array('ADD',$auto_methods) || in_array('EDIT',$auto_methods))){
  475. if(in_array('ADD',$auto_methods)){
  476. $str .="\t\tif(empty(\$data['$pk'])){\n";
  477. if('user_auto'==$fieldItem['field_type']){
  478. $str .="\t\t\t\$data['$field'] = \$this->userinfo->user_id;\n";
  479. }elseif('org_auto'==$fieldItem['field_type']){
  480. $str .="\t\t\t\$data['$field'] = \$this->userinfo->org_id;\n";
  481. }
  482. $str .="\t\t}\n";
  483. }
  484. if(in_array('EDIT',$auto_methods)){
  485. $str .="\t\tif(!empty(\$data['$pk'])){\n";
  486. if('user_auto'==$fieldItem['field_type']){
  487. $str .="\t\t\t\$data['$field'] = \$this->userinfo->user_id;\n";
  488. }elseif('org_auto'==$fieldItem['field_type']){
  489. $str .="\t\t\t\$data['$field'] = \$this->userinfo->org_id;\n";
  490. }
  491. $str .="\t\t}\n";
  492. }
  493. }
  494. }
  495. $str .="\t\t\$res = \$this->model->replace()->save(\$data);\n\t\treturn res(1,'编辑成功');\n\t}\n";
  496. return $note.$str;
  497. }
  498. /**
  499. * @title: 创建删除的代码
  500. * @desc: 描述
  501. * @return {*}
  502. * @author: Rock
  503. * @method: POST
  504. * @Date: 2023-04-19 14:43:08
  505. */
  506. static public function createDoDelete($username,$pk,$fieldList)
  507. {
  508. $date = date('Y-m-d H:i:s');
  509. $params = "\t * @param {int} {$pk} {} {信息ID}\n";
  510. $str = "\tpublic function doDelete(int \$$pk=0)\n\t{\n";
  511. $str .="\t\t\$info = \$this->model->find(\$$pk);\n";
  512. $hasAuto = false;
  513. foreach($fieldList as $fieldItem){
  514. $field = $fieldItem['field'];
  515. $auto_methods = $fieldItem['auto_methods'];
  516. if(!empty($auto_methods) && in_array('DEL',$auto_methods)){
  517. if('user_auto'==$fieldItem['field_type']){
  518. $str .="\t\t\$info->$field = \$this->userinfo->user_id;\n";
  519. }elseif('org_auto'==$fieldItem['field_type']){
  520. $str .="\t\t\$info->$field = \$this->userinfo->org_id;\n";
  521. }
  522. $hasAuto = true;
  523. }
  524. }
  525. if($hasAuto){
  526. $str .="\t\t\$info->save();\n";
  527. }
  528. $str .="\t\t\$info->delete();\n";
  529. $str .="\t\treturn res(1,'删除成功');\n\t}\n";
  530. $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";
  531. return $note.$str;
  532. }
  533. /**
  534. * @title: 创建获取可选项的代码
  535. * @desc: 描述
  536. * @param {array} $fieldList
  537. * @param {string} $classname
  538. * @return {*}
  539. * @author: Rock
  540. * @method: POST
  541. * @Date: 2023-04-19 14:43:20
  542. */
  543. static public function createGetOptions(array $fieldList,string $classname,string $username)
  544. {
  545. $fieldTxt = "";
  546. foreach($fieldList as $item){
  547. if(in_array($item['field_type'],['radio','select','switch','selects','checkbox'])){
  548. $camel = self::getCamel($item['field']);
  549. $field = $camel.'List';
  550. $fieldTxt .= "\t\t\t'$field'=>{$classname}Model::{$field}(),\n";
  551. }
  552. }
  553. $date = date('Y-m-d H:i:s');
  554. $fieldTxt = rtrim($fieldTxt,",\n");
  555. $str = "\tpublic function getOptions()\n\t{\n\t\t\$data = [\n".$fieldTxt."\n\t\t];\n\t\treturn res(1,'获取成功',\$data);\n\t}\n";
  556. $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";
  557. return $note.$str;
  558. }
  559. /**
  560. * @title: 创建启用/禁用的代码
  561. * @desc: 描述
  562. * @param {*} $fieldList
  563. * @return {*}
  564. * @author: Rock
  565. * @method: POST
  566. * @Date: 2023-04-19 14:43:35
  567. */
  568. static public function createChangeStatus($fieldList,$pk)
  569. {
  570. $str = "";
  571. $hasStatus = false;
  572. $autoUserListOn = [];
  573. $autoUserListOff = [];
  574. $autoOrgListOn = [];
  575. $autoOrgListOff = [];
  576. foreach($fieldList as $item){
  577. if($item['field']=='status' && $item['field_type']=='switch'){
  578. $hasStatus = true;
  579. }
  580. if('user_auto'==$item['field_type']){
  581. if(in_array('ON',$item['auto_methods'])){
  582. $autoUserListOn[] = $item['field'];
  583. }
  584. if(in_array('OFF',$item['auto_methods'])){
  585. $autoUserListOff[] = $item['field'];
  586. }
  587. }
  588. if('org_auto'==$item['field_type']){
  589. if(in_array('ON',$item['auto_methods'])){
  590. $autoOrgListOn[] = $item['field'];
  591. }
  592. if(in_array('OFF',$item['auto_methods'])){
  593. $autoOrgListOff[] = $item['field'];
  594. }
  595. }
  596. }
  597. if($hasStatus){
  598. $str .="public function changeStatus(int \$$pk=0)\n\t{\n";
  599. $str .="\t\t\$info = \$this->model->find(\$$pk);\n";
  600. $str .="\t\tif(!\$info){\n\t\t\treturn res(2,\"未找到此记录\");\n\t\t}\n";
  601. $str .="\t\t\$info->status = abs(3 * \$info->status - 5);\n";
  602. if(!empty($autoUserListOn) || !empty($autoOrgListOn)){
  603. $str .="\t\tif(\$info->status==1){\n";
  604. foreach($autoUserListOn as $field){
  605. $str .="\t\t\t\$info->$field = \$this->userinfo->user_id;\n";
  606. }
  607. foreach($autoOrgListOn as $field){
  608. $str .="\t\t\t\$info->$field = \$this->userinfo->org_id;\n";
  609. }
  610. $str .="\t\t}\n";
  611. }
  612. if(!empty($autoUserListOff) || !empty($autoOrgListOff)){
  613. $str .="\t\tif(\$info->status==2){\n";
  614. foreach($autoUserListOff as $field){
  615. $str .= "\t\t\t\$info->$field = \$this->userinfo->user_id;\n";
  616. }
  617. foreach($autoOrgListOff as $field){
  618. $str .="\t\t\t\$info->$field = \$this->userinfo->org_id;\n";
  619. }
  620. $str .="\t\t}\n";
  621. }
  622. $str .="\t\t\$info->save();\n\t\treturn res(1,\"操作成功\");\n\t}";
  623. }
  624. return $str;
  625. }
  626. /**
  627. * @title: 创建导出的代码
  628. * @desc: 描述
  629. * @param {*} $fieldList
  630. * @return {*}
  631. * @author: Rock
  632. * @method: POST
  633. * @Date: 2023-04-19 14:43:51
  634. */
  635. static public function createDoExpport(array $fieldList,string $modelTitle,string $username)
  636. {
  637. $with = self::createWith($fieldList);
  638. $note = self::createNote('导出数据',$fieldList,$username);
  639. $str = "private \$fieldList = [\n";
  640. $str .= "\t\t'index'=>'序号',\n";
  641. foreach($fieldList as $item){
  642. $field = $item['field'];
  643. $title = $item['title'];
  644. $str.="\t\t'$field'=>'$title',\n";
  645. }
  646. $fieldList = array_column($fieldList,null,'field');
  647. $str .= "\t\t'create_at'=>'创建时间',\n";
  648. $str .= "\t\t'update_at'=>'更新时间',\n";
  649. $str = rtrim($str,",\n")."\n\t];\n\t";
  650. $str .=$note;
  651. $str .="\tpublic function doExport()\n";
  652. $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";
  653. $str .="\t\t\$head = array_values(\$this->fieldList);\n";
  654. $str .="\t\t\$body = [];\n";
  655. $str .="\t\tforeach(\$list as \$key=>\$item){\n";
  656. $str .="\t\t\t\$index = \$key + 1;\n";
  657. $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";
  658. foreach($fieldList as $fieldItem){
  659. if($fieldItem['field_type']=='relate_single'){
  660. $relationName = $fieldItem['relation_model'];
  661. $tbName = explode('_',$relationName);
  662. $lastTbName = end($tbName);
  663. $relation = $fieldItem['field']==$lastTbName.'_id'?self::getCamel($relationName):self::getCamel($fieldItem['field']);
  664. $relationShow = $fieldItem['relation_show'];
  665. $str .="}elseif(\$k=='{$fieldItem['field']}'){\n\t\t\t\t\t\$body[\$index][]=\$item['$relation']?\$item['$relation']['$relationShow']:'';\n\t\t\t\t";
  666. }elseif($fieldItem['field_type']=='relate_self'){
  667. $relationShow = $fieldItem['relation_show'];
  668. $str .="}elseif(\$k=='{$fieldItem['field']}'){\n\t\t\t\t\t\$body[\$index][]=\$item['Father']?\$item['Father']['$relationShow']:'';\n\t\t\t\t";
  669. }elseif($fieldItem['field_type']=='json'){
  670. $str .="}elseif(\$k=='{$fieldItem['field']}'){\n\t\t\t\t\t\$body[\$index][]=\$item[\$k]?json_encode(\$item[\$k]):'';\n\t\t\t\t";
  671. }
  672. }
  673. $str .="}else{\n\t\t\t\t\t";
  674. $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";
  675. $str .="\t\t}\n";
  676. $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";
  677. $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";
  678. $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";
  679. $str .="\t}";
  680. return $str;
  681. }
  682. }