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

253 lines
9.9 KiB

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