BuyHouseController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 App\Services\Common\Sm4Service;
  8. use Encore\Admin\Controllers\HasResourceActions;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. class BuyHouseController extends Controller
  15. {
  16. use HasResourceActions;
  17. private $house_status = ['未知', '未开始', '申报中', '已结束'];
  18. /**
  19. * Index interface.
  20. *
  21. * @param Content $content
  22. * @return Content
  23. */
  24. public function index(Content $content)
  25. {
  26. return $content
  27. ->header('房源列表')
  28. ->description(' ')
  29. ->body(view('admin.content.buy_house')->with(['grid' => $this->grid()]));
  30. }
  31. /**
  32. * Edit interface.
  33. *
  34. * @param mixed $id
  35. * @param Content $content
  36. * @return Content
  37. */
  38. public function edit($id, Content $content)
  39. {
  40. return $content
  41. ->header('房源')
  42. ->description(' ')
  43. ->body($this->editForm($id)->edit($id));
  44. }
  45. /**
  46. * Create interface.
  47. *
  48. * @param Content $content
  49. * @return Content
  50. */
  51. public function create(Content $content)
  52. {
  53. return $content
  54. ->header('房源')
  55. ->description(' ')
  56. ->body($this->form());
  57. }
  58. /**
  59. * Make a grid builder.
  60. *
  61. * @return Grid
  62. */
  63. protected function grid()
  64. {
  65. $grid = new Grid(new TalentHouse());
  66. $grid->model()->orderBy('updated_at', 'DESC');
  67. $grid->id('ID');
  68. $grid->name('项目名称');
  69. $grid->small_img('缩略图')->display(function () {
  70. if ($this->small_img) {
  71. return '<span class="vtip" title="<img src=\'' . upload_asset($this->small_img) . '\' height=120>">
  72. <img class="avatar small" src="' . upload_asset($this->small_img) . '" align="absmiddle" style="width: 22px;height: 22px;">
  73. </span>';
  74. } else {
  75. return '';
  76. }
  77. });
  78. $grid->status('状态')->display(function () {
  79. $time = time();
  80. if (strtotime($this->apply_time_start) > $time) {
  81. return '未开始';
  82. }
  83. if (strtotime($this->apply_time_end) < $time) {
  84. return '已结束';
  85. }
  86. return '申报中';
  87. });
  88. $grid->declare_time('申报时间')->display(function () {
  89. return date('Y-m-d', strtotime($this->declare_time));
  90. });
  91. $grid->column('报名时间')->display(function () {
  92. return date('Y-m-d', strtotime($this->apply_time_start)) . '至' . date('Y-m-d', strtotime($this->apply_time_end));
  93. });
  94. //新增按钮
  95. $grid->disableCreateButton(false);
  96. //批量删除
  97. $grid->tools(function ($tools) {
  98. $tools->batch(function ($batch) {
  99. $batch->disableDelete(false);
  100. });
  101. });
  102. $grid->actions(function ($actions) {
  103. $actions->disableEdit(false);
  104. $actions->disableDelete(false);
  105. if (strtotime($actions->row['supply_time']) <= time() && $actions->row['sync_status'] == 2) {
  106. $actions->append("&nbsp;<button class='btn btn-default btn-xs select_house' data-id=" . $actions->row['id'] . ">选房顺序号</button>");
  107. $actions->append("&nbsp;<button class='btn btn-warning btn-xs sync' data-id=" . $actions->row['id'] . ">同步到选房系统</button>");
  108. }
  109. if (strtotime($actions->row['supply_time']) <= time()) {
  110. $actions->append("&nbsp;<button class='btn btn-primary btn-xs export_pass' data-id=" . $actions->row['id'] . ">导出通过名单</button>");
  111. $actions->append("&nbsp;<button class='btn btn-danger btn-xs export_no_pass' data-id=" . $actions->row['id'] . ">导出不通过名单</button>");
  112. }
  113. });
  114. $grid->filter(function ($filter) {
  115. $filter->disableIdFilter();
  116. $filter->like('name', '项目名称');
  117. $status_option = ['全部', '未开始', '申报中', '已结束'];
  118. $filter->where(function ($query) {
  119. $date = date('Y-m-d H:i:s');
  120. if ($this->input == 1) {
  121. $query->where('apply_time_start', '>', $date);
  122. }
  123. if ($this->input == 2) {
  124. $query->where('apply_time_start', '<', $date)->where('apply_time_end', '>', $date);
  125. }
  126. if ($this->input == 3) {
  127. $query->where('apply_time_end', '<', $date);
  128. }
  129. }, '状态', 'status')->radio($status_option);
  130. });
  131. return $grid;
  132. }
  133. /**
  134. * Make a form builder.
  135. *
  136. * @return Form
  137. */
  138. protected function form()
  139. {
  140. $form = new ValidateForm(new TalentHouse());
  141. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  142. $form->number('project_id', '选房系统项目id')->rules('required', ['required' => '项目id不能为空。若未确定id,请填0'])->setWidth(4)->setMustMark();
  143. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  144. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  145. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  146. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  147. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  148. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  149. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  150. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  151. $form->saved(function (Form $form) {
  152. //如果没有上传logo,判断是否
  153. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  154. if (!$form->model()->small_img && $small_img) {
  155. $form->model()->small_img = $small_img;
  156. $form->model()->save();
  157. }
  158. });
  159. $form->footer(function ($footer) {
  160. $footer->disableViewCheck();
  161. $footer->disableEditingCheck();
  162. $footer->disableCreatingCheck();
  163. $footer->disableReset();
  164. });
  165. return $form;
  166. }
  167. protected function editForm($id)
  168. {
  169. $form = new ValidateForm(new TalentHouse());
  170. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  171. $form->number('project_id', '选房系统项目id')->rules('required', ['required' => '项目id不能为空。若未确定id,请填0'])->setWidth(4)->setMustMark();
  172. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  173. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  174. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  175. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  176. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  177. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  178. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  179. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  180. $form->saved(function (Form $form) {
  181. //如果没有上传logo,判断是否
  182. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  183. if (!$form->model()->small_img && $small_img) {
  184. $form->model()->small_img = $small_img;
  185. $form->model()->save();
  186. }
  187. });
  188. $form->footer(function ($footer) {
  189. $footer->disableViewCheck();
  190. $footer->disableEditingCheck();
  191. $footer->disableCreatingCheck();
  192. $footer->disableReset();
  193. });
  194. $form->tools(function (Form\Tools $tools) {
  195. $tools->disableDelete();
  196. $tools->disableView();
  197. });
  198. return $form;
  199. }
  200. public function update($id)
  201. {
  202. return $this->editForm($id)->update($id);
  203. }
  204. public function destroy($id)
  205. {
  206. //是否存在报名列表
  207. $ids = explode(',', $id);
  208. $check = TalentHouseApply::whereIn('house_id', $ids)->count();
  209. if ($check > 0) {
  210. return response()->json([
  211. 'status' => false,
  212. 'message' => '存在申报信息的房源不允许删除!',
  213. ]);
  214. }
  215. if ($this->form()->destroy($id)) {
  216. $data = [
  217. 'status' => true,
  218. 'message' => trans('admin.delete_succeeded'),
  219. ];
  220. } else {
  221. $data = [
  222. 'status' => false,
  223. 'message' => trans('admin.delete_failed'),
  224. ];
  225. }
  226. return response()->json($data);
  227. }
  228. /**
  229. * 释放名额
  230. */
  231. public function sock(Request $request)
  232. {
  233. $id = $request->id;
  234. if (empty($id)) {
  235. return response()->json(['code' => 0, 'content' => '数据异常']);
  236. }
  237. $house = TalentHouse::find($id);
  238. if (strtotime($house['supply_time']) > time()) {
  239. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  240. }
  241. if ($house['is_sock'] == 2) {
  242. return response()->json(['code' => 1]);
  243. }
  244. //释放名额
  245. TalentHouse::where('id', $id)->update(['is_sock' => 2]);
  246. TalentHouseApply::where('house_id', $id)->update(['is_sock' => 2]);
  247. return response()->json(['code' => 1]);
  248. }
  249. /**
  250. * 同步选房系统
  251. */
  252. public function sync(Request $request)
  253. {
  254. $id = $request->id;
  255. if (empty($id)) {
  256. return response()->json(['code' => 0, 'content' => '数据异常']);
  257. }
  258. $house = TalentHouse::find($id);
  259. if ($house['project_id'] == 0) {
  260. return response()->json(['code' => 0, 'content' => '请先填选房系统项目id']);
  261. }
  262. if (strtotime($house['supply_time']) > time()) {
  263. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  264. }
  265. if ($house['sync_status'] == 1) {
  266. return response()->json(['code' => 1]);
  267. }
  268. //释放名额
  269. if ($house['is_sock'] == 1) {
  270. TalentHouse::where('id', $id)->update(['is_sock' => 2]);
  271. TalentHouseApply::where('house_id', $id)->update(['is_sock' => 2]);
  272. }
  273. //TODO:同步选房系统,等接口
  274. $apply = TalentHouseApply::with('idcard')->where('house_id', $id)->where('select_house_no', '<', 999999)->orderBy('select_house_no', 'asc')->get();
  275. if (empty($apply)) {
  276. return response()->json(['code' => 0, 'content' => '请先填写选房顺序号']);
  277. }
  278. $api_data = [
  279. 'xmId' => $house['project_id'],
  280. 'xmName' => $house['name'],
  281. 'data' => [],
  282. ];
  283. foreach ($apply as $v) {
  284. $data_item = [];
  285. $data_item['index'] = $v['select_house_no'];
  286. $data_item['xm1'] = $v['name'];
  287. $data_item['card1'] = $v['idcard']['id_card'];
  288. $family = $v['family'] ? json_decode($v['family'], true) : [];
  289. $data_item['card3'] = $data_item['xm3'] = $data_item['card2'] = $data_item['xm2'] = '';
  290. //家庭关系
  291. if (!empty($family)) {
  292. $child_names = [];
  293. $child_cards = [];
  294. foreach ($family as $fa) {
  295. if ($fa['relation'] == '配偶') {
  296. $data_item['xm2'] = $fa['realname'] == '无' ? '' : $fa['realname'];
  297. $data_item['card2'] = $fa['idcard'] == '无' ? '' : $fa['idcard'];
  298. } else {
  299. if ($fa['realname'] != '无') {
  300. $child_names[] = $fa['realname'] == '无' ? '' : $fa['realname'];
  301. $child_cards[] = $fa['idcard'] == '无' ? '' : $fa['idcard'];
  302. }
  303. }
  304. }
  305. $data_item['xm3'] = implode('/', $child_names);
  306. $data_item['card3'] = implode('/', $child_cards);
  307. }
  308. $data_item['hjh'] = '';
  309. $data_item['phone'] = $v['mobile'];
  310. $data_item['hunyin'] = $v['marry'] == 1 ? '未婚' : '已婚';
  311. $data_item['kx'] = '是';
  312. $api_data['data'][] = $data_item;
  313. }
  314. $sm4 = new Sm4Service();
  315. $sm4_key = 'ydkjjjrcgjrcdrjk';
  316. $ch = curl_init();
  317. curl_setopt($ch, CURLOPT_URL, "http://jjzf.fjeda.com:9001/admin/xuanfang/importGaojiRencai");
  318. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  319. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  320. ]
  321. );
  322. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  323. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  324. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  325. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  326. curl_setopt($ch, CURLOPT_POST, true);
  327. curl_setopt($ch, CURLOPT_POSTFIELDS, $sm4->encrypt($sm4_key, json_encode($api_data)));
  328. $result = curl_exec($ch);
  329. curl_close($ch);
  330. if (empty($result)) {
  331. return response()->json(['code' => 0, 'content' => '连接对方服务器失败,请联系管理员']);
  332. }
  333. $ret = json_decode($sm4->decrypt($sm4_key, $result), true);
  334. if ($ret['code'] == '0000') {
  335. TalentHouse::where('id', $id)->update(['sync_status' => 1]);
  336. return response()->json(['code' => 1]);
  337. } else {
  338. return response()->json(['code' => 0, 'content' => $ret['msg']]);
  339. }
  340. }
  341. /**
  342. * 导出
  343. */
  344. public function export(Request $request)
  345. {
  346. //数据获取
  347. $id = $request->id;
  348. $status = $request->status;
  349. $house = TalentHouse::find($id);
  350. $query = TalentHouseApply::with(['house', 'idcard'])
  351. ->where('house_id', $id)
  352. ->where('is_back', 2)
  353. ->where('is_draft', 2)
  354. ->orderByRaw(DB::raw("FIELD(talent_level,'第一层次','第二层次','第三层次','第四层次','第五层次','第六层次','第七层次') asc"));
  355. if ($status == 1) {
  356. $filename = $house['name'] . '审核不通过名单.xls';
  357. $query = $query->whereIn('status', [1, 3, 4]);
  358. } else {
  359. $filename = $house['name'] . '审核通过名单.xls';
  360. $query = $query->where('status', 2);
  361. }
  362. $data = $query->get();
  363. if ($data->isEmpty()) {
  364. return '暂无数据';
  365. }
  366. //数据处理
  367. $status = ['', '待审核', '审核通过', '审核驳回', '审核不通过'];
  368. $marry = ['未知', '未婚', '已婚', '离异', '丧偶'];
  369. $type = ['', '优秀人才', '集成电路优秀人才'];
  370. $no = 1;
  371. foreach ($data as $row) {
  372. $family = empty($row->family) ? '' : json_decode($row->family, true);
  373. if (!empty($family)) {
  374. $relation = [];
  375. $child = [];
  376. $id_card = [];
  377. foreach ($family as $v) {
  378. $relation[] = $v['relation'];
  379. $child[] = $v['realname'];
  380. $id_card[] = $v['idcard'];
  381. }
  382. $row['relation'] = implode('/', $relation);
  383. $row['relation_name'] = implode('/', $child);
  384. $row['relation_id_card'] = implode('/', $id_card);
  385. }
  386. $row['no'] = $no;
  387. $no++;
  388. $row['marry_text'] = $marry[$row['marry']];
  389. $row['type_text'] = $type[$row['type']];
  390. }
  391. header("Content-type: application/vnd.ms-excel; charset=utf-8");
  392. header("Content-Disposition: attachment; filename=$filename");
  393. $view = view('admin.content.export_house_result')->with(['data' => $data, 'house' => $house, 'status' => $status]);
  394. $str = response($view)->getContent();
  395. echo $str;
  396. exit;
  397. }
  398. /**
  399. * 图片
  400. */
  401. public function image(Request $request)
  402. {
  403. $src = $request->src;
  404. $photo_data[] = [
  405. 'alt' => '',
  406. 'pid' => 1,
  407. 'src' => $src,
  408. ];
  409. $photos = [
  410. 'title' => '图片信息',
  411. 'id' => 1,
  412. 'start' => 0,
  413. 'data' => $photo_data,
  414. ];
  415. return view('admin.content.buy_house_image', ['src' => $src, 'photos' => $photos]);
  416. }
  417. }