User.php 61 KB

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