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

139 lines
4.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminSupplier\Controllers;
  3. use App\AdminSupplier\Repositories\DemandProduct;
  4. use App\Common\ProductStatus;
  5. use App\Models\AgentProduct;
  6. use App\Models\Category;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Repositories\EloquentRepository;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Illuminate\Support\Facades\DB;
  14. class DemandProductController extends AdminController
  15. {
  16. protected $title = '产品';
  17. /**
  18. * Make a grid builder.
  19. *
  20. * @return Grid
  21. */
  22. protected function grid()
  23. {
  24. return Grid::make(new DemandProduct(['category:id,name']), function (Grid $grid) {
  25. $grid->model()->where('supplier_id', Admin::user()->id);
  26. $grid->column('id')->sortable();
  27. $grid->column('category.name', '产品分类');
  28. $grid->column('title');
  29. $grid->column('picture')->image('', 60, 60);
  30. //$grid->column('price');
  31. //$grid->column('original_price');
  32. //$grid->column('stock');
  33. $grid->column('sale');
  34. $grid->column('status')->using(ProductStatus::array());
  35. $grid->column('created_at');
  36. $grid->column('updated_at');
  37. $grid->filter(function (Grid\Filter $filter) {
  38. $filter->equal('id');
  39. });
  40. });
  41. }
  42. /**
  43. * Make a show builder.
  44. *
  45. * @param mixed $id
  46. *
  47. * @return Show
  48. */
  49. protected function detail($id)
  50. {
  51. return Show::make($id, new DemandProduct(), function (Show $show) {
  52. $show->field('id');
  53. $show->field('supplier_id');
  54. $show->field('category_id');
  55. $show->field('title');
  56. //$show->field('price');
  57. //$show->field('original_price');
  58. $show->field('pictures')->image('', 80, 80);
  59. //$show->field('stock');
  60. $show->field('sale');
  61. $show->field('status');
  62. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  63. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  64. $show->field('created_at');
  65. $show->field('updated_at');
  66. });
  67. }
  68. /**
  69. * Make a form builder.
  70. *
  71. * @return Form
  72. */
  73. protected function form()
  74. {
  75. return Form::make(new DemandProduct(), function (Form $form) {
  76. //不允许编辑非自己数据
  77. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  78. return $form->response()->error('数据不存在');
  79. }
  80. $form->display('id');
  81. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  82. $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
  83. $form->text('title')->required();
  84. //$form->text('price')->required();
  85. //$form->text('original_price')->required();
  86. $form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
  87. //$form->text('stock')->default(9999)->required();
  88. $form->editor('know');
  89. $form->editor('content')->required();
  90. if ($form->isEditing()) {
  91. $form->confirm('提示', '编辑产品需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
  92. }
  93. })->saving(function (Form $form) {
  94. //不允许编辑非自己数据
  95. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  96. return $form->response()->error('数据不存在');
  97. }
  98. //不允许编辑的字段 TODO 忽略字段不起作用
  99. $form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
  100. //null字段转为''
  101. foreach ($form->input() as $k => $v) {
  102. if (is_null($v)) {
  103. $form->$k = '';
  104. }
  105. }
  106. //特殊字段处理
  107. $form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
  108. $form->status = ProductStatus::UNAUDITED;
  109. $form->supplier_id = Admin::user()->id;
  110. })->saved(function (Form $form, $result) {
  111. //下架代理商产品
  112. if ($result) {
  113. $id = $form->getKey();
  114. AgentProduct::where('product_id', $id)
  115. ->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
  116. ->update(['status' => ProductStatus::SOLD_OUT]);
  117. }
  118. })->deleting(function (Form $form) {
  119. //不允许删除非自己的数据
  120. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  121. return $form->response()->error('数据不存在');
  122. }
  123. });
  124. }
  125. }