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

221 lines
7.3 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. //不允许查看非自己的数据
  94. if ($show->model()->agent_id != Admin::user()->id) {
  95. Admin::exit('数据不存在');
  96. }
  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. $show->html(Alert::make(null, '供应商产品详情')->info());
  109. $show->field('product.id', '供应商产品ID');
  110. $show->field('product.supplier.name');
  111. $show->field('product.title');
  112. $show->field('product.pictures')->image(80, 80);
  113. $show->field('product.original_price');
  114. $show->field('product.price');
  115. $show->field('product.sale');
  116. $show->field('product.stock');
  117. $show->field('product.created_at', '创建时间');
  118. $show->field('product.updated_at', '更新时间');
  119. });
  120. }
  121. /**
  122. * Make a form builder.
  123. *
  124. * @return Form
  125. */
  126. protected function form()
  127. {
  128. return Form::make(new AgentProduct(['product:id,title']), function (Form $form) {
  129. $agent_id = Admin::user()->id;
  130. //不允许查看非自己的数据
  131. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  132. return $form->response()->error('数据不存在');
  133. }
  134. $form->display('id');
  135. $form->hidden('agent_id')->value($agent_id);
  136. $form->hidden('status')->value(ProductStatus::UNAUDITED);
  137. $form->hidden('product_id');
  138. $form->multipleSelectTable('product_ids', '供应商产品')
  139. ->title('选择产品')
  140. ->dialogWidth('80%;min-width:825px;')
  141. ->from(SelectProduct::make())
  142. ->model(Product::class)
  143. ->required();
  144. $form->text('price')->required();
  145. $form->text('original_price')->required();
  146. $form->text('sale')->default(0);
  147. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  148. array_shift($options);
  149. $form->multipleSelect('channel_id')
  150. ->options($options);
  151. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  152. array_shift($options);
  153. $form->select('category_id')
  154. ->options($options)
  155. ->required();
  156. $form->radio('settlement')
  157. ->options(Settlement::array())
  158. ->default(settlement::INSTANT)
  159. ->required();
  160. })->saving(function (Form $form) {
  161. //不允许修改非自己的数据
  162. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  163. return $form->response()->error('数据不存在');
  164. }
  165. $agent_id = Admin::user()->id;
  166. //判断供应商产品是否存在或下架
  167. if (!Product::where(['id' => $form->product_id, 'status' => ProductStatus::ON_SALE])->exists()) {
  168. return $form->response()->error('供应商不存在该产品或已下架,不可销售');
  169. }
  170. //不允许编辑的字段
  171. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']);
  172. //处理特殊字段
  173. $form->agent_id = $agent_id;
  174. $form->status = ProductStatus::UNAUDITED;
  175. $form->product_id = $form->product_ids[0];
  176. //判断是否重复发布产品
  177. $where = [
  178. ['agent_id', '=', $agent_id],
  179. ['product_id', '=', $form->product_id],
  180. ];
  181. if ($form->isEditing()) {
  182. $where[] = ['id', '<>', $form->getKey()];
  183. }
  184. $agent_product = $form->repository()->model()->withTrashed()->where($where)->first();
  185. if ($agent_product) {
  186. //如果已经软删除了,解除软删除后再更新
  187. if ($agent_product->deleted_at) {
  188. $agent_product->deleted_at = null;
  189. $form->deleteInput('sale'); //保留原来的销量
  190. $agent_product->update($form->input());
  191. return $form->response()->success('保存成功')->script('history.back();');
  192. }
  193. return $form->response()->error('该产品已经存在,请勿重复发布');
  194. }
  195. });
  196. }
  197. }