User.php 53 KB

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