BuyHouseController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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->number('project_id', '选房系统项目id')->rules('required', ['required' => '项目id不能为空。若未确定id,请填0'])->setWidth(4)->setMustMark();
  141. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  142. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  143. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  144. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  145. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  146. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  147. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  148. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  149. $form->saved(function (Form $form) {
  150. //如果没有上传logo,判断是否
  151. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  152. if (!$form->model()->small_img && $small_img) {
  153. $form->model()->small_img = $small_img;
  154. $form->model()->save();
  155. }
  156. });
  157. $form->footer(function ($footer) {
  158. $footer->disableViewCheck();
  159. $footer->disableEditingCheck();
  160. $footer->disableCreatingCheck();
  161. $footer->disableReset();
  162. });
  163. return $form;
  164. }
  165. protected function editForm($id)
  166. {
  167. $form = new ValidateForm(new TalentHouse());
  168. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  169. $form->number('project_id', '选房系统项目id')->rules('required', ['required' => '项目id不能为空。若未确定id,请填0'])->setWidth(4)->setMustMark();
  170. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  171. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  172. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  173. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  174. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  175. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  176. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  177. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  178. $form->saved(function (Form $form) {
  179. //如果没有上传logo,判断是否
  180. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  181. if (!$form->model()->small_img && $small_img) {
  182. $form->model()->small_img = $small_img;
  183. $form->model()->save();
  184. }
  185. });
  186. $form->footer(function ($footer) {
  187. $footer->disableViewCheck();
  188. $footer->disableEditingCheck();
  189. $footer->disableCreatingCheck();
  190. $footer->disableReset();
  191. });
  192. $form->tools(function (Form\Tools $tools) {
  193. $tools->disableDelete();
  194. $tools->disableView();
  195. });
  196. return $form;
  197. }
  198. public function update($id)
  199. {
  200. return $this->editForm($id)->update($id);
  201. }
  202. public function destroy($id)
  203. {
  204. //是否存在报名列表
  205. $ids = explode(',', $id);
  206. $check = TalentHouseApply::whereIn('house_id', $ids)->count();
  207. if ($check > 0) {
  208. return response()->json([
  209. 'status' => false,
  210. 'message' => '存在申报信息的房源不允许删除!',
  211. ]);
  212. }
  213. if ($this->form()->destroy($id)) {
  214. $data = [
  215. 'status' => true,
  216. 'message' => trans('admin.delete_succeeded'),
  217. ];
  218. } else {
  219. $data = [
  220. 'status' => false,
  221. 'message' => trans('admin.delete_failed'),
  222. ];
  223. }
  224. return response()->json($data);
  225. }
  226. /**
  227. * 释放名额
  228. */
  229. public function sock(Request $request)
  230. {
  231. $id = $request->id;
  232. if (empty($id)) {
  233. return response()->json(['code' => 0, 'content' => '数据异常']);
  234. }
  235. $house = TalentHouse::find($id);
  236. if (strtotime($house['supply_time']) > time()) {
  237. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  238. }
  239. if ($house['is_sock'] == 2) {
  240. return response()->json(['code' => 1]);
  241. }
  242. //释放名额
  243. TalentHouse::where('id', $id)->update(['is_sock' => 2]);
  244. TalentHouseApply::where('house_id', $id)->update(['is_sock' => 2]);
  245. return response()->json(['code' => 1]);
  246. }
  247. /**
  248. * 同步选房系统
  249. */
  250. public function sync(Request $request)
  251. {
  252. $id = $request->id;
  253. if (empty($id)) {
  254. return response()->json(['code' => 0, 'content' => '数据异常']);
  255. }
  256. $house = TalentHouse::find($id);
  257. if ($house['project_id'] == 0) {
  258. return response()->json(['code' => 0, 'content' => '请先填选房系统项目id']);
  259. }
  260. if (strtotime($house['supply_time']) > time()) {
  261. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  262. }
  263. if ($house['sync_status'] == 1) {
  264. return response()->json(['code' => 1]);
  265. }
  266. //TODO:同步选房系统,等接口
  267. $apply = TalentHouseApply::with('idcard')->where('select_house_no', '<', 999999)->orderBy('select_house_no', 'asc')->get();
  268. if (empty($apply)) {
  269. return response()->json(['code' => 0, 'content' => '请先填写选房顺序号']);
  270. }
  271. $api_data = [
  272. 'xmId' => $house['project_id'],
  273. 'xmName' => $house['name'],
  274. 'data' => [],
  275. ];
  276. foreach ($apply as $v) {
  277. $data_item = [];
  278. $data_item['index'] = $v['select_house_no'];
  279. $data_item['xm1'] = $v['name'];
  280. $data_item['card1'] = $v['idcard']['id_card'];
  281. $family = $v['family'] ? json_decode($v['family'],true) : [];
  282. //家庭关系
  283. if (empty($family)) {
  284. $data_item['xm2'] = $data_item['card2'] = $data_item['xm3'] = $data_item['card3'] = '';
  285. } else {
  286. $child_names = [];
  287. $child_cards = [];
  288. foreach ($family as $fa) {
  289. if ($fa['relation'] == '配偶') {
  290. $data_item['xm2'] = $fa['realname'];
  291. $data_item['card2'] = $fa['idcard'];
  292. } else {
  293. $child_names[] = $fa['realname'];
  294. $child_cards[] = $fa['idcard'];
  295. }
  296. }
  297. $data_item['xm3'] = implode('/', $child_names);
  298. $data_item['card3'] = implode('/', $child_cards);
  299. }
  300. $data_item['hjh'] = '';
  301. $data_item['phone'] = $v['mobile'];
  302. $data_item['hunyin'] = $v['marry'] == 1 ? '未婚' : '已婚';
  303. $data_item['kx'] = '是';
  304. $api_data['data'][] = $data_item;
  305. }
  306. $ch = curl_init();
  307. curl_setopt($ch, CURLOPT_URL, "http://jjzf.fjeda.com:9001/admin/xuanfang/importGaojiRencai");
  308. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  309. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  310. ]
  311. );
  312. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  313. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  314. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  315. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  316. curl_setopt($ch,CURLOPT_POST, true);
  317. curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($api_data));
  318. $result = curl_exec($ch);
  319. curl_close($ch);
  320. $ret = json_decode($result, true);
  321. if ($ret['code'] == '0000') {
  322. TalentHouse::where('id', $id)->update(['sync_status' => 1]);
  323. return response()->json(['code' => 1]);
  324. } else {
  325. return response()->json(['code' => 0, 'content' => $ret['msg']]);
  326. }
  327. }
  328. }