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

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