UserBill.php 10 KB

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