UserBill.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Created by CRMEB.
  4. * Copyright (c) 2017~2019 http://www.crmeb.com All rights reserved.
  5. * Author: liaofei <136327134@qq.com>
  6. * Date: 2019/3/27 21:44
  7. */
  8. namespace app\models\user;
  9. use think\facade\Cache;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. /**
  13. * TODO 用户消费新增金额明细 model
  14. * Class UserBill
  15. * @package app\models\user
  16. */
  17. class UserBill extends BaseModel
  18. {
  19. /**
  20. * 数据表主键
  21. * @var string
  22. */
  23. protected $pk = 'id';
  24. /**
  25. * 模型名称
  26. * @var string
  27. */
  28. protected $name = 'user_bill';
  29. use ModelTrait;
  30. public static function income($title,$uid,$category,$type,$number,$link_id = 0,$balance = 0,$mark = '',$status = 1)
  31. {
  32. $pm = 1;
  33. $add_time = time();
  34. return self::create(compact('title','uid','link_id','category','type','number','balance','mark','status','pm','add_time'));
  35. }
  36. public static function expend($title,$uid,$category,$type,$number,$link_id = 0,$balance = 0,$mark = '',$status = 1)
  37. {
  38. $pm = 0;
  39. $add_time = time();
  40. return self::create(compact('title','uid','link_id','category','type','number','balance','mark','status','pm','add_time'));
  41. }
  42. /**
  43. * 积分/佣金 使用记录
  44. * @param $uid
  45. * @param $page
  46. * @param $limit
  47. * @param string $category
  48. * @return array|\think\Collection
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws \think\exception\DbException
  52. */
  53. public static function userBillList($uid, $page, $limit, $category='integral')
  54. {
  55. if($page){
  56. $list = self::where('uid',$uid)
  57. ->where('category',$category)
  58. ->field('mark,pm,number,add_time')
  59. ->where('status',1)
  60. ->order('add_time DESC')
  61. ->page((int)$page,(int)$limit)
  62. ->select();
  63. }else{
  64. $list = self::where('uid',$uid)
  65. ->where('category',$category)
  66. ->field('mark,pm,number,add_time')
  67. ->where('status',1)
  68. ->order('add_time DESC')
  69. ->select();
  70. }
  71. $list = count($list) ? $list->toArray() : [];
  72. foreach ($list as &$v){
  73. $v['add_time'] = date('Y/m/d H:i',$v['add_time']);
  74. $v['number'] = floatval($v['number']);
  75. }
  76. return $list;
  77. }
  78. /**
  79. * 获取昨日佣金
  80. * @param $uid
  81. * @return float
  82. */
  83. public static function yesterdayCommissionSum($uid)
  84. {
  85. return self::where('uid',$uid)->where('category','now_money')->where('type','brokerage')->where('pm',1)
  86. ->where('status',1)->whereTime('add_time', 'yesterday')->sum('number');
  87. }
  88. /**
  89. * 获取总佣金
  90. * @param $uid
  91. * @return float
  92. */
  93. public static function getBrokerage($uid)
  94. {
  95. return self::where('uid',$uid)->where('category','now_money')->where('type','brokerage')->where('pm',1)
  96. ->where('status',1)->sum('number');
  97. }
  98. /**
  99. * 获取后台添加的余额
  100. * @param $uid
  101. * @return float
  102. */
  103. public static function getSystemAdd($uid)
  104. {
  105. return self::where('uid',$uid)->where('category','now_money')->where('type','system_add')->where('pm',1)
  106. ->where('status',1)->sum('number');
  107. }
  108. /**
  109. * 累计充值
  110. * @param $uid
  111. * @return float
  112. */
  113. public static function getRecharge($uid)
  114. {
  115. return self::where('uid', $uid)
  116. ->where('category', 'now_money')
  117. ->where('type', 'recharge')
  118. ->where('pm', 1)
  119. ->where('status', 1)
  120. ->sum('number');
  121. }
  122. /*
  123. * 获取用户账单明细
  124. * @param int $uid 用户uid
  125. * @param int $page 页码
  126. * @param int $limit 展示多少条
  127. * @param int $type 展示类型
  128. * @return array
  129. * */
  130. public static function getUserBillList($uid,$page,$limit,$type)
  131. {
  132. if(!$limit) return [];
  133. $model=self::where('uid',$uid)->where('category','now_money')->order('add_time desc')
  134. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')->group('time');
  135. switch ((int)$type){
  136. case 0:
  137. $model=$model->where('type','in','recharge,brokerage,pay_product,system_add,pay_product_refund,system_sub');
  138. break;
  139. case 1:
  140. $model=$model->where('type','pay_product');
  141. break;
  142. case 2:
  143. $model=$model->where('type','recharge');
  144. break;
  145. case 3:
  146. $model=$model->where('type','brokerage');
  147. break;
  148. case 4:
  149. $model=$model->where('type','extract');
  150. break;
  151. }
  152. if($page) $model = $model->page((int)$page,(int)$limit);
  153. $list = ($list = $model->select()) ? $list->toArray() : [];
  154. $data = [];
  155. foreach ($list as $item){
  156. $value['time'] = $item['time'];
  157. $value['list'] = self::where('id','in',$item['ids'])->field('FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm')->order('add_time DESC')->select();
  158. array_push($data,$value);
  159. }
  160. return $data;
  161. }
  162. /**
  163. * TODO 获取用户记录 按月查找
  164. * @param $uid $uid 用户编号
  165. * @param int $page $page 分页起始值
  166. * @param int $limit $limit 查询条数
  167. * @param string $category $category 记录类型
  168. * @param string $type $type 记录分类
  169. * @return mixed
  170. */
  171. public static function getRecordList($uid, $page = 1, $limit = 8, $category = 'now_money', $type = ''){
  172. $model = new self;
  173. $model = $model->field("FROM_UNIXTIME(add_time, '%Y-%m') as time");
  174. $model = $model->where('uid', $uid);
  175. if(strlen(trim($type))) $model = $model->whereIn('type', $type);
  176. $model = $model->where('category', $category);
  177. $model = $model->group("FROM_UNIXTIME(add_time, '%Y-%m')");
  178. $model = $model->order('time desc');
  179. $model = $model->page($page, $limit);
  180. return $model->select();
  181. }
  182. /**
  183. * TODO 按月份查找用户记录
  184. * @param $uid $uid 用户编号
  185. * @param int $addTime $addTime 月份
  186. * @param string $category $category 记录类型
  187. * @param string $type $type 记录分类
  188. * @return mixed
  189. */
  190. public static function getRecordListDraw($uid, $addTime = 0,$category = 'now_money',$type = ''){
  191. if(!$uid) [];
  192. $model = new self;
  193. $model = $model->field("title,FROM_UNIXTIME(add_time, '%Y-%m-%d %H:%i') as time,number,pm");
  194. $model = $model->where('uid',$uid);
  195. $model = $model->where("FROM_UNIXTIME(add_time, '%Y-%m')= '{$addTime}'");
  196. $model = $model->where('category',$category);
  197. if(strlen(trim($type))) $model = $model->where('type','in',$type);
  198. $model = $model->order('add_time desc');
  199. $list = $model->select();
  200. if($list) return $list->toArray();
  201. else [];
  202. }
  203. /**
  204. * TODO 获取订单返佣记录
  205. * @param $uid
  206. * @param int $addTime
  207. * @param string $category
  208. * @param string $type
  209. * @return mixed
  210. */
  211. public static function getRecordOrderListDraw($uid, $addTime = 0,$category = 'now_money',$type = 'brokerage'){
  212. if(!strlen(trim($uid))) [];
  213. $model = new self;
  214. $model = $model->field("o.order_id,FROM_UNIXTIME(b.add_time, '%Y-%m-%d %H:%i') as time,b.number,u.avatar,u.nickname");
  215. $model = $model->alias('b');
  216. $model = $model->join('StoreOrder o','o.id=b.link_id');
  217. $model = $model->join('User u','u.uid=o.uid','right');
  218. $model = $model->where('b.uid', $uid);
  219. $model = $model->where("FROM_UNIXTIME(b.add_time, '%Y-%m')= '{$addTime}'");
  220. $model = $model->where('b.category',$category);
  221. $model = $model->whereIn('b.type', $type);
  222. $model = $model->order('time desc');
  223. // dump($model);exit();
  224. $list = $model->select();
  225. if($list) return $list->toArray();
  226. else [];
  227. }
  228. /**
  229. * TODO 获取用户记录总和
  230. * @param $uid
  231. * @param string $category
  232. * @param string $type
  233. * @return mixed
  234. */
  235. public static function getRecordCount($uid, $category = 'now_money', $type = '',$time=''){
  236. $model = new self;
  237. $model = $model->where('uid', $uid);
  238. $model = $model->where('category',$category);
  239. $model = $model->where('status',1);
  240. if(strlen(trim($type))) $model = $model->where('type','in',$type);
  241. if($time) $model=$model->whereTime('add_time',$time);
  242. return $model->sum('number');
  243. }
  244. /**
  245. * TODO 获取订单返佣记录总数
  246. * @param $uid
  247. * @param string $category
  248. * @param string $type
  249. * @return mixed
  250. */
  251. public static function getRecordOrderCount($uid, $category = 'now_money', $type = 'brokerage'){
  252. $model = new self;
  253. $model = $model->where('uid', $uid);
  254. $model = $model->where('category',$category);
  255. if(strlen(trim($type))) $model = $model->whereIn('type', $type);
  256. return $model->count();
  257. }
  258. /*
  259. * 记录分享次数
  260. * @param int $uid 用户uid
  261. * @param int $cd 冷却时间
  262. * @return Boolean
  263. * */
  264. public static function setUserShare($uid,$cd=300){
  265. $user=User::where('uid',$uid)->find();
  266. if(!$user) return self::setErrorInfo('用户不存在!');
  267. $cachename='Share_'.$uid;
  268. if(Cache::has($cachename)) return false;
  269. self::income('用户分享记录',$uid,'share','share',1,0,0,date('Y-m-d H:i:s',time()).':用户分享');
  270. Cache::set($cachename,1,$cd);
  271. event('UserLevelAfter',[$user]);
  272. return true;
  273. }
  274. }