User.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\user;
  12. use app\admin\model\order\StoreOrder;
  13. use app\admin\model\user\UserExtract;
  14. use service\PHPExcelService;
  15. use traits\ModelTrait;
  16. use app\wap\model\user\UserBill;
  17. use basic\ModelBasic;
  18. use app\admin\model\wechat\WechatUser;
  19. use service\SystemConfigService;
  20. /**
  21. * 用户管理 model
  22. * Class User
  23. * @package app\admin\model\user
  24. */
  25. class User extends ModelBasic
  26. {
  27. use ModelTrait;
  28. /**
  29. * @param $where
  30. * @return array
  31. */
  32. public static function systemPage($where)
  33. {
  34. $model = new self;
  35. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  36. if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
  37. if ($where['nickname'] != '') $model = $model->where('nickname|uid', 'like', "%$where[nickname]%");
  38. if (isset($where['uids']) && count($where['uids'])) $model->where('uid', 'in', $where['uids']);
  39. $model = $model->order('uid desc');
  40. return self::page($model, function ($item) {
  41. if ($item['spread_uid']) {
  42. $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname');
  43. } else {
  44. $item['spread_uid_nickname'] = '无';
  45. }
  46. }, $where);
  47. }
  48. public static function getSpreadUidTwo($uid)
  49. {
  50. if (is_array($uid)) $spread_uid = self::where('spread_uid', 'in', $uid)->column('uid');
  51. else $spread_uid = self::where('spread_uid', $uid)->column('uid');
  52. return self::where('spread_uid', 'in', $spread_uid)->column('uid');
  53. }
  54. /*
  55. * 设置搜索条件
  56. *
  57. */
  58. public static function setWhere($where)
  59. {
  60. if ($where['order'] != '') {
  61. $model = self::order(self::setOrder($where['order']));
  62. } else {
  63. $model = self::order('u.uid desc');
  64. }
  65. if($where['nickname'] != ''){
  66. $model = $model->where('u.nickname|u.uid','LIKE',"%$where[nickname]%");
  67. }
  68. if($where['status'] != ''){
  69. $model = $model->where('status',$where['status']);
  70. }
  71. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  72. list($startTime, $endTime) = explode(' - ', $where['user_time']);
  73. $model = $model->where('u.last_time', ['>', strtotime($endTime) + 24 * 3600], ['<', strtotime($startTime)], 'or');
  74. }
  75. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  76. list($startTime, $endTime) = explode(' - ', $where['user_time']);
  77. $model = $model->where('u.last_time', '>', strtotime($startTime));
  78. $model = $model->where('u.last_time', '<', strtotime($endTime) + 24 * 3600);
  79. }
  80. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  81. list($startTime, $endTime) = explode(' - ', $where['user_time']);
  82. $model = $model->where('u.add_time', '>', strtotime($startTime));
  83. $model = $model->where('u.add_time', '<', strtotime($endTime) + 24 * 3600);
  84. }
  85. if ($where['pay_count'] !== '') {
  86. if ($where['pay_count'] == '-1') $model = $model->where('pay_count', 0);
  87. else $model = $model->where('pay_count', '>', $where['pay_count']);
  88. }
  89. if ($where['country'] != '') {
  90. if ($where['country'] == 'domestic') $model = $model->where('w.country', 'EQ', '中国');
  91. else if ($where['country'] == 'abroad') $model = $model->where('w.country', 'NEQ', '中国');
  92. }
  93. switch ($where['is_promoter']) {
  94. case '1':
  95. $model = $model->where('u.is_promoter', 1);
  96. break;
  97. case '2':
  98. $model = $model->where('u.level', 1);
  99. break;
  100. }
  101. return $model;
  102. }
  103. /**
  104. * 异步获取当前用户 信息
  105. * @param $where
  106. * @return array
  107. */
  108. public static function getUserList($where)
  109. {
  110. $model = self::setWhere($where);
  111. $list = $model->alias('u')
  112. ->join('wechat_user w', 'u.uid=w.uid', 'left')
  113. ->field('u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.routine_openid,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time')
  114. ->page((int)$where['page'], (int)$where['limit'])
  115. ->select()
  116. ->each(function ($item) {
  117. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  118. if ($item['last_time']) $item['last_time'] = date('Y-m-d H:i:s', $item['last_time']);//最近一次访问日期
  119. else $item['last_time'] = '无访问';//最近一次访问日期
  120. self::edit(['pay_count' => StoreOrder::getUserCountPay($item['uid'])], $item['uid']);
  121. $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现
  122. if ($item['spread_uid']) {
  123. $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname') . '/' . $item['spread_uid'];
  124. } else {
  125. $item['spread_uid_nickname'] = '无';
  126. }
  127. $item['user_type'] = '公众号类型';
  128. if ($item['sex'] == 1) {
  129. $item['sex'] = '男';
  130. } else if ($item['sex'] == 2) {
  131. $item['sex'] = '女';
  132. } else $item['sex'] = '保密';
  133. if ($item['level']) {
  134. $item['levelType'] = '会员';
  135. }else{
  136. $item['levelType'] = '非会员';
  137. }
  138. $item['_valid_time'] = date('Y-m-d H:i:s', $item['valid_time']);
  139. })->toArray();
  140. $count = self::setWhere($where)->alias('u')->join('WechatUser w', 'u.uid=w.uid', 'left')->count();
  141. return ['count' => $count, 'data' => $list];
  142. }
  143. /**
  144. * 修改用户状态
  145. * @param $uids 用户uid
  146. * @param $status 修改状态
  147. * @return array
  148. */
  149. public static function destrSyatus($uids, $status)
  150. {
  151. if (empty($uids) && !is_array($uids)) return false;
  152. if ($status == '') return false;
  153. self::beginTrans();
  154. try {
  155. $res = self::where('uid', 'in', $uids)->update(['status' => $status]);
  156. self::checkTrans($res);
  157. return true;
  158. } catch (\Exception $e) {
  159. self::rollbackTrans();
  160. return Json::fail($e->getMessage());
  161. }
  162. }
  163. /*
  164. * 获取某季度,某年某年后的时间戳
  165. *
  166. * self::getMonth('n',1) 获取当前季度的上个季度的时间戳
  167. * self::getMonth('n') 获取当前季度的时间戳
  168. */
  169. public static function getMonth($time = '', $ceil = 0)
  170. {
  171. if (empty($time)) {
  172. $firstday = date("Y-m-01", time());
  173. $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
  174. } else if ($time == 'n') {
  175. if ($ceil != 0)
  176. $season = ceil(date('n') / 3) - $ceil;
  177. else
  178. $season = ceil(date('n') / 3);
  179. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  180. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  181. } else if ($time == 'y') {
  182. $firstday = date('Y-01-01');
  183. $lastday = date('Y-12-31');
  184. } else if ($time == 'h') {
  185. $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
  186. $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
  187. }
  188. return array($firstday, $lastday);
  189. }
  190. public static function getcount()
  191. {
  192. return self::alias('a')->join('wechat_user u', 'u.uid=a.uid', 'left')->count();
  193. }
  194. /*
  195. *获取用户某个时间段的消费信息
  196. *
  197. * reutrn Array || number
  198. */
  199. public static function consume($where, $status = '', $keep = '')
  200. {
  201. $model = new self;
  202. $user_id = [];
  203. if (is_array($where)) {
  204. if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
  205. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  206. switch ($where['date']) {
  207. case null:
  208. case 'today':
  209. case 'week':
  210. case 'year':
  211. if ($where['date'] == null) {
  212. $where['date'] = 'month';
  213. }
  214. if ($keep) {
  215. $model = $model->whereTime('add_time', $where['date'])->whereTime('last_time', $where['date']);
  216. } else {
  217. $model = $model->whereTime('add_time', $where['date']);
  218. }
  219. break;
  220. case 'quarter':
  221. $quarter = self::getMonth('n');
  222. $startTime = strtotime($quarter[0]);
  223. $endTime = strtotime($quarter[1]);
  224. if ($keep) {
  225. $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime)->where('last_time', '>', $startTime)->where('last_time', '<', $endTime);
  226. } else {
  227. $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime);
  228. }
  229. break;
  230. default:
  231. //自定义时间
  232. if (strstr($where['date'], '-') !== FALSE) {
  233. list($startTime, $endTime) = explode('-', $where['date']);
  234. $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', strtotime($endTime));
  235. } else {
  236. $model = $model->whereTime('add_time', 'month');
  237. }
  238. break;
  239. }
  240. } else {
  241. if (is_array($status)) {
  242. $model = $model->where('add_time', '>', $status[0])->where('add_time', '<', $status[1]);
  243. }
  244. }
  245. if ($keep === true) {
  246. return $model->count();
  247. }
  248. if ($status === 'default') {
  249. return $model->group('from_unixtime(add_time,\'%Y-%m-%d\')')->field('count(uid) num,from_unixtime(add_time,\'%Y-%m-%d\') add_time,uid')->select()->toArray();
  250. }
  251. $uid = $model->field('uid')->select()->toArray();
  252. foreach ($uid as $val) {
  253. $user_id[] = $val['uid'];
  254. }
  255. if (empty($user_id)) {
  256. $user_id = [0];
  257. }
  258. if ($status === 'xiaofei') {
  259. $list = UserBill::where('uid', 'in', $user_id)
  260. ->group('type')
  261. ->field('sum(number) as top_number,title')
  262. ->select()
  263. ->toArray();
  264. $series = [
  265. 'name' => isset($list[0]['title']) ? $list[0]['title'] : '',
  266. 'type' => 'pie',
  267. 'radius' => ['40%', '50%'],
  268. 'data' => []
  269. ];
  270. foreach ($list as $key => $val) {
  271. $series['data'][$key]['value'] = $val['top_number'];
  272. $series['data'][$key]['name'] = $val['title'];
  273. }
  274. return $series;
  275. } else if ($status === 'form') {
  276. $list = WechatUser::where('uid', 'in', $user_id)->group('city')->field('count(city) as top_city,city')->limit(0, 10)->select()->toArray();
  277. $count = self::getcount();
  278. $option = [
  279. 'legend_date' => [],
  280. 'series_date' => []
  281. ];
  282. foreach ($list as $key => $val) {
  283. $num = $count != 0 ? (bcdiv($val['top_city'], $count, 2)) * 100 : 0;
  284. $t = ['name' => $num . '% ' . (empty($val['city']) ? '未知' : $val['city']), 'icon' => 'circle'];
  285. $option['legend_date'][$key] = $t;
  286. $option['series_date'][$key] = ['value' => $num, 'name' => $t['name']];
  287. }
  288. return $option;
  289. } else {
  290. $number = UserBill::where('uid', 'in', $user_id)->where('type', 'pay_product')->sum('number');
  291. return $number;
  292. }
  293. }
  294. /*
  295. * 获取 用户某个时间段的钱数或者TOP20排行
  296. *
  297. * return Array || number
  298. */
  299. public static function getUserSpend($date, $status = '')
  300. {
  301. $model = new self();
  302. $model = $model->alias('A');
  303. switch ($date) {
  304. case null:
  305. case 'today':
  306. case 'week':
  307. case 'year':
  308. if ($date == null) $date = 'month';
  309. $model = $model->whereTime('A.add_time', $date);
  310. break;
  311. case 'quarter':
  312. list($startTime, $endTime) = User::getMonth('n');
  313. $model = $model->where('A.add_time', '>', strtotime($startTime));
  314. $model = $model->where('A.add_time', '<', strtotime($endTime));
  315. break;
  316. default:
  317. list($startTime, $endTime) = explode('-', $date);
  318. $model = $model->where('A.add_time', '>', strtotime($startTime));
  319. $model = $model->where('A.add_time', '<', strtotime($endTime));
  320. break;
  321. }
  322. if ($status === true) {
  323. return $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
  324. }
  325. $list = $model->join('user_bill B', 'B.uid=A.uid')
  326. ->where('B.type', 'pay_product')
  327. ->where('B.pm', 0)
  328. ->field('sum(B.number) as totel_number,A.nickname,A.avatar,A.now_money,A.uid,A.add_time')
  329. ->order('totel_number desc')
  330. ->limit(0, 20)
  331. ->select()
  332. ->toArray();
  333. if (!isset($list[0]['totel_number'])) {
  334. $list = [];
  335. }
  336. return $list;
  337. }
  338. /*
  339. * 获取 相对于上月或者其他的数据
  340. *
  341. * return Array
  342. */
  343. public static function getPostNumber($date, $status = false, $field = 'A.add_time', $t = '消费')
  344. {
  345. $model = new self();
  346. if (!$status) $model = $model->alias('A');
  347. switch ($date) {
  348. case null:
  349. case 'today':
  350. case 'week':
  351. case 'year':
  352. if ($date == null) {
  353. $date = 'last month';
  354. $title = '相比上月用户' . $t . '增长';
  355. }
  356. if ($date == 'today') {
  357. $date = 'yesterday';
  358. $title = '相比昨天用户' . $t . '增长';
  359. }
  360. if ($date == 'week') {
  361. $date = 'last week';
  362. $title = '相比上周用户' . $t . '增长';
  363. }
  364. if ($date == 'year') {
  365. $date = 'last year';
  366. $title = '相比去年用户' . $t . '增长';
  367. }
  368. $model = $model->whereTime($field, $date);
  369. break;
  370. case 'quarter':
  371. $title = '相比上季度用户' . $t . '增长';
  372. list($startTime, $endTime) = User::getMonth('n', 1);
  373. $model = $model->where($field, '>', $startTime);
  374. $model = $model->where($field, '<', $endTime);
  375. break;
  376. default:
  377. list($startTime, $endTime) = explode('-', $date);
  378. $title = '相比' . $startTime . '-' . $endTime . '时间段用户' . $t . '增长';
  379. $Time = strtotime($endTime) - strtotime($startTime);
  380. $model = $model->where($field, '>', strtotime($startTime) + $Time);
  381. $model = $model->where($field, '<', strtotime($endTime) + $Time);
  382. break;
  383. }
  384. if ($status) {
  385. return [$model->count(), $title];
  386. }
  387. $number = $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
  388. return [$number, $title];
  389. }
  390. //获取用户新增,头部信息
  391. public static function getBadgeList($where)
  392. {
  393. $user_count = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])->count();
  394. $user_count_old = self::getOldDate($where)->count();
  395. $fenxiao = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])->where('spread_uid', '<>', 0)->count();
  396. $fenxiao_count = self::getOldDate($where)->where('spread_uid', 'neq', 0)->count();
  397. $newFemxiao_count = bcsub($fenxiao, $fenxiao_count, 0);
  398. $order_count = bcsub($user_count, $user_count_old, 0);
  399. return [
  400. [
  401. 'name' => '会员人数',
  402. 'field' => '个',
  403. 'count' => $user_count,
  404. 'content' => '会员总人数',
  405. 'background_color' => 'layui-bg-blue',
  406. 'sum' => self::count(),
  407. 'class' => 'fa fa-bar-chart',
  408. ],
  409. [
  410. 'name' => '会员增长',
  411. 'field' => '个',
  412. 'count' => $order_count,
  413. 'content' => '会员增长率',
  414. 'background_color' => 'layui-bg-cyan',
  415. 'sum' => $user_count_old ? bcdiv($order_count, $user_count_old, 2) * 100 : 0,
  416. 'class' => 'fa fa-line-chart',
  417. ],
  418. [
  419. 'name' => '分销人数',
  420. 'field' => '个',
  421. 'count' => $fenxiao,
  422. 'content' => '分销总人数',
  423. 'background_color' => 'layui-bg-green',
  424. 'sum' => self::where('spread_uid', 'neq', 0)->count(),
  425. 'class' => 'fa fa-bar-chart',
  426. ],
  427. [
  428. 'name' => '分销增长',
  429. 'field' => '个',
  430. 'count' => $newFemxiao_count,
  431. 'content' => '分销总人数',
  432. 'background_color' => 'layui-bg-orange',
  433. 'sum' => $fenxiao_count ? bcdiv($newFemxiao_count, $fenxiao_count, 2) * 100 : 0,
  434. 'class' => 'fa fa-cube',
  435. ],
  436. ];
  437. }
  438. /*
  439. * 获取会员增长曲线图和分布图
  440. * $where 查询条件
  441. * $limit 显示条数,是否有滚动条
  442. */
  443. public static function getUserChartList($where, $limit = 20)
  444. {
  445. $list = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])
  446. ->where('add_time', 'neq', 0)
  447. ->field(['FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time', 'count(uid) as num'])
  448. ->order('_add_time asc')
  449. ->group('_add_time')
  450. ->select();
  451. count($list) && $list = $list->toArray();
  452. $seriesdata = [];
  453. $xdata = [];
  454. $Zoom = '';
  455. foreach ($list as $item) {
  456. $seriesdata[] = $item['num'];
  457. $xdata[] = $item['_add_time'];
  458. }
  459. (count($xdata) > $limit) && $Zoom = $xdata[$limit - 5];
  460. //多次购物会员数量饼状图
  461. $count = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter'])->count();
  462. $user_count = self::setWherePage(self::getModelTime($where, self::alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time'), $where, ['is_promoter'])
  463. ->where('r.paid', 1)->count('a.uid');
  464. $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
  465. $shop_data = [];
  466. $count > 0 && $shop_data = [
  467. [
  468. 'value' => bcdiv($user_count, $count, 2) * 100,
  469. 'name' => $shop_xdata[0],
  470. 'itemStyle' => [
  471. 'color' => '#D789FF',
  472. ]
  473. ],
  474. [
  475. 'value' => bcdiv($count - $user_count, $count, 2) * 100,
  476. 'name' => $shop_xdata[1],
  477. 'itemStyle' => [
  478. 'color' => '#7EF0FB',
  479. ]
  480. ]
  481. ];
  482. return compact('shop_data', 'shop_xdata', 'fenbu_data', 'fenbu_xdata', 'seriesdata', 'xdata', 'Zoom');
  483. }
  484. //获取$date的前一天或者其他的时间段
  485. public static function getOldDate($where, $moedls = null)
  486. {
  487. $model = $moedls === null ? self::setWherePage(new self(), $where, ['is_promoter', 'status']) : $moedls;
  488. switch ($where['data']) {
  489. case 'today':
  490. $model = $model->whereTime('add_time', 'yesterday');
  491. break;
  492. case 'week':
  493. $model = $model->whereTime('add_time', 'last week');
  494. break;
  495. case 'month':
  496. $model = $model->whereTime('add_time', 'last month');
  497. break;
  498. case 'year':
  499. $model = $model->whereTime('add_time', 'last year');
  500. break;
  501. case 'quarter':
  502. $time = self::getMonth('n', 1);
  503. $model = $model->where('add_time', 'between', $time);
  504. break;
  505. }
  506. return $model;
  507. }
  508. //获取用户属性和性别分布图
  509. public static function getEchartsData($where)
  510. {
  511. $model = self::alias('a');
  512. $data = self::getModelTime($where, $model, 'a.add_time')
  513. ->join('wechat_user r', 'r.uid=a.uid')
  514. ->group('r.province')
  515. ->field('count(r.province) as count,province')
  516. ->order('count desc')
  517. ->limit(15)
  518. ->select();
  519. if (count($data)) $data = $data->toArray();
  520. $legdata = [];
  521. $dataList = [];
  522. foreach ($data as $value) {
  523. $value['province'] == '' && $value['province'] = '未知省份';
  524. $legdata[] = $value['province'];
  525. $dataList[] = $value['count'];
  526. }
  527. $model = self::alias('a');
  528. $sex = self::getModelTime($where, $model, 'a.add_time')
  529. ->join('wechat_user r', 'r.uid=a.uid')
  530. ->group('r.sex')
  531. ->field('count(r.uid) as count,sex')
  532. ->order('count desc')
  533. ->select();
  534. if (count($sex)) $sex = $sex->toArray();
  535. $sexlegdata = ['男', '女', '未知'];
  536. $sexcount = self::getModelTime($where, new self())->count();
  537. $sexList = [];
  538. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  539. foreach ($sex as $key => $item) {
  540. if ($item['sex'] == 1) {
  541. $item_date['name'] = '男';
  542. } else if ($item['sex'] == 2) {
  543. $item_date['name'] = '女';
  544. } else {
  545. $item_date['name'] = '未知性别';
  546. }
  547. $item_date['value'] = bcdiv($item['count'], $sexcount, 2) * 100;
  548. $item_date['itemStyle']['color'] = $color[$key];
  549. $sexList[] = $item_date;
  550. }
  551. return compact('sexList', 'sexlegdata', 'legdata', 'dataList');
  552. }
  553. //获取佣金记录列表
  554. public static function getCommissionList($where)
  555. {
  556. $list = self::setCommissionWhere($where)
  557. ->page((int)$where['page'], (int)$where['limit'])
  558. ->select();
  559. count($list) && $list = $list->toArray();
  560. foreach ($list as &$value) {
  561. $value['ex_price'] = db('user_extract')->where(['uid' => $value['uid']])->sum('extract_price');
  562. $value['extract_price'] = db('user_extract')->where(['uid' => $value['uid'], 'status' => 1])->sum('extract_price');
  563. }
  564. $count = self::setCommissionWhere($where)->count();
  565. return ['data' => $list, 'count' => $count];
  566. }
  567. //获取佣金记录列表的查询条件
  568. public static function setCommissionWhere($where)
  569. {
  570. $models = self::setWherePage(self::alias('A'), $where, [], ['A.nickname', 'A.uid'])
  571. ->join('user_bill B', 'B.uid=A.uid')
  572. ->group('A.uid')
  573. ->where(['B.category' => 'now_money'])
  574. ->where('B.type', 'in', ['brokerage','brokerage_return', 'rake_back_one', 'rake_back_two', 'rake_back'])
  575. ->field(['B.number', 'A.nickname', 'A.uid', 'A.now_money', 'A.brokerage_price']);
  576. if ($where['order'] == '') {
  577. $models = $models->order('A.brokerage_price desc');
  578. } else {
  579. $models = $models->order($where['order'] == 1 ? 'A.brokerage_price desc' : 'A.brokerage_price asc');
  580. }
  581. if ($where['price_max'] >0 && $where['price_min'] > 0) {
  582. $models = $models->where('A.brokerage_price', 'between', [$where['price_min'], $where['price_max']]);
  583. }
  584. return $models;
  585. }
  586. //获取某人用户推广信息
  587. public static function getUserinfo($uid)
  588. {
  589. $userinfo = self::where(['uid' => $uid])->field(['nickname', 'spread_uid', 'now_money', 'add_time'])->find()->toArray();
  590. $userinfo['number'] = UserBill::getCommissionAmount($uid);
  591. $userinfo['spread_name'] = $userinfo['spread_uid'] ? self::where(['uid' => $userinfo['spread_uid']])->value('nickname') : '';
  592. return $userinfo;
  593. }
  594. public static function getUserinfoV1($uid)
  595. {
  596. $userinfo = self::where(['uid' => $uid])->find()->toArray();
  597. $userinfo['spread_name'] = $userinfo['spread_uid'] ? self::where(['uid' => $userinfo['spread_uid']])->value('nickname') : '';
  598. $spread = self::where(['spread_uid' => $uid])->where('is_promoter', 'neq', 0)->column('uid');
  599. $userinfo['spread_count'] = count($spread);
  600. $userinfo['spread_one'] = UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
  601. ->where('a.type', 'in', ['rake_back_one', 'rake_back'])->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->sum('o.pay_price');
  602. $userinfo['spread_two'] = UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
  603. ->where('a.type', 'in', ['rake_back_two'])->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->sum('o.pay_price');
  604. $userinfo['grade_name'] = self::getDb('grade')->where('id', $userinfo['grade_id'])->value('name');
  605. $userinfo['bill_sum'] = UserBill::where(['category' => 'now_money', 'uid' => $userinfo['uid']])->where('type', 'in', ['rake_back_one', 'rake_back_two', 'rake_back'])->sum('number');
  606. return $userinfo;
  607. }
  608. public static function getPayPrice($uid, $type = ['rake_back_two'])
  609. {
  610. return UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
  611. ->where('a.type', 'in', $type)->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->sum('o.pay_price');
  612. }
  613. public static function getLinkCount($uid, $type = ['rake_back_two'])
  614. {
  615. return UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
  616. ->where('a.type', 'in', $type)->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->count();
  617. }
  618. public static function getSpreadListV1($where)
  619. {
  620. $spread = self::where(['spread_uid' => $where['uid']])->column('uid') ?: [0];
  621. $list = self::where('uid', 'in', $spread)->order('add_time desc')->page((int)$where['page'], (int)$where['limit'])->select();
  622. $list = count($list) ? $list->toArray() : [];
  623. foreach ($list as &$item) {
  624. $item['order_count'] = self::getLinkCount($item['uid'], ['rake_back_one', 'rake_back_two', 'rake_back']);
  625. $item['sum_pay_price'] = self::getPayPrice($item['uid'], ['rake_back_one', 'rake_back_two', 'rake_back']);
  626. $item['sum_number'] = UserBill::where('uid', $item['uid'])->where('category', 'now_money')->where('type', 'in', ['rake_back_one', 'rake_back_two', 'rake_back'])->sum('number');
  627. }
  628. return $list;
  629. }
  630. //获取某用户的详细信息
  631. public static function getUserDetailed($uid)
  632. {
  633. $gold_name=SystemConfigService::get('gold_name');//虚拟币名称
  634. $key_field = ['real_name', 'phone', 'province', 'city', 'district', 'detail', 'post_code'];
  635. $Address = ($thisAddress = db('user_address')->where(['uid' => $uid, 'is_default' => 1])->field($key_field)->find()) ?
  636. $thisAddress :
  637. db('user_address')->where(['uid' => $uid])->field($key_field)->find();
  638. $UserInfo = self::get($uid);
  639. $sex=WechatUser::where('uid',$uid)->value('sex');
  640. if ($sex == 1) {
  641. $UserInfo['sex'] = '男';
  642. } else if ($sex == 2) {
  643. $UserInfo['sex'] = '女';
  644. } else $UserInfo['sex'] = '保密';
  645. if ($UserInfo['last_time']) $UserInfo['last_time'] = date('Y-m-d H:i:s', $UserInfo['last_time']);//最近一次访问日期
  646. else $UserInfo['last_time'] = '无访问';//最近一次访问日期
  647. $UserInfo['add_time'] =date('Y-m-d H:i:s', $UserInfo['add_time']);
  648. $time='首次:'.$UserInfo['add_time'].'最近:'.$UserInfo['last_time'];
  649. return [
  650. ['col' => 12, 'name' => '默认收货地址', 'value' => $thisAddress ? '收货人:' . $thisAddress['real_name']. ' 收货人电话:' . $thisAddress['phone'] . ' 地址:' . $thisAddress['province'] . ' ' . $thisAddress['city'] . ' ' . $thisAddress['district'] . ' ' . $thisAddress['detail'] : ''],
  651. ['name'=>'ID','value'=>$uid],
  652. ['name' => '手机号码', 'value' => $UserInfo['phone']],
  653. ['name' => '微信昵称', 'value' => $UserInfo['nickname']],
  654. ['name' => '性别', 'value' => $UserInfo['sex']],
  655. ['name' => '购买次数', 'value' => StoreOrder::getUserCountPay($uid)],
  656. ['name' => '访问日期', 'value' => $time],
  657. ['name' => "$gold_name".'余额', 'value' => $UserInfo['gold_num']],
  658. ['name' => '上级推广人', 'value' => $UserInfo['spread_uid'] ? self::where(['uid' => $UserInfo['spread_uid']])->value('nickname') : ''],
  659. ['name' => '账户余额', 'value' => $UserInfo['now_money']],
  660. ['name' => '佣金总收入', 'value' => UserBill::getCommissionAmount($uid)],
  661. ['name' => '提现总金额', 'value' => db('user_extract')->where(['uid' => $uid, 'status' => 1])->sum('extract_price')],
  662. ];
  663. }
  664. //获取某用户的订单个数,消费明细
  665. public static function getHeaderList($uid)
  666. {
  667. return [
  668. [
  669. 'title' => '总计订单',
  670. 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->count(),
  671. 'key' => '笔',
  672. 'class' => '',
  673. ],
  674. [
  675. 'title' => '总消费金额',
  676. 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->sum('pay_price'),
  677. 'key' => '元',
  678. 'class' => '',
  679. ],
  680. [
  681. 'title' => '本月订单',
  682. 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->whereTime('add_time', 'month')->count(),
  683. 'key' => '笔',
  684. 'class' => '',
  685. ],
  686. [
  687. 'title' => '本月消费金额',
  688. 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->whereTime('add_time', 'month')->sum('pay_price'),
  689. 'key' => '元',
  690. 'class' => '',
  691. ]
  692. ];
  693. }
  694. /*
  695. * 获取 会员 订单个数,积分明细,优惠劵明细
  696. *
  697. * $uid 用户id;
  698. *
  699. * return array
  700. */
  701. public static function getCountInfo($uid)
  702. {
  703. $order_count = StoreOrder::where(['uid' => $uid])->count();
  704. $integral_count = UserBill::where(['uid' => $uid, 'category' => 'integral'])->where('type', 'in', ['deduction', 'system_add'])->count();
  705. $sign_count = UserBill::where(['uid' => $uid, 'category' => 'integral', 'type' => 'sign'])->count();
  706. $balanceChang_count = UserBill::where(['uid' => $uid, 'category' => 'now_money'])
  707. ->where('type', 'in', ['system_add', 'pay_product', 'extract','extract_fail','pay_goods','pay_sign_up', 'pay_product_refund', 'system_sub'])
  708. ->count();
  709. $coupon_count = 0;
  710. $spread_count = self::where(['spread_uid' => $uid])->count();
  711. $pay_count = self::getDb('special_buy')->where('uid', $uid)->where('is_del', 0)->count();
  712. return compact('order_count', 'integral_count', 'sign_count', 'balanceChang_count', 'coupon_count', 'spread_count', 'pay_count');
  713. }
  714. /*
  715. * 获取 会员业务的
  716. * 购物会员统计
  717. * 会员访问量
  718. *
  719. * 曲线图
  720. *
  721. * $where 查询条件
  722. *
  723. * return array
  724. */
  725. public static function getUserBusinessChart($where, $limit = 20)
  726. {
  727. //获取购物会员人数趋势图
  728. $list = self::getModelTime($where, self::where('a.status', 1)->alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
  729. ->where(['r.paid' => 1, 'a.is_promoter' => 0])
  730. ->where('a.add_time', 'neq', 0)
  731. ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'count(r.uid) as count_user'])
  732. ->group('_add_time')
  733. ->order('_add_time asc')
  734. ->select();
  735. count($list) && $list = $list->toArray();
  736. $seriesdata = [];
  737. $xdata = [];
  738. $zoom = '';
  739. foreach ($list as $item) {
  740. $seriesdata[] = $item['count_user'];
  741. $xdata[] = $item['_add_time'];
  742. }
  743. count($xdata) > $limit && $zoom = $xdata[$limit - 5];
  744. //会员访问量
  745. $visit = self::getModelTime($where, self::alias('a')->join('store_visit t', 't.uid=a.uid'), 't.add_time')
  746. ->where('a.is_promoter', 0)
  747. ->field(['FROM_UNIXTIME(t.add_time,"%Y-%m-%d") as _add_time', 'count(t.uid) as count_user'])
  748. ->group('_add_time')
  749. ->order('_add_time asc')
  750. ->select();
  751. count($visit) && $visit = $visit->toArray();
  752. $visit_data = [];
  753. $visit_xdata = [];
  754. $visit_zoom = '';
  755. foreach ($visit as $item) {
  756. $visit_data[] = $item['count_user'];
  757. $visit_xdata[] = $item['_add_time'];
  758. }
  759. count($visit_xdata) > $limit && $visit_zoom = $visit_xdata[$limit - 5];
  760. //多次购物会员数量饼状图
  761. $count = self::getModelTime($where, self::where('is_promoter', 0))->count();
  762. $user_count = self::getModelTime($where, self::alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
  763. ->where('a.is_promoter', 0)
  764. ->where('r.paid', 1)
  765. ->group('a.uid')
  766. ->count();
  767. $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
  768. $shop_data = [];
  769. $count > 0 && $shop_data = [
  770. [
  771. 'value' => bcdiv($user_count, $count, 2) * 100,
  772. 'name' => $shop_xdata[0],
  773. 'itemStyle' => [
  774. 'color' => '#D789FF',
  775. ]
  776. ],
  777. [
  778. 'value' => bcdiv($count - $user_count, $count, 2) * 100,
  779. 'name' => $shop_xdata[1],
  780. 'itemStyle' => [
  781. 'color' => '#7EF0FB',
  782. ]
  783. ]
  784. ];
  785. return compact('seriesdata', 'xdata', 'zoom', 'visit_data', 'visit_xdata', 'visit_zoom', 'shop_data', 'shop_xdata');
  786. }
  787. /*
  788. * 获取用户
  789. * 积分排行
  790. * 会员余额排行榜
  791. * 分销商佣金总额排行榜
  792. * 购物笔数排行榜
  793. * 购物金额排行榜
  794. * 分销商佣金提现排行榜
  795. * 上月消费排行榜
  796. * $limit 查询多少条
  797. * return array
  798. */
  799. public static function getUserTop10List($limit = 10, $is_promoter = 0)
  800. {
  801. //积分排行
  802. $integral = self::where('status', 1)
  803. ->where('is_promoter', $is_promoter)
  804. ->order('integral desc')
  805. ->field(['nickname', 'phone', 'integral', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'])
  806. ->limit($limit)
  807. ->select();
  808. count($integral) && $integral = $integral->toArray();
  809. //会员余额排行榜
  810. $now_money = self::where('status', 1)
  811. ->where('is_promoter', $is_promoter)
  812. ->order('now_money desc')
  813. ->field(['nickname', 'phone', 'now_money', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'])
  814. ->limit($limit)
  815. ->select();
  816. count($now_money) && $now_money = $now_money->toArray();
  817. //购物笔数排行榜
  818. $shopcount = self::alias('a')
  819. ->join('store_order r', 'r.uid=a.uid')
  820. ->where(['r.paid' => 1, 'a.is_promoter' => $is_promoter])
  821. ->group('r.uid')
  822. ->field(['a.nickname', 'a.phone', 'count(r.uid) as sum_count', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time'])
  823. ->order('sum_count desc')
  824. ->limit($limit)
  825. ->select();
  826. count($shopcount) && $shopcount = $shopcount->toArray();
  827. //购物金额排行榜
  828. $order = self::alias('a')
  829. ->join('store_order r', 'r.uid=a.uid')
  830. ->where(['r.paid' => 1, 'a.is_promoter' => $is_promoter])
  831. ->group('r.uid')
  832. ->field(['a.nickname', 'a.phone', 'sum(r.pay_price) as sum_price', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time', 'r.uid'])
  833. ->order('sum_price desc')
  834. ->limit($limit)
  835. ->select();
  836. count($order) && $order = $order->toArray();
  837. //上月消费排行
  838. $lastorder = self::alias('a')
  839. ->join('store_order r', 'r.uid=a.uid')
  840. ->where(['r.paid' => 1, 'a.is_promoter' => $is_promoter])
  841. ->whereTime('r.pay_time', 'last month')
  842. ->group('r.uid')
  843. ->field(['a.nickname', 'a.phone', 'sum(r.pay_price) as sum_price', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time', 'r.uid'])
  844. ->order('sum_price desc')
  845. ->limit($limit)
  846. ->select();
  847. return compact('integral', 'now_money', 'shopcount', 'order', 'lastorder');
  848. }
  849. /*
  850. * 获取 会员业务
  851. * 会员总余额 会员总积分
  852. * $where 查询条件
  853. *
  854. * return array
  855. */
  856. public static function getUserBusinesHeade($where)
  857. {
  858. return [
  859. [
  860. 'name' => '会员总余额',
  861. 'field' => '元',
  862. 'count' => self::getModelTime($where, self::where('status', 1))->sum('now_money'),
  863. 'background_color' => 'layui-bg-cyan',
  864. 'col' => 6,
  865. ],
  866. [
  867. 'name' => '会员总积分',
  868. 'field' => '分',
  869. 'count' => self::getModelTime($where, self::where('status', 1))->sum('integral'),
  870. 'background_color' => 'layui-bg-cyan',
  871. 'col' => 6
  872. ]
  873. ];
  874. }
  875. /*
  876. * 分销会员头部信息查询获取
  877. *
  878. * 分销商总佣金余额
  879. * 分销商总提现佣金
  880. * 本月分销商业务佣金
  881. * 本月分销商佣金提现金额
  882. * 上月分销商业务佣金
  883. * 上月分销商佣金提现金额
  884. * $where array 时间条件
  885. *
  886. * return array
  887. */
  888. public static function getDistributionBadgeList($where)
  889. {
  890. return [
  891. [
  892. 'name' => '分销商总佣金',
  893. 'field' => '元',
  894. 'count' => self::getModelTime($where, UserBill::where('category', 'now_money')->where('type', 'brokerage'))->where('uid', 'in', function ($query) {
  895. $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
  896. })->sum('number'),
  897. 'background_color' => 'layui-bg-cyan',
  898. 'col' => 3,
  899. ],
  900. [
  901. 'name' => '分销商总佣金余额',
  902. 'field' => '元',
  903. 'count' => self::getModelTime($where, self::where('status', 1)->where('is_promoter', 1))->sum('now_money'),
  904. 'background_color' => 'layui-bg-cyan',
  905. 'col' => 3,
  906. ],
  907. [
  908. 'name' => '分销商总提现佣金',
  909. 'field' => '元',
  910. 'count' => self::getModelTime($where, UserExtract::where('status', 1))->sum('extract_price'),
  911. 'background_color' => 'layui-bg-cyan',
  912. 'col' => 3,
  913. ],
  914. [
  915. 'name' => '本月分销商业务佣金',
  916. 'field' => '元',
  917. 'count' => self::getModelTime(['data' => 'month'], UserBill::where('category', 'now_money')->where('type', 'brokerage'))
  918. ->where('uid', 'in', function ($query) {
  919. $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
  920. })->sum('number'),
  921. 'background_color' => 'layui-bg-cyan',
  922. 'col' => 3,
  923. ],
  924. [
  925. 'name' => '本月分销商佣金提现金额',
  926. 'field' => '元',
  927. 'count' => self::getModelTime(['data' => 'month'], UserExtract::where('status', 1))
  928. ->where('uid', 'in', function ($query) {
  929. $query->name('user')->where('status', 1)->where('is_promoter', 1)->field('uid');
  930. })->sum('extract_price'),
  931. 'background_color' => 'layui-bg-cyan',
  932. 'col' => 4,
  933. ],
  934. [
  935. 'name' => '上月分销商业务佣金',
  936. 'field' => '元',
  937. 'count' => self::getOldDate(['data' => 'year'], UserBill::where('category', 'now_money')->where('uid', 'in', function ($query) {
  938. $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
  939. })->where('type', 'brokerage'))->sum('number'),
  940. 'background_color' => 'layui-bg-cyan',
  941. 'col' => 4,
  942. ],
  943. [
  944. 'name' => '上月分销商佣金提现金额',
  945. 'field' => '元',
  946. 'count' => self::getOldDate(['data' => 'year'], UserBill::where('category', 'now_money')->where('uid', 'in', function ($query) {
  947. $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
  948. })->where('type', 'brokerage'))->sum('number'),
  949. 'background_color' => 'layui-bg-cyan',
  950. 'col' => 4,
  951. ],
  952. ];
  953. }
  954. /*
  955. * 分销会员
  956. * 分销数量 饼状图
  957. * 分销商会员访问量 曲线
  958. * 获取购物会员人数趋势图 曲线
  959. * 多次购物分销会员数量 饼状图
  960. * $where array 条件
  961. * $limit int n条数据后出拖动条
  962. * return array
  963. */
  964. public static function getUserDistributionChart($where, $limit = 20)
  965. {
  966. //分销数量
  967. $fenbu_user = self::getModelTime($where, new self)->field(['count(uid) as num'])->group('is_promoter')->select();
  968. count($fenbu_user) && $fenbu_user = $fenbu_user->toArray();
  969. $sum_user = 0;
  970. $fenbu_data = [];
  971. $fenbu_xdata = ['分销商', '非分销商'];
  972. $color = ['#81BCFE', '#91F3FE'];
  973. foreach ($fenbu_user as $item) {
  974. $sum_user += $item['num'];
  975. }
  976. foreach ($fenbu_user as $key => $item) {
  977. $value['value'] = bcdiv($item['num'], $sum_user, 2) * 100;
  978. $value['name'] = isset($fenbu_xdata[$key]) ? $fenbu_xdata[$key] . ' %' . $value['value'] : '';
  979. $value['itemStyle']['color'] = $color[$key];
  980. $fenbu_data[] = $value;
  981. }
  982. //分销商会员访问量
  983. $visit = self::getModelTime($where, self::alias('a')->join('store_visit t', 't.uid=a.uid'), 't.add_time')
  984. ->where('a.is_promoter', 1)
  985. ->field(['FROM_UNIXTIME(t.add_time,"%Y-%m-%d") as _add_time', 'count(t.uid) as count_user'])
  986. ->group('_add_time')
  987. ->order('_add_time asc')
  988. ->select();
  989. count($visit) && $visit = $visit->toArray();
  990. $visit_data = [];
  991. $visit_xdata = [];
  992. $visit_zoom = '';
  993. foreach ($visit as $item) {
  994. $visit_data[] = $item['count_user'];
  995. $visit_xdata[] = $item['_add_time'];
  996. }
  997. count($visit_xdata) > $limit && $visit_zoom = $visit_xdata[$limit - 5];
  998. //获取购物会员人数趋势图
  999. $list = self::getModelTime($where, self::where('a.status', 1)->alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
  1000. ->where(['r.paid' => 1, 'a.is_promoter' => 1])
  1001. ->where('a.add_time', 'neq', 0)
  1002. ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'count(r.uid) as count_user'])
  1003. ->group('_add_time')
  1004. ->order('_add_time asc')
  1005. ->select();
  1006. count($list) && $list = $list->toArray();
  1007. $seriesdata = [];
  1008. $xdata = [];
  1009. $zoom = '';
  1010. foreach ($list as $item) {
  1011. $seriesdata[] = $item['count_user'];
  1012. $xdata[] = $item['_add_time'];
  1013. }
  1014. count($xdata) > $limit && $zoom = $xdata[$limit - 5];
  1015. //多次购物分销会员数量饼状图
  1016. $count = self::getModelTime($where, self::where('is_promoter', 1))->count();
  1017. $user_count = self::getModelTime($where, self::alias('a')
  1018. ->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
  1019. ->where('a.is_promoter', 1)
  1020. ->where('r.paid', 1)
  1021. ->group('a.uid')
  1022. ->count();
  1023. $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
  1024. $shop_data = [];
  1025. $count > 0 && $shop_data = [
  1026. [
  1027. 'value' => bcdiv($user_count, $count, 2) * 100,
  1028. 'name' => $shop_xdata[0] . $user_count . '人',
  1029. 'itemStyle' => [
  1030. 'color' => '#D789FF',
  1031. ]
  1032. ],
  1033. [
  1034. 'value' => bcdiv($count - $user_count, $count, 2) * 100,
  1035. 'name' => $shop_xdata[1] . ($count - $user_count) . '人',
  1036. 'itemStyle' => [
  1037. 'color' => '#7EF0FB',
  1038. ]
  1039. ]
  1040. ];
  1041. return compact('fenbu_data', 'fenbu_xdata', 'visit_data', 'visit_xdata', 'visit_zoom', 'seriesdata', 'xdata', 'zoom', 'shop_xdata', 'shop_data');
  1042. }
  1043. /*
  1044. * 分销商佣金提现排行榜
  1045. * 分销商佣金总额排行榜
  1046. * $limit 截取条数
  1047. * return array
  1048. */
  1049. public static function getUserDistributionTop10List($limit)
  1050. {
  1051. //分销商佣金提现排行榜
  1052. $extract = self::alias('a')
  1053. ->join('user_extract t', 'a.uid=t.uid')
  1054. ->where(['t.status' => 1, 'a.is_promoter' => 1])
  1055. ->group('t.uid')
  1056. ->field(['a.nickname', 'a.phone', 'sum(t.extract_price) as sum_price', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time', 't.uid'])
  1057. ->order('sum_price desc')
  1058. ->limit($limit)
  1059. ->select();
  1060. count($extract) && $extract = $extract->toArray();
  1061. //分销商佣金总额排行榜
  1062. $commission = UserBill::alias('l')
  1063. ->join('user a', 'l.uid=a.uid')
  1064. ->where(['l.status' => '1', 'l.category' => 'now_money', 'l.type' => 'brokerage', 'a.is_promoter' => 1])
  1065. ->group('l.uid')
  1066. ->field(['a.nickname', 'a.phone', 'sum(number) as sum_number', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time'])
  1067. ->order('sum_number desc')
  1068. ->limit($limit)
  1069. ->select();
  1070. count($commission) && $commission = $commission->toArray();
  1071. return compact('extract', 'commission');
  1072. }
  1073. public static function getSpreadList($uid, $page, $limit)
  1074. {
  1075. $list = self::where(['spread_uid' => $uid])->field(['uid', 'nickname', 'now_money', 'gold_num', 'add_time'])
  1076. ->order('uid desc')->page((int)$page, (int)$limit)->select();
  1077. count($list) && $list = $list->toArray();
  1078. foreach ($list as &$item) {
  1079. $item['add_time'] = date('Y-m-d H', $item['add_time']);
  1080. }
  1081. return $list;
  1082. }
  1083. /**
  1084. * 设置推广人查询条件
  1085. * @param $where
  1086. * @param string $alias
  1087. * @param int $spread_type
  1088. * @param null $model
  1089. * @return $this
  1090. */
  1091. public static function setSpreadBadgeWhere($where, $alias = '', $spread_type = 0, $model = null)
  1092. {
  1093. $model = is_null($model) ? new self() : $model;
  1094. $alias = $alias ? $alias . '.' : '';
  1095. if ($spread_type) $where['spread_type'] = $spread_type;
  1096. if ($where['nickname'] && ($uids = self::where('nickname|phone|uid', 'like', "%$where[nickname]%")->column('uid'))) {
  1097. $model = $model->where($alias . 'spread_uid', 'in', $uids);
  1098. }
  1099. if ($where['phone']) $model = $model->where("{$alias}nickname|{$alias}phone", 'like', "%$where[phone]%");
  1100. if ($where['start_time'] && $where['stop_time']) {
  1101. $model = $model->whereTime("{$alias}add_time", 'between', [$where['start_time'], $where['stop_time']]);
  1102. }
  1103. $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
  1104. if ($storeBrokerageStatu == 1) {
  1105. $model = $model->where("{$alias}is_promoter", 1);
  1106. }
  1107. return $model;
  1108. }
  1109. public static function getextractPrice($uid, $where = [])
  1110. {
  1111. if (is_array($uid)) {
  1112. if (!count($uid)) return 0;
  1113. } else
  1114. $uid = [$uid];
  1115. $brokerage = UserBill::getBrokerage($uid, 'now_money', 'brokerage', $where);//获取总佣金
  1116. $return = UserBill::getReturnBrokerage($uid, 'now_money', 'brokerage_return', $where);//获取返还佣金
  1117. $brokerage=bcsub($brokerage,$return,2);
  1118. $recharge = UserBill::getBrokerage($uid, 'now_money', 'recharge', $where);//累计充值
  1119. $extractTotalPrice = UserExtract::userExtractTotalPrice($uid, 1, $where);//累计提现
  1120. if ($brokerage > $extractTotalPrice) {
  1121. $orderYuePrice = self::getModelTime($where, StoreOrder::where('uid', 'in', $uid)->where(['is_del' => 0, 'paid' => 1]))->sum('pay_price');//余额累计消费
  1122. $systemAdd = UserBill::getBrokerage($uid, 'now_money', 'system_add', $where);//后台添加余额
  1123. $yueCount = bcadd($recharge, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  1124. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
  1125. $brokerage = bcsub($brokerage, $extractTotalPrice, 2);//减去已提现金额
  1126. $extract_price = UserExtract::userExtractTotalPrice($uid, 0, $where);
  1127. $brokerage = $extract_price < $brokerage ? bcsub($brokerage, $extract_price, 2) : 0;//减去审核中的提现金额
  1128. $brokerage = $brokerage > $orderYuePrice ? bcsub($brokerage, $orderYuePrice, 2) : 0;//减掉余额支付
  1129. } else {
  1130. $brokerage = 0;
  1131. }
  1132. $num = (float)bcsub($brokerage, $extractTotalPrice, 2);
  1133. return $num > 0 ? $num : 0;//可提现
  1134. }
  1135. /**
  1136. * @param $where
  1137. * @return array
  1138. */
  1139. public static function getSpreadBadgeList($where)
  1140. {
  1141. $bill = new UserBill();
  1142. return [
  1143. [
  1144. 'name' => '一级佣金',
  1145. 'field' => '元',
  1146. 'count' => self::setSpreadBadgeWhere($where, 'a', 1)->alias('a')->join('__USER_BILL__ u', 'a.uid=u.uid')
  1147. ->where(['u.category' => 'now_money', 'u.type' => 'rake_back_one'])->sum('u.number'),
  1148. 'background_color' => 'layui-bg-blue',
  1149. 'col' => 2
  1150. ],
  1151. [
  1152. 'name' => '一级订单',
  1153. 'field' => '元',
  1154. 'count' => self::setSpreadBadgeWhere($where, 'r', 0, $bill)->join('__USER__ r', 'r.uid=a.uid')->alias('a')->join('__STORE_ORDER__ u', 'a.link_id=u.id')->where('u.paid', 1)->sum('u.pay_price'),
  1155. 'background_color' => 'layui-bg-blue',
  1156. 'col' => 2
  1157. ],
  1158. [
  1159. 'name' => '二级佣金',
  1160. 'field' => '元',
  1161. 'count' => self::setSpreadBadgeWhere($where, 'a')->alias('a')->join('__USER_BILL__ u', 'u.uid=a.uid')
  1162. ->where(['u.category' => 'now_money'])->where('u.type', 'rake_back_two')->sum('u.number'),
  1163. 'background_color' => 'layui-bg-blue',
  1164. 'col' => 2
  1165. ],
  1166. ];
  1167. }
  1168. /**
  1169. * 获取推广人列表
  1170. * @param array $where 查询条件
  1171. * @return array
  1172. * */
  1173. public static function SpreadList($where)
  1174. {
  1175. $model = self::setSpreadBadgeWhere($where)->field('phone,uid,nickname,add_time,spread_uid,is_promoter,is_senior');
  1176. if ($where['export']) $data = $model->select();
  1177. else $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
  1178. $data = count($data) ? $data->toArray() : [];
  1179. foreach ($data as &$item) {
  1180. $item['spread_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname');
  1181. $item['spread_name'] = '普通';
  1182. //直推订单
  1183. $uids = User::where('spread_uid', $item['uid'])->column('uid');
  1184. if (count($uids)) {
  1185. $item['sum_pay_price'] = StoreOrder::whereIn('uid', $uids)->sum('pay_price');
  1186. $ids = User::whereIn('spread_uid', $uids)->column('uid');
  1187. if (count($ids)) {
  1188. $item['pay_price'] = StoreOrder::whereIn('uid', $ids)->sum('pay_price');
  1189. }
  1190. } else {
  1191. $item['sum_pay_price'] = 0;
  1192. $item['pay_price'] = 0;
  1193. }
  1194. unset($ids, $uids);
  1195. $item['rake_back'] =UserBill::getCommissionAmount($item['uid']);
  1196. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  1197. }
  1198. if ($where['export']) self::SaveExcel($data);
  1199. $count = self::setSpreadBadgeWhere($where)->count();
  1200. return compact('data', 'count');
  1201. }
  1202. /*
  1203. * 保存并下载excel
  1204. * $list array
  1205. * return
  1206. */
  1207. public static function SaveExcel($list)
  1208. {
  1209. $export = [];
  1210. foreach ($list as $index => $item) {
  1211. $export[] = [
  1212. $item['spread_name'],
  1213. $item['nickname'],
  1214. $item['spread_nickname'],
  1215. $item['phone'],
  1216. $item['add_time'],
  1217. $item['sum_pay_price'],
  1218. $item['rake_back'],
  1219. ];
  1220. }
  1221. PHPExcelService::setExcelHeader(['推广人身份', '昵称', '所属上级', '手机号码', '加入时间', '订单金额', '佣金'])
  1222. ->setExcelTile('推广人列表导出', '推广人信息' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  1223. ->setExcelContent($export)
  1224. ->ExcelSave();
  1225. }
  1226. public static function guestWhere($where, $guest, $model = null)
  1227. {
  1228. if ($model == null) $model = new self;
  1229. if (isset($where['guest_name']) && $where['guest_name'] != '') $model = $model->where('nickname|uid', 'LIKE', "%$where[guest_name]%");
  1230. $model = $model->where('uid', 'IN', $guest->guest);
  1231. return $model;
  1232. }
  1233. }