海南旅游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.

94 lines
2.5 KiB

4 years ago
  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. use Illuminate\Http\Request;
  11. class LoadSystemCategory extends AbstractTool
  12. {
  13. /**
  14. * @return string
  15. */
  16. protected $title = '导入系统分类';
  17. /**
  18. * Handle the action request.
  19. *
  20. * @param Request $request
  21. *
  22. * @return Response
  23. */
  24. public function handle(Request $request)
  25. {
  26. $system_cate = Category::where('agent_id', 0)->orderBy('pid')->get();
  27. $system_name = $system_cate->pluck('name')->toArray();
  28. $own_cate = Category::where('agent_id', Admin::user()->id)->get();
  29. $own_name = $own_cate->pluck('name')->toArray();
  30. $no_name = array_diff($system_name, $own_name); //找到系统有,但自己没有的分类名
  31. $max_id = Category::where('agent_id', Admin::user()->id)->max('id');
  32. Category::insert(array_map(function ($name) {
  33. return [
  34. 'agent_id' => Admin::user()->id,
  35. 'name' => $name,
  36. 'pid' => 0,
  37. 'sort' => 255,
  38. ];
  39. }, $no_name));
  40. $own_cate = Category::where('agent_id', Admin::user()->id)->orderBy('id')->get(); //获取所有分类,包含新插入的分类
  41. $system_pid = $system_cate->pluck('pid', 'name')->toArray(); //获取原来系统的PID
  42. $own_id = $own_cate->pluck('id', 'name')->toArray(); //获取自己的PID,不考虑name重复的情况
  43. foreach ($own_cate as $cate) {
  44. //不是新插入的分类不处理
  45. if ($cate->id <= $max_id) continue;
  46. //如果原来系统PID不是0,则获取父类的名称,并在自己的分类中获取对应名称ID,再设置父分类
  47. $sys_pid = $system_pid[$cate['name']];
  48. if ($sys_pid != 0) {
  49. $sys_pid_name = $system_cate->find($sys_pid)['name'];
  50. $cate->pid = $own_id[$sys_pid_name];
  51. $cate->save();
  52. }
  53. }
  54. return $this->response()->success('操作成功!')->refresh();
  55. }
  56. /**
  57. * @return string|void
  58. */
  59. protected function href()
  60. {
  61. // return admin_url('auth/users');
  62. }
  63. /**
  64. * @return string|array|void
  65. */
  66. public function confirm()
  67. {
  68. return ['操作确认', '确定要导入系统分类吗?'];
  69. }
  70. /**
  71. * @param Model|Authenticatable|HasPermissions|null $user
  72. *
  73. * @return bool
  74. */
  75. protected function authorize($user): bool
  76. {
  77. return true;
  78. }
  79. }