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

366 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::array())
  69. ->dot([
  70. ProductStatus::ON_SALE => 'success',
  71. ProductStatus::UNAUDITED => '',
  72. ProductStatus::REFUSE => 'danger',
  73. ProductStatus::SOLD_OUT => 'warning',
  74. ], 'primary');
  75. $grid->column('is_rec')->switch()->help('推荐后将在“我的”页面下方显示');
  76. $grid->column('updated_at');
  77. $grid->filter(function (Grid\Filter $filter) {
  78. $filter->panel();
  79. $filter->model()->where('agent_id', Admin::user()->id);
  80. $filter->equal('id')->width(2);
  81. $filter->like('product.title', '产品标题')->width(3);
  82. $filter->equal('status')->select(ProductStatus::array())->width(2);
  83. $options = Supplier::where('status', 1)->pluck('name', 'id')->toArray();
  84. $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
  85. });
  86. });
  87. }
  88. /**
  89. * Make a show builder.
  90. *
  91. * @param mixed $id
  92. *
  93. * @return Show
  94. */
  95. protected function detail($id)
  96. {
  97. return Show::make($id, new AgentProduct(['agent:id,name', 'product.supplier:id,name', 'user:id,nickname', 'guide:id,name']), function (Show $show) {
  98. //不允许查看非自己的数据
  99. if ($show->model()->agent_id != Admin::user()->id) {
  100. Admin::exit('数据不存在');
  101. }
  102. $show->field('id');
  103. $show->field('agent_id');
  104. $show->field('product_id');
  105. $show->field('price');
  106. $show->field('original_price');
  107. $show->field('sale');
  108. $show->field('stock');
  109. $show->field('channel_id');
  110. $show->field('category_id');
  111. $show->field('status')->using(ProductStatus::array());
  112. $show->field('verifier.nickname', '核销人员');
  113. if (Admin::user()->type == AgentType::CLUSTER) {
  114. $show->field('guide.name', '地接');
  115. }
  116. $show->field('is_rec')->using(['未推荐', '已推荐']);
  117. $show->field('title');
  118. $show->field('pictures')->image('', 80, 80);
  119. $show->field('know')->unescape();
  120. $show->field('content')->unescape();
  121. $show->field('created_at');
  122. $show->field('updated_at');
  123. $show->html(Alert::make(null, '供应商产品详情')->info());
  124. $show->field('product.id', '供应商产品ID');
  125. $show->field('product.supplier.name');
  126. $show->field('product.title');
  127. $show->field('product.pictures')->image('', 80, 80);
  128. $show->field('product.original_price');
  129. $show->field('product.price');
  130. $show->field('product.sale');
  131. $show->field('product.stock');
  132. $show->field('product.created_at', '创建时间');
  133. $show->field('product.updated_at', '更新时间');
  134. });
  135. }
  136. /**
  137. * Make a form builder.
  138. *
  139. * @return Form
  140. */
  141. protected function form()
  142. {
  143. return Form::make(new AgentProduct(), function (Form $form) {
  144. $agent_id = Admin::user()->id;
  145. //不允许查看非自己的数据
  146. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  147. return $form->response()->error('数据不存在');
  148. }
  149. $form->display('id');
  150. $form->radio('type')
  151. ->options(['单品销售', '组合销售'])
  152. ->default(0)
  153. ->when(0, function (Form $form) {
  154. /** 单品销售 **/
  155. $form->selectTable('product_id', '选择产品')
  156. ->help('产品列表显示的是该产品的标题和图片')
  157. ->title('选择产品')
  158. ->dialogWidth('80%;min-width:825px;')
  159. ->from(SelectProduct::make())
  160. ->model(Product::class);
  161. })->when(1, function (Form $form) {
  162. /** 组合销售 **/
  163. $form->multipleSelectTable('product_ids', '选择产品')
  164. ->help('可单选或多选组合销售')
  165. ->title('选择产品')
  166. ->dialogWidth('80%;min-width:825px;')
  167. ->from(SelectProduct::make())
  168. ->model(Product::class);
  169. /** 自定义内容 **/
  170. $form->text('title');
  171. $form->multipleImage('pictures');
  172. $form->editor('know');
  173. $form->editor('content');
  174. });
  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));
  181. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  182. $form->select('category_id')
  183. ->options(array_slice($options, 1))
  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. })->saving(function (Form $form) {
  209. //不允许修改非自己的数据
  210. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  211. return $form->response()->error('数据不存在');
  212. }
  213. //推荐按钮开关
  214. if ($form->product_id === null && $form->product_ids === null && $form->is_rec !== null) {
  215. $form->model()->is_rec = $form->is_rec ? 1 : 0;
  216. $form->model()->save();
  217. return $form->response()->success('更新成功');
  218. }
  219. //单品销售
  220. if ($form->type == 0) {
  221. $form->product_id = (int)$form->product_id;
  222. if (!$form->product_id) {
  223. return $form->response()->error('请选择产品');
  224. }
  225. $form->product_ids = $form->product_id;
  226. //将供应商产品写入title,pictures,know,content
  227. $product = Product::find($form->product_id);
  228. if ($product->status != ProductStatus::ON_SALE) {
  229. return $form->response()->error('产品ID '. $form->product_id .' 已下架');
  230. } else if ($product->stock < $form->stock) {
  231. return $form->response()->error('库存不足,你设置的库存应小于等于' . $form->stock);
  232. }
  233. $form->title = $product->title;
  234. $form->pictures = $product->pictures;
  235. $form->know = $product->know;
  236. $form->content = $product->content;
  237. }
  238. //组合销售
  239. else if ($form->type == 1) {
  240. if (!$form->product_ids) {
  241. return $form->response()->error('请选择产品');
  242. }
  243. $product_ids = explode(',', $form->product_ids);
  244. if (count($product_ids) < 2) {
  245. return $form->response()->error('组合销售必须选择两个以上产品,否则请选择单品销售');
  246. }
  247. $form->product_id = $product_ids[0];
  248. $required_fields = ['title', 'pictures', 'know', 'content'];
  249. foreach ($required_fields as $field) {
  250. if (!$form->$field) {
  251. return $form->response()->error('内容输入不完整,标题、产品图片、旅游须知、产品详情必填');
  252. }
  253. }
  254. //判断供应商产品是否存在或下架
  255. $not_in_id = Product::query()
  256. ->whereIn('id', $product_ids)
  257. ->where(function ($query) use ($form) {
  258. $query->where('status', '<>', ProductStatus::ON_SALE)
  259. ->orWhere('stock', '<', $form->stock);
  260. })
  261. ->pluck('id')
  262. ->toArray();
  263. if ($not_in_id) {
  264. return $form->response()->error('产品ID ' . join(',', $not_in_id) . ' 库存小于你设置的库存' . $form->stock . ',或不存在、已下架');
  265. }
  266. } else {
  267. return $form->response()->error('不存在此销售方式');
  268. }
  269. $agent_id = Admin::user()->id;
  270. //处理特殊字段
  271. $form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
  272. $form->agent_id = $agent_id;
  273. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  274. $form->guide_id = $form->guide_id ?? 0;
  275. //不允许编辑的字段
  276. $form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  277. //判断是否重复发布产品
  278. $where = [
  279. ['agent_id', '=', $agent_id],
  280. ['product_id', '=', $form->product_id],
  281. ['product_ids', '=', $form->product_ids],
  282. ];
  283. if ($form->isEditing()) {
  284. $where[] = ['id', '<>', $form->getKey()];
  285. }
  286. if ($form->repository()->model()->where($where)->exists()) {
  287. return $form->response()->error('该产品已经存在,请勿重复发布');
  288. }
  289. })->saved(function (Form $form) {
  290. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  291. $product_ids = explode(',', $form->product_ids);
  292. $product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray();
  293. $agent_product_id = $form->getKey();
  294. $insert_data = [];
  295. foreach ($product as $k => &$v) {
  296. $insert_data[$v['id']] = [
  297. 'agent_product_id' => $agent_product_id,
  298. 'agent_id' => Admin::user()->id,
  299. 'supplier_id' => $v['supplier_id'],
  300. 'product_id' => $v['id'],
  301. ];
  302. }
  303. if ($form->isCreating()) {
  304. AgentProductItem::insert($insert_data);
  305. } else if ($form->isEditing()) {
  306. //先查出来原来的数据
  307. $old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray();
  308. //新ID在原来数据里面没有的,删除掉
  309. foreach ($old_data as $k => $v) {
  310. //删除已经删除掉的product_id
  311. if (!in_array($k, $product_ids)) {
  312. AgentProductItem::query()->find($v)->delete();
  313. }
  314. //将新数据和旧数据中都存在的product_id unset掉
  315. if (array_key_exists($k, $insert_data)) {
  316. unset($insert_data[$k]);
  317. }
  318. }
  319. //将剩余(新增)的product_id保存
  320. if ($insert_data) {
  321. AgentProductItem::insert($insert_data);
  322. }
  323. }
  324. })->deleting(function (Form $form) {
  325. //不允许删除非自己的数据
  326. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  327. return $form->response()->error('数据不存在');
  328. }
  329. });
  330. }
  331. }