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

305 lines
10 KiB

  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. use Dcat\Admin\Widgets\Table;
  23. class AgentProductController extends AdminController
  24. {
  25. /**
  26. * Make a grid builder.
  27. *
  28. * @return Grid
  29. */
  30. protected function grid()
  31. {
  32. return Grid::make(new AgentProduct(['product.supplier:id,name', 'category:id,name']), function (Grid $grid) {
  33. $agent_id = Admin::user()->id;
  34. $grid->model()->where('agent_id', $agent_id);
  35. $grid->column('id')->sortable();
  36. $grid->column('product.picture', '产品图片')->image('', 60, 60);
  37. $grid->column('product.title', '产品名称')->limit(15);
  38. $grid->column('product_id', '产品详情')
  39. ->display('查看')
  40. ->modal(function ($v) {
  41. $titles = ['供应商', '产品标题', '产品图片', '原价', '现价', '销量', '库存'];
  42. $pic = isset($this->product->picture)
  43. ? "<img data-action=\"preview-img\" src=\"{$this->product->picture}\" style=\"max-width:80px;max-height:200px;cursor:pointer\" class=\"img img-thumbnail\">"
  44. : '';
  45. $data = [[
  46. $this->product->supplier->name ?? '',
  47. $this->product->title ?? '',
  48. $pic,
  49. $this->product->original_price ?? '',
  50. $this->product->price ?? '',
  51. $this->product->sale ?? '',
  52. $this->product->stock ?? '',
  53. ]];
  54. return Table::make($titles, $data);
  55. });
  56. $grid->column('price');
  57. $grid->column('original_price');
  58. $grid->column('sale');
  59. $grid->column('stock');
  60. $channels = Channel::where('agent_id', $agent_id)->pluck('name', 'id')->toArray();
  61. $grid->column('channel_id', '频道')
  62. ->display(function ($modal) use ($channels) {
  63. $data = array_flip(explode(',', $this->channel_id));
  64. return join(',',array_intersect_key($channels, $data));
  65. })
  66. ->limit(10);
  67. $grid->column('category.name', '分类')->label();
  68. $grid->column('status')
  69. ->using(ProductStatus::array())
  70. ->dot([
  71. ProductStatus::ON_SALE => 'success',
  72. ProductStatus::UNAUDITED => '',
  73. ProductStatus::REFUSE => 'danger',
  74. ProductStatus::SOLD_OUT => 'warning',
  75. ], 'primary');
  76. $grid->column('created_at');
  77. $grid->column('updated_at');
  78. $grid->filter(function (Grid\Filter $filter) {
  79. $filter->panel();
  80. $filter->model()->where('agent_id', Admin::user()->id);
  81. $filter->equal('id')->width(2);
  82. $filter->like('product.title', '产品标题')->width(3);
  83. $filter->equal('status')->select(ProductStatus::array())->width(2);
  84. $options = Supplier::where('status', 1)->pluck('name', 'id')->toArray();
  85. $filter->equal('product.supplier_Id', '供应商')->select($options)->width(2);
  86. });
  87. });
  88. }
  89. /**
  90. * Make a show builder.
  91. *
  92. * @param mixed $id
  93. *
  94. * @return Show
  95. */
  96. protected function detail($id)
  97. {
  98. return Show::make($id, new AgentProduct(['agent:id,name', 'product.supplier:id,name', 'user:id,nickname', 'guide:id,name']), function (Show $show) {
  99. //不允许查看非自己的数据
  100. if ($show->model()->agent_id != Admin::user()->id) {
  101. Admin::exit('数据不存在');
  102. }
  103. $show->field('id');
  104. $show->field('agent_id');
  105. $show->field('product_id');
  106. $show->field('price');
  107. $show->field('original_price');
  108. $show->field('sale');
  109. $show->field('stock');
  110. $show->field('channel_id');
  111. $show->field('category_id');
  112. $show->field('status')->using(ProductStatus::array());
  113. $show->field('verifier.nickname', '核销人员');
  114. if (Admin::user()->type == AgentType::CLUSTER) {
  115. $show->field('guide.name', '地接');
  116. }
  117. $show->field('created_at');
  118. $show->field('updated_at');
  119. $show->html(Alert::make(null, '供应商产品详情')->info());
  120. $show->field('product.id', '供应商产品ID');
  121. $show->field('product.supplier.name');
  122. $show->field('product.title');
  123. $show->field('product.pictures')->image('', 80, 80);
  124. $show->field('product.original_price');
  125. $show->field('product.price');
  126. $show->field('product.sale');
  127. $show->field('product.stock');
  128. $show->field('product.created_at', '创建时间');
  129. $show->field('product.updated_at', '更新时间');
  130. });
  131. }
  132. /**
  133. * Make a form builder.
  134. *
  135. * @return Form
  136. */
  137. protected function form()
  138. {
  139. return Form::make(new AgentProduct(['product:id,title']), function (Form $form) {
  140. $agent_id = Admin::user()->id;
  141. //不允许查看非自己的数据
  142. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  143. return $form->response()->error('数据不存在');
  144. }
  145. $form->display('id');
  146. $form->hidden('product_id');
  147. $form->selectTable('product_id', '封面产品')
  148. ->help('产品列表显示的是该产品的标题和图片')
  149. ->title('选择产品')
  150. ->dialogWidth('80%;min-width:825px;')
  151. ->from(SelectProduct::make())
  152. ->model(Product::class)
  153. ->required();
  154. $form->multipleSelectTable('product_ids', '选择产品')
  155. ->help('可单选或多选组合销售')
  156. ->title('选择产品')
  157. ->dialogWidth('80%;min-width:825px;')
  158. ->from(SelectProduct::make())
  159. ->model(Product::class)
  160. ->required();
  161. $form->text('price')->required();
  162. $form->text('original_price')->required();
  163. $form->text('sale')->default(0);
  164. $form->text('stock')->default(9999);
  165. $options = Channel::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  166. $form->multipleSelect('channel_id')->options(array_slice($options, 1));
  167. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  168. $form->select('category_id')
  169. ->options(array_slice($options, 1))
  170. ->required();
  171. $form->radio('status')
  172. ->default(ProductStatus::ON_SALE)
  173. ->options([
  174. ProductStatus::ON_SALE => '上架',
  175. ProductStatus::SOLD_OUT => '下架',
  176. ])
  177. ->required();
  178. $form->selectTable('verifier')
  179. ->title('选择核销人员')
  180. ->dialogWidth('50%;min-width:600px;') //不起作用
  181. ->from(SelectUser::make())
  182. ->model(User::class, 'id', 'nickname')
  183. ->customFormat(fn($v) => !$v ? '' : $v)
  184. ->required();
  185. //组团版旅行社可以选择地接
  186. if (Admin::user()->type == AgentType::CLUSTER) {
  187. $form->selectTable('guide_id', '地接人员')
  188. ->title('选择地接人员')
  189. ->dialogWidth('50%;min-width:600px;') //不起作用
  190. ->from(SelectGuide::make())
  191. ->model(Guide::class, 'id', 'name');
  192. }
  193. })->saving(function (Form $form) {
  194. //不允许修改非自己的数据
  195. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  196. return $form->response()->error('数据不存在');
  197. }
  198. $agent_id = Admin::user()->id;
  199. $product_ids = explode(',', $form->product_ids);
  200. if (empty($product_ids)) {
  201. return $form->response()->error('请选择产品');
  202. }
  203. //判断供应商产品是否存在或下架
  204. $not_in_id = Product::query()
  205. ->whereIn('id', $product_ids)
  206. ->where(function ($query) use ($form) {
  207. $query->where('status', '<>', ProductStatus::ON_SALE)
  208. ->orWhere('stock', '<', $form->stock);
  209. })
  210. ->pluck('id')
  211. ->toArray();
  212. if ($not_in_id) {
  213. return $form->response()->error('产品ID '. join(',', $not_in_id) .' 库存小于你设置的库存' . $form->stock . ',或不存在、已下架');
  214. }
  215. //处理特殊字段
  216. $form->hidden(['agent_id', 'status']); //表单没有的字段,必须加这句才能够重写
  217. $form->agent_id = $agent_id;
  218. $form->status = $form->status == ProductStatus::ON_SALE ? ProductStatus::ON_SALE : ProductStatus::SOLD_OUT;
  219. //不允许编辑的字段
  220. $form->ignore(['id', 'agent_id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  221. //判断是否重复发布产品
  222. $where = [
  223. ['agent_id', '=', $agent_id],
  224. ['product_id', '=', $form->product_id],
  225. ['product_ids', '=', $form->product_ids],
  226. ];
  227. if ($form->isEditing()) {
  228. $where[] = ['id', '<>', $form->getKey()];
  229. }
  230. if ($form->repository()->model()->where($where)->exists()) {
  231. return $form->response()->error('该产品已经存在,请勿重复发布');
  232. }
  233. })->saved(function (Form $form) {
  234. /** 保存到组合产品明细,先查询出之前明细,再跟新的比较,若没有则删除,新的产品原来明细表没有的,则插入 **/
  235. $product_ids = explode(',', $form->product_ids);
  236. $product = Product::whereIn('id', $product_ids)->orderBy('id')->get(['id', 'supplier_id'])->toArray();
  237. $agent_product_id = $form->getKey();
  238. $insert_data = [];
  239. foreach ($product as $k => &$v) {
  240. $insert_data[$v['id']] = [
  241. 'agent_product_id' => $agent_product_id,
  242. 'agent_id' => Admin::user()->id,
  243. 'supplier_id' => $v['supplier_id'],
  244. 'product_id' => $v['id'],
  245. ];
  246. }
  247. if ($form->isCreating()) {
  248. AgentProductItem::insert($insert_data);
  249. } else if ($form->isEditing()) {
  250. //先查出来原来的数据
  251. $old_data = AgentProductItem::where('agent_product_id', $agent_product_id)->pluck('id', 'product_id')->toArray();
  252. //新ID在原来数据里面没有的,删除掉
  253. foreach ($old_data as $k => $v) {
  254. //删除已经删除掉的product_id
  255. if (!in_array($k, $product_ids)) {
  256. AgentProductItem::query()->find($v)->delete();
  257. }
  258. //将新数据和旧数据中都存在的product_id unset掉
  259. if (array_key_exists($k, $insert_data)) {
  260. unset($insert_data[$k]);
  261. }
  262. }
  263. //将剩余(新增)的product_id保存
  264. if ($insert_data) {
  265. AgentProductItem::insert($insert_data);
  266. }
  267. }
  268. })->deleting(function (Form $form) {
  269. //不允许删除非自己的数据
  270. if ($form->model()[0]['agent_id'] != Admin::user()->id) {
  271. return $form->response()->error('数据不存在');
  272. }
  273. });
  274. }
  275. }