User.php 53 KB

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