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

374 lines
13 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('created_at');
  117. $show->field('updated_at');
  118. $show->html(Alert::make(null, '供应商产品详情')->info());
  119. $show->field('product.id', '供应商产品ID');
  120. $show->field('product.supplier.name');
  121. $show->field('product.title');
  122. $show->field('product.pictures')->image('', 80, 80);
  123. $show->field('product.original_price');
  124. $show->field('product.price');
  125. $show->field('product.sale');
  126. $show->field('product.stock');
  127. $show->field('product.created_at', '创建时间');
  128. $show->field('product.updated_at', '更新时间');
  129. });
  130. }
  131. /**
  132. * Make a form builder.
  133. *
  134. * @return Form
  135. */
  136. protected function form()
  137. {
  138. return Form::make(new AgentProduct(), function (Form $form) {
  139. $agent_id = Admin::user()->id;
  140. //不允许查看非自己的数据
  141. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  142. return $form->response()->error('数据不存在');
  143. }
  144. $form->display('id');
  145. $form->radio('type')
  146. ->options(['单品销售', '组合销售'])
  147. ->default(0)
  148. ->when(0, function (Form $form) {
  149. /** 单品销售 **/
  150. $form->selectTable('product_id', '选择产品')
  151. ->help('产品列表显示的是该产品的标题和图片')
  152. ->title('选择产品')
  153. ->dialogWidth('80%;min-width:825px;')
  154. ->from(SelectProduct::make())
  155. ->model(Product::class);
  156. })->when(1, function (Form $form) {
  157. /** 组合销售 **/
  158. $form->multipleSelectTable('product_ids', '选择产品')
  159. ->help('可单选或多选组合销售')
  160. ->title('选择产品')
  161. ->dialogWidth('80%;min-width:825px;')
  162. ->from(SelectProduct::make())
  163. ->model(Product::class);
  164. /** 自定义内容 **/
  165. $form->text('title');
  166. $form->multipleImage('pictures')->removable(false)->uniqueName();
  167. $form->editor('know');
  168. $form->editor('content');
  169. });
  170. $form->text('title', '产品名称');
  171. $form->text('price')->required();
  172. $form->text('original_price')->required();
  173. $form->text('sale')->default(0);
  174. $form->text('stock')->default(8888);
  175. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  176. $form->multipleSelect('channel_id')->options(array_slice($options, 1, null, true));
  177. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  178. $form->select('category_id')
  179. ->options(array_slice($options, 1, null, true))
  180. ->required();
  181. $form->radio('status')
  182. ->default(ProductStatus::ON_SALE)
  183. ->options([
  184. ProductStatus::ON_SALE => '上架',
  185. ProductStatus::SOLD_OUT => '下架',
  186. ])
  187. ->required();
  188. $form->switch('is_rec')->help('推荐后将在“我的”页面下方显示');
  189. $form->selectTable('verifier')
  190. ->title('选择核销人员')
  191. ->dialogWidth('50%;min-width:600px;') //不起作用
  192. ->from(SelectUser::make(['is_verify' => 1]))
  193. ->model(User::class, 'id', 'nickname')
  194. ->customFormat(fn($v) => !$v ? '' : $v)
  195. ->required();
  196. //组团版旅行社可以选择地接
  197. if (Admin::user()->type == AgentType::CLUSTER) {
  198. $form->selectTable('guide_id', '地接人员')
  199. ->title('选择地接人员')
  200. ->dialogWidth('50%;min-width:600px;') //不起作用
  201. ->from(SelectGuide::make())
  202. ->model(Guide::class, 'id', 'name');
  203. }
  204. })->saving(function (Form $form) {
  205. //不允许修改非自己的数据
  206. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  207. return $form->response()->error('数据不存在');
  208. }
  209. if ($form->product_id === null && $form->product_ids === null) {
  210. //推荐按钮开关
  211. if ($form->is_rec !== null) {
  212. $form->model()->is_rec = $form->is_rec ? 1 : 0;
  213. $form->model()->save();
  214. return $form->response()->success('更新成功!');
  215. }
  216. //上下架状态按钮开关
  217. if ($form->status !== null) {
  218. $form->model()->status = $form->status == 1 ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  219. $form->model()->save();
  220. return $form->response()->success('更新成功!');
  221. }
  222. }
  223. //单品销售
  224. if ($form->type == 0) {
  225. $form->product_id = (int)$form->product_id;
  226. if (!$form->product_id) {
  227. return $form->response()->error('请选择产品');
  228. }
  229. $form->product_ids = $form->product_id;
  230. //将供应商产品写入title,pictures,know,content
  231. $product = Product::find($form->product_id);
  232. if ($product->status != ProductStatus::ON_SALE) {
  233. return $form->response()->error('产品ID '. $form->product_id .' 已下架');
  234. } else if ($product->stock < $form->stock) {
  235. return $form->response()->error('库存不足,你设置的库存应小于等于' . $form->stock);
  236. }
  237. $form->title = $product->title;
  238. $form->pictures = $product->pictures;
  239. $form->know = $product->know;
  240. $form->content = $product->content;
  241. }
  242. //组合销售
  243. else if ($form->type == 1) {
  244. if (!$form->product_ids) {
  245. return $form->response()->error('请选择产品');
  246. }
  247. $product_ids = explode(',', $form->product_ids);
  248. if (count($product_ids) < 2) {
  249. return $form->response()->error('组合销售必须选择两个以上产品,否则请选择单品销售');
  250. }
  251. $form->product_id = $product_ids[0];
  252. $required_fields = ['title', 'pictures', 'know', 'content'];
  253. foreach ($required_fields as $field) {
  254. if (!$form->$field) {
  255. return $form->response()->error('内容输入不完整,标题、产品图片、旅游须知、产品详情必填');
  256. }
  257. }
  258. //判断供应商产品是否存在或下架
  259. $not_in_id = Product::query()
  260. ->whereIn('id', $product_ids)
  261. ->where(function ($query) use ($form) {
  262. $query->where('status', '<>', ProductStatus::ON_SALE)
  263. ->orWhere('stock', '<', $form->stock);
  264. })
  265. ->pluck('id')
  266. ->toArray();
  267. if ($not_in_id) {
  268. return $form->response()->error('产品ID ' . join(',', $not_in_id) . ' 库存小于你设置的库存' . $form->stock . ',或不存在、已下架');
  269. }
  270. } else {
  271. return $form->response()->error('不存在此销售方式');
  272. }
  273. $agent_id = Admin::user()->id;
  274. //处理特殊字段
  275. $form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
  276. $form->agent_id = $agent_id;
  277. if (array_key_exists($form->status, $form->input())) {
  278. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  279. }
  280. if (array_key_exists($form->guide_id, $form->input())) {
  281. $form->guide_id = $form->guide_id ?? 0;
  282. }
  283. //不允许编辑的字段
  284. $form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  285. //判断是否重复发布产品
  286. $where = [
  287. ['agent_id', '=', $agent_id],
  288. ['product_id', '=', $form->product_id],
  289. ['product_ids', '=', $form->product_ids],
  290. ];
  291. if ($form->isEditing()) {
  292. $where[] = ['id', '<>', $form->getKey()];
  293. }
  294. if ($form->repository()->model()->where($where)->exists()) {
  295. return $form->response()->error('该产品已经存在,请勿重复发布');
  296. }
  297. })->saved(function (Form $form) {
  298. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  299. $product_ids = explode(',', $form->product_ids);
  300. $product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray();
  301. $agent_product_id = $form->getKey();
  302. $insert_data = [];
  303. foreach ($product as $k => &$v) {
  304. $insert_data[$v['id']] = [
  305. 'agent_product_id' => $agent_product_id,
  306. 'agent_id' => Admin::user()->id,
  307. 'supplier_id' => $v['supplier_id'],
  308. 'product_id' => $v['id'],
  309. ];
  310. }
  311. if ($form->isCreating()) {
  312. AgentProductItem::insert($insert_data);
  313. } else if ($form->isEditing()) {
  314. //先查出来原来的数据
  315. $old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray();
  316. //新ID在原来数据里面没有的,删除掉
  317. foreach ($old_data as $k => $v) {
  318. //删除已经删除掉的product_id
  319. if (!in_array($k, $product_ids)) {
  320. AgentProductItem::query()->find($v)->delete();
  321. }
  322. //将新数据和旧数据中都存在的product_id unset掉
  323. if (array_key_exists($k, $insert_data)) {
  324. unset($insert_data[$k]);
  325. }
  326. }
  327. //将剩余(新增)的product_id保存
  328. if ($insert_data) {
  329. AgentProductItem::insert($insert_data);
  330. }
  331. }
  332. })->deleting(function (Form $form) {
  333. //不允许删除非自己的数据
  334. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  335. return $form->response()->error('数据不存在');
  336. }
  337. });
  338. }
  339. }