WechatUser.php 29 KB

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