UserBill.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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\user;
  12. use service\SystemConfigService;
  13. use traits\ModelTrait;
  14. use basic\ModelBasic;
  15. use app\admin\model\wechat\WechatUser;
  16. /**
  17. * 用户消费新增金额明细 model
  18. * Class User
  19. * @package app\admin\model\user
  20. */
  21. class UserBill extends ModelBasic
  22. {
  23. use ModelTrait;
  24. protected $insert = ['add_time'];
  25. protected function setAddTimeAttr()
  26. {
  27. return time();
  28. }
  29. public static function income($title,$uid,$category,$type,$number,$link_id = 0,$balance = 0,$mark = '',$status = 1){
  30. $pm = 1;
  31. return self::set(compact('title','uid','link_id','category','type','number','balance','mark','status','pm'));
  32. }
  33. public static function expend($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1)
  34. {
  35. $pm = 0;
  36. return self::set(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm'));
  37. }
  38. /*
  39. * 获取佣金记录
  40. * */
  41. public static function getBillList($where,$uid)
  42. {
  43. $model=self::where('uid',$uid)->where('category','now_money')->where('type','in',['rake_back','rake_back_one','rake_back_two','extract'])
  44. ->order('add_time desc');
  45. if($where['start_date'] && $where['end_date']) $model=$model->where('add_time','between',[strtotime($where['start_date']),strtotime($where['end_date'])]);
  46. if($where['excel'])
  47. $list=$model->select();
  48. else
  49. $list=$model->page((int)$where['page'],(int)$where['limit'])->select();
  50. $list=count($list) ? $list->toArray() : [];
  51. $excel=[];
  52. foreach($list as &$item){
  53. $item['add_time']=date('Y-m-d H:i:s',$item['add_time']);
  54. $item['_type']=$item['pm'] ? '入账':'提现';
  55. switch ($item['type']){
  56. case 'rake_back':
  57. $item['order_type']='直推订单';
  58. break;
  59. case 'rake_back_one':
  60. $item['order_type']='直推订单';
  61. break;
  62. case 'rake_back_two':
  63. $item['order_type']='裂变订单';
  64. break;
  65. default:
  66. $item['order_type']='';
  67. break;
  68. }
  69. if($item['pm']){
  70. $item['pay_pice']=self::getDb('store_order')->where('id',$item['link_id'])->value('pay_price');
  71. }else{
  72. $item['pay_pice']=0;
  73. }
  74. $excel[]=[$item['add_time'],$item['_type'],$item['order_type'],$item['pay_pice'],$item['number'],$item['balance']];
  75. }
  76. if($where['excel']){
  77. \service\PHPExcelService::setExcelHeader(['时间','入账/结算','订单类型','订单金额','佣金金额','佣金余额'])
  78. ->setExcelTile('佣金记录导出','佣金记录信息'.time(),' 生成时间:'.date('Y-m-d H:i:s',time()))
  79. ->setExcelContent($excel)->ExcelSave();
  80. }
  81. return $list;
  82. }
  83. /**获取用户佣金金额
  84. * @param int $uid
  85. */
  86. public static function getCommissionAmount($uid=0){
  87. $brokerage=self::where('uid','in',$uid)->where('category','now_money')
  88. ->where('type','brokerage')->where('pm',1)->where('status',1)->sum('number');
  89. $brokerage_return=self::where('uid','in',$uid)->where('category','now_money')
  90. ->where('type','brokerage_return')->where('pm',0)->where('status',1)->sum('number');
  91. $commission=bcsub($brokerage,$brokerage_return,2);
  92. return $commission;
  93. }
  94. /**
  95. * 获取柱状图和饼状图数据
  96. *
  97. * */
  98. public static function getUserBillChart($where,$category='now_money',$type='brokerage',$pm=1,$zoom=15){
  99. $model=self::getModelTime($where,new self());
  100. $list=$model->field(['FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time','sum(number) as sum_number'])
  101. ->order('un_time asc')
  102. ->where(['category'=>$category,'type'=>$type,'pm'=>$pm])
  103. ->group('un_time')
  104. ->select();
  105. if(count($list)) $list=$list->toArray();
  106. $legdata=[];
  107. $listdata=[];
  108. $dataZoom='';
  109. foreach ($list as $item){
  110. $legdata[]=$item['un_time'];
  111. $listdata[]=$item['sum_number'];
  112. }
  113. if(count($legdata)>=$zoom) $dataZoom=$legdata[$zoom-1];
  114. //获取用户分布钱数
  115. $fenbulist=self::getModelTime($where,new self(),'a.add_time')
  116. ->alias('a')
  117. ->join('user r','a.uid=r.uid')
  118. ->field(['a.uid','sum(a.number) as sum_number','r.nickname'])
  119. ->where(['a.category'=>$category,'a.type'=>$type,'a.pm'=>$pm])
  120. ->order('sum_number desc')
  121. ->group('a.uid')
  122. ->limit(8)
  123. ->select();
  124. //获取用户当前时间段总钱数
  125. $sum_number=self::getModelTime($where,new self())
  126. ->where(['category'=>$category,'type'=>$type,'pm'=>$pm])
  127. ->sum('number');
  128. if(count($fenbulist)) $fenbulist=$fenbulist->toArray();
  129. $fenbudate=[];
  130. $fenbu_legend=[];
  131. $color=['#ffcccc','#99cc00','#fd99cc','#669966','#66CDAA','#ADFF2F','#00BFFF','#00CED1','#66cccc','#ff9900','#ffcc00','#336699','#cccc00','#99ccff','#990066'];
  132. foreach ($fenbulist as $key=>$value){
  133. $fenbu_legend[]=$value['nickname'];
  134. $items['name']=$value['nickname'];
  135. $items['value']=bcdiv($value['sum_number'],$sum_number,2)*100;
  136. $items['itemStyle']['color']=$color[$key];
  137. $fenbudate[]=$items;
  138. }
  139. return compact('legdata','listdata','fenbudate','fenbu_legend','dataZoom');
  140. }
  141. //获取头部信息
  142. public static function getRebateBadge($where){
  143. $datawhere=['category'=>'now_money','type'=>'brokerage','pm'=>1];
  144. return [
  145. [
  146. 'name'=>'返利数(笔)',
  147. 'field'=>'个',
  148. 'count'=>self::getModelTime($where,new self())->where($datawhere)->count(),
  149. 'content'=>'返利总笔数',
  150. 'background_color'=>'layui-bg-blue',
  151. 'sum'=>self::where($datawhere)->count(),
  152. 'class'=>'fa fa-bar-chart',
  153. ],
  154. [
  155. 'name'=>'返利金额(元)',
  156. 'field'=>'个',
  157. 'count'=>self::getModelTime($where,new self())->where($datawhere)->sum('number'),
  158. 'content'=>'返利总金额',
  159. 'background_color'=>'layui-bg-cyan',
  160. 'sum'=>self::where($datawhere)->sum('number'),
  161. 'class'=>'fa fa-line-chart',
  162. ],
  163. ];
  164. }
  165. //获取返佣用户信息列表
  166. public static function getFanList($where){
  167. $datawhere=['a.category'=>'now_money','a.type'=>'brokerage','a.pm'=>1];
  168. $list=self::alias('a')->join('user r','a.uid=r.uid')
  169. ->where($datawhere)
  170. ->order('a.number desc')
  171. ->join('store_order o','o.id=a.link_id')
  172. ->field(['o.order_id','FROM_UNIXTIME(a.add_time,"%Y-%c-%d") as add_time','a.uid','o.uid as down_uid','r.nickname','r.avatar','r.spread_uid','r.level','a.number'])
  173. ->page((int)$where['page'],(int)$where['limit'])
  174. ->select();
  175. if(count($list)) $list=$list->toArray();
  176. return $list;
  177. }
  178. //获取返佣用户总人数
  179. public static function getFanCount(){
  180. $datawhere=['a.category'=>'now_money','a.type'=>'brokerage','a.pm'=>1];
  181. return self::alias('a')->join('user r','a.uid=r.uid')->join('store_order o','o.id=a.link_id')->where($datawhere)->count();
  182. }
  183. //获取用户充值数据
  184. public static function getEchartsRecharge($where,$limit=15){
  185. $datawhere=['category'=>'now_money','pm'=>1];
  186. $list=self::getModelTime($where,self::where($datawhere)->where('type','in',['recharge','system_add']))
  187. ->field(['sum(number) as sum_money','FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time','count(id) as count'])
  188. ->group('un_time')
  189. ->order('un_time asc')
  190. ->select();
  191. if(count($list)) $list=$list->toArray();
  192. $sum_count=self::getModelTime($where,self::where($datawhere)->where('type','in',['recharge','system_add']))->count();
  193. $xdata=[];
  194. $seriesdata=[];
  195. $data=[];
  196. $zoom='';
  197. foreach ($list as $value){
  198. $xdata[]=$value['un_time'];
  199. $seriesdata[]=$value['sum_money'];
  200. $data[]=$value['count'];
  201. }
  202. if(count($xdata)>$limit){
  203. $zoom=$xdata[$limit-5];
  204. }
  205. return compact('xdata','seriesdata','data','zoom');
  206. }
  207. //获取佣金提现列表
  208. public static function getExtrctOneList($where,$uid){
  209. $list=self::setOneWhere($where,$uid)
  210. ->field(['number','link_id','mark','FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i:%s") as _add_time','status'])
  211. ->select();
  212. count($list) && $list=$list->toArray();
  213. $count=self::setOneWhere($where,$uid)->count();
  214. foreach ($list as &$value){
  215. $value['order_id']=db('store_order')->where(['order_id'=>$value['link_id']])->value('order_id');
  216. }
  217. return ['data'=>$list,'count'=>$count];
  218. }
  219. //设置单个用户查询
  220. public static function setOneWhere($where,$uid){
  221. $model=self::where(['uid'=>$uid,'category'=>'now_money','type'=>'brokerage']);
  222. $time['data']='';
  223. if($where['start_time']!='' && $where['end_time']!=''){
  224. $time['data']=$where['start_time'].' - '.$where['end_time'];
  225. $model=self::getModelTime($time,$model);
  226. }
  227. if($where['nickname']!=''){
  228. $model=$model->where('link_id|mark','like',"%$where[nickname]%");
  229. }
  230. return $model;
  231. }
  232. //查询积分个人明细
  233. public static function getOneIntegralList($where){
  234. return self::setWhereList(
  235. $where,
  236. ['deduction','system_add'],
  237. ['title','number','balance','mark','FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time']
  238. );
  239. }
  240. //查询个人签到明细
  241. public static function getOneSignList($where){
  242. return self::setWhereList(
  243. $where,'sign',
  244. ['title','number','mark','FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time']
  245. );
  246. }
  247. //查询个人余额变动记录
  248. public static function getOneBalanceChangList($where){
  249. $list=self::setWhereList(
  250. $where,
  251. ['system_add', 'pay_product', 'extract','extract_fail','pay_goods','pay_sign_up', 'pay_product_refund', 'system_sub'],
  252. ['FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time','title','type','mark','number','balance','pm','status'],
  253. 'now_money'
  254. );
  255. foreach ($list as &$item){
  256. switch ($item['type']){
  257. case 'system_add':
  258. $item['_type']='系统添加';
  259. break;
  260. case 'pay_product':
  261. $item['_type']='商品购买';
  262. break;
  263. case 'extract':
  264. $item['_type']='提现';
  265. break;
  266. case 'extract_fail':
  267. $item['_type']='提现失败';
  268. break;
  269. case 'pay_goods':
  270. $item['_type']='购买商品';
  271. break;
  272. case 'pay_sign_up':
  273. $item['_type']='活动报名';
  274. break;
  275. case 'pay_product_refund':
  276. $item['_type']='退款';
  277. break;
  278. case 'system_sub':
  279. $item['_type']='系统减少';
  280. break;
  281. }
  282. $item['_pm']=$item['pm']==1 ? '获得': '支出';
  283. }
  284. return $list;
  285. }
  286. //设置where条件分页.返回数据
  287. public static function setWhereList($where,$type='',$field=[],$category='gold_num'){
  288. $models=self::where('uid',$where['uid'])
  289. ->where('category',$category)
  290. ->page((int)$where['page'],(int)$where['limit'])
  291. ->field($field);
  292. if(is_array($type)){
  293. $models=$models->where('type','in',$type);
  294. }else{
  295. $models=$models->where('type',$type);
  296. }
  297. return ($list=$models->select()) && count($list) ? $list->toArray():[];
  298. }
  299. //获取积分统计头部信息
  300. public static function getScoreBadgeList($where){
  301. return [
  302. [
  303. 'name'=>'总积分',
  304. 'field'=>'个',
  305. 'count'=>self::getModelTime($where,new self())->where('category','integral')->where('type','in',['gain','system_sub','deduction','sign'])->sum('number'),
  306. 'background_color'=>'layui-bg-blue',
  307. 'col'=>4,
  308. ],
  309. [
  310. 'name'=>'已使用积分',
  311. 'field'=>'个',
  312. 'count'=>self::getModelTime($where,new self())->where('category','integral')->where('type','deduction')->sum('number'),
  313. 'background_color'=>'layui-bg-cyan',
  314. 'col'=>4,
  315. ],
  316. [
  317. 'name'=>'未使用积分',
  318. 'field'=>'个',
  319. 'count'=>self::getModelTime($where,db('user'))->sum('integral'),
  320. 'background_color'=>'layui-bg-cyan',
  321. 'col'=>4,
  322. ],
  323. ];
  324. }
  325. //获取积分统计曲线图和柱状图
  326. public static function getScoreCurve($where){
  327. //发放积分趋势图
  328. $list=self::getModelTime($where,self::where('category','integral')
  329. ->field(['FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time','sum(number) as sum_number'])
  330. ->group('_add_time')->order('_add_time asc'))->select()->toArray();
  331. $date=[];
  332. $zoom='';
  333. $seriesdata=[];
  334. foreach ($list as $item){
  335. $date[]=$item['_add_time'];
  336. $seriesdata[]=$item['sum_number'];
  337. }
  338. unset($item);
  339. if(count($date)>$where['limit']){
  340. $zoom=$date[$where['limit']-5];
  341. }
  342. //使用积分趋势图
  343. $deductionlist=self::getModelTime($where,self::where('category','integral')->where('type','deduction')
  344. ->field(['FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time','sum(number) as sum_number'])
  345. ->group('_add_time')->order('_add_time asc'))->select()->toArray();
  346. $deduction_date=[];
  347. $deduction_zoom='';
  348. $deduction_seriesdata=[];
  349. foreach ($deductionlist as $item){
  350. $deduction_date[]=$item['_add_time'];
  351. $deduction_seriesdata[]=$item['sum_number'];
  352. }
  353. if(count($deductionlist)>$where['limit']){
  354. $deduction_zoom=$deductionlist[$where['limit']-5];
  355. }
  356. return compact('date','seriesdata','zoom','deduction_date','deduction_zoom','deduction_seriesdata');
  357. }
  358. public static function getGroupList($where){
  359. $where['data']=$where['start_time'].' - '.$where['end_time'];
  360. $data=self::getGroupWhere($where)->page((int)$where['page'],(int)$where['limit'])
  361. ->field(['a.*','u.nickname','u.name','g.phone','g.share_name','g.share_uid'])->select();
  362. $data=count($data) ? $data->toArray() : [];
  363. $count=self::getGroupWhere($where)->count();
  364. foreach ($data as &$item){
  365. $item['spread_uid']=$item['link_id'];
  366. $item['spread_nickname']=User::where('uid',$item['link_id'])->value('nickname');
  367. $item['spread_phone']=Group::where('uid',$item['link_id'])->value('phone');
  368. }
  369. return compact('data','count');
  370. }
  371. public static function getGroupWhere($where){
  372. $model=self::getModelTime($where,self::where(['a.category'=>'now_money','a.pm'=>1,'a.status'=>1])
  373. ->alias('a')->join('__USER__ u','a.uid=u.uid','LEFT')->where('a.type','in',['rake_back','become_partner']),'a.add_time')->join('__GROUP__ g','g.uid=a.uid','LEFT');
  374. if($where['nickname']!='') $model=$model->where('g.user_name|u.name','LIKE',"%$where[nickname]%");
  375. return $model;
  376. }
  377. public static function setBadgeWhere($model,$where,$alias='',$like='',$key='add_time'){
  378. $where['data']=$where['start_time'].' - '.$where['end_time'];
  379. $alias && $alias.='.';
  380. $key=$alias ? $alias.$key : $key;
  381. $model=self::getModelTime($where,$model,$key);
  382. if($where['nickname']) $model=$model->where($like,$where['nickname']);
  383. return $model;
  384. }
  385. public static function getGroupBadge($where){
  386. $group_count=self::setBadgeWhere(Member::alias('a')->join('__USER__ u','u.uid=a.uid'),$where,'a','u.uid')->count();
  387. $truePrice=bcmul($group_count-1,500,2);
  388. $uids=self::setBadgeWhere(User::where(['a.status'=>1,'a.is_partner'=>1])->alias('a')->join('group g','a.uid=g.uid'),$where,'a','a.uid')->column('a.uid');
  389. $count=Group::where('share_uid','in',$uids)->group('share_uid')->count();
  390. $GroupCount=self::setBadgeWhere(new Group,$where,'','uid')->count();
  391. $freezing_amount=SystemConfigService::get('freezing_amount');
  392. $partner_money=SystemConfigService::get('partner_money');
  393. return [
  394. [
  395. 'name'=>'总组数',
  396. 'field'=>'个',
  397. 'count'=>$group_count,
  398. 'background_color'=>'layui-bg-cyan',
  399. 'col'=>2,
  400. ],
  401. [
  402. 'name'=>'总单数',
  403. 'field'=>'单',
  404. 'count'=>$GroupCount,
  405. 'background_color'=>'layui-bg-cyan',
  406. 'col'=>2,
  407. ],
  408. [
  409. 'name'=>'总收入',
  410. 'field'=>'元',
  411. 'count'=>bcmul($GroupCount,$partner_money,2),
  412. 'background_color'=>'layui-bg-cyan',
  413. 'col'=>2,
  414. ],
  415. [
  416. 'name'=>'总支出',
  417. 'field'=>'元',
  418. 'count'=>self::setBadgeWhere(UserExtract::where('a.status',1)->alias('a')->join('__USER__ u','u.uid=a.uid'),$where,'a','u.uid')->sum('a.extract_price'),
  419. 'background_color'=>'layui-bg-cyan',
  420. 'col'=>2,
  421. ],
  422. [
  423. 'name'=>'实际提现佣金',
  424. 'field'=>'元',
  425. 'count'=>self::setBadgeWhere(UserExtract::where('a.status',1)->alias('a')
  426. ->join('__USER__ u','a.uid=u.uid'),$where,'a','u.uid')->sum('a.extract_price'),
  427. 'background_color'=>'layui-bg-cyan',
  428. 'col'=>2,
  429. ],
  430. [
  431. 'name'=>'未提现佣金',
  432. 'field'=>'元',
  433. 'count'=>self::setBadgeWhere(User::where('status',1),$where,'','uid')->sum('now_money'),
  434. 'background_color'=>'layui-bg-cyan',
  435. 'col'=>2,
  436. ],
  437. [
  438. 'name'=>'冻结佣金',
  439. 'field'=>'元',
  440. 'count'=>bcmul($count,$freezing_amount,2),
  441. 'background_color'=>'layui-bg-cyan',
  442. 'col'=>2,
  443. ],
  444. [
  445. 'name'=>'总盈利',
  446. 'field'=>'元',
  447. 'count'=>bcadd(bcmul($GroupCount,500,2),$truePrice,2),
  448. 'background_color'=>'layui-bg-cyan',
  449. 'col'=>2,
  450. ],
  451. ];
  452. }
  453. }