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

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