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

304 lines
10 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectGuide;
  4. use App\AdminAgent\Renderable\SelectProduct;
  5. use App\AdminAgent\Renderable\SelectUser;
  6. use App\AdminAgent\Repositories\AgentProduct;
  7. use App\Common\AgentType;
  8. use App\Common\ProductStatus;
  9. use App\Models\AgentProductItem;
  10. use App\Models\Category;
  11. use App\Models\Channel;
  12. use App\Models\Guide;
  13. use App\Models\Product;
  14. use App\Models\Supplier;
  15. use App\Models\User;
  16. use Dcat\Admin\Admin;
  17. use Dcat\Admin\Form;
  18. use Dcat\Admin\Grid;
  19. use Dcat\Admin\Show;
  20. use Dcat\Admin\Http\Controllers\AdminController;
  21. use Dcat\Admin\Widgets\Alert;
  22. use Dcat\Admin\Widgets\Table;
  23. class AgentProductController extends AdminController
  24. {
  25. /**
  26. * Make a grid builder.
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(new AgentProduct(['product.supplier:id,name', 'category:id,name']), function (Grid $grid) {
  33. $agent_id = Admin::user()->id;
  34. $grid->model()->where('agent_id', $agent_id);
  35. $grid->column('id')->sortable();
  36. $grid->column('product.picture', '产品图片')->image('', 60, 60);
  37. $grid->column('product.title', '产品名称')->limit(15);
  38. $grid->column('product_id', '产品详情')
  39. ->display('查看')
  40. ->modal(function ($v) {
  41. $titles = ['供应商', '产品标题', '产品图片', '原价', '现价', '销量', '库存'];
  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. $grid->column('stock');
  60. $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
  61. $grid->column('channel_id', '频道')
  62. ->display(function ($modal) use ($channels) {
  63. $data = array_flip(explode(',', $this->channel_id));
  64. return join(',',array_intersect_key($channels, $data));
  65. })
  66. ->limit(10);
  67. $grid->column('category.name', '分类')->label();
  68. $grid->column('status')
  69. ->using(ProductStatus::array())
  70. ->dot([
  71. ProductStatus::ON_SALE => 'success',
  72. ProductStatus::UNAUDITED => '',
  73. ProductStatus::REFUSE => 'danger',
  74. ProductStatus::SOLD_OUT => 'warning',
  75. ], 'primary');
  76. $grid->column('created_at');
  77. $grid->column('updated_at');
  78. $grid->filter(function (Grid\Filter $filter) {
  79. $filter->panel();
  80. $filter->model()->where('agent_id', Admin::user()->id);
  81. $filter->equal('id')->width(2);
  82. $filter->like('product.title', '产品标题')->width(3);
  83. $filter->equal('status')->select(ProductStatus::array())->width(2);
  84. $options = Supplier::where('status', 1)->pluck('name', 'id')->toArray();
  85. $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
  86. });
  87. });
  88. }
  89. /**
  90. * Make a show builder.
  91. *
  92. * @param mixed $id
  93. *
  94. * @return Show
  95. */
  96. protected function detail($id)
  97. {
  98. return Show::make($id, new AgentProduct(['agent:id,name', 'product.supplier:id,name', 'user:id,nickname', 'guide:id,name']), function (Show $show) {
  99. //不允许查看非自己的数据
  100. if ($show->model()->agent_id != Admin::user()->id) {
  101. Admin::exit('数据不存在');
  102. }
  103. $show->field('id');
  104. $show->field('agent_id');
  105. $show->field('product_id');
  106. $show->field('price');
  107. $show->field('original_price');
  108. $show->field('sale');
  109. $show->field('stock');
  110. $show->field('channel_id');
  111. $show->field('category_id');
  112. $show->field('status')->using(ProductStatus::array());
  113. $show->field('verifier.nickname', '核销人员');
  114. if (Admin::user()->type == AgentType::CLUSTER) {
  115. $show->field('guide.name', '地接');
  116. }
  117. $show->field('created_at');
  118. $show->field('updated_at');
  119. $show->html(Alert::make(null, '供应商产品详情')->info());
  120. $show->field('product.id', '供应商产品ID');
  121. $show->field('product.supplier.name');
  122. $show->field('product.title');
  123. $show->field('product.pictures')->image('', 80, 80);
  124. $show->field('product.original_price');
  125. $show->field('product.price');
  126. $show->field('product.sale');
  127. $show->field('product.stock');
  128. $show->field('product.created_at', '创建时间');
  129. $show->field('product.updated_at', '更新时间');
  130. });
  131. }
  132. /**
  133. * Make a form builder.
  134. *
  135. * @return Form
  136. */
  137. protected function form()
  138. {
  139. return Form::make(new AgentProduct(['product:id,title']), function (Form $form) {
  140. $agent_id = Admin::user()->id;
  141. //不允许查看非自己的数据
  142. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  143. return $form->response()->error('数据不存在');
  144. }
  145. $form->display('id');
  146. $form->hidden('product_id');
  147. $form->selectTable('product_id', '封面产品')
  148. ->help('产品列表显示的是该产品的标题和图片')
  149. ->title('选择产品')
  150. ->dialogWidth('80%;min-width:825px;')
  151. ->from(SelectProduct::make())
  152. ->model(Product::class)
  153. ->required();
  154. $form->multipleSelectTable('product_ids', '选择产品')
  155. ->help('可多选组合销售')
  156. ->title('选择产品')
  157. ->dialogWidth('80%;min-width:825px;')
  158. ->from(SelectProduct::make())
  159. ->model(Product::class)
  160. ->required();
  161. $form->text('price')->required();
  162. $form->text('original_price')->required();
  163. $form->text('sale')->default(0);
  164. $form->text('stock')->default(9999);
  165. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  166. $form->multipleSelect('channel_id')->options(array_slice($options, 1));
  167. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  168. $form->select('category_id')
  169. ->options(array_slice($options, 1))
  170. ->required();
  171. $form->radio('status')
  172. ->default(ProductStatus::ON_SALE)
  173. ->options([
  174. ProductStatus::ON_SALE => '上架',
  175. ProductStatus::SOLD_OUT => '下架',
  176. ])
  177. ->required();
  178. $form->selectTable('verifier')
  179. ->title('选择核销人员')
  180. ->dialogWidth('50%;min-width:600px;') //不起作用
  181. ->from(SelectUser::make())
  182. ->model(User::class, 'id', 'nickname')
  183. ->required();
  184. //组团版旅行社可以选择地接
  185. if (Admin::user()->type == AgentType::CLUSTER) {
  186. $form->selectTable('guide_id', '地接人员')
  187. ->title('选择地接人员')
  188. ->dialogWidth('50%;min-width:600px;') //不起作用
  189. ->from(SelectGuide::make())
  190. ->model(Guide::class, 'id', 'name');
  191. }
  192. })->saving(function (Form $form) {
  193. //不允许修改非自己的数据
  194. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  195. return $form->response()->error('数据不存在');
  196. }
  197. $agent_id = Admin::user()->id;
  198. $product_ids = explode(',', $form->product_ids);
  199. if (empty($product_ids)) {
  200. return $form->response()->error('请选择产品');
  201. }
  202. //判断供应商产品是否存在或下架
  203. $not_in_id = Product::query()
  204. ->whereIn('id', $product_ids)
  205. ->where(function ($query) use ($form) {
  206. $query->where('status', '<>', ProductStatus::ON_SALE)
  207. ->orWhere('stock', '<', $form->stock);
  208. })
  209. ->pluck('id')
  210. ->toArray();
  211. if ($not_in_id) {
  212. return $form->response()->error('产品ID'. join(',', $not_in_id) .'不存在或已下架,或库存小于你设置的库存' . $form->stock);
  213. }
  214. //处理特殊字段
  215. $form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
  216. $form->agent_id = $agent_id;
  217. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  218. //不允许编辑的字段
  219. $form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  220. //判断是否重复发布产品
  221. $where = [
  222. ['agent_id', '=', $agent_id],
  223. ['product_id', '=', $form->product_id],
  224. ['product_ids', '=', $form->product_ids],
  225. ];
  226. if ($form->isEditing()) {
  227. $where[] = ['id', '<>', $form->getKey()];
  228. }
  229. if ($form->repository()->model()->where($where)->exists()) {
  230. return $form->response()->error('该产品已经存在,请勿重复发布');
  231. }
  232. })->saved(function (Form $form) {
  233. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  234. $product_ids = explode(',', $form->product_ids);
  235. $product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray();
  236. $agent_product_id = $form->getKey();
  237. $insert_data = [];
  238. foreach ($product as $k => &$v) {
  239. $insert_data[$v['id']] = [
  240. 'agent_product_id' => $agent_product_id,
  241. 'agent_id' => Admin::user()->id,
  242. 'supplier_id' => $v['supplier_id'],
  243. 'product_id' => $v['id'],
  244. ];
  245. }
  246. if ($form->isCreating()) {
  247. AgentProductItem::insert($insert_data);
  248. } else if ($form->isEditing()) {
  249. //先查出来原来的数据
  250. $old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray();
  251. //新ID在原来数据里面没有的,删除掉
  252. foreach ($old_data as $k => $v) {
  253. //删除已经删除掉的product_id
  254. if (!in_array($k, $product_ids)) {
  255. AgentProductItem::query()->find($v)->delete();
  256. }
  257. //将新数据和旧数据中都存在的product_id unset掉
  258. if (array_key_exists($k, $insert_data)) {
  259. unset($insert_data[$k]);
  260. }
  261. }
  262. //将剩余(新增)的product_id保存
  263. if ($insert_data) {
  264. AgentProductItem::insert($insert_data);
  265. }
  266. }
  267. })->deleting(function (Form $form) {
  268. //不允许删除非自己的数据
  269. if ($form->model()[0]['agent_id'] != Admin::user()->id) {
  270. return $form->response()->error('数据不存在');
  271. }
  272. });
  273. }
  274. }