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

416 lines
15 KiB

4 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. ->when(0, function (Form $form) {
  151. /** 单品销售 **/
  152. $form->selectTable('product_id', '选择产品')
  153. ->help('产品列表显示的是该产品的标题和图片')
  154. ->title('选择产品')
  155. ->dialogWidth('80%;min-width:825px;')
  156. ->from(SelectProduct::make())
  157. ->model(Product::class);
  158. })->when(1, function (Form $form) {
  159. /** 组合销售 **/
  160. $form->multipleSelectTable('product_ids', '选择产品')
  161. ->help('可单选或多选组合销售')
  162. ->title('选择产品')
  163. ->dialogWidth('80%;min-width:825px;')
  164. ->from(SelectProduct::make())
  165. ->model(Product::class);
  166. /** 自定义内容 **/
  167. $form->text('title');
  168. $form->multipleImage('pictures')->removable(false)->uniqueName();
  169. $form->editor('know');
  170. $form->editor('content');
  171. });
  172. $form->text('title', '产品名称');
  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->text('earnest')->default(0)->help('单位:元。不输入或输入 0 则不支持定金支付,必须和定金超时时间同时设置才会生效');
  207. $form->text('earnest_timeout')->default(0)->help('单位:分钟。超过这个时间未支付,订单将自动关闭');
  208. $form->text('deposit')->default(0)->help('单位:元。不输入或输入 0 则不支持订金支付,必须和订金超时时间同时设置才会生效');
  209. $form->text('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()->success('供应商产品已下架或库存不足!');
  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("供应商当前库存为{$form->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('status', $form->input())) {
  301. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  302. }
  303. if (array_key_exists('guide_id', $form->input())) {
  304. $form->guide_id = $form->guide_id ?? 0;
  305. }
  306. //订金
  307. if ($form->earnest <= 0 || $form->earnest_timeout <= 0) {
  308. $form->earnest = 0;
  309. $form->earnest_timeout = 0;
  310. } else if ($form->earnest > $form->price) {
  311. return $form->response()->error('订金不能大于商品价');
  312. }
  313. //定金
  314. if ($form->deposit <= 0 || $form->deposit_timeout <= 0) {
  315. $form->deposit = 0;
  316. $form->deposit_timeout = 0;
  317. } else if ($form->earnest > $form->price) {
  318. return $form->response()->error('定金不能大于商品价');
  319. }
  320. //不允许编辑的字段
  321. $form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  322. //判断是否重复发布产品
  323. $where = [
  324. ['agent_id', '=', $agent_id],
  325. ['product_id', '=', $form->product_id],
  326. ['product_ids', '=', $form->product_ids],
  327. ];
  328. if ($form->isEditing()) {
  329. $where[] = ['id', '<>', $form->getKey()];
  330. }
  331. if ($form->repository()->model()->where($where)->exists()) {
  332. return $form->response()->error('该产品已经存在,请勿重复发布');
  333. }
  334. })->saved(function (Form $form) {
  335. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  336. $product_ids = explode(',', $form->product_ids);
  337. $product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray();
  338. $agent_product_id = $form->getKey();
  339. $insert_data = [];
  340. foreach ($product as $k => &$v) {
  341. $insert_data[$v['id']] = [
  342. 'agent_product_id' => $agent_product_id,
  343. 'agent_id' => Admin::user()->id,
  344. 'supplier_id' => $v['supplier_id'],
  345. 'product_id' => $v['id'],
  346. ];
  347. }
  348. if ($form->isCreating()) {
  349. AgentProductItem::insert($insert_data);
  350. } else if ($form->isEditing()) {
  351. //先查出来原来的数据
  352. $old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray();
  353. //新ID在原来数据里面没有的,删除掉
  354. foreach ($old_data as $k => $v) {
  355. //删除已经删除掉的product_id
  356. if (!in_array($k, $product_ids)) {
  357. AgentProductItem::query()->find($v)->delete();
  358. }
  359. //将新数据和旧数据中都存在的product_id unset掉
  360. if (array_key_exists($k, $insert_data)) {
  361. unset($insert_data[$k]);
  362. }
  363. }
  364. //将剩余(新增)的product_id保存
  365. if ($insert_data) {
  366. AgentProductItem::insert($insert_data);
  367. }
  368. }
  369. })->deleting(function (Form $form) {
  370. //不允许删除非自己的数据
  371. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  372. return $form->response()->error('数据不存在');
  373. }
  374. });
  375. }
  376. }