UserBill.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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,$code='')
  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','code'));
  36. }
  37. public static function expend($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1,$code='')
  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','code'));
  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. * @return float
  83. */
  84. public static function yesterdayCommissionSum($uid)
  85. {
  86. $get_commission = self::where('uid', $uid)->where('type', 'in','brokerage,all_stock_right_bonus,code_stock_right_bonus,give_stock_right_bonus,recommend_stock_right_bonus')->where('pm', 1)
  87. ->where('status', 1)->whereTime('add_time', 'yesterday')->sum('number');
  88. $refund_commission = self::where('uid', $uid)->where('type', 'in','brokerage,all_stock_right_bonus,code_stock_right_bonus,give_stock_right_bonus,recommend_stock_right_bonus')->where('pm', 0)
  89. ->where('status', 1)->whereTime('add_time', 'yesterday')->sum('number');
  90. if ($get_commission > $refund_commission)
  91. $yesterday_commision = bcsub($get_commission, $refund_commission, 2);
  92. else
  93. $yesterday_commision = 0;
  94. return $yesterday_commision;
  95. }
  96. /**
  97. * 获取总佣金
  98. * @param $uid
  99. * @return float
  100. */
  101. public static function getBrokerage($uid)
  102. {
  103. $count1 = self::where('uid', $uid)->where('type', 'in','brokerage,all_stock_right_bonus,code_stock_right_bonus,give_stock_right_bonus,recommend_stock_right_bonus')->where('pm', 1)
  104. ->where('status', 1)->sum('number');
  105. $count2 = self::where('uid', $uid)->where('type', 'in','brokerage,all_stock_right_bonus,code_stock_right_bonus,give_stock_right_bonus,recommend_stock_right_bonus')->where('pm', 0)
  106. ->where('status', 1)->sum('number');
  107. if ($count1 > $count2)
  108. $count = bcsub($count1, $count2, 2);
  109. else
  110. $count = 0;
  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)->order('add_time desc')->where('number', '<>', 0)
  149. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(DISTINCT id ORDER BY id DESC SEPARATOR ",") ids')->group('time');
  150. switch ((int)$type) {
  151. case 0:
  152. $model = $model->where('type', 'in', 'recharge,brokerage,pay_money,system_add,pay_product_refund,system_sub');
  153. break;
  154. case 1:
  155. $model = $model->where('type', 'pay_money');
  156. break;
  157. case 2:
  158. $model = $model->where('type', 'in', 'recharge,system_add');
  159. break;
  160. case 3:
  161. $model = $model->where('type', 'in','brokerage,brokerage_price,all_stock_right_bonus,code_stock_right_bonus,give_stock_right_bonus,recommend_stock_right_bonus,senior_stock_right_bonus');
  162. break;
  163. case 4:
  164. $model = $model->where('type', 'extract');
  165. break;
  166. case 5: //公司分红
  167. $model = $model->where('type', 'all_stock_right_bonus');
  168. break;
  169. case 6: //全民分红
  170. $model = $model->where('type', 'code_stock_right_bonus');
  171. break;
  172. case 7: //赠送分红
  173. $model = $model->where('type', 'give_stock_right_bonus');
  174. break;
  175. case 8: //推荐分红
  176. $model = $model->where('type', 'in','recommend_stock_right_bonus,senior_stock_right_bonus');
  177. break;
  178. }
  179. if ($page) $model = $model->page((int)$page, (int)$limit);
  180. $list = ($list = $model->select()) ? $list->toArray() : [];
  181. $data = [];
  182. foreach ($list as $item) {
  183. $value['time'] = $item['time'];
  184. $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();
  185. array_push($data, $value);
  186. }
  187. return $data;
  188. }
  189. /**
  190. * TODO 获取用户记录 按月查找
  191. * @param $uid $uid 用户编号
  192. * @param int $page $page 分页起始值
  193. * @param int $limit $limit 查询条数
  194. * @param string $category $category 记录类型
  195. * @param string $type $type 记录分类
  196. * @return mixed
  197. */
  198. public static function getRecordList($uid, $page = 1, $limit = 8, $category = 'now_money', $type = '')
  199. {
  200. $uids = User::where('spread_uid', $uid)->column('uid');
  201. $model = new self;
  202. $model = $model->alias('b');
  203. $model = $model->field("FROM_UNIXTIME(b.add_time, '%Y-%m') as time");
  204. // $model = $model->where('b.uid', $uid);
  205. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  206. $model = $model->where('o.refund_status', 0);
  207. // if (strlen(trim($type))) $model = $model->whereIn('b.type', $type);
  208. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  209. $query->where(function ($query1) use ($uid, $type) {
  210. $query1->where('b.uid', $uid)->where('b.type', $type);
  211. })->whereOr(function ($query2) use ($uids, $type) {
  212. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  213. });
  214. });
  215. $model = $model->group("FROM_UNIXTIME(b.add_time, '%Y-%m')");
  216. $model = $model->order('time desc');
  217. $model = $model->page($page, $limit);
  218. return $model->select();
  219. }
  220. /**
  221. * TODO 按月份查找用户记录
  222. * @param $uid $uid 用户编号
  223. * @param int $addTime $addTime 月份
  224. * @param string $category $category 记录类型
  225. * @param string $type $type 记录分类
  226. * @return mixed
  227. */
  228. public static function getRecordListDraw($uid, $addTime = 0, $category = 'now_money', $type = '')
  229. {
  230. if (!$uid) [];
  231. $model = new self;
  232. $model = $model->field("title,FROM_UNIXTIME(add_time, '%Y-%m-%d %H:%i') as time,number,pm");
  233. $model = $model->where('uid', $uid);
  234. $model = $model->where("FROM_UNIXTIME(add_time, '%Y-%m')= '{$addTime}'");
  235. if (strlen(trim($type))) $model = $model->where('type', 'in', $type);
  236. $model = $model->order('add_time desc');
  237. $list = $model->select();
  238. if ($list) return $list->toArray();
  239. else [];
  240. }
  241. /**
  242. * TODO 获取订单返佣记录
  243. * @param $uid
  244. * @param int $addTime
  245. * @param string $category
  246. * @param string $type
  247. * @return mixed
  248. */
  249. public static function getRecordOrderListDraw($uid, $addTime = 0, $category = 'now_money', $type = 'brokerage')
  250. {
  251. if (!strlen(trim($uid))) return [];
  252. $uids = User::where('spread_uid', $uid)->column('uid');
  253. $model = new self;
  254. $model = $model->alias('b');
  255. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  256. $model = $model->join('User u', 'u.uid=o.uid', 'right');
  257. $model = $model->where('o.refund_status', 0);
  258. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  259. $query->where(function ($query1) use ($uid, $type) {
  260. $query1->where('b.uid', $uid)->where('b.type', $type);
  261. })->whereOr(function ($query2) use ($uids, $type) {
  262. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  263. });
  264. });
  265. $model = $model->where("FROM_UNIXTIME(b.add_time, '%Y-%m')= '{$addTime}'");
  266. $model = $model->where('b.take', 0);
  267. $model = $model->order('b.add_time desc');
  268. $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");
  269. $list = $model->select();
  270. if ($list) return $list->toArray();
  271. else return [];
  272. }
  273. /**
  274. * TODO 获取用户记录总和
  275. * @param $uid
  276. * @param string $category
  277. * @param string $type
  278. * @return mixed
  279. */
  280. public static function getRecordCount($uid, $category = 'now_money', $type = '', $time = '', $pm = false)
  281. {
  282. $model = new self;
  283. $model = $model->where('uid', $uid);
  284. $model = $model->where('status', 1);
  285. if (strlen(trim($type))) $model = $model->where('type', 'in', $type);
  286. if ($time) $model = $model->whereTime('add_time', $time);
  287. if ($pm) {
  288. $model = $model->where('pm', 0);
  289. } else {
  290. $model = $model->where('pm', 1);
  291. }
  292. return $model->sum('number');
  293. }
  294. /**
  295. * TODO 获取订单返佣记录总数
  296. * @param $uid
  297. * @param string $category
  298. * @param string $type
  299. * @return mixed
  300. */
  301. public static function getRecordOrderCount($uid, $category = 'now_money', $type = 'brokerage')
  302. {
  303. if (!strlen(trim($uid))) return 0;
  304. $uids = User::where('spread_uid', $uid)->column('uid');
  305. $model = new self;
  306. $model = $model->alias('b');
  307. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  308. $model = $model->where('o.refund_status', 0);
  309. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  310. $query->where(function ($query1) use ($uid, $type) {
  311. $query1->where('b.uid', $uid)->where('b.type', $type);
  312. })->whereOr(function ($query2) use ($uids, $type) {
  313. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  314. });
  315. });
  316. $model = $model->where('b.take', 0);
  317. return $model->count();
  318. }
  319. /*
  320. * 记录分享次数
  321. * @param int $uid 用户uid
  322. * @param int $cd 冷却时间
  323. * @return Boolean
  324. * */
  325. public static function setUserShare($uid, $cd = 300)
  326. {
  327. $user = User::where('uid', $uid)->find();
  328. if (!$user) return self::setErrorInfo('用户不存在!');
  329. $cachename = 'Share_' . $uid;
  330. if (Cache::has($cachename)) return false;
  331. self::income('用户分享记录', $uid, 'share', 'share', 1, 0, 0, date('Y-m-d H:i:s', time()) . ':用户分享');
  332. Cache::set($cachename, 1, $cd);
  333. event('UserLevelAfter', [$user]);
  334. return true;
  335. }
  336. }