UserBill.php 13 KB

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