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

187 lines
5.9 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectProduct;
  4. use App\AdminAgent\Repositories\AgentProduct;
  5. use App\Common\ProductStatus;
  6. use App\Models\Category;
  7. use App\Models\Channel;
  8. use App\Models\Product;
  9. use App\Models\Supplier;
  10. use Dcat\Admin\Admin;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Show;
  14. use Dcat\Admin\Http\Controllers\AdminController;
  15. use Dcat\Admin\Widgets\Table;
  16. class AgentProductController extends AdminController
  17. {
  18. /**
  19. * Make a grid builder.
  20. *
  21. * @return Grid
  22. */
  23. protected function grid()
  24. {
  25. return Grid::make(new AgentProduct(['product.supplier:id,name', 'category:id,name']), function (Grid $grid) {
  26. $agent_id = Admin::user()->id;
  27. $grid->model()->where('agent_id', $agent_id);
  28. $grid->column('id')->sortable();
  29. $grid->column('product.title', '产品名称');
  30. $grid->column('product_id', '产品信息')
  31. ->display('查看')
  32. ->modal(function ($v) {
  33. $titles = [
  34. '供应商',
  35. '产品标题',
  36. '产品图片',
  37. '原价',
  38. '现价',
  39. '销量',
  40. '库存',
  41. ];
  42. $pic = isset($this->product->picture)
  43. ? '<img data-action="preview-img" src="' . $this->product->picture . '" style="max-width:80px;max-height:200px;cursor:pointer" class="img img-thumbnail">'
  44. : '';
  45. $data = [[
  46. $this->product->supplier->name ?? '',
  47. $this->product->title ?? '',
  48. $pic,
  49. $this->product->original_price ?? '',
  50. $this->product->price ?? '',
  51. $this->product->sale ?? '',
  52. $this->product->stock ?? '',
  53. ]];
  54. return Table::make($titles, $data);
  55. });
  56. $grid->column('price');
  57. $grid->column('original_price');
  58. $grid->column('sale');
  59. $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
  60. $grid->column('channel_id', '频道')
  61. ->display(function ($v) use ($grid, $channels) {
  62. $arr = array_flip(explode(',', $v));
  63. return join(',', array_intersect_key($channels, $arr));
  64. });
  65. $grid->column('category.name', '分类')->label();
  66. $grid->column('status')
  67. ->using(ProductStatus::array())
  68. ->dot([
  69. ProductStatus::ON_SALE => 'success',
  70. ProductStatus::UNAUDITED => '',
  71. ProductStatus::REFUSE => 'danger',
  72. ProductStatus::SOLD_OUT => 'warning',
  73. ], 'primary');
  74. $grid->column('created_at');
  75. $grid->column('updated_at');
  76. $grid->filter(function (Grid\Filter $filter) {
  77. $filter->panel();
  78. $filter->model()->where('agent_id', Admin::user()->id);
  79. $filter->equal('id')->width(2);
  80. $filter->like('product.title', '产品标题')->width(3);
  81. $filter->equal('status')->select(ProductStatus::array())->width(2);
  82. $options = Supplier::where('status', 1)->pluck('name', 'id')->toArray();
  83. $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
  84. });
  85. });
  86. }
  87. /**
  88. * Make a show builder.
  89. *
  90. * @param mixed $id
  91. *
  92. * @return Show
  93. */
  94. protected function detail($id)
  95. {
  96. return Show::make($id, new AgentProduct(), function (Show $show) {
  97. $show->field('id');
  98. $show->field('agent_id');
  99. $show->field('product_id');
  100. $show->field('price');
  101. $show->field('original_price');
  102. $show->field('sale');
  103. $show->field('channel_id');
  104. $show->field('category_id');
  105. $show->field('status');
  106. $show->field('created_at');
  107. $show->field('updated_at');
  108. });
  109. }
  110. /**
  111. * Make a form builder.
  112. *
  113. * @return Form
  114. */
  115. protected function form()
  116. {
  117. return Form::make(new AgentProduct(), function (Form $form) {
  118. $agent_id = Admin::user()->id;
  119. $form->display('id');
  120. $form->hidden('agent_id')->value($agent_id);
  121. $form->selectTable('product_id')
  122. ->title('选择产品')
  123. ->dialogWidth('80%;min-width:825px;')
  124. ->from(SelectProduct::make())
  125. ->required();
  126. $form->text('price')->required();
  127. $form->text('original_price')->required();
  128. $form->text('sale')->default(0);
  129. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  130. array_shift($options);
  131. $form->multipleSelect('channel_id')
  132. ->options($options);
  133. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  134. array_shift($options);
  135. $form->select('category_id')
  136. ->options($options)
  137. ->required();
  138. $form->select('status')
  139. ->options([
  140. ProductStatus::ON_SALE => '上架',
  141. ProductStatus::SOLD_OUT => '下架',
  142. ])
  143. ->default(ProductStatus::ON_SALE)
  144. ->required();
  145. })->saving(function (Form $form) {
  146. $agent_id = Admin::user()->id;
  147. //判断供应商产品是否存在或下架
  148. if (!Product::query()->where(['id' => $form->product_id, 'status' => ProductStatus::ON_SALE])->exists()) {
  149. return $form->response()->error('供应商不存在该产品或已下架,不可销售');
  150. }
  151. //不允许编辑的字段
  152. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']);
  153. //处理特殊字段
  154. $form->agent_id = $agent_id;
  155. $form->status = ($form->status == ProductStatus::ON_SALE) ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  156. //判断是否重复
  157. $agent_product = $form->repository()->model()->withTrashed()->where(['agent_id' => $agent_id, 'product_id' => $form->product_id])->first();
  158. if ($agent_product) {
  159. //如果已经软删除了,解除软删除后再更新
  160. if ($agent_product->deleted_at) {
  161. $agent_product->deleted_at = null;
  162. $agent_product->update($form->input());
  163. return $form->response()->success('保存成功');
  164. }
  165. return $form->response()->error('该产品已经存在,请勿重复发布');
  166. }
  167. });
  168. }
  169. }