BuyHouseController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Admin\Controllers\Content;
  3. use App\Admin\Extensions\Form\ValidateForm;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\TalentHouse;
  6. use App\Models\TalentHouseApply;
  7. use Encore\Admin\Controllers\HasResourceActions;
  8. use Encore\Admin\Form;
  9. use Encore\Admin\Grid;
  10. use Encore\Admin\Layout\Content;
  11. use Illuminate\Http\Request;
  12. class BuyHouseController extends Controller
  13. {
  14. use HasResourceActions;
  15. private $house_status = ['未知', '未开始', '申报中', '已结束'];
  16. /**
  17. * Index interface.
  18. *
  19. * @param Content $content
  20. * @return Content
  21. */
  22. public function index(Content $content)
  23. {
  24. return $content
  25. ->header('房源列表')
  26. ->description(' ')
  27. ->body(view('admin.content.buy_house')->with(['grid' => $this->grid()]));
  28. }
  29. /**
  30. * Edit interface.
  31. *
  32. * @param mixed $id
  33. * @param Content $content
  34. * @return Content
  35. */
  36. public function edit($id, Content $content)
  37. {
  38. return $content
  39. ->header('房源')
  40. ->description(' ')
  41. ->body($this->editForm($id)->edit($id));
  42. }
  43. /**
  44. * Create interface.
  45. *
  46. * @param Content $content
  47. * @return Content
  48. */
  49. public function create(Content $content)
  50. {
  51. return $content
  52. ->header('房源')
  53. ->description(' ')
  54. ->body($this->form());
  55. }
  56. /**
  57. * Make a grid builder.
  58. *
  59. * @return Grid
  60. */
  61. protected function grid()
  62. {
  63. $grid = new Grid(new TalentHouse());
  64. $grid->model()->orderBy('updated_at', 'DESC');
  65. $status_text = $this->house_status;
  66. $grid->id('ID');
  67. $grid->name('项目名称');
  68. $grid->small_img('缩略图')->display(function () {
  69. if ($this->small_img) {
  70. return '<span class="vtip" title="<img src=\'' . upload_asset($this->small_img) . '\' height=120>">
  71. <img class="avatar small" src="' . upload_asset($this->small_img) . '" align="absmiddle" style="width: 22px;height: 22px;">
  72. </span>';
  73. } else {
  74. return '';
  75. }
  76. });
  77. $grid->status('状态')->display(function () {
  78. $time = time();
  79. if (strtotime($this->apply_time_start) > $time) {
  80. return '未开始';
  81. }
  82. if (strtotime($this->apply_time_end) < $time) {
  83. return '已结束';
  84. }
  85. return '申报中';
  86. });
  87. $grid->declare_time('申报时间')->display(function () {
  88. return date('Y-m-d', strtotime($this->declare_time));
  89. });
  90. $grid->column('报名时间')->display(function () {
  91. return date('Y-m-d', strtotime($this->apply_time_start)) . '至' . date('Y-m-d', strtotime($this->apply_time_end));
  92. });
  93. //新增按钮
  94. $grid->disableCreateButton(false);
  95. //批量删除
  96. $grid->tools(function ($tools) {
  97. $tools->batch(function ($batch) {
  98. $batch->disableDelete(false);
  99. });
  100. });
  101. $grid->actions(function ($actions) {
  102. $actions->disableEdit(false);
  103. $actions->disableDelete(false);
  104. if ($actions->row['is_sock'] == 1) {
  105. $actions->append("&nbsp;<button class='btn btn-primary btn-xs sock' data-id=" . $actions->row['id'] . ">释放名额</button>");
  106. }
  107. if (strtotime($actions->row['supply_time']) <= time() && $actions->row['sync_status'] == 2) {
  108. $actions->append("&nbsp;<button class='btn btn-default btn-xs select_house' data-id=" . $actions->row['id'] . ">选房顺序号</button>");
  109. $actions->append("&nbsp;<button class='btn btn-warning btn-xs sync' data-id=" . $actions->row['id'] . ">同步到选房系统</button>");
  110. }
  111. });
  112. $grid->filter(function ($filter) {
  113. $filter->disableIdFilter();
  114. $filter->like('name', '项目名称');
  115. $status_option = ['全部', '未开始', '申报中', '已结束'];
  116. $filter->where(function ($query) {
  117. $date = date('Y-m-d H:i:s');
  118. if ($this->input == 1) {
  119. $query->where('apply_time_start', '>', $date);
  120. }
  121. if ($this->input == 2) {
  122. $query->where('apply_time_start', '<', $date)->where('apply_time_end', '>', $date);
  123. }
  124. if ($this->input == 3) {
  125. $query->where('apply_time_end', '<', $date);
  126. }
  127. }, '状态', 'status')->radio($status_option);
  128. });
  129. return $grid;
  130. }
  131. /**
  132. * Make a form builder.
  133. *
  134. * @return Form
  135. */
  136. protected function form()
  137. {
  138. $form = new ValidateForm(new TalentHouse());
  139. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  140. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  141. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  142. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  143. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  144. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  145. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  146. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  147. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  148. $form->saved(function (Form $form) {
  149. //如果没有上传logo,判断是否
  150. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  151. if (!$form->model()->small_img && $small_img) {
  152. $form->model()->small_img = $small_img;
  153. $form->model()->save();
  154. }
  155. });
  156. $form->footer(function ($footer) {
  157. $footer->disableViewCheck();
  158. $footer->disableEditingCheck();
  159. $footer->disableCreatingCheck();
  160. $footer->disableReset();
  161. });
  162. return $form;
  163. }
  164. protected function editForm($id)
  165. {
  166. $form = new ValidateForm(new TalentHouse());
  167. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  168. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  169. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  170. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  171. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  172. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  173. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  174. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  175. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  176. $form->saved(function (Form $form) {
  177. //如果没有上传logo,判断是否
  178. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  179. if (!$form->model()->small_img && $small_img) {
  180. $form->model()->small_img = $small_img;
  181. $form->model()->save();
  182. }
  183. });
  184. $form->footer(function ($footer) {
  185. $footer->disableViewCheck();
  186. $footer->disableEditingCheck();
  187. $footer->disableCreatingCheck();
  188. $footer->disableReset();
  189. });
  190. $form->tools(function (Form\Tools $tools) {
  191. $tools->disableDelete();
  192. $tools->disableView();
  193. });
  194. return $form;
  195. }
  196. public function update($id)
  197. {
  198. return $this->editForm($id)->update($id);
  199. }
  200. public function destroy($id)
  201. {
  202. //是否存在报名列表
  203. $ids = explode(',', $id);
  204. $check = TalentHouseApply::whereIn('house_id', $ids)->count();
  205. if ($check > 0) {
  206. return response()->json([
  207. 'status' => false,
  208. 'message' => '存在申报信息的房源不允许删除!',
  209. ]);
  210. }
  211. if ($this->form()->destroy($id)) {
  212. $data = [
  213. 'status' => true,
  214. 'message' => trans('admin.delete_succeeded'),
  215. ];
  216. } else {
  217. $data = [
  218. 'status' => false,
  219. 'message' => trans('admin.delete_failed'),
  220. ];
  221. }
  222. return response()->json($data);
  223. }
  224. /**
  225. * 释放名额
  226. */
  227. public function sock(Request $request)
  228. {
  229. $id = $request->id;
  230. if (empty($id)) {
  231. return response()->json(['code' => 0, 'content' => '数据异常']);
  232. }
  233. $house = TalentHouse::find($id);
  234. if (strtotime($house['supply_time']) > time()) {
  235. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  236. }
  237. if ($house['is_sock'] == 2) {
  238. return response()->json(['code' => 1]);
  239. }
  240. //释放名额
  241. TalentHouse::where('id', $id)->update(['is_sock' => 2]);
  242. TalentHouseApply::where('house_id', $id)->update(['is_sock' => 2]);
  243. return response()->json(['code' => 1]);
  244. }
  245. /**
  246. * 同步选房系统
  247. */
  248. public function sync(Request $request)
  249. {
  250. $id = $request->id;
  251. if (empty($id)) {
  252. return response()->json(['code' => 0, 'content' => '数据异常']);
  253. }
  254. $house = TalentHouse::find($id);
  255. if (strtotime($house['supply_time']) > time()) {
  256. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  257. }
  258. if ($house['sync_status'] == 1) {
  259. return response()->json(['code' => 1]);
  260. }
  261. //TODO:同步选房系统,等接口
  262. TalentHouse::where('id', $id)->update(['sync_status' => 1]);
  263. return response()->json(['code' => 1]);
  264. }
  265. }