UserBill.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace app\admin\model\user;
  3. use crmeb\traits\ModelTrait;
  4. use crmeb\basic\BaseModel;
  5. use think\facade\Db;
  6. /**
  7. * 用户消费新增金额明细 model
  8. * Class User
  9. * @package app\admin\model\user
  10. */
  11. class UserBill extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'user_bill';
  23. use ModelTrait;
  24. /*
  25. * 获取总佣金
  26. * */
  27. public static function getBrokerage($uid,$category = 'now_money',$type='brokerage',$where)
  28. {
  29. return self::getModelTime($where,self::where('uid','in',$uid)->where('category',$category)
  30. ->where('type',$type)->where('pm',1)->where('status',1))->sum('number');
  31. }
  32. //修改积分减少积分记录
  33. public static function expend($title,$uid,$category,$type,$number,$link_id = 0,$balance = 0,$mark = '',$status = 1)
  34. {
  35. $pm = 0;
  36. $add_time = time();
  37. return self::create(compact('title','uid','link_id','category','type','number','balance','mark','status','pm','add_time'));
  38. }
  39. //修改积分增加积分记录
  40. public static function income($title,$uid,$category,$type,$number,$link_id = 0,$balance = 0,$mark = '',$status = 1){
  41. $pm = 1;
  42. $add_time = time();
  43. return self::create(compact('title','uid','link_id','category','type','number','balance','mark','status','pm','add_time'));
  44. }
  45. //获取柱状图和饼状图数据
  46. public static function getUserBillChart($where,$category='now_money',$type='brokerage',$pm=1,$zoom=15){
  47. $model=self::getModelTime($where,new self());
  48. $list=$model->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,sum(number) as sum_number')
  49. ->order('un_time asc')
  50. ->where('category', $category)
  51. ->where('type', $type)
  52. ->where('pm', $pm)
  53. ->group('un_time')
  54. ->select();
  55. if(count($list)) $list=$list->toArray();
  56. $legdata = [];
  57. $listdata = [];
  58. $dataZoom = '';
  59. foreach ($list as $item){
  60. $legdata[]=$item['un_time'];
  61. $listdata[]=$item['sum_number'];
  62. }
  63. if(count($legdata)>=$zoom) $dataZoom=$legdata[$zoom-1];
  64. //获取用户分布钱数
  65. $fenbulist=self::getModelTime($where,new self(),'a.add_time')
  66. ->alias('a')
  67. ->join('user r','a.uid=r.uid')
  68. ->field('a.uid,sum(a.number) as sum_number,r.nickname')
  69. ->where('a.category',$category)
  70. ->where('a.type', $type)
  71. ->where('a.pm', $pm)
  72. ->order('sum_number desc')
  73. ->group('a.uid')
  74. ->limit(8)
  75. ->select();
  76. //获取用户当前时间段总钱数
  77. $sum_number=self::getModelTime($where,new self())
  78. ->alias('a')
  79. ->where('a.category',$category)
  80. ->where('a.type', $type)
  81. ->where('a.pm', $pm)
  82. ->sum('number');
  83. if(count($fenbulist)) $fenbulist=$fenbulist->toArray();
  84. $fenbudate=[];
  85. $fenbu_legend=[];
  86. $color=['#ffcccc','#99cc00','#fd99cc','#669966','#66CDAA','#ADFF2F','#00BFFF','#00CED1','#66cccc','#ff9900','#ffcc00','#336699','#cccc00','#99ccff','#990066'];
  87. foreach ($fenbulist as $key=>$value){
  88. $fenbu_legend[]=$value['nickname'];
  89. $items['name']=$value['nickname'];
  90. $items['value']=bcdiv($value['sum_number'],$sum_number,2)*100;
  91. $items['itemStyle']['color']=$color[$key];
  92. $fenbudate[]=$items;
  93. }
  94. return compact('legdata','listdata','fenbudate','fenbu_legend','dataZoom');
  95. }
  96. //获取头部信息
  97. public static function getRebateBadge($where){
  98. $datawhere=['category'=>'now_money','type'=>'brokerage','pm'=>1];
  99. return [
  100. [
  101. 'name'=>'返利数(笔)',
  102. 'field'=>'个',
  103. 'count'=>self::getModelTime($where,new self())->where($datawhere)->count(),
  104. 'content'=>'返利总笔数',
  105. 'background_color'=>'layui-bg-blue',
  106. 'sum'=>self::where($datawhere)->count(),
  107. 'class'=>'fa fa-bar-chart',
  108. ],
  109. [
  110. 'name'=>'返利金额(元)',
  111. 'field'=>'个',
  112. 'count'=>self::getModelTime($where,new self())->where($datawhere)->sum('number'),
  113. 'content'=>'返利总金额',
  114. 'background_color'=>'layui-bg-cyan',
  115. 'sum'=>self::where($datawhere)->sum('number'),
  116. 'class'=>'fa fa-line-chart',
  117. ],
  118. ];
  119. }
  120. //获取返佣用户信息列表
  121. public static function getFanList($where){
  122. $list=self::alias('a')->join('user r','a.uid=r.uid')
  123. ->where('a.category','now_money')
  124. ->where('a.type', 'brokerage')
  125. ->where('a.pm', 1)
  126. ->order('a.number desc')
  127. ->join('store_order o','o.id=a.link_id')
  128. ->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')
  129. ->page((int)$where['page'],(int)$where['limit'])
  130. ->select();
  131. if(count($list)) $list=$list->toArray();
  132. return $list;
  133. }
  134. //获取返佣用户总人数
  135. public static function getFanCount(){
  136. return self::alias('a')->join('user r','a.uid=r.uid')->join('store_order o','o.id=a.link_id')->where('a.category','now_money')->where('a.type', 'brokerage')->where('a.pm', 1)->count();
  137. }
  138. //获取用户充值数据
  139. public static function getEchartsRecharge($where,$limit=15){
  140. $datawhere=['category'=>'now_money','pm'=>1];
  141. $list=self::getModelTime($where,self::where($datawhere)->where('type','in',['recharge','system_add']))
  142. ->field('sum(number) as sum_money,FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count')
  143. ->group('un_time')
  144. ->order('un_time asc')
  145. ->select();
  146. if(count($list)) $list=$list->toArray();
  147. $sum_count = self::getModelTime($where,self::where($datawhere)->where('type','in','recharge,system_add'))->count();
  148. $xdata = [];
  149. $seriesdata = [];
  150. $data = [];
  151. $zoom = '';
  152. foreach ($list as $value){
  153. $xdata[]=$value['un_time'];
  154. $seriesdata[]=$value['sum_money'];
  155. $data[]=$value['count'];
  156. }
  157. if(count($xdata)>$limit){
  158. $zoom=$xdata[$limit-5];
  159. }
  160. return compact('xdata','seriesdata','data','zoom');
  161. }
  162. //获取佣金提现列表
  163. public static function getExtrctOneList($where,$uid){
  164. $list=self::setOneWhere($where,$uid)
  165. ->field('number,link_id,mark,FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i:%s") as _add_time,status')
  166. ->select();
  167. count($list) && $list=$list->toArray();
  168. $count=self::setOneWhere($where,$uid)->count();
  169. foreach ($list as &$value){
  170. $value['order_id'] = Db::name('store_order')->where('order_id', $value['link_id'])->value('order_id');
  171. }
  172. return ['data'=>$list,'count'=>$count];
  173. }
  174. //设置单个用户查询
  175. public static function setOneWhere($where,$uid){
  176. $model=self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage');
  177. $time['data'] = '';
  178. if(strlen(trim($where['start_time'])) && strlen(trim($where['end_time']))){
  179. $time['data'] = $where['start_time'].' - '.$where['end_time'];
  180. $model = self::getModelTime($time,$model);
  181. }
  182. if(strlen(trim($where['nickname']))){
  183. $model = $model->where('link_id|mark','like',"%$where[nickname]%");
  184. }
  185. return $model;
  186. }
  187. //查询积分个人明细
  188. public static function getOneIntegralList($where){
  189. return self::setWhereList(
  190. $where,'',
  191. // ['deduction','system_add','sign'],
  192. ['title','number','balance','mark','FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'],
  193. 'integral'
  194. );
  195. }
  196. //查询个人签到明细
  197. public static function getOneSignList($where){
  198. return self::setWhereList(
  199. $where,'sign',
  200. ['title','number','mark','FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time']
  201. );
  202. }
  203. //查询个人余额变动记录
  204. public static function getOneBalanceChangList($where){
  205. $list=self::setWhereList(
  206. $where,'',
  207. // ['system_add','pay_product','extract','pay_product_refund','system_sub','brokerage','recharge','user_recharge_refund'],
  208. ['FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time','title','type','mark','number','balance','pm','status'],
  209. 'now_money'
  210. );
  211. foreach ($list as &$item){
  212. switch ($item['type']){
  213. case 'system_add':
  214. $item['_type']='系统添加';
  215. break;
  216. case 'pay_product':
  217. $item['_type']='商品购买';
  218. break;
  219. case 'extract':
  220. $item['_type']='提现';
  221. break;
  222. case 'pay_product_refund':
  223. $item['_type']='退款';
  224. break;
  225. case 'system_sub':
  226. $item['_type']='系统减少';
  227. break;
  228. case 'brokerage':
  229. $item['_type']='系统返佣';
  230. break;
  231. case 'recharge':
  232. $item['_type']='余额充值';
  233. break;
  234. case 'user_recharge_refund':
  235. $item['_type']='系统退款';
  236. break;
  237. }
  238. $item['_pm']=$item['pm']==1 ? '获得': '支出';
  239. }
  240. return $list;
  241. }
  242. //设置where条件分页.返回数据
  243. public static function setWhereList($where,$type='',$field=[],$category='integral'){
  244. $models=self::where('uid',$where['uid'])
  245. ->where('category',$category)
  246. ->page((int)$where['page'],(int)$where['limit'])
  247. ->order('id','desc')
  248. ->field($field);
  249. if(is_array($type)){
  250. $models=$models->where('type','in',$type);
  251. }else{
  252. if(!empty($type))$models=$models->where('type',$type);
  253. }
  254. return ($list=$models->select()) && count($list) ? $list->toArray():[];
  255. }
  256. //获取积分统计头部信息
  257. public static function getScoreBadgeList($where){
  258. return [
  259. [
  260. 'name'=>'历史总积分',
  261. 'field'=>'个',
  262. 'count'=>self::getModelTime($where,new self())->where('category','integral')->where('type','in','gain,system_sub,deduction,sign')->sum('number'),
  263. 'background_color'=>'layui-bg-blue',
  264. 'col'=>4,
  265. ],
  266. [
  267. 'name'=>'已使用积分',
  268. 'field'=>'个',
  269. 'count'=>self::getModelTime($where,new self())->where('category','integral')->where('type','deduction')->sum('number'),
  270. 'background_color'=>'layui-bg-cyan',
  271. 'col'=>4,
  272. ],
  273. [
  274. 'name'=>'未使用积分',
  275. 'field'=>'个',
  276. 'count'=>self::getModelTime($where,Db::name('user'))->sum('integral'),
  277. 'background_color'=>'layui-bg-cyan',
  278. 'col'=>4,
  279. ],
  280. ];
  281. }
  282. //获取积分统计曲线图和柱状图
  283. public static function getScoreCurve($where){
  284. //发放积分趋势图
  285. $list=self::getModelTime($where,self::where('category','integral')
  286. ->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time,sum(number) as sum_number')
  287. ->group('_add_time')->order('_add_time asc'))->select()->toArray();
  288. $date=[];
  289. $zoom='';
  290. $seriesdata=[];
  291. foreach ($list as $item){
  292. $date[]=$item['_add_time'];
  293. $seriesdata[]=$item['sum_number'];
  294. }
  295. unset($item);
  296. if(count($date)>$where['limit']){
  297. $zoom=$date[$where['limit']-5];
  298. }
  299. //使用积分趋势图
  300. $deductionlist=self::getModelTime($where,self::where('category','integral')->where('type','deduction')
  301. ->field('FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time,sum(number) as sum_number')
  302. ->group('_add_time')->order('_add_time asc'))->select()->toArray();
  303. $deduction_date=[];
  304. $deduction_zoom='';
  305. $deduction_seriesdata=[];
  306. foreach ($deductionlist as $item){
  307. $deduction_date[]=$item['_add_time'];
  308. $deduction_seriesdata[]=$item['sum_number'];
  309. }
  310. if(count($deductionlist)>$where['limit']){
  311. $deduction_zoom=$deductionlist[$where['limit']-5];
  312. }
  313. return compact('date','seriesdata','zoom','deduction_date','deduction_zoom','deduction_seriesdata');
  314. }
  315. /**
  316. * 用户获得总佣金
  317. * @return float
  318. */
  319. public static function getBrokerageCount()
  320. {
  321. return self::where('type', 'brokerage')->where('category', 'now_money')->where('status', 1)->where('pm', 1)->sum('number');
  322. }
  323. }