UserBill.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 Psr\SimpleCache\InvalidArgumentException;
  11. use think\Collection;
  12. use think\db\exception\DataNotFoundException;
  13. use think\db\exception\DbException;
  14. use think\db\exception\ModelNotFoundException;
  15. use think\facade\Cache;
  16. use crmeb\traits\ModelTrait;
  17. use crmeb\basic\BaseModel;
  18. /**
  19. * TODO 用户消费新增金额明细 model
  20. * Class UserBill
  21. * @package app\models\user
  22. */
  23. class UserBill extends BaseModel
  24. {
  25. /**
  26. * 数据表主键
  27. * @var string
  28. */
  29. protected $pk = 'id';
  30. /**
  31. * 模型名称
  32. * @var string
  33. */
  34. protected $name = 'user_bill';
  35. use ModelTrait;
  36. /**
  37. * @param $mer_id
  38. * @return mixed
  39. */
  40. public static function merSet($mer_id)
  41. {
  42. return $mer_id ? self::where('mer_id', $mer_id) : new self;
  43. }
  44. public static function income($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1, $mer_id = 0)
  45. {
  46. $pm = 1;
  47. $add_time = time();
  48. return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'mer_id'));
  49. }
  50. public static function expend($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1, $mer_id = 0)
  51. {
  52. $pm = 0;
  53. $add_time = time();
  54. return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'mer_id'));
  55. }
  56. /**
  57. * 积分/佣金 使用记录
  58. * @param $uid
  59. * @param $page
  60. * @param $limit
  61. * @param string $category
  62. * @return array|Collection
  63. * @throws DataNotFoundException
  64. * @throws ModelNotFoundException
  65. * @throws DbException
  66. */
  67. public static function userBillList($uid, $page, $limit, $category = 'integral')
  68. {
  69. if ($page) {
  70. $list = self::where('uid', $uid)
  71. ->where('category', $category)
  72. ->field('mark,pm,number,add_time')
  73. ->where('status', 1)
  74. ->order('add_time DESC')
  75. ->page((int)$page, (int)$limit)
  76. ->select();
  77. } else {
  78. $list = self::where('uid', $uid)
  79. ->where('category', $category)
  80. ->field('mark,pm,number,add_time')
  81. ->where('status', 1)
  82. ->order('add_time DESC')
  83. ->select();
  84. }
  85. $list = count($list) ? $list->toArray() : [];
  86. foreach ($list as &$v) {
  87. $v['add_time'] = date('Y/m/d H:i', $v['add_time']);
  88. $v['number'] = floatval($v['number']);
  89. }
  90. return $list;
  91. }
  92. /**
  93. * 获取昨日佣金
  94. * @param $uid
  95. * @return float
  96. */
  97. public static function yesterdayCommissionSum($uid)
  98. {
  99. return self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage')->where('pm', 1)
  100. ->where('status', 1)->whereTime('add_time', 'yesterday')->sum('number');
  101. }
  102. /**
  103. * 获取总佣金
  104. * @param $uid
  105. * @return float
  106. */
  107. public static function getBrokerage($uid)
  108. {
  109. $count1 = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage')->where('pm', 1)
  110. ->where('status', 1)->sum('number');
  111. $count2 = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage')->where('pm', 0)
  112. ->where('status', 1)->sum('number');
  113. $count = $count1 - $count2;
  114. return $count;
  115. }
  116. /**
  117. * @param $uid
  118. * @param string $category
  119. * @param string $type
  120. * @param $where
  121. * @return mixed
  122. */
  123. public static function getAdminBrokerage($uid, $category = 'now_money', $type = 'brokerage', $where)
  124. {
  125. return self::getModelTime($where, self::where('uid', 'in', $uid)->where('category', $category)
  126. ->where('type', $type)->where('pm', 1)->where('status', 1))->sum('number');
  127. }
  128. /**
  129. * 获取后台添加的余额
  130. * @param $uid
  131. * @return float
  132. */
  133. public static function getSystemAdd($uid)
  134. {
  135. return self::where('uid', $uid)->where('category', 'now_money')->where('type', 'system_add')->where('pm', 1)
  136. ->where('status', 1)->sum('number');
  137. }
  138. /**
  139. * 累计充值
  140. * @param $uid
  141. * @return float
  142. */
  143. public static function getRecharge($uid)
  144. {
  145. return self::where('uid', $uid)
  146. ->where('category', 'now_money')
  147. ->where('type', 'recharge')
  148. ->where('pm', 1)
  149. ->where('status', 1)
  150. ->sum('number');
  151. }
  152. /**
  153. * 获取用户账单明细
  154. * @param int $uid 用户uid
  155. * @param int $page 页码
  156. * @param int $limit 展示多少条
  157. * @param int $type 展示类型
  158. * @return array
  159. * */
  160. public static function getUserBillList($uid, $page, $limit, $type)
  161. {
  162. if (!$limit) return [];
  163. $model = self::where('uid', $uid)->where('category', 'now_money')->order('add_time desc')->where('number', '<>', 0)
  164. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')->group('time');
  165. switch ((int)$type) {
  166. case 0:
  167. $model = $model->where('type', 'in', 'recharge,brokerage,pay_money,system_add,pay_product_refund,system_sub');
  168. break;
  169. case 1:
  170. $model = $model->where('type', 'pay_money');
  171. break;
  172. case 2:
  173. $model = $model->where('type', 'in', 'recharge,system_add');
  174. break;
  175. case 3:
  176. $model = $model->where('type', 'brokerage');
  177. break;
  178. case 4:
  179. $model = $model->where('type', 'extract');
  180. break;
  181. }
  182. if ($page) $model = $model->page((int)$page, (int)$limit);
  183. $list = ($list = $model->select()) ? $list->toArray() : [];
  184. $data = [];
  185. foreach ($list as $item) {
  186. $value['time'] = $item['time'];
  187. $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();
  188. array_push($data, $value);
  189. }
  190. return $data;
  191. }
  192. /**
  193. * TODO 获取用户记录 按月查找
  194. * @param $uid $uid 用户编号
  195. * @param int $page $page 分页起始值
  196. * @param int $limit $limit 查询条数
  197. * @param string $category $category 记录类型
  198. * @param string $type $type 记录分类
  199. * @return mixed
  200. */
  201. public static function getRecordList($uid, $page = 1, $limit = 8, $category = 'now_money', $type = '')
  202. {
  203. $uids = User::where('spread_uid', $uid)->column('uid');
  204. $model = new self;
  205. $model = $model->alias('b');
  206. $model = $model->field("FROM_UNIXTIME(b.add_time, '%Y-%m') as time");
  207. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  208. $model = $model->where('o.refund_status', 0);
  209. $model = $model->where('b.category', $category);
  210. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  211. $query->where(function ($query1) use ($uid, $type) {
  212. $query1->where('b.uid', $uid)->where('b.type', $type);
  213. })->whereOr(function ($query2) use ($uids, $type) {
  214. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  215. });
  216. });
  217. $model = $model->group("FROM_UNIXTIME(b.add_time, '%Y-%m')");
  218. $model = $model->order('time desc');
  219. $model = $model->page($page, $limit);
  220. return $model->select();
  221. }
  222. /**
  223. * TODO 按月份查找用户记录
  224. * @param $uid $uid 用户编号
  225. * @param int $addTime $addTime 月份
  226. * @param string $category $category 记录类型
  227. * @param string $type $type 记录分类
  228. * @return mixed
  229. */
  230. public static function getRecordListDraw($uid, $addTime = 0, $category = 'now_money', $type = '')
  231. {
  232. if (!$uid) [];
  233. $model = new self;
  234. $model = $model->field("title,FROM_UNIXTIME(add_time, '%Y-%m-%d %H:%i') as time,number,pm");
  235. $model = $model->where('uid', $uid);
  236. $model = $model->where("FROM_UNIXTIME(add_time, '%Y-%m')= '{$addTime}'");
  237. $model = $model->where('category', $category);
  238. if (strlen(trim($type))) $model = $model->where('type', 'in', $type);
  239. $model = $model->order('add_time desc');
  240. $list = $model->select();
  241. if ($list) return $list->toArray();
  242. else [];
  243. }
  244. /**
  245. * TODO 获取订单返佣记录
  246. * @param $uid
  247. * @param int $addTime
  248. * @param string $category
  249. * @param string $type
  250. * @return mixed
  251. */
  252. public static function getRecordOrderListDraw($uid, $addTime = 0, $category = 'now_money', $type = 'brokerage')
  253. {
  254. if (!strlen(trim($uid))) return [];
  255. $uids = User::where('spread_uid', $uid)->column('uid');
  256. $model = new self;
  257. $model = $model->alias('b');
  258. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  259. $model = $model->join('User u', 'u.uid=o.uid', 'right');
  260. $model = $model->where('o.refund_status', 0);
  261. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  262. $query->where(function ($query1) use ($uid, $type) {
  263. $query1->where('b.uid', $uid)->where('b.type', $type);
  264. })->whereOr(function ($query2) use ($uids, $type) {
  265. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  266. });
  267. });
  268. $model = $model->where("FROM_UNIXTIME(b.add_time, '%Y-%m')= '{$addTime}'");
  269. $model = $model->where('b.category', $category);
  270. $model = $model->where('b.take', 0);
  271. $model = $model->order('b.add_time desc');
  272. $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");
  273. $list = $model->select();
  274. if ($list) return $list->toArray();
  275. else return [];
  276. }
  277. /**
  278. * TODO 获取用户记录总和
  279. * @param $uid
  280. * @param string $category
  281. * @param string $type
  282. * @return mixed
  283. */
  284. public static function getRecordCount($uid, $category = 'now_money', $type = '', $time = '', $pm = false)
  285. {
  286. $model = new self;
  287. $model = $model->where('uid', $uid);
  288. $model = $model->where('category', $category);
  289. $model = $model->where('status', 1);
  290. if (strlen(trim($type))) $model = $model->where('type', 'in', $type);
  291. if ($time) $model = $model->whereTime('add_time', $time);
  292. if ($pm) {
  293. $model = $model->where('pm', 0);
  294. }
  295. return $model->sum('number');
  296. }
  297. /**
  298. * TODO 获取订单返佣记录总数
  299. * @param $uid
  300. * @param string $category
  301. * @param string $type
  302. * @return mixed
  303. */
  304. public static function getRecordOrderCount($uid, $category = 'now_money', $type = 'brokerage')
  305. {
  306. $uids = User::where('spread_uid', $uid)->column('uid');
  307. $model = new self;
  308. $model = $model->alias('b');
  309. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  310. $model = $model->where('o.refund_status', 0);
  311. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  312. $query->where(function ($query1) use ($uid, $type) {
  313. $query1->where('b.uid', $uid)->where('b.type', $type);
  314. })->whereOr(function ($query2) use ($uids, $type) {
  315. $query2->where('b.uid', 'in', $uids)->where('b.type', 'now_money');
  316. });
  317. });
  318. $model = $model->where('b.category', $category);
  319. $model = $model->where('b.take', 0);
  320. return $model->count('b.id');
  321. }
  322. /**
  323. * 记录分享次数
  324. * @param int $uid 用户uid
  325. * @param int $cd 冷却时间
  326. * @param bool $mer_id
  327. * @return Boolean
  328. * @throws DataNotFoundException
  329. * @throws DbException
  330. * @throws ModelNotFoundException
  331. * @throws InvalidArgumentException
  332. */
  333. public static function setUserShare($uid, $cd = 300, $mer_id = false)
  334. {
  335. $user = User::merSet($mer_id)->where('uid', $uid)->find();
  336. if (!$user) return self::setErrorInfo('用户不存在!');
  337. $cachename = 'Share_' . $uid;
  338. if (Cache::has($cachename)) return false;
  339. self::income('用户分享记录', $uid, 'share', 'share', 1, 0, 0, date('Y-m-d H:i:s', time()) . ':用户分享');
  340. Cache::set($cachename, 1, $cd);
  341. event('UserLevelAfter', [$user]);
  342. return true;
  343. }
  344. //查询积分个人明细
  345. public static function getOneIntegralList($where)
  346. {
  347. return self::setWhereList(
  348. $where, '',
  349. // ['deduction','system_add','sign'],
  350. ['title', 'number', 'balance', 'mark', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time'],
  351. 'integral'
  352. );
  353. }
  354. //设置where条件分页.返回数据
  355. public static function setWhereList($where, $type = '', $field = [], $category = 'integral')
  356. {
  357. $models = self::where('uid', $where['id'])
  358. ->where('category', $category)
  359. ->page((int)$where['page'], (int)$where['limit'])
  360. ->order('id', 'desc')
  361. ->field($field);
  362. if (is_array($type)) {
  363. $models = $models->where('type', 'in', $type);
  364. } else {
  365. if (!empty($type)) $models = $models->where('type', $type);
  366. }
  367. $list = $models->select();
  368. $data['count'] = $models->count();
  369. $data['list'] = count($list) ? $list->toArray() : [];
  370. return $data;
  371. }
  372. //查询个人签到明细
  373. public static function getOneSignList($where)
  374. {
  375. return self::setWhereList(
  376. $where, 'sign',
  377. ['title', 'number', 'mark', 'FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time']
  378. );
  379. }
  380. //查询个人余额变动记录
  381. public static function getOneBalanceChangList($where)
  382. {
  383. $list = self::setWhereList(
  384. $where, '',
  385. // ['system_add','pay_product','extract','pay_product_refund','system_sub','brokerage','recharge','user_recharge_refund'],
  386. ['FROM_UNIXTIME(add_time,"%Y-%m-%d") as add_time', 'title', 'type', 'mark', 'number', 'balance', 'pm', 'status'],
  387. 'now_money'
  388. );
  389. foreach ($list['list'] as &$item) {
  390. switch ($item['type']) {
  391. case 'system_add':
  392. $item['type'] = '系统添加';
  393. break;
  394. case 'pay_product':
  395. $item['type'] = '商品购买';
  396. break;
  397. case 'extract':
  398. $item['type'] = '提现';
  399. break;
  400. case 'pay_product_refund':
  401. $item['type'] = '退款';
  402. break;
  403. case 'system_sub':
  404. $item['type'] = '系统减少';
  405. break;
  406. case 'brokerage':
  407. $item['type'] = '系统返佣';
  408. break;
  409. case 'recharge':
  410. $item['type'] = '余额充值';
  411. break;
  412. case 'user_recharge_refund':
  413. $item['type'] = '系统退款';
  414. break;
  415. }
  416. $item['pm'] = $item['pm'] == 1 ? '获得' : '支出';
  417. }
  418. return $list;
  419. }
  420. //获取佣金提现列表
  421. public static function getExtrctOneList($where, $uid)
  422. {
  423. $list = self::setOneWhere($where, $uid)
  424. ->field('number,link_id,mark,FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i:%s") as _add_time,status')
  425. ->select();
  426. count($list) && $list = $list->toArray();
  427. $count = self::setOneWhere($where, $uid)->count();
  428. foreach ($list as &$value) {
  429. $value['order_id'] = StoreOrder::where('order_id', $value['link_id'])->value('order_id');
  430. }
  431. return ['data' => $list, 'count' => $count];
  432. }
  433. //设置单个用户查询
  434. public static function setOneWhere($where, $uid)
  435. {
  436. $model = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage');
  437. $time['data'] = '';
  438. if (strlen(trim($where['start_time'])) && strlen(trim($where['end_time']))) {
  439. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  440. $model = self::getModelTime($time, $model);
  441. }
  442. if (strlen(trim($where['nickname']))) {
  443. $model = $model->where('link_id|mark', 'like', "%$where[nickname]%");
  444. }
  445. return $model;
  446. }
  447. /**
  448. * 用户获得总佣金
  449. * @return float
  450. */
  451. public static function getBrokerageCount($where, $mer_id = '')
  452. {
  453. $model = new self;
  454. $model = self::getModelTime($where, $model, 'add_time');
  455. $model = $model->where('type', 'brokerage');
  456. $model = $model->where('category', 'now_money');
  457. $model = $model->where('status', 1);
  458. $model = $model->where('pm', 1);
  459. $model = $model->where('mer_id', $mer_id);
  460. return $model->sum('number');
  461. }
  462. /**
  463. * 获取用户时间段内返佣金额
  464. * @param $type
  465. * @return float
  466. */
  467. public static function getBrokeragePrice($type, $uid)
  468. {
  469. $model = new self;
  470. $model = $model->where('category', 'now_money')->where('type', 'brokerage');
  471. if ($type == 'week') {
  472. $model = $model->whereWeek('add_time');
  473. } elseif ($type == 'month') {
  474. $model = $model->whereMonth('add_time');
  475. }
  476. return $model->where('uid', $uid)->where('pm', 1)->sum('number');
  477. }
  478. }