User.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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\SystemStore;
  9. use app\admin\model\system\SystemUserLevel;
  10. use app\models\user\UserStorePromoter;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\basic\BaseModel;
  13. use app\admin\model\wechat\WechatUser;
  14. use app\admin\model\store\StoreCouponUser;
  15. use crmeb\services\PHPExcelService;
  16. /**
  17. * 用户管理 model
  18. * Class User
  19. * @package app\admin\model\user
  20. */
  21. class User extends BaseModel
  22. {
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'uid';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'user';
  33. use ModelTrait;
  34. /*
  35. * 获取和提现金额
  36. * @param array $uid
  37. * @return float
  38. * */
  39. public static function getextractPrice($uid, $where = [])
  40. {
  41. if (is_array($uid)) {
  42. if (!count($uid)) return 0;
  43. } else
  44. $uid = [$uid];
  45. $brokerage = UserBill::getBrokerage($uid, 'now_money', 'brokerage', $where);//获取总佣金
  46. $recharge = UserBill::getBrokerage($uid, 'now_money', 'recharge', $where);//累计充值
  47. $extractTotalPrice = UserExtract::userExtractTotalPrice($uid, 1, $where);//累计提现
  48. if ($brokerage > $extractTotalPrice) {
  49. $orderYuePrice = self::getModelTime($where, StoreOrder::where('uid', 'in', $uid)->where(['is_del' => 0, 'paid' => 1]))->sum('pay_price');//余额累计消费
  50. $systemAdd = UserBill::getBrokerage($uid, 'now_money', 'system_add', $where);//后台添加余额
  51. $yueCount = bcadd($recharge, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
  52. $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
  53. $brokerage = bcsub($brokerage, $extractTotalPrice, 2);//减去已提现金额
  54. $extract_price = UserExtract::userExtractTotalPrice($uid, 0, $where);
  55. $brokerage = $extract_price < $brokerage ? bcsub($brokerage, $extract_price, 2) : 0;//减去审核中的提现金额
  56. $brokerage = $brokerage > $orderYuePrice ? bcsub($brokerage, $orderYuePrice, 2) : 0;//减掉余额支付
  57. } else {
  58. $brokerage = 0;
  59. }
  60. $num = (float)bcsub($brokerage, $extractTotalPrice, 2);
  61. return $num > 0 ? $num : 0;//可提现
  62. }
  63. /**
  64. * @param $where
  65. * @return array
  66. */
  67. public static function systemPage($where)
  68. {
  69. $model = new self;
  70. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  71. if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
  72. if (isset($where['user_type']) && $where['user_type'] != '') $model = $model->where('user_type', $where['user_type']);
  73. if ($where['nickname'] != '') $model = $model->where('nickname|uid', 'like', "%$where[nickname]%");
  74. $model = $model->order('uid desc');
  75. return self::page($model, function ($item) {
  76. if ($item['spread_uid']) {
  77. $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname');
  78. } else {
  79. $item['spread_uid_nickname'] = '无';
  80. }
  81. }, $where);
  82. }
  83. /*
  84. * 设置搜索条件
  85. *
  86. */
  87. public static function setWhere($where)
  88. {
  89. if ($where['order'] != '') {
  90. $model = self::order(self::setOrder($where['order']));
  91. } else {
  92. $model = self::order('u.uid desc');
  93. }
  94. if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
  95. list($startTime, $endTime) = explode(' - ', $where['user_time']);
  96. $endTime = strtotime($endTime) + 24 * 3600;
  97. $model = $model->where("u.last_time < " . strtotime($startTime) . " OR u.last_time > " . $endTime);
  98. }
  99. if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
  100. list($startTime, $endTime) = explode(' - ', $where['user_time']);
  101. $model = $model->where('u.last_time', '>', strtotime($startTime));
  102. $model = $model->where('u.last_time', '<', strtotime($endTime) + 24 * 3600);
  103. }
  104. if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
  105. list($startTime, $endTime) = explode(' - ', $where['user_time']);
  106. $model = $model->where('u.add_time', '>', strtotime($startTime));
  107. $model = $model->where('u.add_time', '<', strtotime($endTime) + 24 * 3600);
  108. }
  109. if ($where['pay_count'] !== '') {
  110. if ($where['pay_count'] == '-1') $model = $model->where('pay_count', 0);
  111. else $model = $model->where('pay_count', '>', $where['pay_count']);
  112. }
  113. if ($where['user_type'] != '') {
  114. if ($where['user_type'] == 'routine') $model = $model->where('w.routine_openid', 'not null');
  115. else if ($where['user_type'] == 'wechat') $model = $model->where('w.openid', 'not null');
  116. else $model = $model->where('u.user_type', $where['user_type']);
  117. }
  118. if ($where['country'] != '') {
  119. if ($where['country'] == 'domestic') $model = $model->where('w.country', '中国');
  120. else if ($where['country'] == 'abroad') $model = $model->where('w.country', '<>', '中国');
  121. }
  122. if ($where['level'] !== '') {
  123. $model = $model->where('level', $where['level'])->where('clean_time', 0);
  124. }
  125. if ($where['group_id'] !== '') {
  126. $model = $model->where('group_id', $where['group_id']);
  127. }
  128. return $model;
  129. }
  130. /**
  131. * 异步获取当前用户 信息
  132. * @param $where
  133. * @return array
  134. */
  135. public static function getUserList($where)
  136. {
  137. $model = self::setWherePage(self::setWhere($where), $where, ['w.sex', 'w.province', 'w.city', 'u.status', 'u.is_promoter'], ['u.nickname', 'u.uid', 'u.phone']);
  138. $list = $model->alias('u')
  139. ->join('WechatUser w', 'u.uid=w.uid')
  140. ->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')
  141. ->page((int)$where['page'], (int)$where['limit'])
  142. ->select()
  143. ->each(function ($item) {
  144. $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  145. if ($item['last_time']) $item['last_time'] = date('Y-m-d H:i:s', $item['last_time']);//最近一次访问日期
  146. else $item['last_time'] = '无访问';//最近一次访问日期
  147. self::edit(['pay_count' => StoreOrder::getUserCountPay($item['uid'])], $item['uid']);
  148. $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现
  149. if ($item['spread_uid']) {
  150. $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname') . '/' . $item['spread_uid'];
  151. } else {
  152. $item['spread_uid_nickname'] = '无';
  153. }
  154. 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['openid'] != '' && $item['routine_openid'] == '') {
  159. $item['user_type'] = '公众号';
  160. } else if ($item['user_type'] == 'h5') {
  161. $item['user_type'] = 'H5';
  162. } else $item['user_type'] = '其他';
  163. if ($item['sex'] == 1) {
  164. $item['sex'] = '男';
  165. } else if ($item['sex'] == 2) {
  166. $item['sex'] = '女';
  167. } else $item['sex'] = '保密';
  168. $item['vip_name'] = false;
  169. $levelinfo = UserLevel::where('uid', $item['uid'])->where('is_del', 0)->order('grade desc')->field('level_id,is_forever,valid_time')->find();
  170. if ($levelinfo) {
  171. if ($levelinfo['is_forever']) $item['vip_name'] = SystemUserLevel::where('id', $levelinfo['level_id'])->value('name');
  172. else if (time() > $levelinfo['valid_time']) $item['vip_name'] = SystemUserLevel::where('id', $levelinfo['level_id'])->value('name');
  173. }
  174. });//->toArray();
  175. $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();
  176. return ['count' => $count, 'data' => $list];
  177. }
  178. /**
  179. * 修改用户状态
  180. * @param $uids 用户uid
  181. * @param $status 修改状态
  182. * @return array
  183. */
  184. public static function destrSyatus($uids, $status)
  185. {
  186. if (empty($uids) && !is_array($uids)) return false;
  187. if ($status == '') return false;
  188. self::beginTrans();
  189. try {
  190. $res = self::where('uid', 'in', $uids)->update(['status' => $status]);
  191. self::checkTrans($res);
  192. return true;
  193. } catch (\Exception $e) {
  194. self::rollbackTrans();
  195. return Json::fail($e->getMessage());
  196. }
  197. }
  198. /*
  199. * 获取某季度,某年某年后的时间戳
  200. *
  201. * self::getMonth('n',1) 获取当前季度的上个季度的时间戳
  202. * self::getMonth('n') 获取当前季度的时间戳
  203. */
  204. public static function getMonth($time = '', $ceil = 0)
  205. {
  206. if (empty($time)) {
  207. $firstday = date("Y-m-01", time());
  208. $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
  209. } else if ($time == 'n') {
  210. if ($ceil != 0)
  211. $season = ceil(date('n') / 3) - $ceil;
  212. else
  213. $season = ceil(date('n') / 3);
  214. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  215. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  216. } else if ($time == 'y') {
  217. $firstday = date('Y-01-01');
  218. $lastday = date('Y-12-31');
  219. } else if ($time == 'h') {
  220. $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
  221. $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
  222. }
  223. return array($firstday, $lastday);
  224. }
  225. public static function getcount()
  226. {
  227. return self::count();
  228. }
  229. /*
  230. *获取用户某个时间段的消费信息
  231. *
  232. * reutrn Array || number
  233. */
  234. public static function consume($where, $status = '', $keep = '')
  235. {
  236. $model = new self;
  237. $user_id = [];
  238. if (is_array($where)) {
  239. if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
  240. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  241. switch ($where['date']) {
  242. case null:
  243. case 'today':
  244. case 'week':
  245. case 'year':
  246. if ($where['date'] == null) {
  247. $where['date'] = 'month';
  248. }
  249. if ($keep) {
  250. $model = $model->whereTime('add_time', $where['date'])->whereTime('last_time', $where['date']);
  251. } else {
  252. $model = $model->whereTime('add_time', $where['date']);
  253. }
  254. break;
  255. case 'quarter':
  256. $quarter = self::getMonth('n');
  257. $startTime = strtotime($quarter[0]);
  258. $endTime = strtotime($quarter[1]);
  259. if ($keep) {
  260. $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime)->where('last_time', '>', $startTime)->where('last_time', '<', $endTime);
  261. } else {
  262. $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime);
  263. }
  264. break;
  265. default:
  266. //自定义时间
  267. if (strstr($where['date'], '-') !== FALSE) {
  268. list($startTime, $endTime) = explode('-', $where['date']);
  269. $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', bcadd(strtotime($endTime), 86400, 0));
  270. } else {
  271. $model = $model->whereTime('add_time', 'month');
  272. }
  273. break;
  274. }
  275. } else {
  276. if (is_array($status)) {
  277. $model = $model->where('add_time', '>', $status[0])->where('add_time', '<', $status[1]);
  278. }
  279. }
  280. if ($keep === true) {
  281. return $model->count();
  282. }
  283. if ($status === 'default') {
  284. 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();
  285. }
  286. if ($status === 'grouping') {
  287. return $model->group('user_type')->field('user_type')->select()->toArray();
  288. }
  289. $uid = $model->field('uid')->select()->toArray();
  290. foreach ($uid as $val) {
  291. $user_id[] = $val['uid'];
  292. }
  293. if (empty($user_id)) {
  294. $user_id = [0];
  295. }
  296. if ($status === 'xiaofei') {
  297. $list = UserBill::where('uid', 'in', $user_id)
  298. ->group('type')
  299. ->field('sum(number) as top_number,title')
  300. ->select()
  301. ->toArray();
  302. $series = [
  303. 'name' => isset($list[0]['title']) ? $list[0]['title'] : '',
  304. 'type' => 'pie',
  305. 'radius' => ['40%', '50%'],
  306. 'data' => []
  307. ];
  308. foreach ($list as $key => $val) {
  309. $series['data'][$key]['value'] = $val['top_number'];
  310. $series['data'][$key]['name'] = $val['title'];
  311. }
  312. return $series;
  313. } else if ($status === 'form') {
  314. $list = WechatUser::where('uid', 'in', $user_id)->group('city')->field('count(city) as top_city,city')->limit(0, 10)->select()->toArray();
  315. $count = self::getcount();
  316. $option = [
  317. 'legend_date' => [],
  318. 'series_date' => []
  319. ];
  320. foreach ($list as $key => $val) {
  321. $num = $count != 0 ? (bcdiv($val['top_city'], $count, 2)) * 100 : 0;
  322. $t = ['name' => $num . '% ' . (empty($val['city']) ? '未知' : $val['city']), 'icon' => 'circle'];
  323. $option['legend_date'][$key] = $t;
  324. $option['series_date'][$key] = ['value' => $num, 'name' => $t['name']];
  325. }
  326. return $option;
  327. } else {
  328. $number = UserBill::where('uid', 'in', $user_id)->where('type', 'pay_product')->sum('number');
  329. return $number;
  330. }
  331. }
  332. /*
  333. * 获取 用户某个时间段的钱数或者TOP20排行
  334. *
  335. * return Array || number
  336. */
  337. public static function getUserSpend($date, $status = '')
  338. {
  339. $model = new self();
  340. $model = $model->alias('A');
  341. switch ($date) {
  342. case null:
  343. case 'today':
  344. case 'week':
  345. case 'year':
  346. if ($date == null) $date = 'month';
  347. $model = $model->whereTime('A.add_time', $date);
  348. break;
  349. case 'quarter':
  350. list($startTime, $endTime) = User::getMonth('n');
  351. $model = $model->where('A.add_time', '>', strtotime($startTime));
  352. $model = $model->where('A.add_time', '<', bcadd(strtotime($endTime), 86400, 0));
  353. break;
  354. default:
  355. list($startTime, $endTime) = explode('-', $date);
  356. $model = $model->where('A.add_time', '>', strtotime($startTime));
  357. $model = $model->where('A.add_time', '<', bcadd(strtotime($endTime), 86400, 0));
  358. break;
  359. }
  360. if ($status === true) {
  361. return $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
  362. }
  363. $list = $model->join('user_bill B', 'B.uid=A.uid')
  364. ->where('B.type', 'pay_product')
  365. ->where('B.pm', 0)
  366. ->field('sum(B.number) as totel_number,A.nickname,A.avatar,A.now_money,A.uid,A.add_time')
  367. ->order('totel_number desc')
  368. ->limit(0, 20)
  369. ->select()
  370. ->toArray();
  371. if (!isset($list[0]['totel_number'])) {
  372. $list = [];
  373. }
  374. return $list;
  375. }
  376. /*
  377. * 获取 相对于上月或者其他的数据
  378. *
  379. * return Array
  380. */
  381. public static function getPostNumber($date, $status = false, $field = 'A.add_time', $t = '消费')
  382. {
  383. $model = new self();
  384. if (!$status) $model = $model->alias('A');
  385. switch ($date) {
  386. case null:
  387. case 'today':
  388. case 'week':
  389. case 'year':
  390. if ($date == null) {
  391. $date = 'last month';
  392. $title = '相比上月用户' . $t . '增长';
  393. }
  394. if ($date == 'today') {
  395. $date = 'yesterday';
  396. $title = '相比昨天用户' . $t . '增长';
  397. }
  398. if ($date == 'week') {
  399. $date = 'last week';
  400. $title = '相比上周用户' . $t . '增长';
  401. }
  402. if ($date == 'year') {
  403. $date = 'last year';
  404. $title = '相比去年用户' . $t . '增长';
  405. }
  406. $model = $model->whereTime($field, $date);
  407. break;
  408. case 'quarter':
  409. $title = '相比上季度用户' . $t . '增长';
  410. list($startTime, $endTime) = User::getMonth('n', 1);
  411. $model = $model->where($field, '>', $startTime);
  412. $model = $model->where($field, '<', $endTime);
  413. break;
  414. default:
  415. list($startTime, $endTime) = explode('-', $date);
  416. $title = '相比' . $startTime . '-' . $endTime . '时间段用户' . $t . '增长';
  417. $Time = strtotime($endTime) - strtotime($startTime);
  418. $model = $model->where($field, '>', strtotime($startTime) + $Time);
  419. $model = $model->where($field, '<', strtotime($endTime) + $Time);
  420. break;
  421. }
  422. if ($status) {
  423. return [$model->count(), $title];
  424. }
  425. $number = $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
  426. return [$number, $title];
  427. }
  428. //获取用户新增,头部信息
  429. public static function getBadgeList($where)
  430. {
  431. $user_count = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])->count();
  432. $user_count_old = self::getOldDate($where)->count();
  433. $store_brokerage_statu = sys_config('store_brokerage_statu');
  434. if ($store_brokerage_statu == 1)
  435. $fenxiao = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])->where('spread_uid', '<>', 0)->count();
  436. else
  437. $fenxiao = self::count();
  438. $fenxiao_count = self::getOldDate($where)->where('spread_uid', '<>', 0)->count();
  439. $newFemxiao_count = bcsub($fenxiao, $fenxiao_count, 0);
  440. $order_count = bcsub($user_count, $user_count_old, 0);
  441. return [
  442. [
  443. 'name' => '会员人数',
  444. 'field' => '个',
  445. 'count' => $user_count,
  446. 'content' => '会员总人数',
  447. 'background_color' => 'layui-bg-blue',
  448. 'sum' => self::count(),
  449. 'class' => 'fa fa-bar-chart',
  450. ],
  451. [
  452. 'name' => '会员增长',
  453. 'field' => '个',
  454. 'count' => $order_count,
  455. 'content' => '会员增长率',
  456. 'background_color' => 'layui-bg-cyan',
  457. 'sum' => $user_count_old ? bcdiv($order_count, $user_count_old, 2) * 100 : 0,
  458. 'class' => 'fa fa-line-chart',
  459. ],
  460. [
  461. 'name' => '分销人数',
  462. 'field' => '个',
  463. 'count' => $fenxiao,
  464. 'content' => '分销总人数',
  465. 'background_color' => 'layui-bg-green',
  466. 'sum' => $store_brokerage_statu == 1 ? self::where('spread_uid', '<>', 0)->count() : $fenxiao,
  467. 'class' => 'fa fa-bar-chart',
  468. ],
  469. [
  470. 'name' => '分销增长',
  471. 'field' => '个',
  472. 'count' => $newFemxiao_count,
  473. 'content' => '分销总人数',
  474. 'background_color' => 'layui-bg-orange',
  475. 'sum' => $fenxiao_count ? bcdiv($newFemxiao_count, $fenxiao_count, 2) * 100 : 0,
  476. 'class' => 'fa fa-cube',
  477. ],
  478. ];
  479. }
  480. /*
  481. * 获取会员增长曲线图和分布图
  482. * $where 查询条件
  483. * $limit 显示条数,是否有滚动条
  484. */
  485. public static function getUserChartList($where, $limit = 20)
  486. {
  487. $list = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])
  488. ->where('add_time', '<>', 0)
  489. ->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time,count(uid) as num')
  490. ->order('_add_time asc')
  491. ->group('_add_time')
  492. ->select();
  493. count($list) && $list = $list->toArray();
  494. $seriesdata = [];
  495. $xdata = [];
  496. $Zoom = '';
  497. foreach ($list as $item) {
  498. $seriesdata[] = $item['num'];
  499. $xdata[] = $item['_add_time'];
  500. }
  501. (count($xdata) > $limit) && $Zoom = $xdata[$limit - 5];
  502. //多次购物会员数量饼状图
  503. $count = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter'])->count();
  504. $user_count = self::setWherePage(self::getModelTime($where, self::alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time'), $where, ['is_promoter'])
  505. ->where('r.paid', 1)->count('a.uid');
  506. $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
  507. $shop_data = [];
  508. $count > 0 && $shop_data = [
  509. [
  510. 'value' => bcdiv($user_count, $count, 2) * 100,
  511. 'name' => $shop_xdata[0],
  512. 'itemStyle' => [
  513. 'color' => '#D789FF',
  514. ]
  515. ],
  516. [
  517. 'value' => bcdiv($count - $user_count, $count, 2) * 100,
  518. 'name' => $shop_xdata[1],
  519. 'itemStyle' => [
  520. 'color' => '#7EF0FB',
  521. ]
  522. ]
  523. ];
  524. return compact('shop_data', 'shop_xdata', 'seriesdata', 'Zoom');
  525. }
  526. //获取$date的前一天或者其他的时间段
  527. public static function getOldDate($where, $moedls = null)
  528. {
  529. $model = $moedls === null ? self::setWherePage(new self(), $where, ['is_promoter', 'status']) : $moedls;
  530. switch ($where['data']) {
  531. case 'today':
  532. $model = $model->whereTime('add_time', 'yesterday');
  533. break;
  534. case 'week':
  535. $model = $model->whereTime('add_time', 'last week');
  536. break;
  537. case 'month':
  538. $model = $model->whereTime('add_time', 'last month');
  539. break;
  540. case 'year':
  541. $model = $model->whereTime('add_time', 'last year');
  542. break;
  543. case 'quarter':
  544. $time = self::getMonth('n', 1);
  545. $model = $model->where('add_time', 'between', $time);
  546. break;
  547. }
  548. return $model;
  549. }
  550. //获取用户属性和性别分布图
  551. public static function getEchartsData($where)
  552. {
  553. $model = self::alias('a');
  554. $data = self::getModelTime($where, $model, 'a.add_time')
  555. ->join('wechat_user r', 'r.uid=a.uid')
  556. ->group('r.province')
  557. ->field('count(r.province) as count,province')
  558. ->order('count desc')
  559. ->limit(15)
  560. ->select();
  561. if (count($data)) $data = $data->toArray();
  562. $legdata = [];
  563. $dataList = [];
  564. foreach ($data as $value) {
  565. $value['province'] == '' && $value['province'] = '未知省份';
  566. $legdata[] = $value['province'];
  567. $dataList[] = $value['count'];
  568. }
  569. $model = self::alias('a');
  570. $sex = self::getModelTime($where, $model, 'a.add_time')
  571. ->join('wechat_user r', 'r.uid=a.uid')
  572. ->group('r.sex')
  573. ->field('count(r.uid) as count,sex')
  574. ->order('count desc')
  575. ->select();
  576. if (count($sex)) $sex = $sex->toArray();
  577. $sexlegdata = ['男', '女', '未知'];
  578. $sexcount = self::getModelTime($where, new self())->count();
  579. $sexList = [];
  580. $color = ['#FB7773', '#81BCFE', '#91F3FE'];
  581. foreach ($sex as $key => $item) {
  582. if ($item['sex'] == 1) {
  583. $item_date['name'] = '男';
  584. } else if ($item['sex'] == 2) {
  585. $item_date['name'] = '女';
  586. } else {
  587. $item_date['name'] = '未知性别';
  588. }
  589. $item_date['value'] = bcdiv($item['count'], $sexcount, 2) * 100;
  590. $item_date['itemStyle']['color'] = $color[$key];
  591. $sexList[] = $item_date;
  592. }
  593. return compact('sexList', 'sexlegdata', 'legdata', 'dataList');
  594. }
  595. //获取佣金记录列表
  596. public static function getCommissionList($where)
  597. {
  598. $model = self::setCommissionWhere($where);
  599. if ($where['excel'])
  600. $list = $model->select();
  601. else
  602. $list = $model->page((int)$where['page'], (int)$where['limit'])->select();
  603. count($list) && $list = $list->toArray();
  604. $export = [];
  605. foreach ($list as &$value) {
  606. $value['ex_price'] = UserExtract::where('uid', $value['uid'])->sum('extract_price');
  607. $value['extract_price'] = UserExtract::where('uid', $value['uid'])->where('status', 1)->sum('extract_price');
  608. $cashPrice = UserExtract::where('uid', $value['uid'])->where('status', 0)->sum('extract_price');
  609. $value['money'] = bcsub($value['ex_price'], $value['extract_price'], 2);
  610. $value['money'] = bcsub($value['money'], $cashPrice, 2);
  611. $export[] = [
  612. $value['nickname'],
  613. $value['sum_number'],
  614. $value['now_money'],
  615. $value['brokerage_price'],
  616. $value['extract_price'],
  617. ];
  618. }
  619. if ($where['excel']) {
  620. PHPExcelService::setExcelHeader(['昵称/姓名', '总佣金金额', '账户余额', '账户佣金', '提现到账佣金'])
  621. ->setExcelTile('拥金记录', '拥金记录' . time(), ' 生成时间:' . date('Y-m-d H:i:s', time()))
  622. ->setExcelContent($export)
  623. ->ExcelSave();
  624. }
  625. $count = self::setCommissionWhere($where)->count();
  626. return ['data' => $list, 'count' => $count];
  627. }
  628. //获取佣金记录列表的查询条件
  629. public static function setCommissionWhere($where)
  630. {
  631. $models = self::setWherePage(self::alias('A'), $where, [], ['A.nickname', 'A.uid'])
  632. ->join('user_bill B', 'B.uid=A.uid')
  633. ->group('A.uid')
  634. ->where('B.type', 'brokerage')
  635. ->where('B.category', 'now_money')
  636. ->field('sum(B.number) as sum_number,A.nickname,A.uid,A.now_money,A.brokerage_price');
  637. if ($where['order'] == '') {
  638. $models = $models->order('sum_number desc');
  639. } else {
  640. $models = $models->order($where['order'] == 1 ? 'sum_number desc' : 'sum_number asc');
  641. }
  642. if ($where['price_max'] != '' && $where['price_min'] != '') {
  643. $models = $models->where('now_money', 'between', [$where['price_max'], $where['price_min']]);
  644. }
  645. return $models;
  646. }
  647. /**获取用户详细信息
  648. * @param $uid
  649. * @return array
  650. */
  651. public static function getUserInfos($uid)
  652. {
  653. $userInfo = self::where('uid', $uid)->find();
  654. if (!$userInfo) exception('读取用户信息失败!');
  655. return $userInfo->toArray();
  656. }
  657. //获取某人用户推广信息
  658. public static function getUserinfo($uid)
  659. {
  660. $userinfo = self::where('uid', $uid)->field('nickname,spread_uid,now_money,add_time')->find()->toArray();
  661. $userinfo['number'] = (float)UserBill::where('category', 'now_money')->where('uid', $uid)->where('type', 'brokerage')->sum('number');
  662. $userinfo['spread_name'] = $userinfo['spread_uid'] ? self::where('uid', $userinfo['spread_uid'])->value('nickname') : '';
  663. return $userinfo;
  664. }
  665. //获取某用户的详细信息
  666. public static function getUserDetailed($uid)
  667. {
  668. $key_field = ['real_name', 'phone', 'province', 'city', 'district', 'detail', 'post_code'];
  669. $Address = ($thisAddress = UserAddress::where('uid', $uid)->where('is_default', 1)->field($key_field)->find()) ?
  670. $thisAddress :
  671. UserAddress::where('uid', $uid)->field($key_field)->find();
  672. $UserInfo = self::get($uid);
  673. return [
  674. ['col' => 12, 'name' => '默认收货地址', 'value' => $thisAddress ? '收货人:' . $thisAddress['real_name'] . '邮编:' . $thisAddress['post_code'] . ' 收货人电话:' . $thisAddress['phone'] . ' 地址:' . $thisAddress['province'] . ' ' . $thisAddress['city'] . ' ' . $thisAddress['district'] . ' ' . $thisAddress['detail'] : ''],
  675. // ['name'=>'微信OpenID','value'=>WechatUser::where('uid', $uid)->value('openid'),'col'=>8],
  676. ['name' => '手机号码', 'value' => $UserInfo['phone']],
  677. // ['name'=>'ID','value'=>$uid],
  678. ['name' => '姓名', 'value' => ''],
  679. ['name' => '微信昵称', 'value' => $UserInfo['nickname']],
  680. ['name' => '邮箱', 'value' => ''],
  681. ['name' => '生日', 'value' => ''],
  682. ['name' => '积分', 'value' => $UserInfo['integral']],
  683. ['name' => '上级推广人', 'value' => $UserInfo['spread_uid'] ? self::where('uid', $UserInfo['spread_uid'])->value('nickname') : ''],
  684. ['name' => '账户余额', 'value' => $UserInfo['now_money']],
  685. ['name' => '佣金总收入', 'value' => UserBill::where('category', 'now_money')->where('type', 'brokerage')->where('uid', $uid)->sum('number')],
  686. ['name' => '提现总金额', 'value' => UserExtract::where('uid', $uid)->where('status', 1)->sum('extract_price')],
  687. ];
  688. }
  689. //获取某用户的订单个数,消费明细
  690. public static function getHeaderList($uid)
  691. {
  692. return [
  693. [
  694. 'title' => '总计订单',
  695. 'value' => StoreOrder::where('uid', $uid)->count(),
  696. 'key' => '笔',
  697. 'class' => '',
  698. ],
  699. [
  700. 'title' => '总消费金额',
  701. 'value' => StoreOrder::where('uid', $uid)->where('paid', 1)->sum('total_price'),
  702. 'key' => '元',
  703. 'class' => '',
  704. ],
  705. [
  706. 'title' => '本月订单',
  707. 'value' => StoreOrder::where('uid', $uid)->whereTime('add_time', 'month')->count(),
  708. 'key' => '笔',
  709. 'class' => '',
  710. ],
  711. [
  712. 'title' => '本月消费金额',
  713. 'value' => StoreOrder::where('uid', $uid)->where('paid', 1)->whereTime('add_time', 'month')->sum('total_price'),
  714. 'key' => '元',
  715. 'class' => '',
  716. ]
  717. ];
  718. }
  719. /*
  720. * 获取 会员 订单个数,积分明细,优惠劵明细
  721. *
  722. * $uid 用户id;
  723. *
  724. * return array
  725. */
  726. public static function getCountInfo($uid)
  727. {
  728. $order_count = StoreOrder::where('uid', $uid)->count();
  729. $integral_count = UserBill::where('uid', $uid)->where('category', 'integral')->where('type', 'in', 'deduction,system_add')->count();
  730. $sign_count = UserBill::where('type', 'sign')->where('uid', $uid)->where('category', 'integral')->count();
  731. $balanceChang_count = UserBill::where('category', 'now_money')->where('uid', $uid)
  732. ->where('type', 'in', 'system_add,pay_product,extract,pay_product_refund,system_sub')
  733. ->count();
  734. $coupon_count = StoreCouponUser::where('uid', $uid)->count();
  735. $spread_count = self::where('spread_uid', $uid)->count();
  736. $spread_store_count = UserStorePromoter::where('uid', $uid)->count();
  737. return compact('order_count', 'integral_count', 'sign_count', 'balanceChang_count', 'coupon_count', 'spread_count', 'spread_store_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. public static function getSpreadStoreList($uid, $page, $limit)
  1137. {
  1138. $list = UserStorePromoter::where('uid', $uid)->page($page, $limit)->select();
  1139. count($list) && $list = $list->toArray();
  1140. foreach ($list as &$item) {
  1141. $item['store'] = SystemStore::where('id', $item['store_id'])->value('name');
  1142. }
  1143. return $list;
  1144. }
  1145. /**
  1146. * 一级分销
  1147. * @param $orderInfo
  1148. * @return bool
  1149. */
  1150. public static function backOrderBrokerage($orderInfo)
  1151. {
  1152. $userInfo = User::getUserInfo($orderInfo['uid']);
  1153. if (!$userInfo || !$userInfo['spread_uid']) return true;
  1154. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  1155. if ($storeBrokerageStatu == 1) {
  1156. if (!User::be(['uid' => $userInfo['spread_uid'], 'is_promoter' => 1])) return true;
  1157. }
  1158. $brokerageRatio = (sys_config('store_brokerage_ratio') ?: 0) / 100;
  1159. if ($brokerageRatio <= 0) return true;
  1160. $cost = isset($orderInfo['cost']) ? $orderInfo['cost'] : 0;//成本价
  1161. if ($cost > $orderInfo['pay_price']) return true;//成本价大于支付价格时直接返回
  1162. $brokeragePrice = bcmul(bcsub($orderInfo['pay_price'], $cost, 2), $brokerageRatio, 2);
  1163. //返佣之后余额
  1164. $spNow_money = User::where('uid', $userInfo['spread_uid'])->value('now_money');
  1165. $balance = bcadd($spNow_money, $brokeragePrice, 2);
  1166. if ($brokeragePrice <= 0) return true;
  1167. $mark = $userInfo['nickname'] . '成功消费' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokeragePrice);
  1168. self::beginTrans();
  1169. $res1 = UserBill::income('获得推广佣金', $userInfo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $balance, $mark);
  1170. $res2 = self::bcInc($userInfo['spread_uid'], 'now_money', $brokeragePrice, 'uid');
  1171. $res = $res1 && $res2;
  1172. self::checkTrans($res);
  1173. if ($res) self::backOrderBrokerageTwo($orderInfo);
  1174. return $res;
  1175. }
  1176. /**
  1177. * 二级推广
  1178. * @param $orderInfo
  1179. * @return bool
  1180. */
  1181. public static function backOrderBrokerageTwo($orderInfo)
  1182. {
  1183. $userInfo = User::getUserInfo($orderInfo['uid']);
  1184. $userInfoTwo = User::getUserInfo($userInfo['spread_uid']);
  1185. if (!$userInfoTwo || !$userInfoTwo['spread_uid']) return true;
  1186. $storeBrokerageStatu = sys_config('store_brokerage_statu') ?: 1;//获取后台分销类型
  1187. if ($storeBrokerageStatu == 1) {
  1188. if (!User::be(['uid' => $userInfoTwo['spread_uid'], 'is_promoter' => 1])) return true;
  1189. }
  1190. $brokerageRatio = (sys_config('store_brokerage_two') ?: 0) / 100;
  1191. if ($brokerageRatio <= 0) return true;
  1192. $cost = isset($orderInfo['cost']) ? $orderInfo['cost'] : 0;//成本价
  1193. if ($cost > $orderInfo['pay_price']) return true;//成本价大于支付价格时直接返回
  1194. $brokeragePrice = bcmul(bcsub($orderInfo['pay_price'], $cost, 2), $brokerageRatio, 2);
  1195. //返佣之后余额
  1196. $spNow_money = User::where('uid', $userInfo['spread_uid'])->value('now_money');
  1197. $balance = bcadd($spNow_money, $brokeragePrice, 2);
  1198. if ($brokeragePrice <= 0) return true;
  1199. $mark = '二级推广人' . $userInfo['nickname'] . '成功消费' . floatval($orderInfo['pay_price']) . '元,奖励推广佣金' . floatval($brokeragePrice);
  1200. self::beginTrans();
  1201. $res1 = UserBill::income('获得推广佣金', $userInfoTwo['spread_uid'], 'now_money', 'brokerage', $brokeragePrice, $orderInfo['id'], $balance, $mark);
  1202. $res2 = self::bcInc($userInfoTwo['spread_uid'], 'now_money', $brokeragePrice, 'uid');
  1203. $res = $res1 && $res2;
  1204. self::checkTrans($res);
  1205. return $res;
  1206. }
  1207. }