海南旅游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
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->disableRowSelector();
  38. $grid->filter(function (Grid\Filter $filter) {
  39. $filter->equal('id');
  40. });
  41. });
  42. }
  43. /**
  44. * Make a show builder.
  45. *
  46. * @param mixed $id
  47. *
  48. * @return Show
  49. */
  50. protected function detail($id)
  51. {
  52. return Show::make($id, new DemandProduct(), function (Show $show) {
  53. $show->field('id');
  54. $show->field('supplier_id');
  55. $show->field('category_id');
  56. $show->field('title');
  57. //$show->field('price');
  58. //$show->field('original_price');
  59. $show->field('pictures')->image('', 80, 80);
  60. //$show->field('stock');
  61. $show->field('sale');
  62. $show->field('status');
  63. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  64. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  65. $show->field('created_at');
  66. $show->field('updated_at');
  67. });
  68. }
  69. /**
  70. * Make a form builder.
  71. *
  72. * @return Form
  73. */
  74. protected function form()
  75. {
  76. return Form::make(new DemandProduct(), function (Form $form) {
  77. //不允许编辑非自己数据
  78. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  79. return $form->response()->error('数据不存在');
  80. }
  81. $form->display('id');
  82. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  83. $form->select('category_id')->options(array_slice($options, 1, null, true))->required();
  84. $form->text('title')->required();
  85. //$form->text('price')->required();
  86. //$form->text('original_price')->required();
  87. $form->multipleImage('pictures')->required()->removable(false)->retainable()->uniqueName();
  88. //$form->text('stock')->default(9999)->required();
  89. $form->editor('know');
  90. $form->editor('content')->required();
  91. if ($form->isEditing()) {
  92. $form->confirm('提示', '编辑产品需要重新审核,同时<span class="btn-danger">下架所有</span>关联的代理商产品,是否继续?');
  93. }
  94. })->saving(function (Form $form) {
  95. //不允许编辑非自己数据
  96. if ($form->isEditing() && $form->model()->supplier_id != Admin::user()->id) {
  97. return $form->response()->error('数据不存在');
  98. }
  99. //不允许编辑的字段 TODO 忽略字段不起作用
  100. $form->ignore(['id', 'supplier_id', 'sale', 'status', 'created_at', 'updated_at', 'deleted_at']);
  101. //null字段转为''
  102. foreach ($form->input() as $k => $v) {
  103. if (is_null($v)) {
  104. $form->$k = '';
  105. }
  106. }
  107. //特殊字段处理
  108. $form->hidden(['status', 'supplier_id']); //表单没有的字段,必须加上这句才能重置值
  109. $form->status = ProductStatus::UNAUDITED;
  110. $form->supplier_id = Admin::user()->id;
  111. })->saved(function (Form $form, $result) {
  112. //下架代理商产品
  113. if ($result) {
  114. $id = $form->getKey();
  115. AgentProduct::where('product_id', $id)
  116. ->orWhere(DB::raw('FIND_IN_SET(' . $id . ', product_ids)')) //TODO product_ids字段可能会去掉
  117. ->update(['status' => ProductStatus::SOLD_OUT]);
  118. }
  119. })->deleting(function (Form $form) {
  120. //不允许删除非自己的数据
  121. if (array_filter($form->model()->toArray(), fn($v) => $v['supplier_id'] != Admin::user()->id)) {
  122. return $form->response()->error('数据不存在');
  123. }
  124. });
  125. }
  126. }