WechatUser.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\wechat;
  12. use app\admin\model\order\StoreOrder;
  13. use app\admin\model\order\StoreOrderStatus;
  14. use app\admin\model\user\User;
  15. use app\admin\model\user\UserExtract;
  16. use service\ExportService;
  17. use service\QrcodeService;
  18. use think\Cache;
  19. use think\Config;
  20. use traits\ModelTrait;
  21. use basic\ModelBasic;
  22. use service\WechatService;
  23. use service\PHPExcelService;
  24. use service\SystemConfigService;
  25. use app\admin\model\user\UserBill;
  26. /**
  27. * 微信用户 model
  28. * Class WechatUser
  29. * @package app\admin\model\wechat
  30. */
  31. class WechatUser extends ModelBasic
  32. {
  33. use ModelTrait;
  34. protected $insert = ['add_time'];
  35. /**
  36. * 用uid获得openid
  37. * @param $uid
  38. * @return mixed
  39. */
  40. public static function uidToOpenid($uid,$update = false)
  41. {
  42. $cacheName = 'openid_'.$uid;
  43. $openid = Cache::get($cacheName);
  44. if($openid && !$update) return $openid;
  45. $openid = self::where('uid',$uid)->value('openid');
  46. if(!$openid) exception('对应的openid不存在!');
  47. Cache::set($cacheName,$openid,0);
  48. return $openid;
  49. }
  50. public static function setAddTimeAttr($value)
  51. {
  52. return time();
  53. }
  54. /**
  55. * .添加新用户
  56. * @param $openid
  57. * @return object
  58. */
  59. public static function setNewUser($openid)
  60. {
  61. $userInfo = WechatService::getUserInfo($openid);
  62. $userInfo['tagid_list'] = implode(',',$userInfo['tagid_list']);
  63. return self::set($userInfo);
  64. }
  65. /**
  66. * 更新用户信息
  67. * @param $openid
  68. * @return bool
  69. */
  70. public static function updateUser($openid)
  71. {
  72. $userInfo = WechatService::getUserInfo($openid);
  73. $userInfo['tagid_list'] = implode(',',$userInfo['tagid_list']);
  74. return self::edit($userInfo,$openid,'openid');
  75. }
  76. /**
  77. * 用户存在就更新 不存在就添加
  78. * @param $openid
  79. */
  80. public static function saveUser($openid)
  81. {
  82. self::be($openid,'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  83. }
  84. /**
  85. * 用户取消关注
  86. * @param $openid
  87. * @return bool
  88. */
  89. public static function unSubscribe($openid)
  90. {
  91. return self::edit(['subscribe'=>0],$openid,'openid');
  92. }
  93. /**
  94. * 获取微信用户
  95. * @param array $where
  96. * @return array
  97. */
  98. public static function systemPage($where = array(),$isall=false){
  99. $model = new self;
  100. $model = $model->where('openid','NOT NULL');
  101. if($where['nickname'] !== '') $model = $model->where('nickname','LIKE',"%$where[nickname]%");
  102. if($where['data'] !== ''){
  103. list($startTime,$endTime) = explode(' - ',$where['data']);
  104. $model = $model->where('add_time','>',strtotime($startTime));
  105. $model = $model->where('add_time','<',strtotime($endTime));
  106. }
  107. if(isset($where['tagid_list']) && $where['tagid_list'] !== ''){
  108. $tagid_list = explode(',',$where['tagid_list']);
  109. foreach ($tagid_list as $v){
  110. $model = $model->where('tagid_list','LIKE',"%$v%");
  111. }
  112. }
  113. if(isset($where['groupid']) && $where['groupid'] !== '-1' ) $model = $model->where('groupid',"$where[groupid]");
  114. if(isset($where['sex']) && $where['sex'] !== '' ) $model = $model->where('sex',"$where[sex]");
  115. if(isset($where['subscribe']) && $where['subscribe'] !== '' ) $model = $model->where('subscribe',"$where[subscribe]");
  116. $model = $model->order('uid desc');
  117. if(isset($where['export']) && $where['export'] == 1){
  118. $list = $model->select()->toArray();
  119. $export = [];
  120. foreach ($list as $index=>$item){
  121. $export[] = [
  122. $item['nickname'],
  123. $item['sex'],
  124. $item['country'].$item['province'].$item['city'],
  125. $item['subscribe'] == 1? '关注':'未关注',
  126. ];
  127. $list[$index] = $item;
  128. }
  129. PHPExcelService::setExcelHeader(['名称','性别','地区','是否关注公众号'])
  130. ->setExcelTile('微信用户导出','微信用户导出'.time(),' 生成时间:'.date('Y-m-d H:i:s',time()))
  131. ->setExcelContent($export)
  132. ->ExcelSave();
  133. }
  134. return self::page($model,$where);
  135. }
  136. public static function setSpreadWhere($where=[],$alias='a',$model=null)
  137. {
  138. $model=is_null($model) ? new self() : $model;
  139. if($alias){
  140. $model=$model->alias($alias)->join('user u','a.uid=u.uid')->order('u.uid desc');
  141. $alias.='.';
  142. }
  143. $status = (int)SystemConfigService::get('store_brokerage_statu');
  144. if ($status == 1) {
  145. if ($Listuids = User::where('is_promoter',1)->field('uid')->select()) {
  146. $newUids = [];
  147. foreach ($Listuids as $item){
  148. $newUids[] = $item['uid'];
  149. }
  150. $uids = $newUids;
  151. unset($newUids);
  152. $model = $model->where($alias . 'uid', 'in', implode(',', $uids));
  153. }else
  154. $model = $model->where($alias.'uid',-1);
  155. }
  156. if($where['nickname'] !== '') $model = $model->where("{$alias}nickname|{$alias}uid|u.phone",'LIKE',"%$where[nickname]%");
  157. if((isset($where['start_time']) && isset($where['end_time'])) && $where['start_time'] !== '' && $where['end_time'] !== ''){
  158. $model = $model->where("{$alias}add_time",'between',[strtotime($where['start_time']),strtotime($where['end_time'])]);
  159. }
  160. if(isset($where['sex']) && $where['sex'] !== '' ) $model = $model->where($alias.'sex',$where['sex']);
  161. if(isset($where['subscribe']) && $where['subscribe'] !== '' ) $model = $model->where($alias.'subscribe',$where['subscribe']);
  162. if(isset($where['order']) && $where['order'] != '') $model = $model->order($where['order']);
  163. if(isset($where['is_time']) && isset($where['data']) && $where['data']) $model = self::getModelTime($where,$model,$alias.'add_time');
  164. return $model;
  165. }
  166. /*
  167. * 获取
  168. * */
  169. public static function getSpreadBadge($where)
  170. {
  171. $where['is_time']=1;
  172. $listuids = self::setSpreadWhere($where)->field('u.uid')->select();
  173. $newUids = [];
  174. foreach ($listuids as $item){
  175. $newUids[] =$item['uid'];
  176. }
  177. $uids = $newUids;
  178. unset($uid,$newUids);
  179. //分销员人数
  180. $data['sum_count'] = count($uids);
  181. $data['spread_sum'] = 0;
  182. $data['order_count'] = 0;
  183. $data['pay_price'] = 0;
  184. $data['number'] = 0;
  185. $data['extract_count'] = 0;
  186. $data['extract_price'] = 0;
  187. if($data['sum_count']){
  188. //发展会员人数
  189. $data['spread_sum'] = User::where('spread_uid','in',$uids)->count();
  190. //订单总数
  191. $data['order_count'] = StoreOrder::where('uid','in',$uids)->where(['paid'=>1,'refund_status'=>0,'type'=>0])->count();
  192. //订单金额
  193. $data['pay_price'] = StoreOrder::where('uid','in',$uids)->where(['paid'=>1,'refund_status'=>0,'type'=>0])->sum('pay_price');
  194. //可提现金额
  195. $data['number'] = User::where('uid','in',$uids)->sum('brokerage_price');
  196. //提现次数
  197. $data['extract_count'] = UserExtract::where('uid','in',$uids)->count();
  198. //获取某个用户可提现金额
  199. $data['extract_price'] = User::getextractPrice($uids,$where);
  200. }
  201. return [
  202. [
  203. 'name'=>'分销员人数',
  204. 'field'=>'人',
  205. 'count'=>$data['sum_count'],
  206. 'background_color'=>'layui-bg-cyan',
  207. 'col'=>2,
  208. ],
  209. [
  210. 'name'=>'发展会员人数',
  211. 'field'=>'人',
  212. 'count'=>$data['spread_sum'],
  213. 'background_color'=>'layui-bg-cyan',
  214. 'col'=>2,
  215. ],
  216. [
  217. 'name'=>'分销订单数',
  218. 'field'=>'单',
  219. 'count'=>$data['order_count'],
  220. 'background_color'=>'layui-bg-cyan',
  221. 'col'=>2,
  222. ],
  223. [
  224. 'name'=>'订单金额',
  225. 'field'=>'元',
  226. 'count'=>$data['pay_price'],
  227. 'background_color'=>'layui-bg-cyan',
  228. 'col'=>2,
  229. ],
  230. [
  231. 'name'=>'提现金额',
  232. 'field'=>'元',
  233. 'count'=>$data['number'],
  234. 'background_color'=>'layui-bg-cyan',
  235. 'col'=>2,
  236. ],
  237. [
  238. 'name'=>'提现次数',
  239. 'field'=>'次',
  240. 'count'=>$data['extract_count'],
  241. 'background_color'=>'layui-bg-cyan',
  242. 'col'=>2,
  243. ],
  244. [
  245. 'name'=>'未提现金额',
  246. 'field'=>'元',
  247. 'count'=>$data['extract_price'],
  248. 'background_color'=>'layui-bg-cyan',
  249. 'col'=>2,
  250. ],
  251. ];
  252. }
  253. /**
  254. * 获取分销用户
  255. * @param array $where
  256. * @return array
  257. */
  258. public static function agentSystemPage($where = array()){
  259. $where['is_time']=1;
  260. $model=self::setSpreadWhere($where);
  261. $status =SystemConfigService::get('store_brokerage_statu');
  262. if(isset($where['excel']) && $where['excel'] == 1){
  263. $list = $model->field(['a.uid','u.phone','a.nickname','a.sex','a.country','a.province','a.city','a.now_money','a.subscribe','u.brokerage_price'])->select()->toArray();
  264. $export = [];
  265. foreach ($list as $index=>$item){
  266. $Listuids = self::getModelTime($where,User::where('spread_uid',$item['uid']))->field('uid')->select();
  267. $newUids = [];
  268. foreach ($Listuids as $val){
  269. $newUids[] = $val['uid'];
  270. }
  271. $uids = $newUids;
  272. unset($uid,$newUids);
  273. $item['spread_count'] = count($uids);
  274. if(count($uids)){
  275. $ListUidTwo = User::where('spread_uid','in',$uids)->field('uid')->select();
  276. $newUids = [];
  277. foreach ($ListUidTwo as $val){
  278. $newUids[] = $val['uid'];
  279. }
  280. $uidTwo = $newUids;
  281. unset($uid,$newUids);
  282. $uids = array_merge($uids,$uidTwo);
  283. $uids = array_unique($uids);
  284. $uids = array_merge($uids);
  285. }
  286. $item['extract_sum_price'] = self::getModelTime($where,UserExtract::where('uid',$item['uid']))->sum('extract_price');
  287. $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现金额
  288. $item['extract_count_num'] = UserExtract::getUserCountNum($item['uid'],$where);//提现次数
  289. $item['order_price'] = count($uids) ? StoreOrder::where('uid','in',$uids)->where(['paid'=>1,'refund_status'=>0,'type'=>0])->sum('pay_price') : 0;//订单金额
  290. $item['order_count'] = count($uids) ? StoreOrder::where('uid','in',$uids)->where(['paid'=>1,'refund_status'=>0,'type'=>0])->count() : 0;//订单数量
  291. $item['stair'] = self::getUserSpreadUidCount($item['uid'],0,$where);//一级推荐人
  292. $item['second'] = self::getUserSpreadUidCount($item['uid'],1,$where);//二级推荐人
  293. $item['order_stair'] = self::getUserSpreadOrderCount($item['uid'],0,$where);//一级推荐人订单
  294. $item['order_second'] = self::getUserSpreadOrderCount($item['uid'],1,$where);//二级推荐人订单
  295. //可提现佣金
  296. $item['new_money'] = $item['brokerage_price'];
  297. //总共佣金
  298. $income=self::getModelTime($where,UserBill::where(['uid'=>$item['uid'],'category'=>'now_money','type'=>'brokerage','pm'=>1,'status'=>1]))->sum('number');
  299. $return=self::getModelTime($where,UserBill::where(['uid'=>$item['uid'],'category'=>'now_money','type'=>'brokerage_return','pm'=>0,'status'=>1]))->sum('number');
  300. $item['brokerage_money'] = bcsub($income,$return,2);
  301. $item['spread_name']='暂无';
  302. if($spread_uid=User::where('uid',$item['uid'])->value('spread_uid')) {
  303. if($user=User::where('uid',$spread_uid)->field(['uid','nickname'])->find()){
  304. $item['spread_name']=$user['nickname'].'/'.$user['uid'];
  305. }
  306. }
  307. $export[] = [
  308. $item['uid'],
  309. $item['nickname'],
  310. $item['phone'],
  311. $item['spread_count'],
  312. $item['order_count'],
  313. $item['order_price'],
  314. $item['brokerage_money'],
  315. $item['extract_count_price'],
  316. $item['extract_count_num'],
  317. $item['new_money'],
  318. $item['spread_name'],
  319. ];
  320. }
  321. PHPExcelService::setExcelHeader(['用户编号','昵称','电话号码','推广用户数量','订单数量','推广订单金额','佣金金额','已提现金额','提现次数','未提现金额','上级推广人'])
  322. ->setExcelTile('推广用户','推广用户导出'.time(),' 生成时间:'.date('Y-m-d H:i:s',time()))
  323. ->setExcelContent($export)
  324. ->ExcelSave();
  325. }
  326. $data = $model->page((int)$where['page'],(int)$where['limit'])->select();
  327. $data = count($data) ? $data->toArray() : [];
  328. foreach ($data as &$item){
  329. if((int)$status==2) $item['is_show']=false;
  330. else $item['is_show']=true;
  331. $Listuids = self::getModelTime($where,User::where('spread_uid',$item['uid']))->field('uid')->select();
  332. $newUids = [];
  333. foreach ($Listuids as $val){
  334. $newUids[] = $val['uid'];
  335. }
  336. $uids = $newUids;
  337. unset($uid,$newUids);
  338. $item['spread_count'] = count($uids);
  339. if(count($uids)){
  340. $ListUidTwo = User::where('spread_uid','in',$uids)->field('uid')->select();
  341. $newUids = [];
  342. foreach ($ListUidTwo as $val){
  343. $newUids[] = $val['uid'];
  344. }
  345. $uidTwo = $newUids;
  346. unset($uid,$newUids);
  347. $uids = array_merge($uids,$uidTwo);
  348. $uids = array_unique($uids);
  349. $uids = array_merge($uids);
  350. }
  351. $item['extract_sum_price'] = self::getModelTime($where,UserExtract::where('uid',$item['uid']))->sum('extract_price');
  352. $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现金额
  353. $item['extract_count_num'] = UserExtract::getUserCountNum($item['uid'],$where);//提现次数
  354. $item['order_price'] = count($uids) ? StoreOrder::where('uid','in',$uids)->where(['paid'=>1,'refund_status'=>0])->sum('pay_price') : 0;//订单金额
  355. $item['order_count'] = count($uids) ? StoreOrder::where('uid','in',$uids)->where(['paid'=>1,'refund_status'=>0])->count() : 0;//订单数量
  356. if($item['openid'])
  357. $item['type_name'] = '公众号';
  358. $item['subscribe_name']=$item['subscribe'] ? '已关注' : ($item['routine_openid'] && !$item['unionid'] ? '暂无' : '未关注') ;
  359. if($item['sex']==1)
  360. $item['sex_name'] = '男';
  361. else if($item['sex']==2)
  362. $item['sex_name'] = '女';
  363. else if($item['sex']==0)
  364. $item['sex_name'] = '未知';
  365. $item['spread_name']='暂无';
  366. if($spread_uid=User::where('uid',$item['uid'])->value('spread_uid')) {
  367. if($user=User::where('uid',$spread_uid)->field(['uid','nickname'])->find()){
  368. $item['spread_name']=$user['nickname'].'/'.$user['uid'];
  369. }
  370. }
  371. $income=self::getModelTime($where,UserBill::where(['uid'=>$item['uid'],'category'=>'now_money','type'=>'brokerage','pm'=>1,'status'=>1]))->sum('number');
  372. $return=self::getModelTime($where,UserBill::where(['uid'=>$item['uid'],'category'=>'now_money','type'=>'brokerage_return','pm'=>0,'status'=>1]))->sum('number');
  373. //总共佣金
  374. $item['brokerage_money'] = bcsub($income,$return,2);
  375. //可提现佣金
  376. $item['new_money'] = $item['brokerage_price'];
  377. $item['stair'] = self::getUserSpreadUidCount($item['uid'],0,$where);//一级推荐人
  378. $item['second'] = self::getUserSpreadUidCount($item['uid'],1,$where);//二级推荐人
  379. $item['order_stair'] = self::getUserSpreadOrderCount($item['uid'],0,$where);//一级推荐人订单
  380. $item['order_second'] = self::getUserSpreadOrderCount($item['uid'],1,$where);//二级推荐人订单
  381. }
  382. $count = self::setSpreadWhere($where)->count();
  383. return compact('data','count');
  384. }
  385. /**
  386. * 获取推广人数
  387. * @param $uid //用户的uid
  388. * @param int $spread
  389. * $spread 0 一级推广人数 1 二级推广人数
  390. * @return int|string
  391. */
  392. public static function getUserSpreadUidCount($uid,$spread = 1){
  393. $userStair = User::where('spread_uid',$uid)->column('uid','uid');//获取一级推家人
  394. if($userStair){
  395. if(!$spread) return count($userStair);//返回一级推人人数
  396. else return User::where('spread_uid','IN',implode(',',$userStair))->count();//二级推荐人数
  397. }else return 0;
  398. }
  399. /**
  400. * 获取推广人的订单
  401. * @param $uid
  402. * @param int $spread
  403. * $spread 0 一级推广总订单 1 所有推广总订单
  404. * @return int|string
  405. */
  406. public static function getUserSpreadOrderCount($uid,$spread = 1){
  407. $userStair = User::where('spread_uid',$uid)->column('uid','uid');//获取一级推家人uid
  408. if($userStair){
  409. if(!$spread){
  410. return StoreOrder::where('uid','IN',implode(',',$userStair))->where('paid',1)->where('refund_status',0)->where('status',2)->count();//获取一级推广人订单数
  411. }
  412. else{
  413. $userSecond = User::where('spread_uid','IN',implode(',',$userStair))->column('uid','uid');//二级推广人的uid
  414. if($userSecond){
  415. return StoreOrder::where('uid','IN',implode(',',$userSecond))->where('paid',1)->where('refund_status',0)->where('status',2)->count();//获取二级推广人订单数
  416. }else return 0;
  417. }
  418. }else return 0;
  419. }
  420. /**
  421. * 获取筛选后的所有用户uid
  422. * @param array $where
  423. * @return array
  424. */
  425. public static function getAll($where = array()){
  426. $model = new self;
  427. if($where['nickname'] !== '') $model = $model->where('nickname','LIKE',"%$where[nickname]%");
  428. if($where['data'] !== ''){
  429. list($startTime,$endTime) = explode(' - ',$where['data']);
  430. $model = $model->where('add_time','>',strtotime($startTime));
  431. $model = $model->where('add_time','<',strtotime($endTime));
  432. }
  433. if($where['tagid_list'] !== ''){
  434. $model = $model->where('tagid_list','LIKE',"%$where[tagid_list]%");
  435. }
  436. if($where['groupid'] !== '-1' ) $model = $model->where('groupid',"$where[groupid]");
  437. if($where['sex'] !== '' ) $model = $model->where('sex',"$where[sex]");
  438. return $model->column('uid','uid');
  439. }
  440. /**
  441. * 获取已关注的用户
  442. * @param $field
  443. */
  444. public static function getSubscribe($field){
  445. return self::where('subscribe',1)->column($field);
  446. }
  447. public static function getUserAll($field){
  448. return self::column($field);
  449. }
  450. public static function getUserTag()
  451. {
  452. $tagName = Config::get('system_wechat_tag');
  453. return Cache::tag($tagName)->remember('_wechat_tag',function () use($tagName){
  454. Cache::tag($tagName,['_wechat_tag']);
  455. $tag = WechatService::userTagService()->lists()->toArray()['tags']?:array();
  456. $list = [];
  457. foreach ($tag as $g){
  458. $list[$g['id']] = $g;
  459. }
  460. return $list;
  461. });
  462. }
  463. public static function clearUserTag()
  464. {
  465. Cache::rm('_wechat_tag');
  466. }
  467. public static function getUserGroup()
  468. {
  469. $tagName = Config::get('system_wechat_tag');
  470. return Cache::tag($tagName)->remember('_wechat_group',function () use($tagName){
  471. Cache::tag($tagName,['_wechat_group']);
  472. $tag = WechatService::userGroupService()->lists()->toArray()['groups']?:array();
  473. $list = [];
  474. foreach ($tag as $g){
  475. $list[$g['id']] = $g;
  476. }
  477. return $list;
  478. });
  479. }
  480. public static function clearUserGroup()
  481. {
  482. Cache::rm('_wechat_group');
  483. }
  484. /**
  485. * 同步微信用户表内的 一级推荐人 二级推荐人 一级推荐人订单 二级推荐人订单
  486. */
  487. public static function setWechatUserOrder(){
  488. $uidAll = self::column('uid','uid');
  489. $item = [];
  490. foreach ($uidAll as $k=>$v){
  491. $item['stair'] = self::getUserSpreadUidCount($v,0);//一级推荐人
  492. $item['second'] = self::getUserSpreadUidCount($v);//二级推荐人
  493. $item['order_stair'] = self::getUserSpreadOrderCount($v,0);//一级推荐人订单
  494. $item['order_second'] = self::getUserSpreadOrderCount($v);//二级推荐人订单
  495. $item['now_money'] = User::where('uid',$v)->value('now_money');//佣金
  496. if(!$item['stair'] && !$item['second'] && !$item['order_stair'] && !$item['order_second'] && !$item['now_money']) continue;
  497. else self::edit($item,$v);
  498. }
  499. }
  500. /*
  501. * 推广订单
  502. * @param array $where
  503. * @return array
  504. * */
  505. public static function getStairOrderList($where)
  506. {
  507. if(!isset($where['uid'])) return [];
  508. $data = self::setSairOrderWhere($where,new StoreOrder())->page((int)$where['page'],(int)$where['limit'])->select();
  509. $data = count($data) ? $data->toArray() : [];
  510. $Info = User::where('uid',$where['uid'])->find();
  511. foreach ($data as &$item){
  512. $userInfo = User::where('uid',$item['uid'])->find();
  513. $item['user_info'] = '';
  514. $item['avatar'] = '';
  515. if($userInfo){
  516. $item['user_info'] = $userInfo->nickname.'|'.($userInfo->phone ? $userInfo->phone .'|' : '').$userInfo->name;
  517. $item['avatar'] = $userInfo->avatar;
  518. }
  519. $item['spread_info'] = $Info->nickname."|".($Info->phone ? $Info->phone."|" : '').$Info->uid;
  520. $brokerage = UserBill::where(['category'=>'now_money','type'=>'brokerage','link_id'=>$item['id']])->value('number');
  521. $brokerage_return = UserBill::where(['category'=>'now_money','type'=>'brokerage_return','link_id'=>$item['id']])->value('number');
  522. $item['number_price'] =bcsub($brokerage,$brokerage_return,2);
  523. $item['_pay_time'] = date('Y-m-d H:i:s',$item['pay_time']);
  524. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  525. $item['take_time'] = ($change_time = StoreOrderStatus::where(['change_type'=>'user_take_delivery','oid'=>$item['id']])->value('change_time')) ?
  526. date('Y-m-d H:i:s',$change_time) : '暂无';
  527. }
  528. $count = self::setSairOrderWhere($where,new StoreOrder())->count();
  529. return compact('data','count');
  530. }
  531. public static function setSairOrderWhere($where,$model = null,$alias='')
  532. {
  533. $model = $model === null ? new self() : $model;
  534. if(!isset($where['uid'])) return $model;
  535. if($alias){
  536. $model = $model->alias($alias);
  537. $alias .= '.';
  538. }
  539. if(isset($where['type'])){
  540. switch ((int)$where['type']){
  541. case 1:
  542. $uids = User::where('spread_uid',$where['uid'])->column('uid');
  543. if(count($uids))
  544. $model = $model->where("{$alias}uid",'in',$uids);
  545. else
  546. $model = $model->where("{$alias}uid",0);
  547. break;
  548. case 2:
  549. $uids = User::where('spread_uid',$where['uid'])->column('uid');
  550. if(count($uids))
  551. $spread_uid_two=User::where('spread_uid','in',$uids)->column('uid');
  552. else
  553. $spread_uid_two=[0];
  554. if(count($spread_uid_two))
  555. $model = $model->where("{$alias}uid",'in',$spread_uid_two);
  556. else
  557. $model = $model->where("{$alias}uid",0);
  558. break;
  559. default:
  560. $uids = User::where('spread_uid',$where['uid'])->column('uid');
  561. if(count($uids)) {
  562. if($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')){
  563. $uids = array_merge($uids,$spread_uid_two);
  564. $uids = array_unique($uids);
  565. $uids = array_merge($uids);
  566. }
  567. $model = $model->where("{$alias}uid",'in',$uids);
  568. }else
  569. $model = $model->where("{$alias}uid",0);
  570. break;
  571. }
  572. }
  573. if(isset($where['data']) && $where['data']) $model = self::getModelTime($where,$model,"{$alias}add_time");
  574. return $model->where("{$alias}is_del",0)->where($alias.'paid',1);
  575. }
  576. /*
  577. * 推广订单统计
  578. * @param array $where
  579. * @return array
  580. * */
  581. public static function getStairOrderBadge($where)
  582. {
  583. if(!isset($where['uid'])) return [];
  584. $data['order_count'] = self::setSairOrderWhere($where,new StoreOrder())->count();
  585. $data['order_price'] = self::setSairOrderWhere($where,new StoreOrder())->sum('pay_price');
  586. $ids = self::setSairOrderWhere($where,new StoreOrder())->where(['paid'=>1,'is_del'=>0,'refund_status'=>0])->where('status','>',1)->column('id');
  587. $data['number_price'] = 0;
  588. if(count($ids)){
  589. $brokerage = UserBill::where(['category'=>'now_money','type'=>'brokerage','uid'=>$where['uid']])->where('link_id','in',$ids)->sum('number');
  590. $brokerage_return = UserBill::where(['category'=>'now_money','type'=>'brokerage_return','uid'=>$where['uid']])->where('link_id','in',$ids)->sum('number');
  591. $data['number_price'] =bcsub($brokerage,$brokerage_return,2);
  592. }
  593. $where['type'] = 1;
  594. $data['one_price'] = self::setSairOrderWhere($where,new StoreOrder())->sum('pay_price');
  595. $data['one_count'] = self::setSairOrderWhere($where,new StoreOrder())->count();
  596. $where['type'] = 2;
  597. $data['two_price'] = self::setSairOrderWhere($where,new StoreOrder())->sum('pay_price');
  598. $data['two_count'] = self::setSairOrderWhere($where,new StoreOrder())->count();
  599. return [
  600. [
  601. 'name'=>'总金额',
  602. 'field'=>'元',
  603. 'count'=>$data['order_price'],
  604. 'background_color'=>'layui-bg-cyan',
  605. 'col'=>3,
  606. ],
  607. [
  608. 'name'=>'订单总数',
  609. 'field'=>'单',
  610. 'count'=>$data['order_count'],
  611. 'background_color'=>'layui-bg-cyan',
  612. 'col'=>3,
  613. ],
  614. [
  615. 'name'=>'返佣总金额',
  616. 'field'=>'元',
  617. 'count'=>$data['number_price'],
  618. 'background_color'=>'layui-bg-cyan',
  619. 'col'=>3,
  620. ],
  621. [
  622. 'name'=>'一级总金额',
  623. 'field'=>'元',
  624. 'count'=>$data['one_price'],
  625. 'background_color'=>'layui-bg-cyan',
  626. 'col'=>3,
  627. ],
  628. [
  629. 'name'=>'一级订单数',
  630. 'field'=>'单',
  631. 'count'=>$data['one_count'],
  632. 'background_color'=>'layui-bg-cyan',
  633. 'col'=>3,
  634. ],
  635. [
  636. 'name'=>'二级总金额',
  637. 'field'=>'元',
  638. 'count'=>$data['two_price'],
  639. 'background_color'=>'layui-bg-cyan',
  640. 'col'=>3,
  641. ],
  642. [
  643. 'name'=>'二级订单数',
  644. 'field'=>'单',
  645. 'count'=>$data['two_count'],
  646. 'background_color'=>'layui-bg-cyan',
  647. 'col'=>3,
  648. ],
  649. ];
  650. }
  651. public static function getStairList($where)
  652. {
  653. if(!isset($where['uid'])) return [];
  654. $data = self::setSairWhere($where,new User())->order('add_time desc')->page((int)$where['page'],(int)$where['limit'])->select();
  655. $data = count($data) ? $data->toArray() : [];
  656. $userInfo = User::where('uid',$where['uid'])->find();
  657. foreach ($data as &$item){
  658. $item['spread_count'] = User::where('spread_uid',$item['uid'])->count();
  659. $item['order_count'] = StoreOrder::where('uid',$item['uid'])->where(['paid'=>1,'is_del'=>0])->count();
  660. $item['promoter_name'] = $item['is_promoter'] ? '是' : '否';
  661. $item['add_time'] = date("Y-m-d H:i:s",$item['add_time']);
  662. }
  663. $count = self::setSairWhere($where,new User())->count();
  664. return compact('data','count');
  665. }
  666. /*
  667. * 设置查询条件
  668. * @param array $where
  669. * @param object $model
  670. * @param string $alias
  671. * */
  672. public static function setSairWhere($where,$model = null,$alias='')
  673. {
  674. $model = $model === null ? new self() : $model;
  675. if(!isset($where['uid'])) return $model;
  676. if($alias){
  677. $model = $model->alias($alias);
  678. $alias .= '.';
  679. }
  680. if(isset($where['type'])){
  681. switch ((int)$where['type']){
  682. case 1:
  683. $uids = User::where('spread_uid',$where['uid'])->column('uid');
  684. if(count($uids))
  685. $model = $model->where("{$alias}uid",'in',$uids);
  686. else
  687. $model = $model->where("{$alias}uid",0);
  688. break;
  689. case 2:
  690. $uids = User::where('spread_uid',$where['uid'])->column('uid');
  691. if(count($uids))
  692. $spread_uid_two=User::where('spread_uid','in',$uids)->column('uid');
  693. else
  694. $spread_uid_two=[0];
  695. if(count($spread_uid_two))
  696. $model = $model->where("{$alias}uid",'in',$spread_uid_two);
  697. else
  698. $model = $model->where("{$alias}uid",0);
  699. break;
  700. default:
  701. $uids = User::where('spread_uid',$where['uid'])->column('uid');
  702. if(count($uids)) {
  703. if($spread_uid_two = User::where('spread_uid', 'in', $uids)->column('uid')){
  704. $uids = array_merge($uids,$spread_uid_two);
  705. $uids = array_unique($uids);
  706. $uids = array_merge($uids);
  707. }
  708. $model = $model->where("{$alias}uid",'in',$uids);
  709. }else
  710. $model = $model->where("{$alias}uid",0);
  711. break;
  712. }
  713. }
  714. if(isset($where['data']) && $where['data']) $model = self::getModelTime($where,$model,"{$alias}add_time");
  715. if(isset($where['nickname']) && $where['nickname']) $model = $model->where("{$alias}phone|{$alias}nickname|{$alias}name|{$alias}uid",'LIKE',"%$where[nickname]%");
  716. return $model->where($alias.'status',1);
  717. }
  718. public static function getSairBadge($where)
  719. {
  720. $data['number'] = self::setSairWhere($where,new User())->count();
  721. $where['type'] = 1;
  722. $data['one_number'] = self::setSairWhere($where,new User())->count();
  723. $where['type'] = 2;
  724. $data['two_number'] = self::setSairWhere($where,new User())->count();
  725. $col = $data['two_number'] > 0 ? 4 : 6;
  726. return [
  727. [
  728. 'name'=>'总人数',
  729. 'field'=>'人',
  730. 'count'=>$data['number'],
  731. 'background_color'=>'layui-bg-cyan',
  732. 'col'=>$col,
  733. ],
  734. [
  735. 'name'=>'一级人数',
  736. 'field'=>'人',
  737. 'count'=>$data['one_number'],
  738. 'background_color'=>'layui-bg-cyan',
  739. 'col'=>$col,
  740. ],
  741. [
  742. 'name'=>'二级人数',
  743. 'field'=>'人',
  744. 'count'=>$data['two_number'],
  745. 'background_color'=>'layui-bg-cyan',
  746. 'col'=>$col,
  747. ],
  748. ];
  749. }
  750. }