User.php 56 KB

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