Dao.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\crud;
  12. use crmeb\services\crud\enum\SearchEnum;
  13. /**
  14. * Class Business
  15. * @package crmeb\services
  16. */
  17. class Dao extends Make
  18. {
  19. /**
  20. * 当前命令名称
  21. * @var string
  22. */
  23. protected $name = "dao";
  24. /**
  25. * @return string
  26. * @author 等风来
  27. * @email 136327134@qq.com
  28. * @date 2023/4/4
  29. */
  30. protected function setBaseDir(): string
  31. {
  32. return 'app' . DS . 'dao' . DS . 'crud';
  33. }
  34. /**
  35. * 执行替换
  36. * @param string $name
  37. * @param array $options
  38. * @return Dao
  39. * @author 等风来
  40. * @email 136327134@qq.com
  41. * @date 2023/8/3
  42. */
  43. public function handle(string $name, array $options = [])
  44. {
  45. $this->setSearchDaoPhpContent($options['searchField'] ?? []);
  46. return parent::handle($name, $options);
  47. }
  48. /**
  49. * 获取搜索dao的php代码
  50. * @param array $fields
  51. * @return Dao
  52. * @author 等风来
  53. * @email 136327134@qq.com
  54. * @date 2023/8/3
  55. */
  56. protected function setSearchDaoPhpContent(array $fields)
  57. {
  58. $templateContent = file_get_contents($this->getStub('search'));
  59. $contentSearchPhp = '';
  60. foreach ($fields as $item) {
  61. $tab2 = $this->tab(2);
  62. $contentStr = <<<CONTENT
  63. ->when(!empty(\$where['$item[field]']), function(\$query) use (\$where) {
  64. $tab2 \$query->{%WHERE%}('$item[field]', '{%SEARCH%}', \$where['$item[field]']);
  65. $tab2})
  66. CONTENT;
  67. if (isset($item['search']) && $item['search']) {
  68. switch ($item['search']) {
  69. case SearchEnum::SEARCH_TYPE_EQ:
  70. case SearchEnum::SEARCH_TYPE_GTEQ:
  71. case SearchEnum::SEARCH_TYPE_LTEQ:
  72. case SearchEnum::SEARCH_TYPE_NEQ:
  73. $contentSearchPhp .= str_replace([
  74. '{%WHERE%}',
  75. '{%SEARCH%}'
  76. ], [
  77. 'where',
  78. $item['search']
  79. ], $contentStr);
  80. break;
  81. case SearchEnum::SEARCH_TYPE_LIKE:
  82. $contentSearchPhp .= <<<CONTENT
  83. ->when(!empty(\$where['$item[field]']), function(\$query) use (\$where) {
  84. $tab2 \$query->whereLike('$item[field]', '%'.\$where['$item[field]'].'%');
  85. $tab2})
  86. CONTENT;
  87. break;
  88. case SearchEnum::SEARCH_TYPE_BETWEEN:
  89. $contentSearchPhp .= <<<CONTENT
  90. ->when(!empty(\$where['$item[field]']), function(\$query) use (\$where) {
  91. $tab2 \$query->whereBetween('$item[field]', \$where['$item[field]']);
  92. $tab2})
  93. CONTENT;
  94. break;
  95. }
  96. }
  97. }
  98. $this->value['CONTENT_PHP'] = str_replace(['{%CONTENT_SEARCH_PHP%}'], [$contentSearchPhp . ';'], $templateContent);
  99. return $this;
  100. }
  101. /**
  102. * 模板文件
  103. * @param string $type
  104. * @return string
  105. * @author 等风来
  106. * @email 136327134@qq.com
  107. * @date 2023/3/13
  108. */
  109. protected function getStub(string $type = '')
  110. {
  111. $daoPath = __DIR__ . DS . 'stubs' . DS . 'dao' . DS;
  112. $stubs = [
  113. 'dao' => $daoPath . 'crudDao.stub',
  114. 'search' => $daoPath . 'search.stub',
  115. ];
  116. return $type ? $stubs[$type] : $stubs['dao'];
  117. }
  118. }