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

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