海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.7 KiB

  1. <?php
  2. namespace App\AdminAgent\Actions\Tree;
  3. use App\Models\Category;
  4. use Dcat\Admin\Actions\Response;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Traits\HasPermissions;
  7. use Dcat\Admin\Tree\AbstractTool;
  8. use Illuminate\Contracts\Auth\Authenticatable;
  9. use Illuminate\Database\Eloquent\Model;
  10. class LoadSystemCategory extends AbstractTool
  11. {
  12. /**
  13. * @return string
  14. */
  15. protected $title = '导入系统分类';
  16. # 该属性主要是为了总后台审核成功时自动插入系统分类
  17. private int $agent_id = 0;
  18. public function __construct($title = null, $agent_id = 0)
  19. {
  20. parent::__construct($title);
  21. $this->agent_id = $agent_id ?: Admin::user()->id;
  22. }
  23. /**
  24. * Handle the action request.
  25. *
  26. * @return Response
  27. */
  28. public function handle()
  29. {
  30. $sys_cate = Category::where('agent_id', 0)->orderBy('sort')->get(['id', 'pid', 'name', 'sort', 'publish_type'])->toArray();
  31. $sys_cate = $this->node_merge($sys_cate);
  32. $self_cate = Category::where('agent_id', $this->agent_id)->orderBy('sort')->get(['id', 'pid', 'name', 'sort', 'publish_type'])->toArray();
  33. $self_cate = $this->node_merge($self_cate);
  34. $this->insert_cate($sys_cate, $self_cate);
  35. return $this->response()->success('导入系统分类成功!')->refresh();
  36. }
  37. private function insert_cate($sys_cate, $self_cate, $pid = 0)
  38. {
  39. $agent_id = $this->agent_id;
  40. foreach ($sys_cate as $cate) {
  41. $insert_data = $cate;
  42. $insert_data['agent_id'] = $agent_id;
  43. $insert_data['pid'] = $pid;
  44. unset($insert_data['id'], $insert_data['child']);
  45. # 如果原来已经有的(通过name和publish_type判断是否已经存在),不再重复插入,只获取pid值
  46. $exists_cate = array_filter($self_cate, fn($a) => $a['name'] == $cate['name'] && $a['publish_type'] == $cate['publish_type']);
  47. if ($exists_cate) {
  48. $pid2 = current($exists_cate)['id'];
  49. } else {
  50. $pid2 = Category::insertGetId($insert_data);
  51. }
  52. if (!empty($cate['child']) && is_array($cate['child'])) {
  53. # 如果有子栏目含有下级栏目,则一个个插入
  54. if (array_filter($cate['child'], fn($a) => !empty($a['child']))) {
  55. $this->insert_cate($cate['child'], current($exists_cate)['child'] ?? [], $pid2);
  56. }
  57. # 否则直接整个child插入
  58. else {
  59. $insert_data = array_map(function ($a) use ($agent_id, $pid2) {
  60. $a['agent_id'] = $agent_id;
  61. $a['pid'] = $pid2;
  62. unset($a['id'], $a['child']);
  63. return $a;
  64. }, $cate['child']);
  65. # 如果已经存在,则不再重复插入
  66. $child = !empty(current($exists_cate)['child']) ? current($exists_cate)['child'] : [];
  67. if (!empty($child)) {
  68. foreach ($insert_data as $k => &$cate2) {
  69. if (array_filter($child, fn($a) => $a['name'] == $cate2['name'] && $a['publish_type'] == $cate2['publish_type'])) {
  70. unset($insert_data[$k]);
  71. }
  72. }
  73. }
  74. Category::insert($insert_data);
  75. }
  76. }
  77. }
  78. }
  79. private function node_merge(array $arr, int $pid = 0, string $pidKey = 'pid', string $childrenKey = 'child', string $idKey = 'id'): array
  80. {
  81. $rs = [];
  82. $arr = array_column($arr, null, $idKey);
  83. foreach ($arr as $k => $v) {
  84. if ($v[$pidKey] === $pid) {
  85. $rs[] = &$arr[$k];
  86. } else {
  87. $arr[$v[$pidKey]][$childrenKey][] = &$arr[$k];
  88. }
  89. }
  90. return $rs;
  91. }
  92. /**
  93. * @return string|void
  94. */
  95. protected function href()
  96. {
  97. // return admin_url('auth/users');
  98. }
  99. /**
  100. * @return string|array|void
  101. */
  102. public function confirm()
  103. {
  104. return ['操作确认', '确定要导入系统分类吗?'];
  105. }
  106. /**
  107. * @param Model|Authenticatable|HasPermissions|null $user
  108. *
  109. * @return bool
  110. */
  111. protected function authorize($user): bool
  112. {
  113. return true;
  114. }
  115. }