UserBill.php 14 KB

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