WechatUser.php 29 KB

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