Broker.php 17 KB

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