Broker.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. namespace app\mainapp\controller;
  3. use app\common\model\BrokerIncome;
  4. use app\common\service\IncomeService;
  5. use app\mainapp\BaseController;
  6. use app\common\model\Param as ParamModel;
  7. use app\common\model\User as UserModel;
  8. use app\common\model\UserFollow as UserFollowModel;
  9. use app\common\model\Broker as BrokerModel;
  10. use app\common\model\BrokerForm as BrokerFormModel;
  11. use app\common\model\DemandSnatch as DemandSnatchModel;
  12. use app\common\model\DemandReport as DemandReportModel;
  13. use app\common\model\Agent as AgentModel;
  14. use app\common\model\Worker as WorkerModel;
  15. use app\common\validate\DemandReport as DemandReportValidate;
  16. use think\exception\ValidateException;
  17. class Broker extends BaseController
  18. {
  19. public function getListBroker()
  20. {
  21. $ppage = input('ppage/d', 1);
  22. $psize = input('psize/d', 20);
  23. $map = [];
  24. $map[] = ['status', '=', 1];
  25. $searchval = input('searchval/s', "");
  26. if (!empty($searchval)) {
  27. $map[] = ['title', 'like', '%' . $searchval . '%'];
  28. }
  29. $district = input('district/s', "");
  30. if (!empty($district)) {
  31. $map[] = ['district', '=', $district];
  32. }
  33. $plist = BrokerModel::with(['muser'])->where($map)->order(['id' => 'desc'])->page($ppage)->limit($psize)->select();
  34. page_result(0, "", [
  35. 'plist' => $plist,
  36. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  37. ]);
  38. }
  39. // 申请注册经纪人
  40. public function pageForm()
  41. {
  42. $param = ParamModel::where(1)->find();
  43. page_result(0, "", [
  44. 'param' => $param,
  45. ]);
  46. }
  47. public function regBroker()
  48. {
  49. $realname = input('realname/s', "");
  50. $mobile = input('mobile/s', "");
  51. $address = input('address/s', "");
  52. $idcard = input('idcard/s', "");
  53. $recommender = input('recommender/s', "");
  54. if (empty($realname) || empty($mobile) || empty($address) || empty($idcard)) {
  55. page_result(1, "姓名、手机号、当前住址、身份证号均不能为空。");
  56. }
  57. $form = new BrokerFormModel;
  58. $form->save([
  59. 'realname' => $realname,
  60. 'mobile' => $mobile,
  61. 'address' => $address,
  62. 'idcard' => $idcard,
  63. 'recommender' => $recommender,
  64. 'status' => 1,
  65. 'remark' => "",
  66. 'createtime' => time(),
  67. ]);
  68. page_result(0, "", []);
  69. }
  70. // 订单信息
  71. public function listOrder()
  72. {
  73. $brokerid = input('brokerid/d', 0);
  74. if (empty($brokerid)) {
  75. page_result(0, "", [
  76. 'plist' => [],
  77. 'pstatus' => 'noMore',
  78. ]);
  79. }
  80. $broker = BrokerModel::find($brokerid);
  81. if (empty($broker)) {
  82. page_result(0, "", [
  83. 'plist' => [],
  84. 'pstatus' => 'noMore',
  85. ]);
  86. }
  87. $workerid = $broker->workerid;
  88. $ppage = input('ppage/d', 1);
  89. $psize = input('psize/d', 20);
  90. $map = [];
  91. $map[] = ['status', '>=', 2];
  92. $map[] = ['worker_id', '=', $workerid];
  93. $plist = DemandSnatchModel::with(['demand' => ['worker']])->where($map)->order(['status' => 'ASC', 'id' => 'DESC'])->page($ppage)->limit($psize)->append(['status_text'])->select();
  94. foreach ($plist as $v) {
  95. $snatch_num = DemandReportModel::where('snatchid', $v['id'])->count();
  96. $v['snatch_num'] = $snatch_num;
  97. }
  98. page_result(0, "", [
  99. 'plist' => $plist,
  100. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  101. ]);
  102. }
  103. // 报备信息
  104. public function getOrder()
  105. {
  106. $id = input('id/d', 0);
  107. $brokerid = input('brokerid/d', 0);
  108. $order = DemandSnatchModel::with(['demand' => ['worker']])->where(['id' => $id])->append(['status_text'])->find();
  109. $order_log = DemandReportModel::where('brokerid', $brokerid)->where('snatchid', $id)->select();
  110. page_result(0, "", [
  111. 'order' => $order,
  112. 'order_log' => $order_log,
  113. ]);
  114. }
  115. public function addReport()
  116. {
  117. $snatchid = input('snatchid/d', 0);
  118. $snatch = DemandSnatchModel::where('id', '=', $snatchid)->findOrEmpty();
  119. if ($snatch->isEmpty()) {
  120. page_result(1, "订单不存在。");
  121. }
  122. if ($snatch['status'] != 2) {
  123. exit(json_encode([
  124. 'code' => 1,
  125. 'msg' => "该订单暂时无法报备",
  126. ]));
  127. }
  128. $brokerid = input('brokerid/d', 0);
  129. $broker = BrokerModel::where('id', '=', $brokerid)->findOrEmpty();
  130. if ($broker->isEmpty()) {
  131. page_result(1, "经纪人信息不存在。");
  132. }
  133. $snatch_num = DemandReportModel::where('snatchid', $snatchid)->count();
  134. if ($snatch_num >= $snatch['num']) {
  135. exit(json_encode([
  136. 'code' => 1,
  137. 'msg' => "人数已满",
  138. ]));
  139. }
  140. //数据检验
  141. $data = [
  142. 'demandid' => $snatch['demand_id'],
  143. 'snatchid' => $snatchid,
  144. 'workerid' => $broker['workerid'],
  145. 'agentid' => $broker['agentid'],
  146. 'brokerid' => $broker['id'],
  147. 'realname' => input('realname/s', ""),
  148. 'mobile' => input('mobile/s', ""),
  149. 'idcard' => input('idcard/s', ""),
  150. 'arrivetime' => input('arrivetime/s', ""),
  151. 'remark' => input('remark/s', ""),
  152. 'retremark' => "",
  153. 'createtime' => date("Y-m-d H:i:s"),
  154. ];
  155. try {
  156. validate(DemandReportValidate::class)->check($data);
  157. } catch (ValidateException $e) {
  158. page_result(1, $e->getError());
  159. }
  160. $mobile_check = DemandReportModel::where('snatchid', $snatchid)->where('mobile', $data['mobile'])->find();
  161. if (!empty($mobile_check)) {
  162. exit(json_encode([
  163. 'code' => 1,
  164. 'msg' => "该手机号已报备,请勿重复",
  165. ]));
  166. }
  167. $idcard_check = DemandReportModel::where('snatchid', $snatchid)->where('idcard', $data['idcard'])->find();
  168. if (!empty($idcard_check)) {
  169. exit(json_encode([
  170. 'code' => 1,
  171. 'msg' => "该身份证号已报备,请勿重复",
  172. ]));
  173. }
  174. DemandReportModel::create($data);
  175. if ($snatch_num + 1 == $snatch['num']) {
  176. $snatch->status == 3;
  177. $snatch->save();
  178. }
  179. page_result(0, "", []);
  180. }
  181. // 报备信息
  182. /*public function getOrder()
  183. {
  184. $orderid = input('orderid/d',0);
  185. $agentid = input('agentid/d',0);
  186. $order = ReportOrderModel::with(['reportFactory'])->where(['id'=>$orderid])->find();
  187. $orderlog = ReportOrderlogModel::where(['agentid'=>$agentid,'orderid'=>$orderid])->order(['id'=>"DESC"])->select();
  188. ReportOrderlogModel::update(['status' => 2], ['agentid'=>$agentid,'orderid'=>$orderid]);
  189. page_result(0, "", array(
  190. 'order' => $order,
  191. 'orderlog' => $orderlog
  192. ));
  193. }
  194. // 订单信息
  195. public function listOrder()
  196. {
  197. $brokerid = input('brokerid/d',0);
  198. $broker = BrokerModel::findOrEmpty($brokerid);
  199. $agentid = $broker->agentid;
  200. $ppage = input('ppage/d', 1);
  201. $psize = input('psize/d', 20);
  202. $map = array();
  203. $map[] = ['ReportOrder.status', '>', 1];
  204. $plist = ReportOrderModel::hasWhere('reportOrderlog',['agentid'=>$agentid])->with(['reportFactory'])->where($map)->order(['status'=>'ASC','id'=>'DESC'])->page($ppage)->limit($psize)->select()->toArray();
  205. foreach($plist as $key=>$val){
  206. $plist[$key]['log'] = ReportOrderlogModel::where(['agentid'=>$agentid,'orderid'=>$val['id']])->order(['id'=>'DESC'])->findOrEmpty()->toArray();
  207. }
  208. page_result(0, "", array(
  209. 'plist' => $plist,
  210. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
  211. ));
  212. }
  213. // 数据信息
  214. public function pageBroker()
  215. {
  216. $brokerid = input('brokerid/d', 0);
  217. $stime = strtotime(date("Y-m-d"),time());
  218. $tuserCount = UserModel::where('brokerid', '=', $brokerid)->where('createtime','between',[$stime,$stime+86400])->count();
  219. $userCount = UserModel::where('brokerid', '=', $brokerid)->count();
  220. $tentryCount = ReportEntryModel::where('brokerid', '=', $brokerid)->where('createtime','between',[$stime,$stime+86400])->count();
  221. $entryCount = ReportEntryModel::where('brokerid', '=', $brokerid)->count();
  222. page_result(0, "", array('countobj' => array(
  223. 'tuser' => $tuserCount,
  224. 'user' => $userCount,
  225. 'tentry' => $tentryCount,
  226. 'entry' => $entryCount
  227. )));
  228. }
  229. // 报备信息
  230. public function getEntry()
  231. {
  232. $entryid = input('entryid/d',0);
  233. $brokerid = input('brokerid/d',0);
  234. $entry = ReportEntryModel::with(['reportFactory'])->where(['brokerid'=>$brokerid,'id'=>$entryid])->find();
  235. $broker = BrokerModel::with('agent')->findOrEmpty($brokerid);
  236. $factorylist = ReportFactoryModel::order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
  237. $factoryidarr = ReportFactoryModel::order(['priority'=>'desc','id'=>'desc'])->column('title');
  238. page_result(0, "", array(
  239. 'entry' => $entry,
  240. 'broker' => $broker,
  241. 'factorylist' => $factorylist,
  242. 'factoryidarr' => array_values($factoryidarr),
  243. 'today' => date("Y-m-d")
  244. ));
  245. }
  246. public function statusEntry()
  247. {
  248. $entryid = input('entryid/d',0);
  249. $brokerid = input('brokerid/d',0);
  250. $entry = ReportEntryModel::with(['reportFactory'])->where('brokerid','=',$brokerid)->findOrEmpty($entryid);
  251. if ($entry->isEmpty()){
  252. page_result(1, "报备信息不存在。");
  253. }
  254. if ($entry->status > 1){
  255. page_result(1, "报备信息已确认过。");
  256. }
  257. $entry->save(['status'=>2]);
  258. page_result(0, "", array(
  259. 'entry' => $entry
  260. ));
  261. }
  262. public function deleteEntry()
  263. {
  264. $entryid = input('entryid/d',0);
  265. $brokerid = input('brokerid/d',0);
  266. $entry = ReportEntryModel::with(['reportFactory'])->where('brokerid','=',$brokerid)->findOrEmpty($entryid);
  267. if ($entry->isEmpty()){
  268. page_result(1, "报备信息不存在。");
  269. }
  270. if ($entry->status > 1){
  271. page_result(1, "报备信息已确认过,不能删除。");
  272. }
  273. $entry->delete();
  274. page_result(0, "", array());
  275. }
  276. public function addEntry()
  277. {
  278. $brokerid = input('brokerid/d',0);
  279. $broker = BrokerModel::with('agent')->findOrEmpty($brokerid);
  280. if ($broker->isEmpty()){
  281. page_result(1, "经纪人信息不存在。");
  282. }
  283. $data = array(
  284. 'workerid' => $broker->workerid,
  285. 'agentid' => $broker->agentid,
  286. 'brokerid' => $brokerid,
  287. 'factoryid' => input('factoryid/d', 0),
  288. 'realname' => input('realname/s', ""),
  289. 'mobile' => input('mobile/s', ""),
  290. 'idcard' => input('idcard/s', ""),
  291. 'gender' => input('gender/s', ""),
  292. 'nation' => input('nation/s', ""),
  293. 'address' => input('address/s', ""),
  294. 'startdate' => input('startdate/s', ""),
  295. 'enddate' => input('enddate/s', ""),
  296. 'reftype' => input('reftype/s', ""),
  297. 'refpolicy' => input('refpolicy/s', ""),
  298. 'agentremark' => input('agentremark/s', ""),
  299. 'status' => input('status/d', ""),
  300. 'remark' => "",
  301. 'createtime' => time()
  302. );
  303. $entry = new ReportEntryModel;
  304. $entry->save($data);
  305. $newentry = ReportEntryModel::with(['reportFactory'])->where('brokerid','=',$brokerid)->find($entry->id);
  306. page_result(0, "", array(
  307. 'entry' => $newentry
  308. ));
  309. }*/
  310. public function setIdcard()
  311. {
  312. $picpath = input('picpath/s', "");
  313. $picpath = root_path() . "public" . $picpath;
  314. $idcard = aliyun_ocr_idcard($picpath);
  315. if ($idcard == false) {
  316. page_result(1, "身份证信息识别失败。");
  317. }
  318. page_result(0, "", [
  319. 'idcard' => $idcard,
  320. ]);
  321. }
  322. /*public function pageEntry()
  323. {
  324. $brokerid = input('brokerid/d',0);
  325. $statuslist = array(
  326. array('value'=>"", 'title'=>'状态不限'),
  327. array('value'=>1, 'title'=>'待确认'),
  328. array('value'=>2, 'title'=>'已确认'),
  329. array('value'=>3, 'title'=>'已入职'),
  330. array('value'=>4, 'title'=>'未通过')
  331. );
  332. $factorylist = ReportFactoryModel::field('id as value, title, priority')->order(['priority'=>'desc','id'=>'desc'])->select()->toArray();
  333. array_unshift($factorylist, array('value'=>"", 'title'=>'工厂不限'));
  334. page_result(0, "", array(
  335. 'statuslist' => $statuslist,
  336. 'factorylist' => $factorylist
  337. ));
  338. }
  339. public function listEntry()
  340. {
  341. $ppage = input('ppage/d', 1);
  342. $psize = input('psize/d', 20);
  343. $map = array();
  344. $brokerid = input('brokerid/d', 0);
  345. $map[] = ['brokerid', '=', $brokerid];
  346. $status = input('status/d', 0);
  347. if (!empty($status)){
  348. $map[] = ['status', '=', $status];
  349. }
  350. $factoryid = input('factoryid/d', 0);
  351. if (!empty($factoryid)){
  352. $map[] = ['factoryid', '=', $factoryid];
  353. }
  354. $searchval = input('searchval/s', "");
  355. if (!empty($searchval)){
  356. $map[] =['realname|mobile', 'like', '%'.$searchval.'%', 'OR'];
  357. }
  358. $plist = ReportEntryModel::with(['reportFactory'])->where($map)->order(['id'=>'desc'])->page($ppage)->limit($psize)->select();
  359. page_result(0, "", array(
  360. 'plist' => $plist,
  361. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more'
  362. ));
  363. }*/
  364. // 我的邀请
  365. public function getMyuser()
  366. {
  367. $userid = input('userid/d', 0);
  368. $brokerid = input('brokerid/d', 0);
  369. $user = UserModel::with(['userFollow'])->where('brokerid', '=', $brokerid)->findOrEmpty($userid);
  370. if ($user->isEmpty()) {
  371. page_result(1, "用户信息不存在");
  372. }
  373. page_result(0, "", [
  374. 'user' => $user,
  375. ]);
  376. }
  377. public function sendFollow()
  378. {
  379. $userid = input('userid/d', 0);
  380. $ftype = input('ftype/s', "");
  381. $remark = input('remark/s', "");
  382. if (empty($remark)) {
  383. page_result(1, "请输入跟进的具体内容。");
  384. }
  385. $followstatus = input('followstatus/d', 1);
  386. UserModel::update(['followstatus' => $followstatus], ['id' => $userid]);
  387. $data = [
  388. 'userid' => $userid,
  389. 'ftype' => $ftype,
  390. 'remark' => $remark,
  391. 'createtime' => time(),
  392. ];
  393. $follow = new UserFollowModel;
  394. $follow->save($data);
  395. $user = UserModel::with(['userFollow'])->findOrEmpty($userid);
  396. page_result(0, "", [
  397. 'user' => $user,
  398. ]);
  399. }
  400. public function listUser()
  401. {
  402. $ppage = input('ppage/d', 1);
  403. $psize = input('psize/d', 20);
  404. $map = [];
  405. $brokerid = input('brokerid/d', 0);
  406. $map[] = ['brokerid', '=', $brokerid];
  407. $followstatus = input('followstatus/d', 0);
  408. if (!empty($followstatus)) {
  409. $map[] = ['followstatus', '=', $followstatus];
  410. }
  411. $keyword = input('keyword', '');
  412. if (!empty($keyword)) {
  413. $map[] = ['nickname|mobile', 'like', "%{$keyword}%"];
  414. }
  415. $plist = UserModel::with(['userFollow'])->where($map)->order(['id' => 'desc'])->page($ppage)->limit($psize)->select();
  416. page_result(0, "", [
  417. 'plist' => $plist,
  418. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  419. ]);
  420. }
  421. /**
  422. * 获取收益列表
  423. */
  424. public function getIncome()
  425. {
  426. $ppage = input('ppage/d', 1);
  427. $psize = input('psize/d', 20);
  428. $brokerid = input('brokerid/d', 0);
  429. if ($brokerid != 0) {
  430. $map[] = ['brokerid', '=', $brokerid];
  431. }
  432. $plist = BrokerIncome::where($map)
  433. ->order(['id' => 'desc'])
  434. ->page($ppage)
  435. ->limit($psize)
  436. ->append(['status_text'])
  437. ->select();
  438. page_result(0, "", [
  439. 'plist' => $plist,
  440. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  441. ]);
  442. }
  443. public function brokerIncome()
  444. {
  445. $brokerid = input('brokerid/d', 0);
  446. $broker = BrokerModel::findOrEmpty($brokerid);
  447. if ($broker->isEmpty()) {
  448. page_result(1, "用户信息不存在。");
  449. }
  450. $month_time = date('Ym', strtotime('-1 month'));
  451. $month_income = BrokerIncome::where(['monthtime' => $month_time, 'brokerid' => $brokerid])->sum('value');
  452. page_result(0, "", [
  453. 'broker' => $broker,
  454. 'month_income' => $month_income,
  455. ]);
  456. }
  457. public function cash()
  458. {
  459. $brokerid = input('brokerid/d', 0);
  460. $value = input('value/d', 0);
  461. $broker = BrokerModel::findOrEmpty($brokerid);
  462. if ($broker->isEmpty()) {
  463. page_result(1, "用户信息不存在。");
  464. }
  465. $agent = AgentModel::findOrEmpty($broker['agentid']);
  466. if ($agent['is_settle'] == 1) {
  467. page_result(1, "请线下与门店结算。");
  468. }
  469. if ($value <= 0) {
  470. page_result(1, "提现金额必须大于0。");
  471. }
  472. if ($value > $broker['income']) {
  473. page_result(1, "收益不足。");
  474. }
  475. $service = new IncomeService();
  476. $service->add($brokerid, -$value, '经纪人发起提现', '', 2);
  477. page_result();
  478. }
  479. public function agent()
  480. {
  481. $agentid = input('agentid/d', 0);
  482. $agent = AgentModel::findOrEmpty($agentid);
  483. page_result(0, '', $agent);
  484. }
  485. public function center()
  486. {
  487. $brokerid = input('brokerid/d', 0);
  488. empty($brokerid) && page_result(1, "用户不存在。");
  489. $broker = BrokerModel::find($brokerid);
  490. empty($broker) && page_result(1, "用户不存在。");
  491. $user_ids = UserModel::where('brokerid', $brokerid)->column('id');
  492. $worker_count = WorkerModel::where('userid', 'in', $user_ids)->where('status', 5)->count();
  493. $statistics = [
  494. 'income_total' => $broker['income_total'],
  495. 'income' => $broker['income'],
  496. 'user' => count($user_ids),
  497. 'worker' => $worker_count,
  498. ];
  499. page_result(0, '', $statistics);
  500. }
  501. public function listWorker()
  502. {
  503. $ppage = input('ppage/d', 1);
  504. $psize = input('psize/d', 20);
  505. $map = [];
  506. $brokerid = input('brokerid/d', 0);
  507. $user_ids = UserModel::where('brokerid', $brokerid)->column('id');
  508. if (empty($user_ids)) {
  509. page_result(0, "", [
  510. 'plist' => [],
  511. 'pstatus' => 'noMore',
  512. ]);
  513. }
  514. $map[] = ['userid', 'in', $user_ids];
  515. $map[] = ['status', '=', 5];
  516. $keyword = input('keyword', '');
  517. if (!empty($keyword)) {
  518. $map[] = ['title|mobile', 'like', "%{$keyword}%"];
  519. }
  520. $plist = WorkerModel::where($map)->page($ppage)->limit($psize)->select();
  521. page_result(0, "", [
  522. 'plist' => $plist,
  523. 'pstatus' => $psize > count($plist) ? 'noMore' : 'more',
  524. ]);
  525. }
  526. public function apply()
  527. {
  528. $data = input('param.');
  529. $broker = BrokerModel::where('userid', $data['userid'])->find();
  530. if (!empty($broker)) {
  531. page_result(1, "您已经是经纪人了!", $data);
  532. }
  533. $check = BrokerFormModel::where('userid', $data['userid'])->find();
  534. if (!empty($check)) {
  535. page_result(1, "请勿重复申请", $data);
  536. }
  537. $data['province'] = '福建省';
  538. $data['city'] = '泉州市';
  539. $data['district'] = '晋江市';
  540. $data['createtime'] = time();
  541. BrokerFormModel::create($data);
  542. page_result();
  543. }
  544. }