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

230 lines
7.6 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\Alert;
  16. use Dcat\Admin\Widgets\Table;
  17. class AgentProductController extends AdminController
  18. {
  19. /**
  20. * Make a grid builder.
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. return Grid::make(new AgentProduct(['product.supplier:id,name', 'category:id,name']), function (Grid $grid) {
  27. $agent_id = Admin::user()->id;
  28. $grid->model()->where('agent_id', $agent_id);
  29. $grid->column('id')->sortable();
  30. $grid->column('product.picture', '产品图片')->image(60, 60);
  31. $grid->column('product.title', '产品名称')->limit(15);
  32. $grid->column('product_id', '产品详情')
  33. ->display('查看')
  34. ->modal(function ($v) {
  35. $titles = ['供应商', '产品标题', '产品图片', '原价', '现价', '销量', '库存'];
  36. $pic = isset($this->product->picture)
  37. ? "<img data-action=\"preview-img\" src=\"{$this->product->picture}\" style=\"max-width:80px;max-height:200px;cursor:pointer\" class=\"img img-thumbnail\">"
  38. : '';
  39. $data = [[
  40. $this->product->supplier->name ?? '',
  41. $this->product->title ?? '',
  42. $pic,
  43. $this->product->original_price ?? '',
  44. $this->product->price ?? '',
  45. $this->product->sale ?? '',
  46. $this->product->stock ?? '',
  47. ]];
  48. return Table::make($titles, $data);
  49. });
  50. $grid->column('price');
  51. $grid->column('original_price');
  52. $grid->column('sale');
  53. $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
  54. $grid->column('channel_id', '频道')
  55. ->display(function ($modal) use ($channels) {
  56. $data = array_flip(explode(',', $this->channel_id));
  57. return join(',',array_intersect_key($channels, $data));
  58. })
  59. ->limit(10);
  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(['agent:id,name', 'product.supplier:id,name']), function (Show $show) {
  92. //不允许查看非自己的数据
  93. if ($show->model()->agent_id != Admin::user()->id) {
  94. Admin::exit('数据不存在');
  95. }
  96. $show->field('id');
  97. $show->field('agent_id');
  98. $show->field('product_id');
  99. $show->field('price');
  100. $show->field('original_price');
  101. $show->field('sale');
  102. $show->field('channel_id');
  103. $show->field('category_id');
  104. $show->field('status')->using(ProductStatus::array());
  105. $show->field('created_at');
  106. $show->field('updated_at');
  107. $show->html(Alert::make(null, '供应商产品详情')->info());
  108. $show->field('product.id', '供应商产品ID');
  109. $show->field('product.supplier.name');
  110. $show->field('product.title');
  111. $show->field('product.pictures')->image(80, 80);
  112. $show->field('product.original_price');
  113. $show->field('product.price');
  114. $show->field('product.sale');
  115. $show->field('product.stock');
  116. $show->field('product.created_at', '创建时间');
  117. $show->field('product.updated_at', '更新时间');
  118. });
  119. }
  120. /**
  121. * Make a form builder.
  122. *
  123. * @return Form
  124. */
  125. protected function form()
  126. {
  127. return Form::make(new AgentProduct(['product:id,title']), function (Form $form) {
  128. $agent_id = Admin::user()->id;
  129. //不允许查看非自己的数据
  130. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  131. return $form->response()->error('数据不存在');
  132. }
  133. $form->display('id');
  134. $form->hidden('agent_id')->value($agent_id);
  135. $form->hidden('status')->value(ProductStatus::UNAUDITED);
  136. $form->hidden('product_id');
  137. $form->multipleSelectTable('product_ids', '选择产品')
  138. ->help('可多选组合销售')
  139. ->title('选择产品')
  140. ->dialogWidth('80%;min-width:825px;')
  141. ->from(SelectProduct::make())
  142. ->model(Product::class)
  143. ->required();
  144. $form->selectTable('product_id', '封面产品')
  145. ->help('当多选产品时,产品列表显示的是该产品的标题和图片')
  146. ->title('选择产品')
  147. ->dialogWidth('80%;min-width:825px;')
  148. ->from(SelectProduct::make())
  149. ->model(Product::class)
  150. ->required();
  151. $form->text('price')->required();
  152. $form->text('original_price')->required();
  153. $form->text('sale')->default(0);
  154. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  155. $form->multipleSelect('channel_id')->options(array_slice($options, 1));
  156. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  157. $form->select('category_id')
  158. ->options(array_slice($options, 1))
  159. ->required();
  160. $form->radio('status')
  161. ->default(ProductStatus::ON_SALE)
  162. ->options([
  163. ProductStatus::ON_SALE => '上架',
  164. ProductStatus::SOLD_OUT => '下架',
  165. ])
  166. ->required();
  167. })->saving(function (Form $form) {
  168. //不允许修改非自己的数据
  169. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  170. return $form->response()->error('数据不存在');
  171. }
  172. $agent_id = Admin::user()->id;
  173. //不允许编辑的字段
  174. $form->ignore(['id', 'agent_id', 'created_at', 'updated_at', 'deleted_at']);
  175. $product_ids = explode(',', $form->product_ids);
  176. if (empty($product_ids)) {
  177. return $form->response()->error('请选择产品');
  178. }
  179. //判断供应商产品是否存在或下架
  180. $not_in_id = Product::query()
  181. ->whereIn('id', $product_ids)
  182. ->where('status', '<>', ProductStatus::ON_SALE)
  183. ->pluck('id')
  184. ->toArray();
  185. if ($not_in_id) {
  186. return $form->response()->error('产品ID'. join(',', $not_in_id) .'不存在或已下架,请重新选择');
  187. }
  188. //处理特殊字段
  189. $form->agent_id = $agent_id;
  190. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  191. //判断是否重复发布产品
  192. $where = [
  193. ['agent_id', '=', $agent_id],
  194. ['product_id', '=', $form->product_id],
  195. ['product_ids', '=', $form->product_ids],
  196. ];
  197. if ($form->isEditing()) {
  198. $where[] = ['id', '<>', $form->getKey()];
  199. }
  200. if ($form->repository()->model()->where($where)->exists()) {
  201. return $form->response()->error('该产品已经存在,请勿重复发布');
  202. }
  203. });
  204. }
  205. }