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

280 lines
11 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Grid\BatchAuditProduct;
  4. use App\Admin\Extensions\Grid\AuditProduct;
  5. use App\Admin\Repositories\Product;
  6. use App\Common\ProductStatus;
  7. use App\Models\AgentProduct;
  8. use App\Models\AgentProductItem;
  9. use App\Models\Category;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Form\NestedForm;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Show;
  14. use Dcat\Admin\Http\Controllers\AdminController;
  15. use Illuminate\Support\Facades\Route;
  16. class ProductController extends AdminController
  17. {
  18. /**
  19. * Make a grid builder.
  20. *
  21. * @return Grid
  22. */
  23. protected function grid()
  24. {
  25. return Grid::make(new Product(['supplier:id,company_name', 'category:id,name']), function (Grid $grid) {
  26. $grid->disableCreateButton();
  27. //如果是审核页面,多加where条件判断
  28. if (strpos(Route::current()->uri, 'audit')) {
  29. $grid->model()->where('status', ProductStatus::UNAUDITED);
  30. }
  31. $category_id = request()->input('cid');
  32. if ($category_id) {
  33. $grid->model()->whereIn('category_id', [$category_id, ...$this->get_category_child_ids($category_id)]);
  34. }
  35. /*$grid->batchActions([
  36. new BatchAuditProduct(null, 1),
  37. new BatchAuditProduct(null, 2),
  38. ]);*/
  39. $grid->column('id')->sortable();
  40. $grid->column('category.name', '分类');
  41. $grid->column('picture')->image('', 60, 60);
  42. $grid->column('title')->limit(15);
  43. $grid->column('original_price');
  44. $grid->column('price');
  45. $grid->column('sale');
  46. $grid->column('stock');
  47. $grid->column('supplier.company_name', '供应商');
  48. $grid->column('status')
  49. ->if(fn() => $this->status == ProductStatus::UNAUDITED)
  50. ->display('')
  51. ->then(function ($column) {
  52. $column->append((new AuditProduct(null, 1))->setKey($this->id))->append('&nbsp;');
  53. $column->append((new AuditProduct(null, 2))->setKey($this->id));
  54. })
  55. ->else()
  56. ->using(ProductStatus::array())
  57. ->dot([
  58. ProductStatus::ON_SALE => 'success',
  59. ProductStatus::UNAUDITED => '',
  60. ProductStatus::REFUSE => 'danger',
  61. ProductStatus::SOLD_OUT => 'warning',
  62. ], 'primary');
  63. $grid->column('single_deposit')->editable();
  64. $grid->column('created_at');
  65. $grid->column('updated_at');
  66. $grid->filter(function (Grid\Filter $filter) {
  67. $filter->panel();
  68. $filter->equal('id')->width(2);
  69. $filter->like('title')->width(3);
  70. $filter->equal('status')->select(ProductStatus::array())->width(2);
  71. $options = array_slice(Category::selectOptions(fn($query) => $query->where('agent_id', 0)), 1, null, true);
  72. $filter->equal('cid', '产品分类')->ignore()->select($options)->width(3);
  73. $filter->equal('type')->select(admin_trans('product.options.publish_type'))->width(2);
  74. });
  75. });
  76. }
  77. //递归获取指定分类下的所有子分类
  78. private function get_category_child_ids($category_id): array
  79. {
  80. static $category = null;
  81. if ($category === null) {
  82. $category = Category::where('agent_id', 0)->get()->toArray();
  83. }
  84. $child = [];
  85. foreach ($category as $cat) {
  86. if ($cat['pid'] == $category_id) {
  87. $child[] = $cat['id'];
  88. $child = array_merge($child, $this->get_category_child_ids($cat['id']));
  89. }
  90. }
  91. return $child;
  92. }
  93. /**
  94. * Make a show builder.
  95. *
  96. * @param mixed $id
  97. *
  98. * @return Show
  99. */
  100. protected function detail($id)
  101. {
  102. return Show::make($id, new Product(['supplier:id,company_name', 'category:id,name']), function (Show $show) {
  103. $show->field('id');
  104. $show->field('category.name', '所属分类');
  105. $show->field('title');
  106. $show->field('pictures')->image('', 80, 80);
  107. $show->field('original_price');
  108. $show->field('price');
  109. $show->field('sale');
  110. $show->field('stock');
  111. $show->field('status')->using(ProductStatus::array());
  112. $show->field('supplier.company_name', '供应商');
  113. $show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  114. $show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
  115. $show->field('created_at');
  116. $show->field('updated_at');
  117. });
  118. }
  119. /**
  120. * Make a form builder.
  121. *
  122. * @return Form
  123. */
  124. protected function form()
  125. {
  126. return Form::make(new Product(), function (Form $form) {
  127. $form->display('id');
  128. $options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
  129. $form->select('category_id', '所属分类')
  130. ->options(array_slice($options, 1, null, true));
  131. $form->text('title')->required();
  132. $form->multipleImage('pictures')->required()->removable(false)->uniqueName();
  133. $form->text('original_price')->required();
  134. $form->text('price')->required();
  135. $form->text('sale')->default(0);
  136. $form->text('stock')->default(9999)->required();
  137. $form->select('status')
  138. ->options(ProductStatus::array())
  139. ->default(ProductStatus::ON_SALE)
  140. ->required();
  141. $form->editor('know');
  142. $form->editor('content');
  143. $form->display('verify_mobile')->required();
  144. $form->radio('type', '产品类型')
  145. ->options(admin_trans('product.options.publish_type'))->disable($form->isEditing())
  146. ->default(current(admin_trans('product.options.publish_type')))
  147. ->when(0, function (Form $form) { //旅游线路
  148. if ($form->isEditing() && $form->model()->type != 0) {
  149. return;
  150. }
  151. $form->table('extends.field_0_project', '包含项目', function (NestedForm $table) {
  152. $table->text('name', '字段1');
  153. $table->text('num', '字段2');
  154. $table->text('price', '字段3');
  155. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  156. $form->dateRange('extends.field_0_date.start', 'extends.field_0_date.end', '行程时间');
  157. })->when(1, function (Form $form) { //酒店
  158. if ($form->isEditing() && $form->model()->type != 1) {
  159. return;
  160. }
  161. $default = [
  162. ['tag' => '行李寄存'], ['tag' => '24小时前台'], ['tag' => '前台保险柜'], ['tag' => '唤醒服务'],
  163. ['tag' => '早餐'], ['tag' => '送餐服务'], ['tag' => '电梯'], ['tag' => '空调'],
  164. ['tag' => '新风系统'], ['tag' => '24小时热水'], ['tag' => '吹风机'], ['tag' => '加湿器'],
  165. ['tag' => '自动售货机'], ['tag' => '健身房'], ['tag' => '桌球室'], ['tag' => '洗衣服务']
  166. ];
  167. $form->table('extends.field_1_tags', '酒店设施', function (NestedForm $table) {
  168. $table->text('tag', '包含项目')->placeholder('如:24小时热水、干洗服务等');
  169. })->value($default)->help('首次创建时,系统会默认填充基本服务,请根据本酒店情况进行删减或新增');
  170. $form->text('extends.field_1_name', '酒店名');
  171. $form->text('extends.field_1_address', '地址');
  172. $form->map('extends.field_1_latitude', 'extends.field_1_longitude', '位置');
  173. })->when(2, function (Form $form) { //景区
  174. if ($form->isEditing() && $form->model()->type != 2) {
  175. return;
  176. }
  177. $form->table('extends.field_2_open_time', '开放时间', function (NestedForm $table) {
  178. $table->text('node', '字段1')->placeholder('如:周一至周五');
  179. $table->text('summer', '字段2')->placeholder('如:08:00~19:00');
  180. $table->text('winter', '字段3')->placeholder('如:08:00~18:00');
  181. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  182. $form->table('extends.field_2_project', '包含项目', function (NestedForm $table) {
  183. $table->text('name', '字段1');
  184. $table->text('num', '字段2');
  185. $table->text('price', '字段3');
  186. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  187. $form->text('extends.field_2_name', '景区名');
  188. $form->text('extends.field_2_address', '地址');
  189. $form->map('extends.field_2_latitude', 'extends.field_2_longitude', '位置');
  190. })->when(3, function (Form $form) { //餐厅
  191. if ($form->isEditing() && $form->model()->type != 3) {
  192. return;
  193. }
  194. $form->table('extends.field_3_open_time', '开放时间', function (NestedForm $table) {
  195. $table->text('week', '字段1')->placeholder('如:周一至周五');
  196. $table->text('section', '字段2')->placeholder('如:上午/下午');
  197. $table->text('time', '字段3')->placeholder('如:08:00~18:00');
  198. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  199. $form->table('extends.field_3_package', '包含套餐', function (NestedForm $table) {
  200. $table->text('name', '字段1')->placeholder('如:清蒸鱿鱼');
  201. $table->text('num', '字段2')->placeholder('如:1条');
  202. $table->text('price', '字段3')->placeholder('如:99元');
  203. })->help('第一行数据默认是表头,如:项目名称、数量、额外费用');
  204. $form->text('extends.field_3_name', '餐厅名');
  205. $form->text('extends.field_3_address', '地址');
  206. $form->map('extends.field_3_latitude', 'extends.field_3_longitude', '位置');
  207. });
  208. })->saving(function (Form $form) {
  209. //不允许编辑的字段
  210. if ($form->isEditing()) {
  211. $form->ignore(['id', 'created_at', 'updated_at', 'deleted_at']);
  212. //列表页编辑交易金
  213. if (!is_null($form->single_deposit)) {
  214. $form->model()->update(['single_deposit' => $form->single_deposit]);
  215. return $form->response()->success('设置交易金成功');
  216. }
  217. }
  218. //特殊字段处理
  219. $form->sale = $form->sale ?? 0;
  220. //过滤null字段
  221. foreach ($form->input() as $k => $v) {
  222. if (is_null($v)) {
  223. $form->$k = '';
  224. }
  225. }
  226. })->saved(function(Form $form) {
  227. //如果是审核的产品,将产品同步到代理商产品
  228. if ($form->isEditing() && $form->status == ProductStatus::ON_SALE) {
  229. $ap_ids = AgentProductItem::where('product_id', $form->getKey())->pluck('agent_product_id')->toArray();
  230. //同步信息到代理商产品,注:组合产品不同步
  231. AgentProduct::whereIn('id', $ap_ids)->where('type', 0)->update([
  232. 'title' => $form->title,
  233. 'pictures' => explode(',', $form->pictures),
  234. 'know' => $form->know,
  235. 'content' => $form->content,
  236. ]);
  237. }
  238. });
  239. }
  240. public function audit()
  241. {
  242. $cloud = \App\Models\Product::query()->where('status',ProductStatus::UNAUDITED)->count();
  243. $industry = \App\Models\IndustryProduct::query()->where('status',ProductStatus::UNAUDITED)->count();
  244. $demand = \App\Models\DemandProduct::query()->where('status',ProductStatus::UNAUDITED)->count();
  245. $total = $cloud + $industry + $demand;
  246. $arr = [
  247. 'cloud' => $cloud ?? 0,
  248. 'industry' => $industry ?? 0,
  249. 'demand' => $demand ?? 0,
  250. 'total' => $total ?? 0,
  251. ];
  252. return json_encode($arr);
  253. }
  254. }