User.php 61 KB

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