BuyHouseController.php 15 KB

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