12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\admin\model\user;
- use app\admin\model\order\StoreOrder;
- use app\admin\model\user\UserExtract;
- use traits\ModelTrait;
- use app\wap\model\user\UserBill;
- use basic\ModelBasic;
- use app\admin\model\wechat\WechatUser;
- use service\SystemConfigService;
- use app\admin\model\order\StoreOrderStatus;
- use app\admin\model\system\SystemMenus;
- use app\admin\model\special\SpecialBuy;
- use app\admin\model\download\DataDownloadBuy;
- use service\PhpSpreadsheetService;
- use app\admin\model\questions\TestPaperObtain;
- use app\wap\model\special\SpecialSource;
- use app\wap\model\special\SpecialRelation;
- use app\admin\model\questions\Relation;
- /**
- * 用户管理 model
- * Class User
- * @package app\admin\model\user
- */
- class User extends ModelBasic
- {
- use ModelTrait;
- /**
- * @param $where
- * @return array
- */
- public static function systemPage($where)
- {
- $model = new self;
- if ($where['status'] != '') $model = $model->where('status', $where['status']);
- if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
- if ($where['nickname'] != '') $model = $model->where('nickname|uid', 'like', "%$where[nickname]%");
- if (isset($where['uids']) && count($where['uids'])) $model->where('uid', 'in', $where['uids']);
- $model = $model->order('uid desc');
- return self::page($model, function ($item) {
- if ($item['spread_uid']) {
- $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname');
- } else {
- $item['spread_uid_nickname'] = '无';
- }
- }, $where);
- }
- public static function getSpreadUidTwo($uid)
- {
- if (is_array($uid)) $spread_uid = self::where('spread_uid', 'in', $uid)->column('uid');
- else $spread_uid = self::where('spread_uid', $uid)->column('uid');
- return self::where('spread_uid', 'in', $spread_uid)->column('uid');
- }
- /*
- * 设置搜索条件
- *
- */
- public static function setTeacherWhere($where)
- {
- if (isset($where['order']) && $where['order'] != '') {
- $model = self::order(self::setOrder($where['order']));
- } else {
- $model = self::order('uid desc');
- }
- if (isset($where['nickname']) && $where['nickname'] != '') {
- $model = $model->where('nickname|uid|phone', 'LIKE', "%$where[nickname]%");
- }
- if (isset($where['identitys']) && $where['identitys'] != '') {
- if ($where['identitys'] == 0) $identitys = [0];
- else if ($where['identitys'] == 1) $identitys = [0, 1];
- else $identitys = [0, 1, 2];
- $model = $model->where('identitys', 'in', $identitys);
- }
- return $model;
- }
- /**
- * 异步获取当前用户 信息
- * @param $where
- * @return array
- */
- public static function add_teacher_user_list($where)
- {
- $model = self::setTeacherWhere($where);
- $list = $model->page((int)$where['page'], (int)$where['limit'])->select();
- $count = self::setTeacherWhere($where)->count();
- return ['count' => $count, 'data' => $list];
- }
- /*
- * 设置搜索条件
- *
- */
- public static function setWhere($where)
- {
- if ($where['order'] != '') {
- $model = self::order(self::setOrder($where['order']));
- } else {
- $model = self::order('u.uid desc');
- }
- if ($where['nickname'] != '') {
- $model = $model->where('u.nickname|u.uid|u.phone|u.realname|u.comment', 'LIKE', "%$where[nickname]%");
- }
- if ($where['status'] != '') {
- $model = $model->where('u.status', $where['status']);
- }
- if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
- list($startTime, $endTime) = explode(' - ', $where['user_time']);
- $model = $model->where('u.last_time', ['>', strtotime($endTime) + 24 * 3600], ['<', strtotime($startTime)], 'or');
- }
- if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
- list($startTime, $endTime) = explode(' - ', $where['user_time']);
- $model = $model->where('u.last_time', '>', strtotime($startTime));
- $model = $model->where('u.last_time', '<', strtotime($endTime) + 24 * 3600);
- }
- if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
- list($startTime, $endTime) = explode(' - ', $where['user_time']);
- $model = $model->where('u.add_time', '>', strtotime($startTime));
- $model = $model->where('u.add_time', '<', strtotime($endTime) + 24 * 3600);
- }
- if ($where['pay_count'] !== '') {
- if ($where['pay_count'] == '-1') $model = $model->where('u.pay_count', 0);
- else $model = $model->where('u.pay_count', '>', $where['pay_count']);
- }
- switch ($where['is_promoter']) {
- case '1':
- $model = $model->where('u.is_promoter', 1);
- break;
- case '2':
- $model = $model->where('u.level', 1);
- break;
- }
- return $model;
- }
- /**
- * 异步获取当前用户 信息
- * @param $where
- * @return array
- */
- public static function getUserList($where)
- {
- $model = self::setWhere($where);
- $list = $model->alias('u')->field('u.*')->page((int)$where['page'], (int)$where['limit'])->select();
- $list = count($list) > 0 ? $list->toArray() : [];
- $isPhoneMenu = is_phone_menu();
- foreach ($list as $k => &$item) {
- if ($item['spread_uid']) {
- $nickname = self::where('uid', $item['spread_uid'])->value('nickname');
- if (check_phone($nickname) && !$isPhoneMenu) {
- $nickname = substr_replace($nickname, '****', 3, 4);
- }
- $item['spread_uid_nickname'] = $nickname . '/' . $item['spread_uid'];
- } else {
- $item['spread_uid_nickname'] = '无';
- }
- if ($item['level']) {
- $item['levelType'] = '会员';
- } else {
- $item['levelType'] = '非会员';
- }
- if ($item['is_h5user'] == 1) {
- $item['user_type'] = 'H5';
- } else if ($item['is_h5user'] == 2) {
- $item['user_type'] = 'PC';
- } else if (!$item['is_h5user']) {
- $item['user_type'] = '公众号';
- }
- if ($item['valid_time']) $item['_valid_time'] = date('Y-m-d H:i:s', $item['valid_time']);
- if (!$isPhoneMenu && $item['phone']) {
- $item['phone'] = substr_replace($item['phone'], '****', 3, 4);
- }
- if (check_phone($item['nickname']) && !$isPhoneMenu) {
- $item['nickname'] = substr_replace($item['nickname'], '****', 3, 4);
- }
- $item['xuefen'] = 0;
- $model = SpecialBuy::alias('sb')->where('sb.uid', $item['uid'])->page(1, 10000);
- $model = $model->join('Special s', 's.id = sb.special_id');
- $model = $model->join('specialSubject ss', 's.subject_id = ss.id');
- $orderList = $model->order('sb.add_time DESC')->field('s.*, sb.add_time as log_time, ss.name as subject_name')->select();
- $orderList = count($orderList) > 0 ? $orderList->toArray() : [];
- $orderList1 = [];
- foreach ($orderList as &$spone) {
- $flag = 1;
- if (!isset($spone['money'])) $spone['money'] = 0;
- $specialSourceId = SpecialSource::alias('ss')
- ->where('ss.special_id', $spone['id'])
- ->field('ss.*, sw.viewing_time, sw.percentage')
- ->join('specialWatch sw', 'ss.special_id = sw.special_id and sw.uid = ' . $item['uid'], 'left')
- ->select();
- foreach ($specialSourceId as $v) {
- if ($v['percentage'] < 96) {
- $flag = 0;
- break;
- }
- }
- if ($flag == 1){
- //关联试题
- //判断是否有考试
- $join1 = [
- ['ExaminationRecord er', 'er.test_id = r.relation_id and er.uid = ' . $item['uid'], 'left'],
- ];
- $examlist = Relation::alias('r')
- ->where('r.relationship_id', $spone['id'])
- ->where('r.relationship', 2)
- ->field('r.relation_id as test_id, er.score')->join($join1)->select();
- if ($examlist) {
- foreach ($examlist as $v) {
- if ($v['score'] < 60) {
- $flag = 0;
- break;
- }
- }
- }
- }
- if ($flag == 1) {
- $item['xuefen'] += $spone['xuefen'];
- }
-
- }
- }
- $count = self::setWhere($where)->alias('u')->count();
- return ['count' => $count, 'data' => $list];
- }
- /**
- * 异步获取当前用户 信息
- * @param $where
- * @return array
- */
- public static function add_get_user_list($where)
- {
- $model = self::setWhere($where);
- $list = $model->alias('u')->field('u.*')->page((int)$where['page'], (int)$where['limit'])->select();
- $count = self::setWhere($where)->alias('u')->count();
- return ['count' => $count, 'data' => $list];
- }
- /**
- * 修改用户状态
- * @param $uids 用户uid
- * @param $status 修改状态
- * @return array
- */
- public static function destrSyatus($uids, $status)
- {
- if (empty($uids) && !is_array($uids)) return false;
- if ($status == '') return false;
- self::beginTrans();
- try {
- $res = self::where('uid', 'in', $uids)->update(['status' => $status]);
- self::checkTrans($res);
- return true;
- } catch (\Exception $e) {
- self::rollbackTrans();
- return Json::fail($e->getMessage());
- }
- }
- /*
- * 获取某季度,某年某年后的时间戳
- *
- * self::getMonth('n',1) 获取当前季度的上个季度的时间戳
- * self::getMonth('n') 获取当前季度的时间戳
- */
- public static function getMonth($time = '', $ceil = 0)
- {
- if (empty($time)) {
- $firstday = date("Y-m-01", time());
- $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
- } else if ($time == 'n') {
- if ($ceil != 0)
- $season = ceil(date('n') / 3) - $ceil;
- else
- $season = ceil(date('n') / 3);
- $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
- $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
- } else if ($time == 'y') {
- $firstday = date('Y-01-01');
- $lastday = date('Y-12-31');
- } else if ($time == 'h') {
- $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
- $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
- }
- return array($firstday, $lastday);
- }
- public static function getcount()
- {
- return self::alias('a')->join('wechat_user u', 'u.uid=a.uid', 'left')->count();
- }
- /*
- *获取用户某个时间段的消费信息
- *
- * reutrn Array || number
- */
- public static function consume($where, $status = '', $keep = '')
- {
- $model = new self;
- $user_id = [];
- if (is_array($where)) {
- if ($where['is_promoter'] != '') $model = $model->where('is_promoter', $where['is_promoter']);
- if ($where['status'] != '') $model = $model->where('status', $where['status']);
- switch ($where['date']) {
- case null:
- case 'today':
- case 'week':
- case 'year':
- if ($where['date'] == null) {
- $where['date'] = 'month';
- }
- if ($keep) {
- $model = $model->whereTime('add_time', $where['date'])->whereTime('last_time', $where['date']);
- } else {
- $model = $model->whereTime('add_time', $where['date']);
- }
- break;
- case 'quarter':
- $quarter = self::getMonth('n');
- $startTime = strtotime($quarter[0]);
- $endTime = strtotime($quarter[1]);
- if ($keep) {
- $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime)->where('last_time', '>', $startTime)->where('last_time', '<', $endTime);
- } else {
- $model = $model->where('add_time', '>', $startTime)->where('add_time', '<', $endTime);
- }
- break;
- default:
- //自定义时间
- if (strstr($where['date'], '-') !== FALSE) {
- list($startTime, $endTime) = explode('-', $where['date']);
- $model = $model->where('add_time', '>', strtotime($startTime))->where('add_time', '<', strtotime($endTime));
- } else {
- $model = $model->whereTime('add_time', 'month');
- }
- break;
- }
- } else {
- if (is_array($status)) {
- $model = $model->where('add_time', '>', $status[0])->where('add_time', '<', $status[1]);
- }
- }
- if ($keep === true) {
- return $model->count();
- }
- if ($status === 'default') {
- 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();
- }
- $uid = $model->field('uid')->select()->toArray();
- foreach ($uid as $val) {
- $user_id[] = $val['uid'];
- }
- if (empty($user_id)) {
- $user_id = [0];
- }
- if ($status === 'xiaofei') {
- $list = UserBill::where('uid', 'in', $user_id)
- ->group('type')
- ->field('sum(number) as top_number,title')
- ->select()
- ->toArray();
- $series = [
- 'name' => isset($list[0]['title']) ? $list[0]['title'] : '',
- 'type' => 'pie',
- 'radius' => ['40%', '50%'],
- 'data' => []
- ];
- foreach ($list as $key => $val) {
- $series['data'][$key]['value'] = $val['top_number'];
- $series['data'][$key]['name'] = $val['title'];
- }
- return $series;
- } else if ($status === 'form') {
- $list = WechatUser::where('uid', 'in', $user_id)->group('city')->field('count(city) as top_city,city')->limit(0, 10)->select()->toArray();
- $count = self::getcount();
- $option = [
- 'legend_date' => [],
- 'series_date' => []
- ];
- foreach ($list as $key => $val) {
- $num = $count != 0 ? (bcdiv($val['top_city'], $count, 2)) * 100 : 0;
- $t = ['name' => $num . '% ' . (empty($val['city']) ? '未知' : $val['city']), 'icon' => 'circle'];
- $option['legend_date'][$key] = $t;
- $option['series_date'][$key] = ['value' => $num, 'name' => $t['name']];
- }
- return $option;
- } else {
- $number = UserBill::where('uid', 'in', $user_id)->where('type', 'pay_product')->sum('number');
- return $number;
- }
- }
- /*
- * 获取 用户某个时间段的钱数或者TOP20排行
- *
- * return Array || number
- */
- public static function getUserSpend($date, $status = '')
- {
- $model = new self();
- $model = $model->alias('A');
- switch ($date) {
- case null:
- case 'today':
- case 'week':
- case 'year':
- if ($date == null) $date = 'month';
- $model = $model->whereTime('A.add_time', $date);
- break;
- case 'quarter':
- list($startTime, $endTime) = self::getMonth('n');
- $model = $model->where('A.add_time', '>', strtotime($startTime));
- $model = $model->where('A.add_time', '<', strtotime($endTime));
- break;
- default:
- list($startTime, $endTime) = explode('-', $date);
- $model = $model->where('A.add_time', '>', strtotime($startTime));
- $model = $model->where('A.add_time', '<', strtotime($endTime));
- break;
- }
- if ($status === true) {
- return $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
- }
- $list = $model->join('user_bill B', 'B.uid=A.uid')
- ->where('B.type', 'pay_product')
- ->where('B.pm', 0)
- ->field('sum(B.number) as totel_number,A.nickname,A.avatar,A.now_money,A.uid,A.add_time')
- ->order('totel_number desc')
- ->limit(0, 20)
- ->select()
- ->toArray();
- if (!isset($list[0]['totel_number'])) {
- $list = [];
- }
- return $list;
- }
- /*
- * 获取 相对于上月或者其他的数据
- *
- * return Array
- */
- public static function getPostNumber($date, $status = false, $field = 'A.add_time', $t = '消费')
- {
- $model = new self();
- if (!$status) $model = $model->alias('A');
- switch ($date) {
- case null:
- case 'today':
- case 'week':
- case 'year':
- if ($date == null) {
- $date = 'last month';
- $title = '相比上月用户' . $t . '增长';
- }
- if ($date == 'today') {
- $date = 'yesterday';
- $title = '相比昨天用户' . $t . '增长';
- }
- if ($date == 'week') {
- $date = 'last week';
- $title = '相比上周用户' . $t . '增长';
- }
- if ($date == 'year') {
- $date = 'last year';
- $title = '相比去年用户' . $t . '增长';
- }
- $model = $model->whereTime($field, $date);
- break;
- case 'quarter':
- $title = '相比上季度用户' . $t . '增长';
- list($startTime, $endTime) = self::getMonth('n', 1);
- $model = $model->where($field, '>', $startTime);
- $model = $model->where($field, '<', $endTime);
- break;
- default:
- list($startTime, $endTime) = explode('-', $date);
- $title = '相比' . $startTime . '-' . $endTime . '时间段用户' . $t . '增长';
- $Time = strtotime($endTime) - strtotime($startTime);
- $model = $model->where($field, '>', strtotime($startTime) + $Time);
- $model = $model->where($field, '<', strtotime($endTime) + $Time);
- break;
- }
- if ($status) {
- return [$model->count(), $title];
- }
- $number = $model->join('user_bill B', 'B.uid=A.uid')->where('B.type', 'pay_product')->where('B.pm', 0)->sum('B.number');
- return [$number, $title];
- }
- //获取用户新增,头部信息
- public static function getBadgeList($where)
- {
- $user_count = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])->count();
- $user_count_old = self::getOldDate($where)->count();
- $fenxiao = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])->where('spread_uid', '<>', 0)->count();
- $fenxiao_count = self::getOldDate($where)->where('spread_uid', 'neq', 0)->count();
- $newFemxiao_count = bcsub($fenxiao, $fenxiao_count, 0);
- $order_count = bcsub($user_count, $user_count_old, 0);
- return [
- [
- 'name' => '会员人数',
- 'field' => '个',
- 'count' => $user_count,
- 'content' => '会员总人数',
- 'background_color' => 'layui-bg-blue',
- 'sum' => self::count(),
- 'class' => 'fa fa-bar-chart',
- ],
- [
- 'name' => '会员增长',
- 'field' => '个',
- 'count' => $order_count,
- 'content' => '会员增长率',
- 'background_color' => 'layui-bg-cyan',
- 'sum' => $user_count_old ? bcdiv($order_count, $user_count_old, 2) * 100 : 0,
- 'class' => 'fa fa-line-chart',
- ],
- [
- 'name' => '分销人数',
- 'field' => '个',
- 'count' => $fenxiao,
- 'content' => '分销总人数',
- 'background_color' => 'layui-bg-green',
- 'sum' => self::where('spread_uid', 'neq', 0)->count(),
- 'class' => 'fa fa-bar-chart',
- ],
- [
- 'name' => '分销增长',
- 'field' => '个',
- 'count' => $newFemxiao_count,
- 'content' => '分销总人数',
- 'background_color' => 'layui-bg-orange',
- 'sum' => $fenxiao_count ? bcdiv($newFemxiao_count, $fenxiao_count, 2) * 100 : 0,
- 'class' => 'fa fa-cube',
- ],
- ];
- }
- /*
- * 获取会员增长曲线图和分布图
- * $where 查询条件
- * $limit 显示条数,是否有滚动条
- */
- public static function getUserChartList($where, $limit = 20)
- {
- $list = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter', 'status'])
- ->where('add_time', 'neq', 0)
- ->field(['FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time', 'count(uid) as num'])
- ->order('_add_time asc')
- ->group('_add_time')
- ->select();
- count($list) && $list = $list->toArray();
- $seriesdata = [];
- $xdata = [];
- $Zoom = '';
- foreach ($list as $item) {
- $seriesdata[] = $item['num'];
- $xdata[] = $item['_add_time'];
- }
- (count($xdata) > $limit) && $Zoom = $xdata[$limit - 5];
- //多次购物会员数量饼状图
- $count = self::setWherePage(self::getModelTime($where, new self), $where, ['is_promoter'])->count();
- $user_count = self::setWherePage(self::getModelTime($where, self::alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time'), $where, ['is_promoter'])
- ->where('r.paid', 1)->count('a.uid');
- $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
- $shop_data = [];
- $count > 0 && $shop_data = [
- [
- 'value' => bcdiv($user_count, $count, 2) * 100,
- 'name' => $shop_xdata[0],
- 'itemStyle' => [
- 'color' => '#D789FF',
- ]
- ],
- [
- 'value' => bcdiv($count - $user_count, $count, 2) * 100,
- 'name' => $shop_xdata[1],
- 'itemStyle' => [
- 'color' => '#7EF0FB',
- ]
- ]
- ];
- return compact('shop_data', 'shop_xdata', 'fenbu_data', 'fenbu_xdata', 'seriesdata', 'xdata', 'Zoom');
- }
- //获取$date的前一天或者其他的时间段
- public static function getOldDate($where, $moedls = null)
- {
- $model = $moedls === null ? self::setWherePage(new self(), $where, ['is_promoter', 'status']) : $moedls;
- switch ($where['data']) {
- case 'today':
- $model = $model->whereTime('add_time', 'yesterday');
- break;
- case 'week':
- $model = $model->whereTime('add_time', 'last week');
- break;
- case 'month':
- $model = $model->whereTime('add_time', 'last month');
- break;
- case 'year':
- $model = $model->whereTime('add_time', 'last year');
- break;
- case 'quarter':
- $time = self::getMonth('n', 1);
- $model = $model->where('add_time', 'between', $time);
- break;
- }
- return $model;
- }
- //获取用户属性和性别分布图
- public static function getEchartsData($where)
- {
- $model = self::alias('a');
- $data = self::getModelTime($where, $model, 'a.add_time')
- ->join('wechat_user r', 'r.uid=a.uid')
- ->group('r.province')
- ->field('count(r.province) as count,province')
- ->order('count desc')
- ->limit(15)
- ->select();
- if (count($data)) $data = $data->toArray();
- $legdata = [];
- $dataList = [];
- foreach ($data as $value) {
- $value['province'] == '' && $value['province'] = '未知省份';
- $legdata[] = $value['province'];
- $dataList[] = $value['count'];
- }
- $model = self::alias('a');
- $sex = self::getModelTime($where, $model, 'a.add_time')
- ->join('wechat_user r', 'r.uid=a.uid')
- ->group('r.sex')
- ->field('count(r.uid) as count,sex')
- ->order('count desc')
- ->select();
- if (count($sex)) $sex = $sex->toArray();
- $sexlegdata = ['男', '女', '未知'];
- $sexcount = self::getModelTime($where, new self())->count();
- $sexList = [];
- $color = ['#FB7773', '#81BCFE', '#91F3FE'];
- foreach ($sex as $key => $item) {
- if ($item['sex'] == 1) {
- $item_date['name'] = '男';
- } else if ($item['sex'] == 2) {
- $item_date['name'] = '女';
- } else {
- $item_date['name'] = '未知性别';
- }
- $item_date['value'] = bcdiv($item['count'], $sexcount, 2) * 100;
- $item_date['itemStyle']['color'] = $color[$key];
- $sexList[] = $item_date;
- }
- return compact('sexList', 'sexlegdata', 'legdata', 'dataList');
- }
- //获取佣金记录列表
- public static function getCommissionList($where)
- {
- $list = self::setCommissionWhere($where)->page((int)$where['page'], (int)$where['limit'])->select();
- count($list) && $list = $list->toArray();
- foreach ($list as &$value) {
- $value['ex_price'] = db('user_extract')->where(['uid' => $value['uid']])->sum('extract_price');
- $value['extract_price'] = db('user_extract')->where(['uid' => $value['uid'], 'status' => 1])->sum('extract_price');
- }
- $count = self::setCommissionWhere($where)->count();
- return ['data' => $list, 'count' => $count];
- }
- //获取佣金记录列表的查询条件
- public static function setCommissionWhere($where)
- {
- $models = self::setWherePage(self::alias('A'), $where, [], ['A.nickname', 'A.uid'])
- ->join('user_bill B', 'B.uid=A.uid')
- ->group('A.uid')
- ->where(['B.category' => 'now_money'])
- ->where('B.type', 'in', ['brokerage', 'brokerage_return'])
- ->field(['B.number', 'A.nickname', 'A.uid', 'A.now_money', 'A.brokerage_price']);
- if (isset($where['order']) && $where['order'] == '') {
- $models = $models->order('A.brokerage_price desc');
- } else {
- $models = $models->order($where['order'] == 1 ? 'A.brokerage_price desc' : 'A.brokerage_price asc');
- }
- if (isset($where['price_max']) && isset($where['price_min']) && $where['price_max'] > 0 && $where['price_min'] > 0) {
- $models = $models->where('A.brokerage_price', 'between', [$where['price_min'], $where['price_max']]);
- }
- return $models;
- }
- //获取某人用户推广信息
- public static function getUserinfo($uid)
- {
- $userinfo = self::where(['uid' => $uid])->field(['nickname', 'spread_uid', 'now_money', 'add_time'])->find()->toArray();
- $userinfo['number'] = UserBill::getCommissionAmount($uid);
- $userinfo['spread_name'] = $userinfo['spread_uid'] ? self::where(['uid' => $userinfo['spread_uid']])->value('nickname') : '';
- return $userinfo;
- }
- /**用户信息
- * @param $uid
- * @return array
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public static function getAllUserinfo($uid)
- {
- $userinfo = self::where(['uid' => $uid])->find();
- return $userinfo;
- }
- public static function getUserinfoV1($uid)
- {
- $isPhoneMenu = is_phone_menu();
- $userinfo = self::where(['uid' => $uid])->find()->toArray();
- if (check_phone($userinfo['nickname']) && !$isPhoneMenu) {
- $userinfo['nickname'] = substr_replace($userinfo['nickname'], '****', 3, 4);
- }
- $userinfo['spread_name'] = $userinfo['spread_uid'] ? self::where(['uid' => $userinfo['spread_uid']])->value('nickname') : '';
- $spread = self::where(['spread_uid' => $uid])->where('is_promoter', 'neq', 0)->column('uid');
- $userinfo['spread_count'] = count($spread);
- $userinfo['spread_one'] = UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
- ->where('a.type', 'in', ['brokerage'])->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->sum('o.pay_price');
- $userinfo['bill_sum'] = UserBill::where(['category' => 'now_money', 'uid' => $userinfo['uid']])->where('type', 'in', ['brokerage'])->sum('number');
- return $userinfo;
- }
- public static function getPayPrice($uid, $type = ['brokerage'])
- {
- return UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
- ->where('a.type', 'in', $type)->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->sum('o.pay_price');
- }
- public static function getLinkCount($uid, $type = ['brokerage'])
- {
- return UserBill::where(['o.paid' => 1, 'a.uid' => $uid, 'a.category' => 'now_money'])
- ->where('a.type', 'in', $type)->alias('a')->join('__STORE_ORDER__ o', 'a.link_id=o.id')->count();
- }
- public static function getSpreadListV1($where)
- {
- $spread = self::where(['spread_uid' => $where['uid']])->column('uid') ?: [0];
- $list = self::where('uid', 'in', $spread)->order('add_time desc')->page((int)$where['page'], (int)$where['limit'])->select();
- $list = count($list) ? $list->toArray() : [];
- foreach ($list as &$item) {
- $item['order_count'] = self::getLinkCount($item['uid'], ['brokerage']);
- $item['sum_pay_price'] = self::getPayPrice($item['uid'], ['brokerage']);
- $item['sum_number'] = UserBill::where('uid', $item['uid'])->where('category', 'now_money')->where('type', 'in', ['brokerage'])->sum('number');
- }
- return $list;
- }
- //获取某用户的详细信息
- public static function getUserDetailed($uid)
- {
- $isPhoneMenu = is_phone_menu();
- $gold_name = SystemConfigService::get('gold_name');//虚拟币名称
- $key_field = ['real_name', 'phone', 'province', 'city', 'district', 'detail', 'post_code'];
- $Address = ($thisAddress = db('user_address')->where(['uid' => $uid, 'is_default' => 1])->field($key_field)->find()) ?
- $thisAddress :
- db('user_address')->where(['uid' => $uid])->field($key_field)->find();
- $UserInfo = self::get($uid);
- if ($UserInfo['last_time']) $UserInfo['last_time'] = date('Y-m-d H:i:s', $UserInfo['last_time']);//最近一次访问日期
- else $UserInfo['last_time'] = '无访问';//最近一次访问日期
- $UserInfo['add_time'] = date('Y-m-d H:i:s', $UserInfo['add_time']);
- $time = '首次:' . $UserInfo['add_time'] . '最近:' . $UserInfo['last_time'];
- if (check_phone($UserInfo['nickname']) && !$isPhoneMenu) {
- $UserInfo['nickname'] = substr_replace($UserInfo['nickname'], '****', 3, 4);
- }
- if (check_phone($UserInfo['phone']) && !$isPhoneMenu) {
- $UserInfo['phone'] = substr_replace($UserInfo['phone'], '****', 3, 4);
- }
- return [
- ['col' => 12, 'name' => '默认收货地址', 'value' => $thisAddress ? '收货人:' . $thisAddress['real_name'] . ' 收货人电话:' . $thisAddress['phone'] . ' 地址:' . $thisAddress['province'] . ' ' . $thisAddress['city'] . ' ' . $thisAddress['district'] . ' ' . $thisAddress['detail'] : ''],
- ['name' => 'ID', 'value' => $uid],
- ['name' => '手机号码', 'value' => $UserInfo['phone']],
- ['name' => '微信昵称', 'value' => $UserInfo['nickname']],
- ['name' => '购买次数', 'value' => StoreOrder::getUserCountPay($uid)],
- // ['name' => "$gold_name" . '余额', 'value' => $UserInfo['gold_num']],
- // ['name' => '上级推广人', 'value' => $UserInfo['spread_uid'] ? self::where(['uid' => $UserInfo['spread_uid']])->value('nickname') : ''],
- // ['name' => '账户余额', 'value' => $UserInfo['now_money']],
- // ['name' => '佣金总收入', 'value' => UserBill::getCommissionAmount($uid)],
- // ['name' => '提现总金额', 'value' => db('user_extract')->where(['uid' => $uid, 'status' => 1])->sum('extract_price')],
- ['name' => '访问日期', 'value' => $time]
- ];
- }
- //获取某用户的订单个数,消费明细
- public static function getHeaderList($uid)
- {
- return [
- [
- 'title' => '总计订单',
- 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->count(),
- 'key' => '笔',
- 'class' => '',
- ],
- [
- 'title' => '总消费金额',
- 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->sum('pay_price'),
- 'key' => '元',
- 'class' => '',
- ],
- [
- 'title' => '本月订单',
- 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->whereTime('add_time', 'month')->count(),
- 'key' => '笔',
- 'class' => '',
- ],
- [
- 'title' => '本月消费金额',
- 'value' => StoreOrder::where(['uid' => $uid, 'paid' => 1])->whereTime('add_time', 'month')->sum('pay_price'),
- 'key' => '元',
- 'class' => '',
- ]
- ];
- }
- public static function getCountData($uid)
- {
- $pay_count = SpecialBuy::where('uid', $uid)->where('is_del', 0)->count();
- $data_page = DataDownloadBuy::where(['uid'=>$uid,'is_del'=>0])->count();
- $test_count = TestPaperObtain::where(['uid'=>$uid,'is_del'=>0])->count();
- return compact('pay_count','data_page','test_count');
- }
- /*
- * 获取 会员 订单个数,积分明细,优惠劵明细
- *
- * $uid 用户id;
- *
- * return array
- */
- public static function getCountInfo($uid)
- {
- $order_count = StoreOrder::where(['uid' => $uid])->count();
- $integral_count = UserBill::where(['uid' => $uid, 'category' => 'integral'])->where('type', 'in', ['deduction', 'system_add'])->count();
- $sign_count = UserBill::where(['uid' => $uid, 'category' => 'integral', 'type' => 'sign'])->count();
- $balanceChang_count = UserBill::where(['uid' => $uid, 'category' => 'now_money'])
- ->where('type', 'in', ['system_add', 'pay_product', 'extract', 'extract_fail', 'pay_goods', 'pay_sign_up', 'pay_product_refund', 'system_sub'])
- ->count();
- $coupon_count = 0;
- $spread_count = self::where(['spread_uid' => $uid])->count();
- $pay_count = self::getDb('special_buy')->where('uid', $uid)->where('is_del', 0)->count();
- return compact('order_count', 'integral_count', 'sign_count', 'balanceChang_count', 'coupon_count', 'spread_count', 'pay_count');
- }
- /*
- * 获取 会员业务的
- * 购物会员统计
- * 会员访问量
- *
- * 曲线图
- *
- * $where 查询条件
- *
- * return array
- */
- public static function getUserBusinessChart($where, $limit = 20)
- {
- //获取购物会员人数趋势图
- $list = self::getModelTime($where, self::where('a.status', 1)->alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
- ->where(['r.paid' => 1, 'a.is_promoter' => 0])
- ->where('a.add_time', 'neq', 0)
- ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'count(r.uid) as count_user'])
- ->group('_add_time')
- ->order('_add_time asc')
- ->select();
- count($list) && $list = $list->toArray();
- $seriesdata = [];
- $xdata = [];
- $zoom = '';
- foreach ($list as $item) {
- $seriesdata[] = $item['count_user'];
- $xdata[] = $item['_add_time'];
- }
- count($xdata) > $limit && $zoom = $xdata[$limit - 5];
- //会员访问量
- $visit = self::getModelTime($where, self::alias('a')->join('store_visit t', 't.uid=a.uid'), 't.add_time')
- ->where('a.is_promoter', 0)
- ->field(['FROM_UNIXTIME(t.add_time,"%Y-%m-%d") as _add_time', 'count(t.uid) as count_user'])
- ->group('_add_time')
- ->order('_add_time asc')
- ->select();
- count($visit) && $visit = $visit->toArray();
- $visit_data = [];
- $visit_xdata = [];
- $visit_zoom = '';
- foreach ($visit as $item) {
- $visit_data[] = $item['count_user'];
- $visit_xdata[] = $item['_add_time'];
- }
- count($visit_xdata) > $limit && $visit_zoom = $visit_xdata[$limit - 5];
- //多次购物会员数量饼状图
- $count = self::getModelTime($where, self::where('is_promoter', 0))->count();
- $user_count = self::getModelTime($where, self::alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
- ->where('a.is_promoter', 0)
- ->where('r.paid', 1)
- ->group('a.uid')
- ->count();
- $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
- $shop_data = [];
- $count > 0 && $shop_data = [
- [
- 'value' => bcdiv($user_count, $count, 2) * 100,
- 'name' => $shop_xdata[0],
- 'itemStyle' => [
- 'color' => '#D789FF',
- ]
- ],
- [
- 'value' => bcdiv($count - $user_count, $count, 2) * 100,
- 'name' => $shop_xdata[1],
- 'itemStyle' => [
- 'color' => '#7EF0FB',
- ]
- ]
- ];
- return compact('seriesdata', 'xdata', 'zoom', 'visit_data', 'visit_xdata', 'visit_zoom', 'shop_data', 'shop_xdata');
- }
- /*
- * 获取用户
- * 积分排行
- * 会员余额排行榜
- * 分销商佣金总额排行榜
- * 购物笔数排行榜
- * 购物金额排行榜
- * 分销商佣金提现排行榜
- * 上月消费排行榜
- * $limit 查询多少条
- * return array
- */
- public static function getUserTop10List($limit = 10, $is_promoter = 0)
- {
- //积分排行
- $integral = self::where('status', 1)
- ->where('is_promoter', $is_promoter)
- ->order('integral desc')
- ->field(['nickname', 'phone', 'integral', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'])
- ->limit($limit)
- ->select();
- count($integral) && $integral = $integral->toArray();
- //会员余额排行榜
- $now_money = self::where('status', 1)
- ->where('is_promoter', $is_promoter)
- ->order('now_money desc')
- ->field(['nickname', 'phone', 'now_money', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'])
- ->limit($limit)
- ->select();
- count($now_money) && $now_money = $now_money->toArray();
- //购物笔数排行榜
- $shopcount = self::alias('a')
- ->join('store_order r', 'r.uid=a.uid')
- ->where(['r.paid' => 1, 'a.is_promoter' => $is_promoter])
- ->group('r.uid')
- ->field(['a.nickname', 'a.phone', 'count(r.uid) as sum_count', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time'])
- ->order('sum_count desc')
- ->limit($limit)
- ->select();
- count($shopcount) && $shopcount = $shopcount->toArray();
- //购物金额排行榜
- $order = self::alias('a')
- ->join('store_order r', 'r.uid=a.uid')
- ->where(['r.paid' => 1, 'a.is_promoter' => $is_promoter])
- ->group('r.uid')
- ->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'])
- ->order('sum_price desc')
- ->limit($limit)
- ->select();
- count($order) && $order = $order->toArray();
- //上月消费排行
- $lastorder = self::alias('a')
- ->join('store_order r', 'r.uid=a.uid')
- ->where(['r.paid' => 1, 'a.is_promoter' => $is_promoter])
- ->whereTime('r.pay_time', 'last month')
- ->group('r.uid')
- ->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'])
- ->order('sum_price desc')
- ->limit($limit)
- ->select();
- return compact('integral', 'now_money', 'shopcount', 'order', 'lastorder');
- }
- /*
- * 获取 会员业务
- * 会员总余额 会员总积分
- * $where 查询条件
- *
- * return array
- */
- public static function getUserBusinesHeade($where)
- {
- return [
- [
- 'name' => '会员总余额',
- 'field' => '元',
- 'count' => self::getModelTime($where, self::where('status', 1))->sum('now_money'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 6,
- ],
- [
- 'name' => '会员总积分',
- 'field' => '分',
- 'count' => self::getModelTime($where, self::where('status', 1))->sum('integral'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 6
- ]
- ];
- }
- /*
- * 分销会员头部信息查询获取
- *
- * 分销商总佣金余额
- * 分销商总提现佣金
- * 本月分销商业务佣金
- * 本月分销商佣金提现金额
- * 上月分销商业务佣金
- * 上月分销商佣金提现金额
- * $where array 时间条件
- *
- * return array
- */
- public static function getDistributionBadgeList($where)
- {
- return [
- [
- 'name' => '分销商总佣金',
- 'field' => '元',
- 'count' => self::getModelTime($where, UserBill::where('category', 'now_money')->where('type', 'brokerage'))->where('uid', 'in', function ($query) {
- $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
- })->sum('number'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '分销商总佣金余额',
- 'field' => '元',
- 'count' => self::getModelTime($where, self::where('status', 1)->where('is_promoter', 1))->sum('now_money'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '分销商总提现佣金',
- 'field' => '元',
- 'count' => self::getModelTime($where, UserExtract::where('status', 1))->sum('extract_price'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '本月分销商业务佣金',
- 'field' => '元',
- 'count' => self::getModelTime(['data' => 'month'], UserBill::where('category', 'now_money')->where('type', 'brokerage'))
- ->where('uid', 'in', function ($query) {
- $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
- })->sum('number'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '本月分销商佣金提现金额',
- 'field' => '元',
- 'count' => self::getModelTime(['data' => 'month'], UserExtract::where('status', 1))
- ->where('uid', 'in', function ($query) {
- $query->name('user')->where('status', 1)->where('is_promoter', 1)->field('uid');
- })->sum('extract_price'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 4,
- ],
- [
- 'name' => '上月分销商业务佣金',
- 'field' => '元',
- 'count' => self::getOldDate(['data' => 'year'], UserBill::where('category', 'now_money')->where('uid', 'in', function ($query) {
- $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
- })->where('type', 'brokerage'))->sum('number'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 4,
- ],
- [
- 'name' => '上月分销商佣金提现金额',
- 'field' => '元',
- 'count' => self::getOldDate(['data' => 'year'], UserBill::where('category', 'now_money')->where('uid', 'in', function ($query) {
- $query->name('user')->where('status', 1)->where('is_promoter', 1)->whereOr('spread_uid', 'neq', 0)->field('uid');
- })->where('type', 'brokerage'))->sum('number'),
- 'background_color' => 'layui-bg-cyan',
- 'col' => 4,
- ],
- ];
- }
- /*
- * 分销会员
- * 分销数量 饼状图
- * 分销商会员访问量 曲线
- * 获取购物会员人数趋势图 曲线
- * 多次购物分销会员数量 饼状图
- * $where array 条件
- * $limit int n条数据后出拖动条
- * return array
- */
- public static function getUserDistributionChart($where, $limit = 20)
- {
- //分销数量
- $fenbu_user = self::getModelTime($where, new self)->field(['count(uid) as num'])->group('is_promoter')->select();
- count($fenbu_user) && $fenbu_user = $fenbu_user->toArray();
- $sum_user = 0;
- $fenbu_data = [];
- $fenbu_xdata = ['分销商', '非分销商'];
- $color = ['#81BCFE', '#91F3FE'];
- foreach ($fenbu_user as $item) {
- $sum_user += $item['num'];
- }
- foreach ($fenbu_user as $key => $item) {
- $value['value'] = bcdiv($item['num'], $sum_user, 2) * 100;
- $value['name'] = isset($fenbu_xdata[$key]) ? $fenbu_xdata[$key] . ' %' . $value['value'] : '';
- $value['itemStyle']['color'] = $color[$key];
- $fenbu_data[] = $value;
- }
- //分销商会员访问量
- $visit = self::getModelTime($where, self::alias('a')->join('store_visit t', 't.uid=a.uid'), 't.add_time')
- ->where('a.is_promoter', 1)
- ->field(['FROM_UNIXTIME(t.add_time,"%Y-%m-%d") as _add_time', 'count(t.uid) as count_user'])
- ->group('_add_time')
- ->order('_add_time asc')
- ->select();
- count($visit) && $visit = $visit->toArray();
- $visit_data = [];
- $visit_xdata = [];
- $visit_zoom = '';
- foreach ($visit as $item) {
- $visit_data[] = $item['count_user'];
- $visit_xdata[] = $item['_add_time'];
- }
- count($visit_xdata) > $limit && $visit_zoom = $visit_xdata[$limit - 5];
- //获取购物会员人数趋势图
- $list = self::getModelTime($where, self::where('a.status', 1)->alias('a')->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
- ->where(['r.paid' => 1, 'a.is_promoter' => 1])
- ->where('a.add_time', 'neq', 0)
- ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'count(r.uid) as count_user'])
- ->group('_add_time')
- ->order('_add_time asc')
- ->select();
- count($list) && $list = $list->toArray();
- $seriesdata = [];
- $xdata = [];
- $zoom = '';
- foreach ($list as $item) {
- $seriesdata[] = $item['count_user'];
- $xdata[] = $item['_add_time'];
- }
- count($xdata) > $limit && $zoom = $xdata[$limit - 5];
- //多次购物分销会员数量饼状图
- $count = self::getModelTime($where, self::where('is_promoter', 1))->count();
- $user_count = self::getModelTime($where, self::alias('a')
- ->join('store_order r', 'r.uid=a.uid'), 'a.add_time')
- ->where('a.is_promoter', 1)
- ->where('r.paid', 1)
- ->group('a.uid')
- ->count();
- $shop_xdata = ['多次购买数量占比', '无购买数量占比'];
- $shop_data = [];
- $count > 0 && $shop_data = [
- [
- 'value' => bcdiv($user_count, $count, 2) * 100,
- 'name' => $shop_xdata[0] . $user_count . '人',
- 'itemStyle' => [
- 'color' => '#D789FF',
- ]
- ],
- [
- 'value' => bcdiv($count - $user_count, $count, 2) * 100,
- 'name' => $shop_xdata[1] . ($count - $user_count) . '人',
- 'itemStyle' => [
- 'color' => '#7EF0FB',
- ]
- ]
- ];
- return compact('fenbu_data', 'fenbu_xdata', 'visit_data', 'visit_xdata', 'visit_zoom', 'seriesdata', 'xdata', 'zoom', 'shop_xdata', 'shop_data');
- }
- /*
- * 分销商佣金提现排行榜
- * 分销商佣金总额排行榜
- * $limit 截取条数
- * return array
- */
- public static function getUserDistributionTop10List($limit)
- {
- //分销商佣金提现排行榜
- $extract = self::alias('a')
- ->join('user_extract t', 'a.uid=t.uid')
- ->where(['t.status' => 1, 'a.is_promoter' => 1])
- ->group('t.uid')
- ->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'])
- ->order('sum_price desc')
- ->limit($limit)
- ->select();
- count($extract) && $extract = $extract->toArray();
- //分销商佣金总额排行榜
- $commission = UserBill::alias('l')
- ->join('user a', 'l.uid=a.uid')
- ->where(['l.status' => '1', 'l.category' => 'now_money', 'l.type' => 'brokerage', 'a.is_promoter' => 1])
- ->group('l.uid')
- ->field(['a.nickname', 'a.phone', 'sum(number) as sum_number', 'FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as add_time'])
- ->order('sum_number desc')
- ->limit($limit)
- ->select();
- count($commission) && $commission = $commission->toArray();
- return compact('extract', 'commission');
- }
- public static function getSpreadList($uid, $page, $limit)
- {
- $list = self::where(['spread_uid' => $uid])->field(['uid', 'nickname', 'now_money', 'gold_num', 'add_time'])
- ->order('uid desc')->page((int)$page, (int)$limit)->select();
- count($list) && $list = $list->toArray();
- $isPhoneMenu = is_phone_menu();
- foreach ($list as &$item) {
- if (check_phone($item['nickname']) && !$isPhoneMenu) {
- $item['nickname'] = substr_replace($item['nickname'], '****', 3, 4);
- }
- $item['add_time'] = date('Y-m-d H', $item['add_time']);
- }
- return $list;
- }
- /**
- * 设置推广人查询条件
- * @param $where
- * @param string $alias
- * @param int $spread_type
- * @param null $model
- * @return $this
- */
- public static function setSpreadBadgeWhere($where, $alias = '', $spread_type = 0, $model = null)
- {
- $model = is_null($model) ? new self() : $model;
- $alias = $alias ? $alias . '.' : '';
- if ($spread_type) $where['spread_type'] = $spread_type;
- if ($where['nickname'] && ($uids = self::where('nickname|phone|uid', 'like', "%$where[nickname]%")->column('uid'))) {
- $model = $model->where($alias . 'spread_uid', 'in', $uids);
- }
- if ($where['phone']) $model = $model->where("{$alias}nickname|{$alias}phone", 'like', "%$where[phone]%");
- if ($where['start_time'] && $where['stop_time']) {
- $model = $model->whereTime("{$alias}add_time", 'between', [$where['start_time'], $where['stop_time']]);
- }
- $storeBrokerageStatu = SystemConfigService::get('store_brokerage_statu') ?: 1;//获取后台分销类型
- if ($storeBrokerageStatu == 1) {
- $model = $model->where("{$alias}is_promoter", 1);
- }
- return $model;
- }
- public static function getextractPrice($uid, $where = [])
- {
- if (is_array($uid)) {
- if (!count($uid)) return 0;
- } else
- $uid = [$uid];
- $brokerage = UserBill::getBrokerage($uid, 'now_money', 'brokerage', $where);//获取总佣金
- $return = UserBill::getReturnBrokerage($uid, 'now_money', 'brokerage_return', $where);//获取返还佣金
- $brokerage = bcsub($brokerage, $return, 2);
- $recharge = UserBill::getBrokerage($uid, 'now_money', 'recharge', $where);//累计充值
- $extractTotalPrice = UserExtract::userExtractTotalPrice($uid, 1, $where);//累计提现
- if ($brokerage > $extractTotalPrice) {
- $orderYuePrice = self::getModelTime($where, StoreOrder::where('uid', 'in', $uid)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1'))->sum('pay_price');//余额累计消费
- $systemAdd = UserBill::getBrokerage($uid, 'now_money', 'system_add', $where);//后台添加余额
- $yueCount = bcadd($recharge, $systemAdd, 2);// 后台添加余额 + 累计充值 = 非佣金的总金额
- $orderYuePrice = $yueCount > $orderYuePrice ? 0 : bcsub($orderYuePrice, $yueCount, 2);// 余额累计消费(使用佣金消费的金额)
- $brokerage = bcsub($brokerage, $extractTotalPrice, 2);//减去已提现金额
- $extract_price = UserExtract::userExtractTotalPrice($uid, 0, $where);
- $brokerage = $extract_price < $brokerage ? bcsub($brokerage, $extract_price, 2) : 0;//减去审核中的提现金额
- $brokerage = $brokerage > $orderYuePrice ? bcsub($brokerage, $orderYuePrice, 2) : 0;//减掉余额支付
- } else {
- $brokerage = 0;
- }
- $num = (float)bcsub($brokerage, $extractTotalPrice, 2);
- return $num > 0 ? $num : 0;//可提现
- }
- /**
- * 获取推广人列表
- * @param array $where 查询条件
- * @return array
- * */
- public static function SpreadList($where)
- {
- $model = self::setSpreadBadgeWhere($where)->field('phone,uid,nickname,add_time,spread_uid,is_promoter,is_senior');
- if ($where['export']) $data = $model->select();
- else $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
- $data = count($data) ? $data->toArray() : [];
- foreach ($data as &$item) {
- $item['spread_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname');
- $item['spread_name'] = '普通';
- //直推订单
- $uids = self::where('spread_uid', $item['uid'])->column('uid');
- if (count($uids)) {
- $item['sum_pay_price'] = StoreOrder::whereIn('uid', $uids)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->sum('pay_price');
- $ids = self::whereIn('spread_uid', $uids)->column('uid');
- if (count($ids)) {
- $item['pay_price'] = StoreOrder::whereIn('uid', $ids)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->sum('pay_price');
- }
- } else {
- $item['sum_pay_price'] = 0;
- $item['pay_price'] = 0;
- }
- unset($ids, $uids);
- $item['rake_back'] = UserBill::getCommissionAmount($item['uid']);
- $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
- }
- if ($where['export']) self::SaveExcel($data);
- $count = self::setSpreadBadgeWhere($where)->count();
- return compact('data', 'count');
- }
- /*
- * 保存并下载excel
- * $list array
- * return
- */
- public static function SaveExcel($list)
- {
- $export = [];
- foreach ($list as $index => $item) {
- $export[] = [
- $item['spread_name'],
- $item['nickname'],
- $item['spread_nickname'],
- $item['phone'],
- $item['add_time'],
- $item['sum_pay_price'],
- $item['rake_back']
- ];
- }
- $filename = '推广人列表导出' . time() . '.xlsx';
- $head = ['推广人身份', '昵称', '所属上级', '手机号码', '加入时间', '订单金额', '佣金'];
- PhpSpreadsheetService::outdata($filename, $export, $head);
- }
- public static function guestWhere($where, $guest, $model = null)
- {
- if ($model == null) $model = new self;
- if (isset($where['guest_name']) && $where['guest_name'] != '') $model = $model->where('nickname|uid', 'LIKE', "%$where[guest_name]%");
- $model = $model->where('uid', 'IN', $guest->guest);
- return $model;
- }
- //分销列表
- public static function setSpreadWhere($where = [], $alias = 'u', $model = null)
- {
- $model = is_null($model) ? new self() : $model;
- if ($alias) {
- $model = $model->alias($alias)->order("u.uid desc");
- $alias .= '.';
- }
- $status = (int)SystemConfigService::get('store_brokerage_statu');
- if ($status == 1) {
- $model = $model->where("{$alias}is_promoter", 1);
- }
- if ($where['nickname'] !== '') $model = $model->where("{$alias}nickname|{$alias}uid|{$alias}phone", 'LIKE', "%$where[nickname]%");
- if ((isset($where['start_time']) && isset($where['end_time'])) && $where['start_time'] !== '' && $where['end_time'] !== '') {
- $model = $model->where("{$alias}add_time", 'between', [strtotime($where['start_time']), strtotime($where['end_time'])]);
- }
- if (isset($where['order']) && $where['order'] != '') $model = $model->order($where['order']);
- if (isset($where['is_time']) && isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, $alias . 'add_time');
- return $model;
- }
- /**
- * 获取推广人数
- * @param $uid //用户的uid
- * @param int $spread
- * $spread 0 一级推广人数 1 二级推广人数
- * @return int|string
- */
- public static function getUserSpreadUidCount($uid, $spread = 1)
- {
- $userStair = self::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人
- if ($userStair) {
- if (!$spread) return count($userStair);//返回一级推人人数
- else return self::where('spread_uid', 'IN', implode(',', $userStair))->count();//二级推荐人数
- } else return 0;
- }
- /**
- * 获取推广人的订单
- * @param $uid
- * @param int $spread
- * $spread 0 一级推广总订单 1 所有推广总订单
- * @return int|string
- */
- public static function getUserSpreadOrderCount($uid, $spread = 1)
- {
- $userStair = self::where('spread_uid', $uid)->column('uid', 'uid');//获取一级推家人uid
- if ($userStair) {
- if (!$spread) {
- return StoreOrder::where('uid', 'IN', implode(',', $userStair))->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->count();//获取一级推广人订单数
- } else {
- $userSecond = self::where('spread_uid', 'IN', implode(',', $userStair))->column('uid', 'uid');//二级推广人的uid
- if ($userSecond) {
- return StoreOrder::where('uid', 'IN', implode(',', $userSecond))->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->count();//获取二级推广人订单数
- } else return 0;
- }
- } else return 0;
- }
- /**
- * 获取分销用户
- * @param array $where
- * @return array
- */
- public static function agentSystemPage($where = array())
- {
- $where['is_time'] = 1;
- $model = self::setSpreadWhere($where, 'u');
- $status = SystemConfigService::get('store_brokerage_statu');
- if (isset($where['excel']) && $where['excel'] == 1) {
- $list = $model->field(['u.uid', 'u.phone', 'u.nickname', 'u.brokerage_price'])->select()->toArray();
- $export = [];
- foreach ($list as $index => $item) {
- $Listuids = self::getModelTime($where, self::where('spread_uid', $item['uid']))->field('uid')->select();
- $newUids = [];
- foreach ($Listuids as $val) {
- $newUids[] = $val['uid'];
- }
- $uids = $newUids;
- unset($uid, $newUids);
- $item['spread_count'] = count($uids);
- if (count($uids)) {
- $ListUidTwo = self::where('spread_uid', 'in', $uids)->field('uid')->select();
- $newUids = [];
- foreach ($ListUidTwo as $val) {
- $newUids[] = $val['uid'];
- }
- $uidTwo = $newUids;
- unset($uid, $newUids);
- $uids = array_merge($uids, $uidTwo);
- $uids = array_unique($uids);
- $uids = array_merge($uids);
- }
- $item['extract_sum_price'] = self::getModelTime($where, UserExtract::where('uid', $item['uid']))->sum('extract_price');
- $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现金额
- $item['extract_count_num'] = UserExtract::getUserCountNum($item['uid'], $where);//提现次数
- $item['order_price'] = count($uids) ? StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->sum('pay_price') : 0;//订单金额
- $item['order_count'] = count($uids) ? StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->count() : 0;//订单数量
- //可提现佣金
- $item['new_money'] = $item['brokerage_price'];
- //总共佣金
- $income = self::getModelTime($where, UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage', 'pm' => 1, 'status' => 1]))->sum('number');
- $return = self::getModelTime($where, UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage_return', 'pm' => 0, 'status' => 1]))->sum('number');
- $item['brokerage_money'] = bcsub($income, $return, 2);
- $item['spread_name'] = '暂无';
- if ($spread_uid = self::where('uid', $item['uid'])->value('spread_uid')) {
- if ($user = self::where('uid', $spread_uid)->field(['uid', 'nickname'])->find()) {
- $item['spread_name'] = $user['nickname'] . '/' . $user['uid'];
- }
- }
- $export[] = [
- $item['uid'],
- $item['nickname'],
- $item['phone'],
- $item['spread_count'],
- $item['order_count'],
- $item['order_price'],
- $item['brokerage_money'],
- $item['extract_count_price'],
- $item['extract_count_num'],
- $item['new_money'],
- $item['spread_name']
- ];
- }
- $filename = '推广用户导出' . time() . '.xlsx';
- $head = ['用户编号', '昵称', '电话号码', '推广用户数量', '订单数量', '推广订单金额', '佣金金额', '已提现金额', '提现次数', '未提现金额', '上级推广人'];
- PhpSpreadsheetService::outdata($filename, $export, $head);
- }
- $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
- $data = count($data) ? $data->toArray() : [];
- foreach ($data as &$item) {
- if ((int)$status == 2) $item['is_show'] = false;
- else $item['is_show'] = true;
- $Listuids = self::getModelTime($where, self::where('spread_uid', $item['uid']))->field('uid')->select();
- $newUids = [];
- foreach ($Listuids as $val) {
- $newUids[] = $val['uid'];
- }
- $uids = $newUids;
- unset($uid, $newUids);
- $item['spread_count'] = count($uids);
- if (count($uids)) {
- $ListUidTwo = self::where('spread_uid', 'in', $uids)->field('uid')->select();
- $newUids = [];
- foreach ($ListUidTwo as $val) {
- $newUids[] = $val['uid'];
- }
- $uidTwo = $newUids;
- unset($uid, $newUids);
- $uids = array_merge($uids, $uidTwo);
- $uids = array_unique($uids);
- $uids = array_merge($uids);
- }
- $item['extract_sum_price'] = self::getModelTime($where, UserExtract::where('uid', $item['uid']))->sum('extract_price');
- $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现金额
- $item['extract_count_num'] = UserExtract::getUserCountNum($item['uid'], $where);//提现次数
- $item['order_price'] = count($uids) ? StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1,2')->sum('pay_price') : 0;//订单金额
- $item['order_count'] = count($uids) ? StoreOrder::where('uid', 'in', $uids)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1,2')->count() : 0;//订单数量
- $item['spread_name'] = '暂无';
- if ($spread_uid = self::where('uid', $item['uid'])->value('spread_uid')) {
- if ($user = self::where('uid', $spread_uid)->field(['uid', 'nickname'])->find()) {
- $item['spread_name'] = $user['nickname'] . '/' . $user['uid'];
- }
- }
- $income = self::getModelTime($where, UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage', 'pm' => 1, 'status' => 1]))->sum('number');
- $return = self::getModelTime($where, UserBill::where(['uid' => $item['uid'], 'category' => 'now_money', 'type' => 'brokerage_return', 'pm' => 0, 'status' => 1]))->sum('number');
- //总共佣金
- $item['brokerage_money'] = bcsub($income, $return, 2);
- //可提现佣金
- $item['new_money'] = $item['brokerage_price'];
- }
- $count = self::setSpreadWhere($where)->count();
- return compact('data', 'count');
- }
- public static function getSpreadBadge($where)
- {
- $where['is_time'] = 1;
- $listuids = self::setSpreadWhere($where)->field('u.uid')->select();
- $newUids = [];
- foreach ($listuids as $item) {
- $newUids[] = $item['uid'];
- }
- $uids = $newUids;
- unset($newUids);
- $uids2 = [];
- foreach ($listuids as $item) {
- $Listuids1 = self::where('spread_uid', $item['uid'])->field('uid')->select();
- $newUids1 = [];
- foreach ($Listuids1 as $val) {
- $newUids1[] = $val['uid'];
- }
- $uids1 = $newUids1;
- unset($newUids1);
- if (count($uids1)) {
- $ListUidTwo = self::where('spread_uid', 'in', $uids)->field('uid')->select();
- $newUids1 = [];
- foreach ($ListUidTwo as $val) {
- $newUids1[] = $val['uid'];
- }
- $uidTwo = $newUids1;
- unset($newUids1);
- $uids1 = array_merge($uids1, $uidTwo);
- $uids1 = array_unique($uids1);
- $uids1 = array_merge($uids1);
- $uids2 = array_merge($uids2, $uids1);
- }
- }
- $uids2 = array_unique($uids2);
- //分销员人数
- $data['sum_count'] = count($uids);
- $data['spread_sum'] = 0;
- $data['order_count'] = 0;
- $data['pay_price'] = 0;
- $data['number'] = 0;
- $data['extract_count'] = 0;
- $data['extract_price'] = 0;
- if ($data['sum_count']) {
- //发展会员人数
- $data['spread_sum'] = self::where('spread_uid', 'in', $uids)->count();
- //订单总数
- $data['order_count'] = StoreOrder::where('uid', 'in', $uids2)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1,2')->count();
- //订单金额
- $data['pay_price'] = StoreOrder::where('uid', 'in', $uids2)->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1,2')->sum('pay_price');
- //可提现金额
- $data['number'] = self::where('uid', 'in', $uids)->sum('brokerage_price');
- //提现次数
- $data['extract_count'] = UserExtract::where('uid', 'in', $uids)->count();
- //获取某个用户可提现金额
- $data['extract_price'] = self::getextractPrice($uids, $where);
- }
- return [
- [
- 'name' => '分销员数量',
- 'field' => '人',
- 'count' => $data['sum_count'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 2,
- ],
- [
- 'name' => '发展人数',
- 'field' => '人',
- 'count' => $data['spread_sum'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 2,
- ],
- [
- 'name' => '总订单数',
- 'field' => '单',
- 'count' => $data['order_count'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 2,
- ],
- [
- 'name' => '总订单金额',
- 'field' => '元',
- 'count' => $data['pay_price'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 2,
- ],
- [
- 'name' => '可提现金额',
- 'field' => '元',
- 'count' => $data['number'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 2,
- ],
- [
- 'name' => '提现次数',
- 'field' => '次',
- 'count' => $data['extract_count'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 2,
- ]
- ];
- }
- public static function getStairList($where)
- {
- if (!isset($where['uid'])) return [];
- $data = self::setSairWhere($where, new self())->order('add_time desc')->page((int)$where['page'], (int)$where['limit'])->select();
- $data = count($data) ? $data->toArray() : [];
- foreach ($data as &$item) {
- $item['spread_count'] = self::where('spread_uid', $item['uid'])->count();
- $item['order_count'] = StoreOrder::where('uid', $item['uid'])->where(['paid' => 1, 'refund_status' => 0, 'is_del' => 0])->where('type', 'in', '0,1')->count();
- $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
- $item['add_time'] = date("Y-m-d H:i:s", $item['add_time']);
- }
- $count = self::setSairWhere($where, new User())->count();
- return compact('data', 'count');
- }
- /**
- * 设置查询条件
- * @param array $where
- * @param object $model
- * @param string $alias
- * */
- public static function setSairWhere($where, $model = null, $alias = '')
- {
- $model = $model === null ? new self() : $model;
- if (!isset($where['uid'])) return $model;
- if ($alias) {
- $model = $model->alias($alias);
- $alias .= '.';
- }
- if (isset($where['type'])) {
- switch ((int)$where['type']) {
- case 1:
- $uids = self::where('spread_uid', $where['uid'])->column('uid');
- if (count($uids))
- $model = $model->where("{$alias}uid", 'in', $uids);
- else
- $model = $model->where("{$alias}uid", 0);
- break;
- case 2:
- $uids = self::where('spread_uid', $where['uid'])->column('uid');
- if (count($uids))
- $spread_uid_two = self::where('spread_uid', 'in', $uids)->column('uid');
- else
- $spread_uid_two = [0];
- if (count($spread_uid_two))
- $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
- else
- $model = $model->where("{$alias}uid", 0);
- break;
- default:
- $uids = self::where('spread_uid', $where['uid'])->column('uid');
- if (count($uids)) {
- if ($spread_uid_two = self::where('spread_uid', 'in', $uids)->column('uid')) {
- $uids = array_merge($uids, $spread_uid_two);
- $uids = array_unique($uids);
- $uids = array_merge($uids);
- }
- $model = $model->where("{$alias}uid", 'in', $uids);
- } else
- $model = $model->where("{$alias}uid", 0);
- break;
- }
- }
- if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
- if (isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}phone|{$alias}nickname|{$alias}name|{$alias}uid", 'LIKE', "%$where[nickname]%");
- return $model->where($alias . 'status', 1);
- }
- public static function getSairBadge($where)
- {
- $data['number'] = self::setSairWhere($where, new self())->count();
- $where['type'] = 1;
- $data['one_number'] = self::setSairWhere($where, new self())->count();
- $where['type'] = 2;
- $data['two_number'] = self::setSairWhere($where, new self())->count();
- $col = $data['two_number'] > 0 ? 4 : 6;
- return [
- [
- 'name' => '总人数',
- 'field' => '人',
- 'count' => $data['number'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => $col,
- ],
- [
- 'name' => '一级人数',
- 'field' => '人',
- 'count' => $data['one_number'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => $col,
- ],
- [
- 'name' => '二级人数',
- 'field' => '人',
- 'count' => $data['two_number'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => $col,
- ],
- ];
- }
- /**
- * 推广订单
- * @param array $where
- * @return array
- * */
- public static function getStairOrderList($where)
- {
- if (!isset($where['uid'])) return [];
- $data = self::setSairOrderWhere($where, new StoreOrder())->page((int)$where['page'], (int)$where['limit'])->select();
- $data = count($data) ? $data->toArray() : [];
- $Info = self::where('uid', $where['uid'])->find();
- foreach ($data as &$item) {
- $userInfo = self::where('uid', $item['uid'])->find();
- $item['user_info'] = '';
- $item['avatar'] = '';
- if ($userInfo) {
- $item['user_info'] = $userInfo->nickname . '|' . ($userInfo->phone ? $userInfo->phone : '');
- $item['avatar'] = $userInfo->avatar;
- }
- $item['spread_info'] = $Info->nickname . "|" . ($Info->phone ? $Info->phone . "|" : '') . $Info->uid;
- $brokerage = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'link_id' => $item['id'], 'uid' => $where['uid']])->value('number');
- $brokerage_return = UserBill::where(['category' => 'now_money', 'type' => 'brokerage_return', 'link_id' => $item['id'], 'uid' => $where['uid']])->value('number');
- $item['number_price'] = bcsub($brokerage, $brokerage_return, 2);
- $item['_pay_time'] = date('Y-m-d H:i:s', $item['pay_time']);
- $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
- $item['take_time'] = ($change_time = StoreOrderStatus::where(['change_type' => 'user_take_delivery', 'oid' => $item['id']])->value('change_time')) ?
- date('Y-m-d H:i:s', $change_time) : '暂无';
- }
- $count = self::setSairOrderWhere($where, new StoreOrder())->count();
- return compact('data', 'count');
- }
- public static function setSairOrderWhere($where, $model = null, $alias = '')
- {
- $model = $model === null ? new self() : $model;
- if (!isset($where['uid'])) return $model;
- if ($alias) {
- $model = $model->alias($alias);
- $alias .= '.';
- }
- if (isset($where['type'])) {
- switch ((int)$where['type']) {
- case 1:
- $uids = self::where('spread_uid', $where['uid'])->column('uid');
- if (count($uids))
- $model = $model->where("{$alias}uid", 'in', $uids);
- else
- $model = $model->where("{$alias}uid", 0);
- break;
- case 2:
- $uids = self::where('spread_uid', $where['uid'])->column('uid');
- if (count($uids))
- $spread_uid_two = self::where('spread_uid', 'in', $uids)->column('uid');
- else
- $spread_uid_two = [0];
- if (count($spread_uid_two))
- $model = $model->where("{$alias}uid", 'in', $spread_uid_two);
- else
- $model = $model->where("{$alias}uid", 0);
- break;
- default:
- $uids = self::where('spread_uid', $where['uid'])->column('uid');
- if (count($uids)) {
- if ($spread_uid_two = self::where('spread_uid', 'in', $uids)->column('uid')) {
- $uids = array_merge($uids, $spread_uid_two);
- $uids = array_unique($uids);
- $uids = array_merge($uids);
- }
- $model = $model->where("{$alias}uid", 'in', $uids);
- } else
- $model = $model->where("{$alias}uid", 0);
- break;
- }
- }
- if (isset($where['data']) && $where['data']) $model = self::getModelTime($where, $model, "{$alias}add_time");
- return $model->where("{$alias}is_del", 0)->where($alias . 'paid', 1)->where($alias . 'refund_status', 0)->where($alias . 'type', 'in', '0,1')->order($alias . 'add_time desc');
- }
- /*
- * 推广订单统计
- * @param array $where
- * @return array
- * */
- public static function getStairOrderBadge($where)
- {
- if (!isset($where['uid'])) return [];
- $data['order_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
- $data['order_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
- $ids = self::setSairOrderWhere($where, new StoreOrder())->where(['paid' => 1, 'is_del' => 0, 'refund_status' => 0])->where('type', 'in', '0,1')->column('id');
- $data['number_price'] = 0;
- if (count($ids)) {
- $brokerage = UserBill::where(['category' => 'now_money', 'type' => 'brokerage', 'uid' => $where['uid']])->where('link_id', 'in', $ids)->sum('number');
- $brokerage_return = UserBill::where(['category' => 'now_money', 'type' => 'brokerage_return', 'uid' => $where['uid']])->where('link_id', 'in', $ids)->sum('number');
- $data['number_price'] = bcsub($brokerage, $brokerage_return, 2);
- }
- $where['type'] = 1;
- $data['one_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
- $data['one_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
- $where['type'] = 2;
- $data['two_price'] = self::setSairOrderWhere($where, new StoreOrder())->sum('pay_price');
- $data['two_count'] = self::setSairOrderWhere($where, new StoreOrder())->count();
- return [
- [
- 'name' => '总金额',
- 'field' => '元',
- 'count' => $data['order_price'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '订单总数',
- 'field' => '单',
- 'count' => $data['order_count'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '返佣总金额',
- 'field' => '元',
- 'count' => $data['number_price'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '一级总金额',
- 'field' => '元',
- 'count' => $data['one_price'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '一级订单数',
- 'field' => '单',
- 'count' => $data['one_count'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '二级总金额',
- 'field' => '元',
- 'count' => $data['two_price'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- [
- 'name' => '二级订单数',
- 'field' => '单',
- 'count' => $data['two_count'],
- 'background_color' => 'layui-bg-cyan',
- 'col' => 3,
- ],
- ];
- }
- }
|