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

418 lines
15 KiB

5 years ago
5 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectGuide;
  4. use App\AdminAgent\Renderable\SelectProduct;
  5. use App\AdminAgent\Repositories\AgentProduct;
  6. use App\Common\AgentType;
  7. use App\Common\ProductStatus;
  8. use App\Models\AgentProductItem;
  9. use App\Models\Category;
  10. use App\Models\Channel;
  11. use App\Models\Guide;
  12. use App\Models\Product;
  13. use App\Models\Supplier;
  14. use Dcat\Admin\Admin;
  15. use Dcat\Admin\Form;
  16. use Dcat\Admin\Grid;
  17. use Dcat\Admin\Show;
  18. use Dcat\Admin\Http\Controllers\AdminController;
  19. use Dcat\Admin\Widgets\Alert;
  20. class AgentProductController extends AdminController
  21. {
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(new AgentProduct(['product.supplier:id,name', 'category:id,name']), function (Grid $grid) {
  30. $agent_id = Admin::user()->id;
  31. $grid->model()->where('agent_id', $agent_id);
  32. $grid->column('id')->sortable();
  33. $grid->column('picture', '产品图片')->image('', 60, 60);
  34. $grid->column('title', '产品名称')->limit(15);
  35. $grid->column('price');
  36. $grid->column('original_price');
  37. $grid->column('sale');
  38. $grid->column('stock')->editable();
  39. $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
  40. $grid->column('channel_id', '频道')
  41. ->display(function ($modal) use ($channels) {
  42. $data = array_flip(explode(',', $this->channel_id));
  43. return join(',',array_intersect_key($channels, $data));
  44. })
  45. ->limit(10);
  46. $grid->column('category.name', '分类');
  47. /*$grid->column('product_ids', '产品详情')
  48. ->display('查看')
  49. ->modal(function ($modal) {
  50. $titles = ['供应商', '产品标题', '产品图片', '原价', '现价', '销量', '库存'];
  51. $pic = isset($this->product->picture)
  52. ? "<img data-action=\"preview-img\" src=\"{$this->product->picture}\" style=\"max-width:80px;max-height:200px;cursor:pointer\" class=\"img img-thumbnail\">"
  53. : '';
  54. $data = [[
  55. $this->product->supplier->name ?? '',
  56. $this->product->title ?? '',
  57. $pic,
  58. $this->product->original_price ?? '',
  59. $this->product->price ?? '',
  60. $this->product->sale ?? '',
  61. $this->product->stock ?? '',
  62. ]];
  63. return Table::make($titles, $data);
  64. });*/
  65. $grid->column('status')
  66. ->using([ProductStatus::SOLD_OUT => 0, ProductStatus::ON_SALE => 1])
  67. ->switch();
  68. $grid->column('is_rec')->switch()->help('推荐后将在“我的”页面下方显示');
  69. $grid->column('updated_at');
  70. $grid->filter(function (Grid\Filter $filter) {
  71. $filter->panel();
  72. $filter->model()->where('agent_id', Admin::user()->id);
  73. $filter->equal('id')->width(2);
  74. $filter->like('product.title', '产品标题')->width(3);
  75. $filter->equal('status')->select(ProductStatus::array())->width(2);
  76. $options = Supplier::where('status', 1)->pluck('name', 'id')->toArray();
  77. $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
  78. });
  79. });
  80. }
  81. /**
  82. * Make a show builder.
  83. *
  84. * @param mixed $id
  85. *
  86. * @return Show
  87. */
  88. protected function detail($id)
  89. {
  90. return Show::make($id, new AgentProduct(['agent:id,name', 'product.supplier:id,name', 'user:id,nickname', 'guide:id,name']), function (Show $show) {
  91. //不允许查看非自己的数据
  92. if ($show->model()->agent_id != Admin::user()->id) {
  93. Admin::exit('数据不存在');
  94. }
  95. $show->field('id');
  96. $show->field('agent_id');
  97. $show->field('product_id');
  98. $show->field('price');
  99. $show->field('original_price');
  100. $show->field('sale');
  101. $show->field('stock');
  102. $show->field('channel_id');
  103. $show->field('category_id');
  104. $show->field('status')->using(ProductStatus::array());
  105. $show->field('verifier.nickname', '核销人员');
  106. if (Admin::user()->type == AgentType::CLUSTER) {
  107. $show->field('guide.name', '地接');
  108. }
  109. $show->field('is_rec')->using(['未推荐', '已推荐']);
  110. $show->field('title');
  111. $show->field('pictures')->image('', 80, 80);
  112. $show->field('know')->unescape();
  113. $show->field('content')->unescape();
  114. $show->field('earnest');
  115. $show->field('earnest_timeout');
  116. $show->field('deposit');
  117. $show->field('deposit_timeout');
  118. $show->field('created_at');
  119. $show->field('updated_at');
  120. $show->html(Alert::make(null, '供应商产品详情')->info());
  121. $show->field('product.id', '供应商产品ID');
  122. $show->field('product.supplier.name');
  123. $show->field('product.title');
  124. $show->field('product.pictures')->image('', 80, 80);
  125. $show->field('product.original_price');
  126. $show->field('product.price');
  127. $show->field('product.sale');
  128. $show->field('product.stock');
  129. $show->field('product.created_at', '创建时间');
  130. $show->field('product.updated_at', '更新时间');
  131. });
  132. }
  133. /**
  134. * Make a form builder.
  135. *
  136. * @return Form
  137. */
  138. protected function form()
  139. {
  140. return Form::make(new AgentProduct(), function (Form $form) {
  141. $agent_id = Admin::user()->id;
  142. //不允许查看非自己的数据
  143. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  144. return $form->response()->error('数据不存在');
  145. }
  146. $form->display('id');
  147. $form->radio('type')
  148. ->options(['单品销售', '组合销售'])
  149. ->default(0)->required()
  150. ->help('单品销售无需审核,组合销售需要审核才能上架')
  151. ->when(0, function (Form $form) {
  152. /** 单品销售 **/
  153. $form->selectTable('product_id', '选择产品')
  154. ->help('产品列表显示的是该产品的标题和图片')
  155. ->title('选择产品')
  156. ->dialogWidth('80%;min-width:825px;')
  157. ->from(SelectProduct::make())
  158. ->model(Product::class);
  159. })->when(1, function (Form $form) {
  160. /** 组合销售 **/
  161. $form->multipleSelectTable('product_ids', '选择产品')
  162. ->help('可单选或多选组合销售')
  163. ->title('选择产品')
  164. ->dialogWidth('80%;min-width:825px;')
  165. ->from(SelectProduct::make())
  166. ->model(Product::class);
  167. /** 自定义内容 **/
  168. $form->text('title');
  169. $form->multipleImage('pictures')->removable(false)->uniqueName();
  170. $form->editor('know');
  171. $form->editor('content');
  172. });
  173. $form->text('price')->required();
  174. $form->text('original_price')->required();
  175. $form->text('sale')->default(0);
  176. $form->text('stock')->default(8888)->required();
  177. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  178. $form->multipleSelect('channel_id')->options(array_slice($options, 1, null, true));
  179. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  180. $form->select('category_id')
  181. ->options(array_slice($options, 1, null, true))
  182. ->required();
  183. $form->radio('status')
  184. ->default(ProductStatus::ON_SALE)
  185. ->options([
  186. ProductStatus::ON_SALE => '上架',
  187. ProductStatus::SOLD_OUT => '下架',
  188. ])
  189. ->required();
  190. $form->switch('is_rec')->help('推荐后将在“我的”页面下方显示');
  191. //$form->selectTable('verifier')
  192. // ->title('选择核销人员')
  193. // ->dialogWidth('50%;min-width:600px;') //不起作用
  194. // ->from(SelectUser::make(['is_verify' => 1]))
  195. // ->model(User::class, 'id', 'nickname')
  196. // ->customFormat(fn($v) => !$v ? '' : $v)
  197. // ->required();
  198. //组团版旅行社可以选择地接
  199. if (Admin::user()->type == AgentType::CLUSTER) {
  200. $form->selectTable('guide_id', '地接人员')
  201. ->title('选择地接人员')
  202. ->dialogWidth('50%;min-width:600px;') //不起作用
  203. ->from(SelectGuide::make())
  204. ->model(Guide::class, 'id', 'name');
  205. }
  206. $form->number('earnest')->width(2)->default(0)->help('单位:元。不输入或输入 0 则不支持定金支付,必须和定金超时时间同时设置才会生效');
  207. $form->number('earnest_timeout')->width(2)->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
  208. $form->number('deposit')->default(0)->help('单位:元。不输入或输入 0 则不支持订金支付,必须和订金超时时间同时设置才会生效');
  209. $form->number('deposit_timeout')->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
  210. })->saving(function (Form $form) {
  211. //不允许修改非自己的数据
  212. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  213. return $form->response()->error('数据不存在');
  214. }
  215. if ($form->isEditing() && $form->product_id === null && $form->product_ids === null) {
  216. //推荐按钮开关
  217. if ($form->is_rec !== null) {
  218. $form->model()->is_rec = $form->is_rec ? 1 : 0;
  219. $form->model()->save();
  220. return $form->response()->success('更新成功!');
  221. }
  222. if ($form->stock !== null || $form->status !== null) {
  223. $ids = explode(',', $form->model()->product_ids);
  224. $count = Product::where([['status', '=', ProductStatus::ON_SALE], ['stock', '>=', $form->model()->stock]])
  225. ->whereIn('id', $ids)->count();
  226. if (count($ids) != $count) {
  227. return $form->response()->error('供应商产品已下架或库存不足!');
  228. }
  229. //修改库存
  230. if ($form->stock !== null) {
  231. $form->model()->stock = $form->stock;
  232. $form->model()->save();
  233. return $form->response()->success('更新成功!');
  234. }
  235. //上下架状态按钮开关
  236. if ($form->status !== null) {
  237. $form->model()->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  238. $form->model()->save();
  239. return $form->response()->success('更新成功!');
  240. }
  241. }
  242. }
  243. if (is_null($form->type)) {
  244. return $form->response()->error('请选择销售类型!');
  245. }
  246. //单品销售
  247. if ($form->type == 0) {
  248. $form->product_id = (int)$form->product_id;
  249. if (!$form->product_id) {
  250. return $form->response()->error('请选择产品');
  251. }
  252. $form->product_ids = $form->product_id;
  253. //将供应商产品写入title,pictures,know,content
  254. $product = Product::find($form->product_id);
  255. if ($product->status != ProductStatus::ON_SALE) {
  256. return $form->response()->error('产品ID '. $form->product_id .' 已下架');
  257. } else if ($product->stock < $form->stock) {
  258. return $form->response()->error("供应商当前库存为{$product->stock},你设置的库存不能超过该数值");
  259. }
  260. $form->title = $product->title;
  261. $form->pictures = $product->pictures;
  262. $form->know = $product->know;
  263. $form->content = $product->content;
  264. }
  265. //组合销售
  266. else if ($form->type == 1) {
  267. if (!$form->product_ids) {
  268. return $form->response()->error('请选择产品');
  269. }
  270. $product_ids = explode(',', $form->product_ids);
  271. if (count($product_ids) < 2) {
  272. return $form->response()->error('组合销售必须选择两个以上产品,否则请选择单品销售');
  273. }
  274. $form->product_id = $product_ids[0];
  275. $required_fields = ['title', 'pictures', 'know', 'content'];
  276. foreach ($required_fields as $field) {
  277. if (!$form->$field) {
  278. return $form->response()->error('内容输入不完整,标题、产品图片、旅游须知、产品详情必填');
  279. }
  280. }
  281. //判断供应商产品是否存在或下架
  282. $not_in_id = Product::query()
  283. ->whereIn('id', $product_ids)
  284. ->where(function ($query) use ($form) {
  285. $query->where('status', '<>', ProductStatus::ON_SALE)
  286. ->orWhere('stock', '<', $form->stock);
  287. })
  288. ->pluck('id')
  289. ->toArray();
  290. if ($not_in_id) {
  291. return $form->response()->error('产品ID ' . join(',', $not_in_id) . ' 库存小于你设置的库存' . $form->stock . ',或不存在、已下架等');
  292. }
  293. } else {
  294. return $form->response()->error('不存在此销售方式');
  295. }
  296. $agent_id = Admin::user()->id;
  297. //处理特殊字段
  298. $form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
  299. $form->agent_id = $agent_id;
  300. if (array_key_exists('guide_id', $form->input())) {
  301. $form->guide_id = $form->guide_id ?? 0;
  302. }
  303. //组合销售需要审核,编辑时是否需要审核在saved里面判断
  304. if ($form->isCreating() && $form->type == 1) {
  305. $form->status = ProductStatus::UNAUDITED;
  306. } else {
  307. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  308. }
  309. //订金
  310. if ($form->earnest <= 0 || $form->earnest_timeout <= 0) {
  311. $form->earnest = 0;
  312. $form->earnest_timeout = 0;
  313. } else if ($form->earnest > $form->price) {
  314. return $form->response()->error('订金不能大于商品价');
  315. }
  316. //定金
  317. if ($form->deposit <= 0 || $form->deposit_timeout <= 0) {
  318. $form->deposit = 0;
  319. $form->deposit_timeout = 0;
  320. } else if ($form->earnest > $form->price) {
  321. return $form->response()->error('定金不能大于商品价');
  322. }
  323. //不允许编辑的字段
  324. $form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  325. //判断是否重复发布产品
  326. $where = [
  327. ['agent_id', '=', $agent_id],
  328. ['product_id', '=', $form->product_id],
  329. ['product_ids', '=', $form->product_ids],
  330. ];
  331. if ($form->isEditing()) {
  332. $where[] = ['id', '<>', $form->getKey()];
  333. }
  334. if ($form->repository()->model()->where($where)->exists()) {
  335. return $form->response()->error('该产品已经存在,请勿重复发布');
  336. }
  337. })->saved(function (Form $form) {
  338. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  339. $product_ids = explode(',', $form->product_ids);
  340. $agent_product_id = $form->getKey();
  341. $product = Product::whereIn('id', $product_ids)->orderBy('id')
  342. ->get(['id AS product_id', 'supplier_id'])->toArray();
  343. $insert_data = array_map(function ($v) use ($agent_product_id) {
  344. $v['agent_product_id'] = $agent_product_id;
  345. $v['agent_id'] = Admin::user()->id;
  346. return $v;
  347. }, $product);
  348. //组合产品编辑关键字段需要审核
  349. if ($form->isEditing() && $form->model()->wasChanged(['title', 'pictures', 'know', 'content'])) {
  350. $form->model()->update(['status' => ProductStatus::UNAUDITED]);
  351. }
  352. if ($form->isCreating()) {
  353. AgentProductItem::insert($insert_data);
  354. } else if ($form->isEditing()) {
  355. //删除原来有,但现在没有的数据
  356. AgentProductItem::query()
  357. ->where('agent_product_id', $agent_product_id)
  358. ->whereNotIn('product_id', $product_ids)->delete();
  359. //插入原来没有,但是现在有的数据
  360. foreach ($insert_data as $v) {
  361. AgentProductItem::query()->firstOrCreate(
  362. ['agent_product_id' => $agent_product_id, 'product_id' => $v['product_id']],
  363. $v
  364. );
  365. }
  366. }
  367. })->deleting(function (Form $form) {
  368. //不允许删除非自己的数据
  369. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  370. return $form->response()->error('数据不存在');
  371. }
  372. //处理组合产品明细表
  373. $ids = array_column($form->model()->toArray(),'id');
  374. AgentProductItem::query()->whereIn('agent_product_id',$ids)->delete();
  375. });
  376. }
  377. }