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

240 lines
9.4 KiB

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