RestaurantController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\restaurant;
  4. use App\code;
  5. use App\hot_spot;
  6. use Encore\Admin\Auth\Database\Administrator;
  7. use Encore\Admin\Form;
  8. use Encore\Admin\Grid;
  9. use Encore\Admin\Facades\Admin;
  10. use Encore\Admin\Layout\Content;
  11. use App\Http\Controllers\Controller;
  12. use Encore\Admin\Controllers\ModelForm;
  13. /**
  14. * 饭店管理
  15. * Class RestaurantController
  16. * @package App\Admin\Controllers
  17. */
  18. class RestaurantController extends Controller
  19. {
  20. use ModelForm;
  21. /**
  22. * Index interface.
  23. *
  24. * @return Content
  25. */
  26. public function index()
  27. {
  28. return Admin::content(function (Content $content) {
  29. $content->header('饭店管理');
  30. $content->description('列表');
  31. $content->body($this->grid());
  32. });
  33. }
  34. /**
  35. * Edit interface.
  36. *
  37. * @param $id
  38. * @return Content
  39. */
  40. public function edit($id)
  41. {
  42. return Admin::content(function (Content $content) use ($id) {
  43. $content->header('饭店管理');
  44. $content->description('编辑');
  45. $content->body($this->form()->edit($id));
  46. });
  47. }
  48. /**
  49. * Create interface.
  50. *
  51. * @return Content
  52. */
  53. public function create()
  54. {
  55. return Admin::content(function (Content $content) {
  56. $content->header('饭店管理');
  57. $content->description('新增');
  58. $content->body($this->form());
  59. });
  60. }
  61. /**
  62. * Make a grid builder.
  63. *
  64. * @return Grid
  65. */
  66. protected function grid()
  67. {
  68. return Admin::grid(restaurant::class, function (Grid $grid) {
  69. $grid->id('序号')->sortable();
  70. $grid->business_status('营业状态')->switch([
  71. 'on' => ['value' => 1, 'text' => '营业', 'color' => 'success'],
  72. 'off' => ['value' => 0, 'text' => '休业', 'color' => 'default'],
  73. ]);;
  74. $grid->column('number', '编号');
  75. $grid->column('name', '名称');
  76. $grid->column('营业时间')->display(function () {
  77. return $this->star_time . '-' . $this->end_time;
  78. });;
  79. $grid->food_type( '商家风格')->display(function ($value){
  80. $name = code::whereIn('id',$value)->pluck('code_dsp_name_cn');
  81. return $name;
  82. })->label();
  83. $grid->hot_status('加入热点')->switch([
  84. 'on' => ['value' => 1, 'text' => '打开', 'color' => 'success'],
  85. 'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
  86. ]);
  87. $grid->column('create_id', '创建者')->display(function ($value){
  88. $name = Administrator::where('id',$value)->pluck('name')->first();
  89. return $name;
  90. });
  91. $grid->column('update_id', '更新者')->display(function ($value){
  92. $name = Administrator::where('id',$value)->pluck('name')->first();
  93. return $name;
  94. });;
  95. $grid->created_at('创建时间');
  96. $grid->updated_at('修改时间');
  97. $grid->disableExport();
  98. });
  99. }
  100. /**
  101. * Make a form builder.
  102. *
  103. * @return Form
  104. */
  105. protected function form()
  106. {
  107. return Admin::form(restaurant::class, function (Form $form) {
  108. $form->display('id', '序号');
  109. $form->text('number','编号')->rules('required|max:10');
  110. $form->text('name','名称')->rules('required|max:10');
  111. $form->text('address','位置')->rules('required|max:30');
  112. $form->text('poi_name','POI名称')->rules('required|max:20');
  113. $form->image('poi_icon','POI图标')->uniqueName();
  114. $form->text('poi_link_url','POI跳转链接')->rules('required|max:20');
  115. $form->text('poi_x_coord','POI X坐标')->rules('required|max:20');
  116. $form->text('poi_y_coord','POI Y坐标')->rules('required|max:20');
  117. $form->timeRange('star_time', 'end_time', '选择营业时间');
  118. $form->currency('price','人均价位')->symbol('¥')->rules('required|max:20');
  119. $form->textarea('shop_intro','商家简介')->rules('required|max:120');
  120. $form->textarea('shop_info','商家描述')->rules('required|max:120');
  121. $form->checkbox('food_type','餐饮风格')->options(code::where('type_id',2)->pluck('code_dsp_name_cn','id'));
  122. $form->checkbox('serve_facility','服务设施')->options(code::where('type_id',3)->pluck('code_dsp_name_cn','id'));
  123. $form->switch('hot_status','加入热点')->states([
  124. 'on' => ['value' => 1, 'text' => '加入', 'color' => 'success'],
  125. 'off' => ['value' => 0, 'text' => '取消', 'color' => 'default'],
  126. ])->default(2);
  127. $form->switch('self_support','是否自营')->states([
  128. 'on' => ['value' => 1, 'text' => '自营', 'color' => 'success'],
  129. 'off' => ['value' => 0, 'text' => '他营', 'color' => 'default'],
  130. ])->default(1);
  131. $form->switch('business_status','营业状态')->states([
  132. 'on' => ['value' => 1, 'text' => '营业', 'color' => 'success'],
  133. 'off' => ['value' => 0, 'text' => '休业 ', 'color' => 'default'],
  134. ])->default(1);
  135. $form->multipleFile('pics','宣传照片')->removable()->uniqueName();
  136. $form->display('update_id', '更新者')->with(function ($value){
  137. if($value){
  138. }else{
  139. $value=Admin::user()->id;
  140. }
  141. $name = Administrator::where('id',$value)->pluck('name')->first();
  142. return $name;
  143. });
  144. $form->display('create_id', '创建者')->with(function ($value){
  145. if($value){
  146. }else{
  147. $value=Admin::user()->id;
  148. }
  149. $name = Administrator::where('id',$value)->pluck('name')->first();
  150. return $name;
  151. });
  152. $form->hidden('create_id')->default(Admin::user()->id);
  153. //保存前回调
  154. $form->saving(function (Form $form) {
  155. $form->update_id =Admin::user()->id;
  156. });
  157. $form->display('created_at', '创建时间');
  158. $form->display('updated_at', '更新时间');
  159. //保存后回调
  160. $form->saved(function (Form $form) {
  161. $hot_status=$form->model()->hot_status;
  162. if($hot_status==1){
  163. //创建-同时创建热点表
  164. //查询该数据是否存在
  165. $hot_spot=hot_spot::where('type','=',2)->where('link_id','=',$form->model()->id)->first();
  166. if(!$hot_spot){
  167. $hot_id=$this->hotSpotSave($form);
  168. }
  169. // else{
  170. // //删除-同时删除热点表
  171. // if ($_POST['_method']=='DELETE'){
  172. // $this->hotSpotDelete($form);
  173. // }
  174. // }
  175. }elseif($hot_status==0){
  176. $this->hotSpotDelete($form);
  177. }
  178. });
  179. });
  180. }
  181. public function hotSpotSave(Form $form){
  182. $hotSpot=new hot_spot();
  183. $hotSpot->open_status=1;
  184. $hotSpot->number=$form->model()->number;
  185. $hotSpot->name=$form->model()->name;
  186. $hotSpot->address=$form->model()->address;
  187. $hotSpot->hot_info=$form->model()->shop_intro;
  188. $hotSpot->star_date=date('y-m-d');
  189. $hotSpot->end_date=date('y-m-d');
  190. $hotSpot->star_time=$form->model()->star_time;
  191. $hotSpot->end_time=$form->model()->end_time;
  192. $hotSpot->pics=$form->model()->pics;
  193. $hotSpot->type=1;
  194. $hotSpot->link_id=$form->model()->id;
  195. $hotSpot->update_id=$form->model()->update_id;
  196. $hotSpot->create_id=$form->model()->create_id;
  197. $hotSpot->save();
  198. return $hotSpot->id;
  199. }
  200. public function hotSpotDelete(Form $form){
  201. $return=hot_spot::where('type','=',1)->where('link_id','=',$form->model()->id)->delete();
  202. return $return;
  203. }
  204. }