UserBill.php 13 KB

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