User.php 60 KB

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