WechatUser.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/28
  5. */
  6. namespace app\admin\model\wechat;
  7. use crmeb\basic\BaseModel;
  8. use crmeb\traits\ModelTrait;
  9. use think\facade\{Cache, Config};
  10. use crmeb\services\{WechatService, PHPExcelService};
  11. use app\admin\model\user\{User, UserBill, UserExtract};
  12. use app\admin\model\order\{StoreOrder, StoreOrderStatus};
  13. /**
  14. * 微信用户 model
  15. * Class WechatUser
  16. * @package app\admin\model\wechat
  17. */
  18. class WechatUser extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'uid';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'wechat_user';
  30. use ModelTrait;
  31. protected $insert = ['add_time'];
  32. /**
  33. * 用uid获得 微信openid
  34. * @param $uid
  35. * @return mixed
  36. * @throws \Exception
  37. */
  38. public static function uidToOpenid($uid)
  39. {
  40. $openid = self::where('uid', $uid)->value('openid');
  41. if (!$openid) exception('对应的openid不存在!');
  42. return $openid;
  43. }
  44. /**
  45. * 用uid获得 小程序 openid
  46. * @param $uid
  47. * @return mixed
  48. * @throws \Exception
  49. */
  50. public static function uidToRoutineOpenid($uid)
  51. {
  52. $openid = self::where('uid', $uid)->value('routine_openid');
  53. if (!$openid) exception('对应的routine_openid不存在!');
  54. return $openid;
  55. }
  56. public static function setAddTimeAttr($value)
  57. {
  58. return time();
  59. }
  60. /**
  61. * .添加新用户
  62. * @param $openid
  63. * @return object
  64. */
  65. public static function setNewUser($openid)
  66. {
  67. $userInfo = WechatService::getUserInfo($openid);
  68. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  69. return self::create($userInfo);
  70. }
  71. /**
  72. * 更新用户信息
  73. * @param $openid
  74. * @return bool
  75. */
  76. public static function updateUser($openid)
  77. {
  78. $userInfo = WechatService::getUserInfo($openid);
  79. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  80. return self::edit($userInfo, $openid, 'openid');
  81. }
  82. /**
  83. * 用户存在就更新 不存在就添加
  84. * @param $openid
  85. */
  86. public static function saveUser($openid)
  87. {
  88. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  89. }
  90. /**
  91. * 用户取消关注
  92. * @param $openid
  93. * @return bool
  94. */
  95. public static function unSubscribe($openid)
  96. {
  97. return self::edit(['subscribe' => 0], $openid, 'openid');
  98. }
  99. /**
  100. * 获取微信用户
  101. * @param array $where
  102. * @return array
  103. */
  104. public static function systemPage($where = [], $isall = false)
  105. {
  106. $model = new self;
  107. $model = $model->where('openid|routine_openid', 'NOT NULL');
  108. if ($where['nickname'] !== '') $model = $model->where('nickname', 'LIKE', "%$where[nickname]%");
  109. if (isset($where['data']) && $where['data'] !== '') $model = self::getModelTime($where, $model, 'add_time');
  110. if (isset($where['tagid_list']) && $where['tagid_list'] !== '') {
  111. $tagid_list = explode(',', $where['tagid_list']);
  112. foreach ($tagid_list as $v) {
  113. $model = $model->where('tagid_list', 'LIKE', "%$v%");
  114. }
  115. }
  116. if (isset($where['groupid']) && $where['groupid'] !== '-1') $model = $model->where('groupid', "$where[groupid]");
  117. if (isset($where['sex']) && $where['sex'] !== '') $model = $model->where('sex', "$where[sex]");
  118. if (isset($where['subscribe']) && $where['subscribe'] !== '') $model = $model->where('subscribe', "$where[subscribe]");
  119. $model = $model->order('uid desc');
  120. if (isset($where['export']) && $where['export'] == 1) {
  121. $list = $model->select()->toArray();
  122. $export = [];
  123. foreach ($list as $index => $item) {
  124. $export[] = [
  125. $item['nickname'],
  126. $item['sex'],
  127. $item['country'] . $item['province'] . $item['city'],
  128. $item['subscribe'] == 1 ? '关注' : '未关注',
  129. ];
  130. $list[$index] = $item;
  131. }
  132. PHPExcelService::setExcelHeader(['名称', '性别', '地区', '是否关注公众号'])
  133. ->setExcelTile('微信用户导出', '微信用户导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  134. ->setExcelContent($export)
  135. ->ExcelSave();
  136. }
  137. return self::page($model, function ($item) {
  138. $item['time'] = $item['add_time'] ? date('Y-m-d H:i', $item['add_time']) : '暂无';
  139. }, $where);
  140. }
  141. public static function setSpreadWhere($where = [], $alias = 'a', $model = null)
  142. {
  143. $model = is_null($model) ? new self() : $model;
  144. if ($alias) {
  145. $model = $model->alias($alias)->join('user u', 'a.uid=u.uid')->order('u.uid desc');
  146. $alias .= '.';
  147. }
  148. $status = (int)sys_config('store_brokerage_statu');
  149. if ($status == 1) {
  150. if ($Listuids = User::where(['is_promoter' => 1])->field('uid')->select()) {
  151. $newUids = [];
  152. foreach ($Listuids as $item) {
  153. $newUids[] = $item['uid'];
  154. }
  155. $uids = $newUids;
  156. unset($uid, $newUids);
  157. $model = $model->where($alias . 'uid', 'in', implode(',', $uids));
  158. } else
  159. $model = $model->where($alias . 'uid', -1);
  160. }
  161. if ($where['nickname'] !== '') $model = $model->where("{$alias}nickname|{$alias}uid|u.phone", 'LIKE', "%$where[nickname]%");
  162. if ((isset($where['start_time']) && isset($where['end_time'])) && $where['start_time'] !== '' && $where['end_time'] !== '') {
  163. $model = $model->where("{$alias}add_time", 'between', [strtotime($where['start_time']), strtotime($where['end_time'])]);
  164. }
  165. if (isset($where['sex']) && $where['sex'] !== '') $model = $model->where($alias . 'sex', $where['sex']);
  166. if (isset($where['subscribe']) && $where['subscribe'] !== '') $model = $model->where($alias . 'subscribe', $where['subscribe']);
  167. if (isset($where['order']) && $where['order'] != '') $model = $model->order($where['order']);
  168. if (isset($where['user_type']) && $where['user_type'] != '') {
  169. if ($where['user_type'] == 1) {
  170. $model = $model->where($alias . 'unionid', 'neq', 'NULL');
  171. } else if ($where['user_type'] == 2)
  172. $model = $model->where($alias . 'openid', 'neq', 'NULL')->where($alias . 'unionid', 'NULL');
  173. else if ($where['user_type'] == 3)
  174. $model = $model->where($alias . 'routine_openid', 'neq', 'NULL')->where($alias . 'unionid', 'NULL');
  175. }
  176. if (isset($where['is_time']) && isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, $alias . 'add_time');
  177. return $model;
  178. }
  179. /**
  180. * 获取分销用户
  181. * @param array $where
  182. * @return array
  183. */
  184. public static function agentSystemPage($where = [])
  185. {
  186. //提现数据
  187. $exteactSql = UserExtract::where(['status' => 1])
  188. ->group('uid')
  189. ->field(['sum(extract_price) as extract_count_price', 'count(id) as extract_count_num', 'uid as euid'])
  190. ->fetchSql(true)
  191. ->select();
  192. //订单数据
  193. $orderSql = StoreOrder::alias('o')
  194. ->where(['o.paid' => 1, 'o.refund_status' => 0])
  195. ->field(['sum(o.pay_price) as order_price', 'count(o.id) as order_count', 'o.uid as ouid'])
  196. ->group('o.uid')
  197. ->fetchSql(true)
  198. ->select();
  199. //佣金数据
  200. $billSql = UserBill::where(['status' => 1])
  201. ->where('type', 'brokerage')
  202. ->where('pm', 1)
  203. ->group('uid')
  204. ->field(['sum(number) as brokerage_money', 'uid as buid'])
  205. ->fetchSql(true)
  206. ->select();
  207. $model = User::where(['status' => 1])->where('is_promoter', 1);
  208. $model = $model->alias('u')
  209. ->join('(' . $orderSql . ') o', 'o.ouid = u.uid', 'left')
  210. ->join('(' . $billSql . ') b', 'b.buid = u.uid', 'left')
  211. ->join('(' . $exteactSql . ') e', 'e.euid = u.uid', 'left');
  212. if ($where['nickname'] !== '') {
  213. $model = $model->where("u.nickname|u.uid|u.phone", 'LIKE', "%$where[nickname]%");
  214. }
  215. if ($where['data']) $model = self::getModelTime($where, $model, 'u.add_time');
  216. $count = $model->count();
  217. $data = $model->field(['avatar as headimgurl', 'brokerage_price as new_money', 'u.*', 'o.*', 'e.*', 'b.*'])
  218. ->page((int)$where['page'], (int)$where['limit'])
  219. ->order('u.uid desc')
  220. ->select();
  221. $data = count($data) ? $data->toArray() : [];
  222. $export = [];
  223. $broken_time = intval(sys_config('extract_time'));
  224. $search_time = time() - 86400 * $broken_time;
  225. foreach ($data as &$item) {
  226. if ($spread_uid = User::where('uid', $item['uid'])->value('spread_uid')) {
  227. if ($user = User::where('uid', $spread_uid)->field(['uid', 'nickname'])->find()) {
  228. $item['spread_name'] = $user['nickname'] . '/' . $user['uid'];
  229. }
  230. }
  231. $item['spread_count'] = User::where('spread_uid', $item['uid'])->count();
  232. //返佣 +
  233. $brokerage_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  234. ->where('add_time', '>', $search_time)
  235. ->where('pm', 1)
  236. ->sum('number');
  237. //退款退的佣金 -
  238. $refund_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  239. ->where('add_time', '>', $search_time)
  240. ->where('pm', 0)
  241. ->sum('number');
  242. $item['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  243. if ($item['broken_commission'] < 0)
  244. $item['broken_commission'] = 0;
  245. if ($item['brokerage_money'] > $refund_commission)
  246. $item['brokerage_money'] = bcsub($item['brokerage_money'], $refund_commission, 2);
  247. else
  248. $item['brokerage_money'] = 0;
  249. //导出数据
  250. $export[] = [
  251. $item['uid'],
  252. $item['nickname'],
  253. $item['phone'],
  254. $item['spread_count'],
  255. $item['order_count'],
  256. $item['order_price'],
  257. $item['brokerage_money'],
  258. $item['extract_count_price'],
  259. $item['extract_count_num'],
  260. $item['new_money'],
  261. $item['spread_name'] ?? '',
  262. ];
  263. }
  264. if (isset($where['excel']) && $where['excel'] == 1) {
  265. PHPExcelService::setExcelHeader(['用户编号', '昵称', '电话号码', '推广用户数量', '订单数量', '推广订单金额', '佣金金额', '已提现金额', '提现次数', '未提现金额', '上级推广人'])
  266. ->setExcelTile('推广用户', '推广用户导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  267. ->setExcelContent($export)
  268. ->ExcelSave();
  269. }
  270. return compact('data', 'count');
  271. }
  272. public static function setSairOrderWhere($where, $model = null, $alias = '')
  273. {
  274. $model = $model === null ? new self() : $model;
  275. if (!isset($where['uid'])) return $model;
  276. if ($alias) {
  277. $model = $model->alias($alias);
  278. $alias .= '.';
  279. }
  280. if (isset($where['type'])) {
  281. switch ((int)$where['type']) {
  282. case 1:
  283. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  284. if (count($uids))
  285. $model = $model->where("{$alias}uid", 'in', $uids);
  286. else
  287. $model = $model->where("{$alias}uid", 0);
  288. break;
  289. case 2:
  290. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  291. if (count($uids))
  292. $spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid');
  293. else
  294. $spread_uid_two = [0];
  295. if (count($spread_uid_two))
  296. $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
  297. else
  298. $model = $model->where("{$alias}uid", 0);
  299. break;
  300. default:
  301. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  302. if (count($uids)) {
  303. if ($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')) {
  304. $uids = array_merge($uids, $spread_uid_two);
  305. $uids = array_unique($uids);
  306. $uids = array_merge($uids);
  307. }
  308. $model = $model->where("{$alias}uid", 'in', $uids);
  309. } else
  310. $model = $model->where("{$alias}uid", 0);
  311. break;
  312. }
  313. }
  314. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  315. return $model->where("{$alias}is_del", 0)->where("{$alias}is_system_del", 0)->where($alias . 'paid', 1);
  316. }
  317. /*
  318. * 推广订单统计
  319. * @param array $where
  320. * @return array
  321. * */
  322. public static function getStairOrderBadge($where)
  323. {
  324. if (!isset($where['uid'])) return [];
  325. $data['order_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  326. $data['order_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  327. $ids = self::setSairOrderWhere($where, new StoreOrder())
  328. ->where(['paid' => 1, 'is_del' => 0, 'refund_status' => 0])
  329. ->where('status', '>', 1)
  330. ->column('id');
  331. $data['number_price'] = 0;
  332. if (count($ids)) $data['number_price'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'uid' => $where['uid']])->where('link_id', 'in', $ids)->sum('number');
  333. $where['type'] = 1;
  334. $data['one_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  335. $data['one_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  336. $where['type'] = 2;
  337. $data['two_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  338. $data['two_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  339. return [
  340. [
  341. 'name' => '总金额',
  342. 'field' => '元',
  343. 'count' => $data['order_price'],
  344. 'background_color' => 'layui-bg-cyan',
  345. 'col' => 3,
  346. ],
  347. [
  348. 'name' => '订单总数',
  349. 'field' => '单',
  350. 'count' => $data['order_count'],
  351. 'background_color' => 'layui-bg-cyan',
  352. 'col' => 3,
  353. ],
  354. [
  355. 'name' => '返佣总金额',
  356. 'field' => '元',
  357. 'count' => $data['number_price'],
  358. 'background_color' => 'layui-bg-cyan',
  359. 'col' => 3,
  360. ],
  361. [
  362. 'name' => '一级总金额',
  363. 'field' => '元',
  364. 'count' => $data['one_price'],
  365. 'background_color' => 'layui-bg-cyan',
  366. 'col' => 3,
  367. ],
  368. [
  369. 'name' => '一级订单数',
  370. 'field' => '单',
  371. 'count' => $data['one_count'],
  372. 'background_color' => 'layui-bg-cyan',
  373. 'col' => 3,
  374. ],
  375. [
  376. 'name' => '二级总金额',
  377. 'field' => '元',
  378. 'count' => $data['two_price'],
  379. 'background_color' => 'layui-bg-cyan',
  380. 'col' => 3,
  381. ],
  382. [
  383. 'name' => '二级订单数',
  384. 'field' => '单',
  385. 'count' => $data['two_count'],
  386. 'background_color' => 'layui-bg-cyan',
  387. 'col' => 3,
  388. ],
  389. ];
  390. }
  391. /*
  392. * 设置查询条件
  393. * @param array $where
  394. * @param object $model
  395. * @param string $alias
  396. * */
  397. public static function setSairWhere($where, $model = null, $alias = '')
  398. {
  399. $model = $model === null ? new self() : $model;
  400. if (!isset($where['uid'])) return $model;
  401. if ($alias) {
  402. $model = $model->alias($alias);
  403. $alias .= '.';
  404. }
  405. if (isset($where['type'])) {
  406. switch ((int)$where['type']) {
  407. case 1:
  408. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  409. if (count($uids))
  410. $model = $model->where("{$alias}uid", 'in', $uids);
  411. else
  412. $model = $model->where("{$alias}uid", 0);
  413. break;
  414. case 2:
  415. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  416. if (count($uids))
  417. $spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid');
  418. else
  419. $spread_uid_two = [0];
  420. if (count($spread_uid_two))
  421. $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
  422. else
  423. $model = $model->where("{$alias}uid", 0);
  424. break;
  425. default:
  426. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  427. if (count($uids)) {
  428. if ($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')) {
  429. $uids = array_merge($uids, $spread_uid_two);
  430. $uids = array_unique($uids);
  431. $uids = array_merge($uids);
  432. }
  433. $model = $model->where("{$alias}uid", 'in', $uids);
  434. } else
  435. $model = $model->where("{$alias}uid", 0);
  436. break;
  437. }
  438. }
  439. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  440. if (isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}phone|{$alias}nickname|{$alias}real_name|{$alias}uid", 'LIKE', "%$where[nickname]%");
  441. return $model->where($alias . 'status', 1);
  442. }
  443. public static function getSairBadge($where)
  444. {
  445. $data['number'] = self::setSairWhere($where, new User())->count();
  446. $where['type'] = 1;
  447. $data['one_number'] = self::setSairWhere($where, new User())->count();
  448. $where['type'] = 2;
  449. $data['two_number'] = self::setSairWhere($where, new User())->count();
  450. $col = $data['two_number'] > 0 ? 4 : 6;
  451. return [
  452. [
  453. 'name' => '总人数',
  454. 'field' => '人',
  455. 'count' => $data['number'],
  456. 'background_color' => 'layui-bg-cyan',
  457. 'col' => $col,
  458. ],
  459. [
  460. 'name' => '一级人数',
  461. 'field' => '人',
  462. 'count' => $data['one_number'],
  463. 'background_color' => 'layui-bg-cyan',
  464. 'col' => $col,
  465. ],
  466. [
  467. 'name' => '二级人数',
  468. 'field' => '人',
  469. 'count' => $data['two_number'],
  470. 'background_color' => 'layui-bg-cyan',
  471. 'col' => $col,
  472. ],
  473. ];
  474. }
  475. public static function getStairList($where)
  476. {
  477. if (!isset($where['uid'])) return [];
  478. $data = self::setSairWhere($where, new User())->order('add_time desc')->page((int)$where['page'], (int)$where['limit'])->select();
  479. $data = count($data) ? $data->toArray() : [];
  480. foreach ($data as &$item) {
  481. $item['spread_count'] = User::where('spread_uid', $item['uid'])->count();
  482. $item['order_count'] = StoreOrder::where('uid', $item['uid'])->where(['paid' => 1, 'is_del' => 0])->count();
  483. $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
  484. $item['add_time'] = $item['add_time'] ? date("Y-m-d H:i:s", $item['add_time']) : '';
  485. }
  486. $count = self::setSairWhere($where, new User())->count();
  487. return compact('data', 'count');
  488. }
  489. /*
  490. * 推广订单
  491. * @param array $where
  492. * @return array
  493. * */
  494. public static function getStairOrderList($where)
  495. {
  496. if (!isset($where['uid'])) return [];
  497. $data = self::setSairOrderWhere($where, new StoreOrder())
  498. ->page((int)$where['page'], (int)$where['limit'])
  499. ->order('add_time desc')
  500. ->select();
  501. $data = count($data) ? $data->toArray() : [];
  502. $Info = User::where('uid', $where['uid'])->find();
  503. foreach ($data as &$item) {
  504. $userInfo = User::where('uid', $item['uid'])->find();
  505. $item['user_info'] = '';
  506. $item['avatar'] = '';
  507. if ($userInfo) {
  508. $item['user_info'] = $userInfo->nickname . '|' . ($userInfo->phone ? $userInfo->phone . '|' : '') . $userInfo->real_name;
  509. $item['avatar'] = $userInfo->avatar;
  510. }
  511. $item['spread_info'] = $Info->nickname . "|" . ($Info->phone ? $Info->phone . "|" : '') . $Info->uid;
  512. $item['number_price'] = UserBill::where(['uid'=>$Info['uid'], 'category' => 'now_money', 'type' => 'brokerage', 'link_id' => $item['id']])->value('number');
  513. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  514. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  515. $item['take_time'] = ($change_time = StoreOrderStatus::where(['change_type' => 'user_take_delivery', 'oid' => $item['id']])->value('change_time')) ?
  516. date('Y-m-d H:i:s', $change_time) : '暂无';
  517. }
  518. $count = self::setSairOrderWhere($where, new StoreOrder())->count();
  519. return compact('data', 'count');
  520. }
  521. /*
  522. * 获取分销员列表头部统计
  523. * */
  524. public static function getSpreadBadge($where)
  525. {
  526. $where['is_time'] = 1;
  527. $listuids = self::setSpreadWhere($where)->field('u.uid')->select();
  528. $newUids = [];
  529. foreach ($listuids as $item) {
  530. $newUids[] = $item['uid'];
  531. }
  532. $uids = $newUids;
  533. unset($uid, $newUids);
  534. //分销员人数
  535. $data['sum_count'] = count($uids);
  536. $data['spread_sum'] = 0;
  537. $data['order_count'] = 0;
  538. $data['pay_price'] = 0;
  539. $data['number'] = 0;
  540. $data['extract_count'] = 0;
  541. $data['extract_price'] = 0;
  542. if ($data['sum_count']) {
  543. //发展会员人数
  544. $data['spread_sum'] = User::where('spread_uid', 'in', $uids)->count();
  545. //订单总数
  546. $data['order_count'] = StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0])->count();
  547. //订单金额
  548. $data['pay_price'] = StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0])->sum('pay_price');
  549. //可提现金额
  550. $data['number'] = User::where('uid', 'in', $uids)->sum('brokerage_price');
  551. //提现次数
  552. $data['extract_count'] = UserExtract::where('uid', 'in', $uids)->count();
  553. //获取某个用户可提现金额
  554. $data['extract_price'] = User::getextractPrice($uids, $where);
  555. }
  556. return [
  557. [
  558. 'name' => '分销员人数',
  559. 'field' => '人',
  560. 'count' => $data['sum_count'],
  561. 'background_color' => 'layui-bg-cyan',
  562. 'col' => 2,
  563. ],
  564. [
  565. 'name' => '发展会员人数',
  566. 'field' => '人',
  567. 'count' => $data['spread_sum'],
  568. 'background_color' => 'layui-bg-cyan',
  569. 'col' => 2,
  570. ],
  571. [
  572. 'name' => '分销订单数',
  573. 'field' => '单',
  574. 'count' => $data['order_count'],
  575. 'background_color' => 'layui-bg-cyan',
  576. 'col' => 2,
  577. ],
  578. [
  579. 'name' => '订单金额',
  580. 'field' => '元',
  581. 'count' => $data['pay_price'],
  582. 'background_color' => 'layui-bg-cyan',
  583. 'col' => 2,
  584. ],
  585. [
  586. 'name' => '提现金额',
  587. 'field' => '元',
  588. 'count' => $data['number'],
  589. 'background_color' => 'layui-bg-cyan',
  590. 'col' => 2,
  591. ],
  592. [
  593. 'name' => '提现次数',
  594. 'field' => '次',
  595. 'count' => $data['extract_count'],
  596. 'background_color' => 'layui-bg-cyan',
  597. 'col' => 2,
  598. ],
  599. [
  600. 'name' => '未提现金额',
  601. 'field' => '元',
  602. 'count' => $data['number'],
  603. 'background_color' => 'layui-bg-cyan',
  604. 'col' => 2,
  605. ],
  606. ];
  607. }
  608. /**
  609. * 获取筛选后的所有用户uid
  610. * @param array $where
  611. * @return array
  612. */
  613. public static function getAll($where = [])
  614. {
  615. $model = new self;
  616. if ($where['nickname'] !== '') $model = $model->where('nickname', 'LIKE', "%$where[nickname]%");
  617. if ($where['data'] !== '') $model = self::getModelTime($where, $model, 'add_time');
  618. if ($where['tagid_list'] !== '') {
  619. $model = $model->where('tagid_list', 'LIKE', "%$where[tagid_list]%");
  620. }
  621. if ($where['groupid'] !== '-1') $model = $model->where('groupid', "$where[groupid]");
  622. if ($where['sex'] !== '') $model = $model->where('sex', "$where[sex]");
  623. return $model->column('uid', 'uid');
  624. }
  625. /**
  626. * 获取已关注的用户
  627. * @param $field
  628. */
  629. public static function getSubscribe($field)
  630. {
  631. return self::where('subscribe', 1)->column($field);
  632. }
  633. public static function getUserAll($field)
  634. {
  635. return self::column($field);
  636. }
  637. public static function getUserTag()
  638. {
  639. $tagName = Config::get('system_wechat_tag');
  640. return Cache::tag($tagName)->remember('_wechat_tag', function () use ($tagName) {
  641. Cache::tag($tagName, ['_wechat_tag']);
  642. $tag = WechatService::userTagService()->lists()->toArray()['tags'] ?: [];
  643. $list = [];
  644. foreach ($tag as $g) {
  645. $list[$g['id']] = $g;
  646. }
  647. return $list;
  648. });
  649. }
  650. public static function clearUserTag()
  651. {
  652. Cache::delete('_wechat_tag');
  653. }
  654. public static function getUserGroup()
  655. {
  656. $tagName = Config::get('system_wechat_tag');
  657. return Cache::tag($tagName)->remember('_wechat_group', function () use ($tagName) {
  658. Cache::tag($tagName, ['_wechat_group']);
  659. $tag = WechatService::userGroupService()->lists()->toArray()['groups'] ?: [];
  660. $list = [];
  661. foreach ($tag as $g) {
  662. $list[$g['id']] = $g;
  663. }
  664. return $list;
  665. });
  666. }
  667. public static function clearUserGroup()
  668. {
  669. Cache::delete('_wechat_group');
  670. }
  671. /**
  672. * 获取推广人数
  673. * @param $uid //用户的uid
  674. * @param int $spread
  675. * $spread 0 一级推广人数 1 二级推广人数
  676. * @return int|string
  677. */
  678. public static function getUserSpreadUidCount($uid, $spread = 1)
  679. {
  680. $userStair = User::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人
  681. if ($userStair) {
  682. if (!$spread) return count($userStair);//返回一级推人人数
  683. else return User::where('spread_uid', 'IN', implode(',', $userStair))->count();//二级推荐人数
  684. } else return 0;
  685. }
  686. /**
  687. * 获取推广人的订单
  688. * @param $uid
  689. * @param int $spread
  690. * $spread 0 一级推广总订单 1 所有推广总订单
  691. * @return int|string
  692. */
  693. public static function getUserSpreadOrderCount($uid, $spread = 1)
  694. {
  695. $userStair = User::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人uid
  696. if ($userStair) {
  697. if (!$spread) {
  698. return StoreOrder::where('uid', 'IN', implode(',', $userStair))->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();//获取一级推广人订单数
  699. } else {
  700. $userSecond = User::where('spread_uid', 'IN', implode(',', $userStair))->column('uid', 'uid');//二级推广人的uid
  701. if ($userSecond) {
  702. return StoreOrder::where('uid', 'IN', implode(',', $userSecond))->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();//获取二级推广人订单数
  703. } else return 0;
  704. }
  705. } else return 0;
  706. }
  707. }