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

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