UserBill.php 14 KB

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