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

391 lines
14 KiB

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