WechatUser.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. $exteactSql = UserExtract::where(['status' => 1])
  187. ->group('uid')
  188. ->field(['sum(extract_price) as extract_count_price', 'count(id) as extract_count_num', 'uid as euid'])
  189. ->fetchSql(true)
  190. ->select();
  191. $orderSql = StoreOrder::alias('o')
  192. ->join('user u', '(u.spread_uid = o.uid) || (u.uid = o.uid)')
  193. ->where(['paid' => 1, 'refund_status' => 0])
  194. ->group('o.uid')
  195. ->field(['sum(pay_price) as order_price', 'count(id) as order_count', 'o.uid as ouid'])
  196. ->fetchSql(true)
  197. ->select();
  198. $billSql = UserBill::where(['status' => 1])
  199. ->where('type', 'brokerage')
  200. ->group('uid')
  201. ->field(['sum(number) as brokerage_money', 'uid as buid'])
  202. ->fetchSql(true)
  203. ->select();
  204. $model = User::where(['status' => 1]);
  205. $model = $model->alias('u')
  206. ->join('(' . $orderSql . ') o', 'o.ouid = u.uid', 'left')
  207. ->join('(' . $billSql . ') b', 'b.buid = u.uid', 'left')
  208. ->join('(' . $exteactSql . ') e', 'e.euid = u.uid', 'left');
  209. if ($where['nickname'] !== '') {
  210. $model = $model->where("u.nickname|u.uid|u.phone", 'LIKE', "%$where[nickname]%");
  211. }
  212. if ($where['data']) $model = self::getModelTime($where, $model, 'u.add_time');
  213. $count = $model->count();
  214. $data = $model->field(['avatar as headimgurl', 'brokerage_price as new_money', 'u.*', 'o.*', 'e.*', 'b.*'])
  215. ->page((int)$where['page'], (int)$where['limit'])
  216. ->order('u.uid desc')
  217. ->select();
  218. $data = count($data) ? $data->toArray() : [];
  219. $export = [];
  220. $broken_time = intval(sys_config('extract_time'));
  221. $search_time = time() - 86400 * $broken_time;
  222. foreach ($data as &$item) {
  223. if ($spread_uid = User::where('uid', $item['uid'])->value('spread_uid')) {
  224. if ($user = User::where('uid', $spread_uid)->field(['uid', 'nickname'])->find()) {
  225. $item['spread_name'] = $user['nickname'] . '/' . $user['uid'];
  226. }
  227. }
  228. $item['spread_count'] = User::where('spread_uid',$item['uid'])->count();
  229. //返佣 +
  230. $brokerage_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  231. ->where('add_time', '>', $search_time)
  232. ->where('pm', 1)
  233. ->sum('number');
  234. //退款退的佣金 -
  235. $refund_commission = UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage'])
  236. ->where('add_time', '>', $search_time)
  237. ->where('pm', 0)
  238. ->sum('number');
  239. $item['broken_commission'] = bcsub($brokerage_commission, $refund_commission, 2);
  240. if ($item['broken_commission'] < 0)
  241. $item['broken_commission'] = 0;
  242. //导出数据
  243. $export[] = [
  244. $item['uid'],
  245. $item['nickname'],
  246. $item['phone'],
  247. $item['spread_count'],
  248. $item['order_count'],
  249. $item['order_price'],
  250. $item['brokerage_money'],
  251. $item['extract_count_price'],
  252. $item['extract_count_num'],
  253. $item['new_money'],
  254. $item['spread_name'] ?? '',
  255. ];
  256. }
  257. if (isset($where['excel']) && $where['excel'] == 1) {
  258. PHPExcelService::setExcelHeader(['用户编号', '昵称', '电话号码', '推广用户数量', '订单数量', '推广订单金额', '佣金金额', '已提现金额', '提现次数', '未提现金额', '上级推广人'])
  259. ->setExcelTile('推广用户', '推广用户导出' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  260. ->setExcelContent($export)
  261. ->ExcelSave();
  262. }
  263. return compact('data', 'count');
  264. }
  265. public static function setSairOrderWhere($where, $model = null, $alias = '')
  266. {
  267. $model = $model === null ? new self() : $model;
  268. if (!isset($where['uid'])) return $model;
  269. if ($alias) {
  270. $model = $model->alias($alias);
  271. $alias .= '.';
  272. }
  273. if (isset($where['type'])) {
  274. switch ((int)$where['type']) {
  275. case 1:
  276. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  277. if (count($uids))
  278. $model = $model->where("{$alias}uid", 'in', $uids);
  279. else
  280. $model = $model->where("{$alias}uid", 0);
  281. break;
  282. case 2:
  283. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  284. if (count($uids))
  285. $spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid');
  286. else
  287. $spread_uid_two = [0];
  288. if (count($spread_uid_two))
  289. $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
  290. else
  291. $model = $model->where("{$alias}uid", 0);
  292. break;
  293. default:
  294. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  295. if (count($uids)) {
  296. if ($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')) {
  297. $uids = array_merge($uids, $spread_uid_two);
  298. $uids = array_unique($uids);
  299. $uids = array_merge($uids);
  300. }
  301. $model = $model->where("{$alias}uid", 'in', $uids);
  302. } else
  303. $model = $model->where("{$alias}uid", 0);
  304. break;
  305. }
  306. }
  307. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  308. return $model->where("{$alias}is_del", 0)->where("{$alias}is_system_del", 0)->where($alias . 'paid', 1);
  309. }
  310. /*
  311. * 推广订单统计
  312. * @param array $where
  313. * @return array
  314. * */
  315. public static function getStairOrderBadge($where)
  316. {
  317. if (!isset($where['uid'])) return [];
  318. $data['order_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  319. $data['order_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  320. $ids = self::setSairOrderWhere($where, new StoreOrder())
  321. ->where(['paid' => 1, 'is_del' => 0, 'refund_status' => 0])
  322. ->where('status', '>', 1)
  323. ->column('id');
  324. $data['number_price'] = 0;
  325. if (count($ids)) $data['number_price'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'uid' => $where['uid']])->where('link_id', 'in', $ids)->sum('number');
  326. $where['type'] = 1;
  327. $data['one_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  328. $data['one_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  329. $where['type'] = 2;
  330. $data['two_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
  331. $data['two_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
  332. return [
  333. [
  334. 'name' => '总金额',
  335. 'field' => '元',
  336. 'count' => $data['order_price'],
  337. 'background_color' => 'layui-bg-cyan',
  338. 'col' => 3,
  339. ],
  340. [
  341. 'name' => '订单总数',
  342. 'field' => '单',
  343. 'count' => $data['order_count'],
  344. 'background_color' => 'layui-bg-cyan',
  345. 'col' => 3,
  346. ],
  347. [
  348. 'name' => '返佣总金额',
  349. 'field' => '元',
  350. 'count' => $data['number_price'],
  351. 'background_color' => 'layui-bg-cyan',
  352. 'col' => 3,
  353. ],
  354. [
  355. 'name' => '一级总金额',
  356. 'field' => '元',
  357. 'count' => $data['one_price'],
  358. 'background_color' => 'layui-bg-cyan',
  359. 'col' => 3,
  360. ],
  361. [
  362. 'name' => '一级订单数',
  363. 'field' => '单',
  364. 'count' => $data['one_count'],
  365. 'background_color' => 'layui-bg-cyan',
  366. 'col' => 3,
  367. ],
  368. [
  369. 'name' => '二级总金额',
  370. 'field' => '元',
  371. 'count' => $data['two_price'],
  372. 'background_color' => 'layui-bg-cyan',
  373. 'col' => 3,
  374. ],
  375. [
  376. 'name' => '二级订单数',
  377. 'field' => '单',
  378. 'count' => $data['two_count'],
  379. 'background_color' => 'layui-bg-cyan',
  380. 'col' => 3,
  381. ],
  382. ];
  383. }
  384. /*
  385. * 设置查询条件
  386. * @param array $where
  387. * @param object $model
  388. * @param string $alias
  389. * */
  390. public static function setSairWhere($where, $model = null, $alias = '')
  391. {
  392. $model = $model === null ? new self() : $model;
  393. if (!isset($where['uid'])) return $model;
  394. if ($alias) {
  395. $model = $model->alias($alias);
  396. $alias .= '.';
  397. }
  398. if (isset($where['type'])) {
  399. switch ((int)$where['type']) {
  400. case 1:
  401. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  402. if (count($uids))
  403. $model = $model->where("{$alias}uid", 'in', $uids);
  404. else
  405. $model = $model->where("{$alias}uid", 0);
  406. break;
  407. case 2:
  408. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  409. if (count($uids))
  410. $spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid');
  411. else
  412. $spread_uid_two = [0];
  413. if (count($spread_uid_two))
  414. $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
  415. else
  416. $model = $model->where("{$alias}uid", 0);
  417. break;
  418. default:
  419. $uids = User::where('spread_uid', $where['uid'])->column('uid');
  420. if (count($uids)) {
  421. if ($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')) {
  422. $uids = array_merge($uids, $spread_uid_two);
  423. $uids = array_unique($uids);
  424. $uids = array_merge($uids);
  425. }
  426. $model = $model->where("{$alias}uid", 'in', $uids);
  427. } else
  428. $model = $model->where("{$alias}uid", 0);
  429. break;
  430. }
  431. }
  432. if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
  433. if (isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}phone|{$alias}nickname|{$alias}real_name|{$alias}uid", 'LIKE', "%$where[nickname]%");
  434. return $model->where($alias . 'status', 1);
  435. }
  436. public static function getSairBadge($where)
  437. {
  438. $data['number'] = self::setSairWhere($where, new User())->count();
  439. $where['type'] = 1;
  440. $data['one_number'] = self::setSairWhere($where, new User())->count();
  441. $where['type'] = 2;
  442. $data['two_number'] = self::setSairWhere($where, new User())->count();
  443. $col = $data['two_number'] > 0 ? 4 : 6;
  444. return [
  445. [
  446. 'name' => '总人数',
  447. 'field' => '人',
  448. 'count' => $data['number'],
  449. 'background_color' => 'layui-bg-cyan',
  450. 'col' => $col,
  451. ],
  452. [
  453. 'name' => '一级人数',
  454. 'field' => '人',
  455. 'count' => $data['one_number'],
  456. 'background_color' => 'layui-bg-cyan',
  457. 'col' => $col,
  458. ],
  459. [
  460. 'name' => '二级人数',
  461. 'field' => '人',
  462. 'count' => $data['two_number'],
  463. 'background_color' => 'layui-bg-cyan',
  464. 'col' => $col,
  465. ],
  466. ];
  467. }
  468. public static function getStairList($where)
  469. {
  470. if (!isset($where['uid'])) return [];
  471. $data = self::setSairWhere($where, new User())->order('add_time desc')->page((int)$where['page'], (int)$where['limit'])->select();
  472. $data = count($data) ? $data->toArray() : [];
  473. foreach ($data as &$item) {
  474. $item['spread_count'] = User::where('spread_uid', $item['uid'])->count();
  475. $item['order_count'] = StoreOrder::where('uid', $item['uid'])->where(['paid' => 1, 'is_del' => 0])->count();
  476. $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
  477. $item['add_time'] = $item['add_time'] ? date("Y-m-d H:i:s", $item['add_time']) : '';
  478. }
  479. $count = self::setSairWhere($where, new User())->count();
  480. return compact('data', 'count');
  481. }
  482. /*
  483. * 推广订单
  484. * @param array $where
  485. * @return array
  486. * */
  487. public static function getStairOrderList($where)
  488. {
  489. if (!isset($where['uid'])) return [];
  490. $data = self::setSairOrderWhere($where, new StoreOrder())
  491. ->page((int)$where['page'], (int)$where['limit'])
  492. ->order('add_time desc')
  493. ->select();
  494. $data = count($data) ? $data->toArray() : [];
  495. $Info = User::where('uid', $where['uid'])->find();
  496. foreach ($data as &$item) {
  497. $userInfo = User::where('uid', $item['uid'])->find();
  498. $item['user_info'] = '';
  499. $item['avatar'] = '';
  500. if ($userInfo) {
  501. $item['user_info'] = $userInfo->nickname . '|' . ($userInfo->phone ? $userInfo->phone . '|' : '') . $userInfo->real_name;
  502. $item['avatar'] = $userInfo->avatar;
  503. }
  504. $item['spread_info'] = $Info->nickname . "|" . ($Info->phone ? $Info->phone . "|" : '') . $Info->uid;
  505. $item['number_price'] = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'link_id' => $item['id']])->value('number');
  506. $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
  507. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  508. $item['take_time'] = ($change_time = StoreOrderStatus::where(['change_type' => 'user_take_delivery', 'oid' => $item['id']])->value('change_time')) ?
  509. date('Y-m-d H:i:s', $change_time) : '暂无';
  510. }
  511. $count = self::setSairOrderWhere($where, new StoreOrder())->count();
  512. return compact('data', 'count');
  513. }
  514. /*
  515. * 获取分销员列表头部统计
  516. * */
  517. public static function getSpreadBadge($where)
  518. {
  519. $where['is_time'] = 1;
  520. $listuids = self::setSpreadWhere($where)->field('u.uid')->select();
  521. $newUids = [];
  522. foreach ($listuids as $item) {
  523. $newUids[] = $item['uid'];
  524. }
  525. $uids = $newUids;
  526. unset($uid, $newUids);
  527. //分销员人数
  528. $data['sum_count'] = count($uids);
  529. $data['spread_sum'] = 0;
  530. $data['order_count'] = 0;
  531. $data['pay_price'] = 0;
  532. $data['number'] = 0;
  533. $data['extract_count'] = 0;
  534. $data['extract_price'] = 0;
  535. if ($data['sum_count']) {
  536. //发展会员人数
  537. $data['spread_sum'] = User::where('spread_uid', 'in', $uids)->count();
  538. //订单总数
  539. $data['order_count'] = StoreOrder::where('uid', 'in', $uids)->count();
  540. //订单金额
  541. $data['pay_price'] = StoreOrder::where('uid', 'in', $uids)->sum('pay_price');
  542. //可提现金额
  543. $data['number'] = User::where('uid', 'in', $uids)->sum('brokerage_price');
  544. //提现次数
  545. $data['extract_count'] = UserExtract::where('uid', 'in', $uids)->count();
  546. //获取某个用户可提现金额
  547. $data['extract_price'] = User::getextractPrice($uids, $where);
  548. }
  549. return [
  550. [
  551. 'name' => '分销员人数',
  552. 'field' => '人',
  553. 'count' => $data['sum_count'],
  554. 'background_color' => 'layui-bg-cyan',
  555. 'col' => 2,
  556. ],
  557. [
  558. 'name' => '发展会员人数',
  559. 'field' => '人',
  560. 'count' => $data['spread_sum'],
  561. 'background_color' => 'layui-bg-cyan',
  562. 'col' => 2,
  563. ],
  564. [
  565. 'name' => '分销订单数',
  566. 'field' => '单',
  567. 'count' => $data['order_count'],
  568. 'background_color' => 'layui-bg-cyan',
  569. 'col' => 2,
  570. ],
  571. [
  572. 'name' => '订单金额',
  573. 'field' => '元',
  574. 'count' => $data['pay_price'],
  575. 'background_color' => 'layui-bg-cyan',
  576. 'col' => 2,
  577. ],
  578. [
  579. 'name' => '提现金额',
  580. 'field' => '元',
  581. 'count' => $data['number'],
  582. 'background_color' => 'layui-bg-cyan',
  583. 'col' => 2,
  584. ],
  585. [
  586. 'name' => '提现次数',
  587. 'field' => '次',
  588. 'count' => $data['extract_count'],
  589. 'background_color' => 'layui-bg-cyan',
  590. 'col' => 2,
  591. ],
  592. [
  593. 'name' => '未提现金额',
  594. 'field' => '元',
  595. 'count' => $data['extract_price'],
  596. 'background_color' => 'layui-bg-cyan',
  597. 'col' => 2,
  598. ],
  599. ];
  600. }
  601. /**
  602. * 获取筛选后的所有用户uid
  603. * @param array $where
  604. * @return array
  605. */
  606. public static function getAll($where = [])
  607. {
  608. $model = new self;
  609. if ($where['nickname'] !== '') $model = $model->where('nickname', 'LIKE', "%$where[nickname]%");
  610. if ($where['data'] !== '') $model = self::getModelTime($where, $model, 'add_time');
  611. if ($where['tagid_list'] !== '') {
  612. $model = $model->where('tagid_list', 'LIKE', "%$where[tagid_list]%");
  613. }
  614. if ($where['groupid'] !== '-1') $model = $model->where('groupid', "$where[groupid]");
  615. if ($where['sex'] !== '') $model = $model->where('sex', "$where[sex]");
  616. return $model->column('uid', 'uid');
  617. }
  618. /**
  619. * 获取已关注的用户
  620. * @param $field
  621. */
  622. public static function getSubscribe($field)
  623. {
  624. return self::where('subscribe', 1)->column($field);
  625. }
  626. public static function getUserAll($field)
  627. {
  628. return self::column($field);
  629. }
  630. public static function getUserTag()
  631. {
  632. $tagName = Config::get('system_wechat_tag');
  633. return Cache::tag($tagName)->remember('_wechat_tag', function () use ($tagName) {
  634. Cache::tag($tagName, ['_wechat_tag']);
  635. $tag = WechatService::userTagService()->lists()->toArray()['tags'] ?: [];
  636. $list = [];
  637. foreach ($tag as $g) {
  638. $list[$g['id']] = $g;
  639. }
  640. return $list;
  641. });
  642. }
  643. public static function clearUserTag()
  644. {
  645. Cache::delete('_wechat_tag');
  646. }
  647. public static function getUserGroup()
  648. {
  649. $tagName = Config::get('system_wechat_tag');
  650. return Cache::tag($tagName)->remember('_wechat_group', function () use ($tagName) {
  651. Cache::tag($tagName, ['_wechat_group']);
  652. $tag = WechatService::userGroupService()->lists()->toArray()['groups'] ?: [];
  653. $list = [];
  654. foreach ($tag as $g) {
  655. $list[$g['id']] = $g;
  656. }
  657. return $list;
  658. });
  659. }
  660. public static function clearUserGroup()
  661. {
  662. Cache::delete('_wechat_group');
  663. }
  664. /**
  665. * 获取推广人数
  666. * @param $uid //用户的uid
  667. * @param int $spread
  668. * $spread 0 一级推广人数 1 二级推广人数
  669. * @return int|string
  670. */
  671. public static function getUserSpreadUidCount($uid, $spread = 1)
  672. {
  673. $userStair = User::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人
  674. if ($userStair) {
  675. if (!$spread) return count($userStair);//返回一级推人人数
  676. else return User::where('spread_uid', 'IN', implode(',', $userStair))->count();//二级推荐人数
  677. } else return 0;
  678. }
  679. /**
  680. * 获取推广人的订单
  681. * @param $uid
  682. * @param int $spread
  683. * $spread 0 一级推广总订单 1 所有推广总订单
  684. * @return int|string
  685. */
  686. public static function getUserSpreadOrderCount($uid, $spread = 1)
  687. {
  688. $userStair = User::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人uid
  689. if ($userStair) {
  690. if (!$spread) {
  691. return StoreOrder::where('uid', 'IN', implode(',', $userStair))->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();//获取一级推广人订单数
  692. } else {
  693. $userSecond = User::where('spread_uid', 'IN', implode(',', $userStair))->column('uid', 'uid');//二级推广人的uid
  694. if ($userSecond) {
  695. return StoreOrder::where('uid', 'IN', implode(',', $userSecond))->where('paid', 1)->where('refund_status', 0)->where('status', 2)->count();//获取二级推广人订单数
  696. } else return 0;
  697. }
  698. } else return 0;
  699. }
  700. }