UserBill.php 13 KB

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