Broker.php 21 KB

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