Broker.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\common\model\Config;
  5. use app\common\model\User as UserModel;
  6. use app\common\model\Worker as WorkerModel;
  7. use app\common\model\Agent as AgentModel;
  8. use app\common\model\Broker as BrokerModel;
  9. use app\common\model\Resident as ResidentModel;
  10. use app\common\model\BrokerForm as BrokerFormModel;
  11. use app\common\model\BrokerIncome as BrokerIncomeModel;
  12. use app\common\validate\Broker as BrokerValidate;
  13. use think\exception\ValidateException;
  14. class Broker extends BaseController
  15. {
  16. public function brokerList()
  17. {
  18. $workerlist = WorkerModel::order(['id' => 'desc'])->select();
  19. $agentlist = AgentModel::order(['id' => 'desc'])->select();
  20. return view('broker/brokerlist', [
  21. 'workerlist' => $workerlist,
  22. 'agentlist' => $agentlist,
  23. ]);
  24. }
  25. public function brokerForm()
  26. {
  27. $id = input('id/d, 0');
  28. $broker = BrokerModel::findOrEmpty($id);
  29. $workerlist = WorkerModel::with(['agent', 'muser'])->order(['id' => 'desc'])->select();
  30. $townlist = file_get_contents(root_path('public/static/jscss') . 'village.json');
  31. return view('broker/brokerform', [
  32. 'townlist' => $townlist,
  33. 'workerlist' => $workerlist,
  34. 'broker' => $broker,
  35. ]);
  36. }
  37. public function editBroker()
  38. {
  39. $id = input('id/d');
  40. $data = [
  41. 'title' => input('title/s', ""),
  42. 'mobile' => input('mobile/s', ""),
  43. 'weixin' => input('weixin/s', ""),
  44. 'qq' => input('qq/s', ""),
  45. 'province' => input('province/s', ""),
  46. 'city' => input('city/s', ""),
  47. 'district' => input('district/s', ""),
  48. 'region' => input('region/s', ""),
  49. 'town' => input('town/s', ""),
  50. 'details' => input('details/s', ""),
  51. 'powerreport' => input('powerreport/d') == 1 ? 1 : 2,
  52. 'status' => input('status/d') == 1 ? 1 : 2,
  53. 'latitude' => input('latitude/f', 0),
  54. 'longitude' => input('longitude/f', 0),
  55. 'type' => input('type/s', ""),
  56. ];
  57. if (empty($data['latitude']) || empty($data['longitude'])) {
  58. exit(json_encode([
  59. 'code' => 1,
  60. 'msg' => '请选择地理位置',
  61. ]));
  62. }
  63. //镇街
  64. $townvillage = input('townvillage');
  65. if (empty($townvillage)) {
  66. exit(json_encode([
  67. 'code' => 1,
  68. 'msg' => '请选择镇街',
  69. ]));
  70. }
  71. $townvillage = explode(',', $townvillage);
  72. $data['town'] = $townvillage[0];
  73. $data['village'] = $townvillage[1];
  74. if (empty($id)) {
  75. $vdata = [
  76. 'id' => $id,
  77. 'mobile' => input('mobile/s'),
  78. ];
  79. try {
  80. validate(BrokerValidate::class)->check($vdata);
  81. } catch (ValidateException $e) {
  82. exit(json_encode([
  83. 'code' => 1,
  84. 'msg' => $e->getError(),
  85. ]));
  86. }
  87. $muser = UserModel::where(['mobile' => input('musermobile/s', '')])->findOrEmpty();
  88. if ($muser->isEmpty()) {
  89. exit(json_encode([
  90. 'code' => 1,
  91. 'msg' => "关联的用户不存在。",
  92. ]));
  93. }
  94. $workeragentarr = explode(",", input('workeragent/s'));
  95. $workerid = isset($workeragentarr[0]) ? $workeragentarr[0] : 0;
  96. $agentid = isset($workeragentarr[1]) ? $workeragentarr[1] : 0;
  97. if (empty($workerid) || empty($agentid)) {
  98. exit(json_encode([
  99. 'code' => 1,
  100. 'msg' => "请选择劳务公司和代理门店。",
  101. ]));
  102. }
  103. $broker_user = BrokerModel::where('userid', $muser->id)->find();
  104. if (!empty($broker_user)) {
  105. exit(json_encode([
  106. 'code' => 1,
  107. 'msg' => "该用户已是经纪人。",
  108. ]));
  109. }
  110. $data['userid'] = $muser->id;
  111. $data['workerid'] = $workerid;
  112. $data['agentid'] = $agentid;
  113. $data['createtime'] = time();
  114. $broker = BrokerModel::create($data);
  115. event('brokerAdd', $broker);
  116. } else {
  117. $broker = BrokerModel::find($id);
  118. $broker->save($data);
  119. }
  120. exit(json_encode([
  121. 'code' => 0,
  122. ]));
  123. }
  124. public function fieldBroker()
  125. {
  126. $id = input('id/d', 0);
  127. $broker = BrokerModel::findOrEmpty($id);
  128. if ($broker->isEmpty()) {
  129. exit(json_encode([
  130. 'code' => 1,
  131. 'msg' => "信息不存在",
  132. ]));
  133. } else {
  134. $broker->save([
  135. input('field/s') => input('value'),
  136. ]);
  137. }
  138. exit(json_encode([
  139. 'code' => 0,
  140. ]));
  141. }
  142. public function delBroker()
  143. {
  144. $access_admin = session('access_admin');
  145. $password = input('password');
  146. if ($access_admin['password'] !== md5($password)) {
  147. exit(json_encode([
  148. 'code' => 1,
  149. 'msg' => "操作密码验证失败",
  150. ]));
  151. }
  152. $idarr = input('idarr/a');
  153. $user_check = UserModel::whereIn('brokerid', $idarr)->findOrEmpty();
  154. if (!empty($user_check)) {
  155. exit(json_encode([
  156. 'code' => 1,
  157. 'msg' => "该经纪人还有下线,请先转移再删除",
  158. ]));
  159. }
  160. $user_ids = BrokerModel::whereIn('id', $idarr)->column('userid');
  161. $resident_check = ResidentModel::whereIn('userid', $user_ids)->findOrEmpty();
  162. if (empty($resident_check)) {
  163. exit(json_encode([
  164. 'code' => 1,
  165. 'msg' => "该经纪人是驻场老师,请取消驻场老师资格",
  166. ]));
  167. }
  168. $result = BrokerModel::whereIn('id', $idarr)->delete();
  169. if ($result) {
  170. exit(json_encode([
  171. 'code' => 0,
  172. 'msg' => "",
  173. ]));
  174. }
  175. exit(json_encode([
  176. 'code' => 1,
  177. 'msg' => "删除失败,请稍后重试",
  178. ]));
  179. }
  180. public function listBroker()
  181. {
  182. $limit = input('limit/d', 20);
  183. $page = input('page/d', 1);
  184. $map = [];
  185. $keywords = input('keywords/s');
  186. if (!empty($keywords)) {
  187. $map[] = ['title', 'like', '%' . $keywords . '%'];
  188. }
  189. $status = input('status/d');
  190. if (!empty($status)) {
  191. $map[] = ['status', '=', $status];
  192. }
  193. $workerid = input('workerid/d');
  194. if (!empty($workerid)) {
  195. $map[] = ['workerid', '=', $workerid];
  196. }
  197. $list = BrokerModel::with(['worker', 'agent', 'muser'])->withCount(['user'])->where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text', 'powerreport_text','type_text'])->select();
  198. $count = BrokerModel::where($map)->count();
  199. if ($count == 0) {
  200. exit(json_encode([
  201. 'code' => 1,
  202. 'msg' => "未查询到数据",
  203. ]));
  204. }
  205. exit(json_encode([
  206. 'code' => 0,
  207. 'msg' => "",
  208. 'count' => $count,
  209. 'data' => $list,
  210. ]));
  211. }
  212. // 申请注册经纪人
  213. public function fbrokerList()
  214. {
  215. return view('broker/fbrokerlist');
  216. }
  217. public function fbrokerForm()
  218. {
  219. $id = input('id/d, 0');
  220. $fbroker = BrokerFormModel::findOrEmpty($id);
  221. return view('broker/fbrokerform', [
  222. 'fbroker' => $fbroker,
  223. ]);
  224. }
  225. public function editFbroker()
  226. {
  227. $id = input('id/d');
  228. $fbroker = BrokerFormModel::findOrEmpty($id);
  229. $fbroker->save([
  230. 'realname' => input('realname/s', ""),
  231. 'mobile' => input('mobile/s', ""),
  232. 'address' => input('address/s', ""),
  233. 'idcard' => input('idcard/s', ""),
  234. 'recommender' => input('recommender/s', ""),
  235. 'status' => input('status/d', 1),
  236. 'remark' => input('remark/s', ""),
  237. 'createtime' => input('createtime/s', ""),
  238. ]);
  239. exit(json_encode([
  240. 'code' => 0,
  241. ]));
  242. }
  243. public function fieldFbroker()
  244. {
  245. $id = input('id/d', 0);
  246. $info = BrokerFormModel::findOrEmpty($id);
  247. if ($info->isEmpty()) {
  248. exit(json_encode([
  249. 'code' => 1,
  250. 'msg' => "信息不存在",
  251. ]));
  252. } else {
  253. $info->save([
  254. input('field/s') => input('value'),
  255. ]);
  256. }
  257. exit(json_encode([
  258. 'code' => 0,
  259. ]));
  260. }
  261. public function delFbroker()
  262. {
  263. $idarr = input('idarr/a');
  264. $fbroker = BrokerFormModel::whereIn('id', $idarr)->select();
  265. $result = $fbroker->delete();
  266. if ($result) {
  267. exit(json_encode([
  268. 'code' => 0,
  269. 'msg' => "",
  270. ]));
  271. }
  272. exit(json_encode([
  273. 'code' => 1,
  274. 'msg' => "删除失败,请稍后重试",
  275. ]));
  276. }
  277. public function listFbroker()
  278. {
  279. $limit = input('limit');
  280. $page = input('page');
  281. $map = [];
  282. $keywords = input('keywords/s');
  283. if (!empty($keywords)) {
  284. $map[] = ['realname|mobile', 'like', '%' . $keywords . '%'];
  285. }
  286. $status = input('status/d');
  287. if (!empty($status)) {
  288. $map[] = ['status', '=', $status];
  289. }
  290. $list = BrokerFormModel::where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text', 'powerreport_text'])->select();
  291. $count = BrokerFormModel::where($map)->count();
  292. if ($count == 0) {
  293. exit(json_encode([
  294. 'code' => 1,
  295. 'msg' => "未查询到数据",
  296. ]));
  297. }
  298. exit(json_encode([
  299. 'code' => 0,
  300. 'msg' => "",
  301. 'count' => $count,
  302. 'data' => $list,
  303. ]));
  304. }
  305. public function brokerdefault()
  306. {
  307. $broker_id = Config::getConfigValue("default_broker");
  308. $broker = [];
  309. if (!empty($broker_id)) {
  310. $broker = BrokerModel::where('id', $broker_id)->find();
  311. }
  312. return view('broker/brokerdefault', [
  313. 'broker' => $broker,
  314. ]);
  315. }
  316. public function brokerajax()
  317. {
  318. $keyword = input('keyword');
  319. $list = BrokerModel::where('title|mobile', 'like', "%{$keyword}%")->limit(10)->select();
  320. exit(json_encode([
  321. 'code' => 0,
  322. 'msg' => "",
  323. 'data' => $list,
  324. ]));
  325. }
  326. public function editdefault()
  327. {
  328. $brokerid = input('brokerid/d', 0);
  329. if (empty($brokerid)) {
  330. exit(json_encode([
  331. 'code' => 1,
  332. 'msg' => "请选择经纪人",
  333. ]));
  334. }
  335. Config::setConfigValue('default_broker', $brokerid);
  336. exit(json_encode(['code' => 0]));
  337. }
  338. public function cashList()
  339. {
  340. return view('broker/cashlist');
  341. }
  342. public function listCash()
  343. {
  344. $limit = input('limit/d', 20);
  345. $page = input('page/d', 1);
  346. $map = [
  347. ['type', '=', 2],
  348. ];
  349. $status = input('status/d');
  350. if (!empty($status)) {
  351. $map[] = ['status', '=', $status];
  352. }
  353. $list = BrokerIncomeModel::with(['broker'])->where($map)->order('status', 'ASC')->limit($limit)->page($page)->append(['status_text'])->select();
  354. foreach ($list as $v) {
  355. $v['value'] = abs($v['value']);
  356. }
  357. $count = BrokerIncomeModel::where($map)->count();
  358. if ($count == 0) {
  359. exit(json_encode([
  360. 'code' => 1,
  361. 'msg' => "未查询到数据",
  362. ]));
  363. }
  364. exit(json_encode([
  365. 'code' => 0,
  366. 'msg' => "",
  367. 'count' => $count,
  368. 'data' => $list,
  369. ]));
  370. }
  371. public function examineCash()
  372. {
  373. $id = input('id/d', 0);
  374. $income = BrokerIncomeModel::findOrEmpty($id);
  375. if ($income->isEmpty()) {
  376. exit('未查询到数据');
  377. }
  378. if ($income->status != 1) {
  379. exit('该记录无需审核');
  380. }
  381. return view('broker/examinecash', [
  382. 'id' => $id,
  383. ]);
  384. }
  385. public function cashExamine()
  386. {
  387. $id = input('id/d', 0);
  388. $income = BrokerIncomeModel::findOrEmpty($id);
  389. if ($income->isEmpty()) {
  390. exit(json_encode([
  391. 'code' => 1,
  392. 'msg' => "未查询到数据",
  393. ]));
  394. }
  395. //更改状态
  396. $status = input('status/d', 0);
  397. if (empty($status)) {
  398. exit(json_encode([
  399. 'code' => 1,
  400. 'msg' => "请选择状态",
  401. ]));
  402. }
  403. $remark = input('remark', '');
  404. $income->status = $status;
  405. $income->remark = $remark;
  406. $income->save();
  407. //审核失败,退钱
  408. if ($status == 3) {
  409. $broker = BrokerModel::where('id', $income['brokerid'])->find();
  410. $broker->income += abs($income['value']);
  411. $broker->save();
  412. }
  413. exit(json_encode([
  414. 'code' => 0,
  415. ]));
  416. }
  417. public function applyList()
  418. {
  419. return view('broker/applylist');
  420. }
  421. public function listApply()
  422. {
  423. $limit = input('limit/d', 20);
  424. $page = input('page/d', 1);
  425. $map = [];
  426. $mobile = input('mobile/s');
  427. if (!empty($mobile)) {
  428. $map[] = ['mobile', 'like', '%' . $mobile . '%'];
  429. }
  430. $status = input('status/d');
  431. if (!empty($status)) {
  432. $map[] = ['status', '=', $status];
  433. }
  434. $type = input('type/d');
  435. if (!empty($type)) {
  436. $map[] = ['type', '=', $type];
  437. }
  438. $list = BrokerFormModel::with(['worker', 'agent'])->where($map)->order('id', 'DESC')->limit($limit)->page($page)->append(['status_text','type_text'])->select();
  439. $count = BrokerFormModel::where($map)->count();
  440. if ($count == 0) {
  441. exit(json_encode([
  442. 'code' => 1,
  443. 'msg' => "未查询到数据",
  444. ]));
  445. }
  446. exit(json_encode([
  447. 'code' => 0,
  448. 'msg' => "",
  449. 'count' => $count,
  450. 'data' => $list,
  451. ]));
  452. }
  453. public function delApply()
  454. {
  455. $idarr = input('idarr/a');
  456. $fbroker = BrokerFormModel::whereIn('id', $idarr)->select();
  457. $result = $fbroker->delete();
  458. if ($result) {
  459. exit(json_encode([
  460. 'code' => 0,
  461. 'msg' => "",
  462. ]));
  463. }
  464. exit(json_encode([
  465. 'code' => 1,
  466. 'msg' => "删除失败,请稍后重试",
  467. ]));
  468. }
  469. public function statusApply()
  470. {
  471. $id = input('id');
  472. $status = input('status', 1);
  473. $fbroker = BrokerFormModel::where('id', $id)->find();
  474. if ($fbroker['status'] != 1) {
  475. exit(json_encode([
  476. 'code' => 1,
  477. 'msg' => "请不要重复审核",
  478. ]));
  479. }
  480. $fbroker->status = $status;
  481. $fbroker->save();
  482. if ($status == 2) {
  483. $broker_user = BrokerModel::where('userid', $fbroker['userid'])->find();
  484. if (!empty($broker_user)) {
  485. exit(json_encode([
  486. 'code' => 0,
  487. 'msg' => "",
  488. ]));
  489. }
  490. $data['userid'] = $fbroker['userid'];
  491. $data['workerid'] = $fbroker['workerid'];
  492. $data['agentid'] = $fbroker['agentid'];
  493. $data['title'] = $fbroker['title'];
  494. $data['avatar'] = $fbroker['avatar'];
  495. $data['mobile'] = $fbroker['mobile'];
  496. $data['province'] = $fbroker['province'];
  497. $data['city'] = $fbroker['city'];
  498. $data['town'] = $fbroker['town'];
  499. $data['village'] = $fbroker['village'];
  500. $data['district'] = $fbroker['district'];
  501. $data['region'] = $fbroker['region'];
  502. $data['type'] = $fbroker['type'];
  503. $data['createtime'] = time();
  504. $broker = BrokerModel::create($data);
  505. event('brokerAdd', $broker);
  506. }
  507. exit(json_encode([
  508. 'code' => 0,
  509. 'msg' => "",
  510. ]));
  511. }
  512. }