WechatUser.php 29 KB

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