Broker.php 18 KB

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