WechatUser.php 29 KB

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