BuyHouseController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. class BuyHouseController extends Controller
  14. {
  15. use HasResourceActions;
  16. private $house_status = ['未知', '未开始', '申报中', '已结束'];
  17. /**
  18. * Index interface.
  19. *
  20. * @param Content $content
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('房源列表')
  27. ->description(' ')
  28. ->body(view('admin.content.buy_house')->with(['grid' => $this->grid()]));
  29. }
  30. /**
  31. * Edit interface.
  32. *
  33. * @param mixed $id
  34. * @param Content $content
  35. * @return Content
  36. */
  37. public function edit($id, Content $content)
  38. {
  39. return $content
  40. ->header('房源')
  41. ->description(' ')
  42. ->body($this->editForm($id)->edit($id));
  43. }
  44. /**
  45. * Create interface.
  46. *
  47. * @param Content $content
  48. * @return Content
  49. */
  50. public function create(Content $content)
  51. {
  52. return $content
  53. ->header('房源')
  54. ->description(' ')
  55. ->body($this->form());
  56. }
  57. /**
  58. * Make a grid builder.
  59. *
  60. * @return Grid
  61. */
  62. protected function grid()
  63. {
  64. $grid = new Grid(new TalentHouse());
  65. $grid->model()->orderBy('updated_at', 'DESC');
  66. $status_text = $this->house_status;
  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. });
  110. $grid->filter(function ($filter) {
  111. $filter->disableIdFilter();
  112. $filter->like('name', '项目名称');
  113. $status_option = ['全部', '未开始', '申报中', '已结束'];
  114. $filter->where(function ($query) {
  115. $date = date('Y-m-d H:i:s');
  116. if ($this->input == 1) {
  117. $query->where('apply_time_start', '>', $date);
  118. }
  119. if ($this->input == 2) {
  120. $query->where('apply_time_start', '<', $date)->where('apply_time_end', '>', $date);
  121. }
  122. if ($this->input == 3) {
  123. $query->where('apply_time_end', '<', $date);
  124. }
  125. }, '状态', 'status')->radio($status_option);
  126. });
  127. return $grid;
  128. }
  129. /**
  130. * Make a form builder.
  131. *
  132. * @return Form
  133. */
  134. protected function form()
  135. {
  136. $form = new ValidateForm(new TalentHouse());
  137. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  138. $form->number('project_id', '选房系统项目id')->rules('required', ['required' => '项目id不能为空。若未确定id,请填0'])->setWidth(4)->setMustMark();
  139. $form->datetime('declare_time', '申报时间')->rules('required', ['required' => '申报时间不能为空。'])->setWidth(4)->setMustMark();
  140. $form->datetime('apply_time_start', '报名开始时间')->rules('required', ['required' => '报名开始时间不能为空。'])->setWidth(4)->setMustMark();
  141. $form->datetime('apply_time_end', '报名结束时间')->rules('required', ['required' => '报名结束时间不能为空。'])->setWidth(4)->setMustMark();
  142. $form->datetime('supply_time', '补件截止时间')->rules('required', ['required' => '补件截止时间不能为空。'])->setWidth(4)->setMustMark();
  143. $form->text('address', '地址')->rules('required|max:100', ['required' => '地址不能为空。', 'max' => '地址长度不能大于100。'])->setWidth(8)->setMustMark();
  144. $form->image('small_img', '缩略图')->uniqueName()->rules('image|mimes:jpeg,bmp,png', ['image' => '缩略图请选择图片文件。', 'mimes' => '请选择jpeg,bmp,png格式的缩略图上传。'])->setWidth(4);
  145. $form->textarea('describe', '描述')->rules('required|max:1024', ['required' => '描述不能为空。', 'max' => '描述长度不能大于1024。'])->setWidth(8)->setMustMark();
  146. $form->editor('content', '内容')->rules('required', ['required' => '内容不能为空。'])->setMustMark();
  147. $form->saved(function (Form $form) {
  148. //如果没有上传logo,判断是否
  149. $small_img = \Illuminate\Support\Facades\Request::input('small_img');
  150. if (!$form->model()->small_img && $small_img) {
  151. $form->model()->small_img = $small_img;
  152. $form->model()->save();
  153. }
  154. });
  155. $form->footer(function ($footer) {
  156. $footer->disableViewCheck();
  157. $footer->disableEditingCheck();
  158. $footer->disableCreatingCheck();
  159. $footer->disableReset();
  160. });
  161. return $form;
  162. }
  163. protected function editForm($id)
  164. {
  165. $form = new ValidateForm(new TalentHouse());
  166. $form->text('name', '项目名称')->rules('required|max:100', ['required' => '标题不能为空。', 'max' => '标题长度不能大于100。'])->setWidth(4)->setMustMark();
  167. $form->number('project_id', '选房系统项目id')->rules('required', ['required' => '项目id不能为空。若未确定id,请填0'])->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 ($house['project_id'] == 0) {
  256. return response()->json(['code' => 0, 'content' => '请先填选房系统项目id']);
  257. }
  258. if (strtotime($house['supply_time']) > time()) {
  259. return response()->json(['code' => 0, 'content' => '报名还未结束']);
  260. }
  261. if ($house['sync_status'] == 1) {
  262. return response()->json(['code' => 1]);
  263. }
  264. //释放名额
  265. if ($house['is_sock'] == 1) {
  266. TalentHouse::where('id', $id)->update(['is_sock' => 2]);
  267. TalentHouseApply::where('house_id', $id)->update(['is_sock' => 2]);
  268. }
  269. //TODO:同步选房系统,等接口
  270. $apply = TalentHouseApply::with('idcard')->where('house_id', $id)->where('select_house_no', '<', 999999)->orderBy('select_house_no', 'asc')->get();
  271. if (empty($apply)) {
  272. return response()->json(['code' => 0, 'content' => '请先填写选房顺序号']);
  273. }
  274. $api_data = [
  275. 'xmId' => $house['project_id'],
  276. 'xmName' => $house['name'],
  277. 'data' => [],
  278. ];
  279. foreach ($apply as $v) {
  280. $data_item = [];
  281. $data_item['index'] = $v['select_house_no'];
  282. $data_item['xm1'] = $v['name'];
  283. $data_item['card1'] = $v['idcard']['id_card'];
  284. $family = $v['family'] ? json_decode($v['family'], true) : [];
  285. $data_item['card3'] = $data_item['xm3'] = $data_item['card2'] = $data_item['xm2'] = '';
  286. //家庭关系
  287. if (!empty($family)) {
  288. $child_names = [];
  289. $child_cards = [];
  290. foreach ($family as $fa) {
  291. if ($fa['relation'] == '配偶') {
  292. $data_item['xm2'] = $fa['realname'] == '无' ? '' : $fa['realname'];
  293. $data_item['card2'] = $fa['idcard'] == '无' ? '' : $fa['idcard'];
  294. } else {
  295. if ($fa['realname'] != '无') {
  296. $child_names[] = $fa['realname'];
  297. $child_cards[] = $fa['idcard'];
  298. }
  299. }
  300. }
  301. $data_item['xm3'] = implode('/', $child_names);
  302. $data_item['card3'] = implode('/', $child_cards);
  303. }
  304. $data_item['hjh'] = '';
  305. $data_item['phone'] = $v['mobile'];
  306. $data_item['hunyin'] = $v['marry'] == 1 ? '未婚' : '已婚';
  307. $data_item['kx'] = '是';
  308. $api_data['data'][] = $data_item;
  309. }
  310. $sm4 = new Sm4Service();
  311. $sm4_key = 'ydkjjjrcgjrcdrjk';
  312. $ch = curl_init();
  313. curl_setopt($ch, CURLOPT_URL, "http://jjzf.fjeda.com:9001/admin/xuanfang/importGaojiRencai");
  314. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  315. 'Content-Type: application/json; charset=utf-8' //json版本需要填写 Content-Type: application/json;
  316. ]
  317. );
  318. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  319. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  320. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  321. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  322. curl_setopt($ch, CURLOPT_POST, true);
  323. curl_setopt($ch, CURLOPT_POSTFIELDS, $sm4->encrypt($sm4_key, json_encode($api_data)));
  324. $result = curl_exec($ch);
  325. curl_close($ch);
  326. if (empty($result)) {
  327. return response()->json(['code' => 0, 'content' => '连接对方服务器失败,请联系管理员']);
  328. }
  329. $ret = json_decode($sm4->decrypt($sm4_key, $result), true);
  330. if ($ret['code'] == '0000') {
  331. TalentHouse::where('id', $id)->update(['sync_status' => 1]);
  332. return response()->json(['code' => 1]);
  333. } else {
  334. return response()->json(['code' => 0, 'content' => $ret['msg']]);
  335. }
  336. }
  337. }