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\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, $award_level = 0)
  32. {
  33. $pm = 1;
  34. $add_time = date("Y-m-d H:i:s");
  35. $id = self::getkeytoid('bill_id');
  36. return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'id', 'award_level'));
  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 = date("Y-m-d H:i:s");
  42. $id = self::getkeytoid('bill_id');
  43. return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time', 'id'));
  44. }
  45. /**
  46. * 积分/佣金 使用记录
  47. * @param $uid
  48. * @param $page
  49. * @param $limit
  50. * @param string $category
  51. * @return array|\think\Collection
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @throws \think\exception\DbException
  55. */
  56. public static function userBillList($uid, $page, $limit, $category = 'integral')
  57. {
  58. if ($page) {
  59. $list = self::where('uid', $uid)
  60. ->where('category', $category)
  61. ->field('mark,pm,number,add_time')
  62. ->where('status', 1)
  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')
  70. ->where('status', 1)
  71. ->order('add_time DESC')
  72. ->select();
  73. }
  74. $list = count($list) ? $list->toArray() : [];
  75. foreach ($list as &$v) {
  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. $get_commission = 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. $refund_commission = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage')->where('pm', 0)
  90. ->where('status', 1)->whereTime('add_time', 'yesterday')->sum('number');
  91. if ($get_commission > $refund_commission)
  92. $yesterday_commision = bcsub($get_commission, $refund_commission, 2);
  93. else
  94. $yesterday_commision = 0;
  95. return $yesterday_commision;
  96. }
  97. /**
  98. * 获取总佣金
  99. * @param $uid
  100. * @return float
  101. */
  102. public static function getBrokerage($uid)
  103. {
  104. $count1 = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage')->where('pm', 1)
  105. ->where('status', 1)->sum('number');
  106. $count2 = self::where('uid', $uid)->where('category', 'now_money')->where('type', 'brokerage')->where('pm', 0)
  107. ->where('status', 1)->sum('number');
  108. if ($count1 > $count2)
  109. $count = bcsub($count1, $count2, 2);
  110. else
  111. $count = 0;
  112. return $count;
  113. }
  114. /**
  115. * 获取后台添加的余额
  116. * @param $uid
  117. * @return float
  118. */
  119. public static function getSystemAdd($uid)
  120. {
  121. return self::where('uid', $uid)->where('category', 'now_money')->where('type', 'system_add')->where('pm', 1)
  122. ->where('status', 1)->sum('number');
  123. }
  124. /**
  125. * 累计充值
  126. * @param $uid
  127. * @return float
  128. */
  129. public static function getRecharge($uid)
  130. {
  131. return self::where('uid', $uid)
  132. ->where('category', 'now_money')
  133. ->where('type', 'recharge')
  134. ->where('pm', 1)
  135. ->where('status', 1)
  136. ->sum('number');
  137. }
  138. /**
  139. * 获取用户账单明细
  140. * @param int $uid 用户uid
  141. * @param int $page 页码
  142. * @param int $limit 展示多少条
  143. * @param int $type 展示类型
  144. * @return array
  145. * */
  146. public static function getUserBillList($uid, $page, $limit, $type, $category = 'now_money')
  147. {
  148. if (!$limit) return [];
  149. $model = self::where('uid', $uid)->where('category', $category)->order('add_time desc')->where('number', '<>', 0)
  150. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(DISTINCT id ORDER BY id DESC SEPARATOR ",") ids')->group('time');
  151. switch ((int)$type) {
  152. case 0:
  153. $model = $model;
  154. break;
  155. case 1:
  156. $model = $model->where('type', '<>', 'brokerage')->where('pm', 0);
  157. break;
  158. case 2:
  159. $model = $model->where('type', '<>', 'brokerage')->where('pm', 1);
  160. break;
  161. case 3:
  162. $model = $model->where('type', 'brokerage')->where('pm', 1);
  163. break;
  164. case 4:
  165. $model = $model->where('type', 'brokerage')->where('pm', 0);
  166. break;
  167. case 5:
  168. $model = $model->where('type', 'in', 'system_add_consumer,spread_add_consumer,consumer_product_refund,qr_add_consumer');
  169. break;
  170. case 6:
  171. $model = $model->where('type', 'in', 'consumer_product,qr_des_consumer');
  172. break;
  173. }
  174. if ($page) $model = $model->page((int)$page, (int)$limit);
  175. $list = ($list = $model->select()) ? $list->toArray() : [];
  176. $data = [];
  177. foreach ($list as $item) {
  178. $value['time'] = $item['time'];
  179. $value['list'] = self::where('id', 'in', $item['ids'])->field('add_time,title,number,pm,mark')->order('add_time DESC')->select();
  180. array_push($data, $value);
  181. }
  182. return $data;
  183. }
  184. /**
  185. * TODO 获取用户记录 按月查找
  186. * @param $uid $uid 用户编号
  187. * @param int $page $page 分页起始值
  188. * @param int $limit $limit 查询条数
  189. * @param string $category $category 记录类型
  190. * @param string $type $type 记录分类
  191. * @return mixed
  192. */
  193. public static function getRecordList($uid, $page = 1, $limit = 8, $category = 'now_money', $type = '')
  194. {
  195. $uids = User::where('spread_uid', $uid)->column('uid');
  196. $model = new self;
  197. $model = $model->alias('b');
  198. $model = $model->field("FROM_UNIXTIME(b.add_time, '%Y-%m') as time");
  199. // $model = $model->where('b.uid', $uid);
  200. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  201. $model = $model->where('o.refund_status', 0);
  202. // if (strlen(trim($type))) $model = $model->whereIn('b.type', $type);
  203. $model = $model->where('b.category', $category);
  204. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  205. $query->where(function ($query1) use ($uid, $type) {
  206. $query1->where('b.uid', $uid)->where('b.type', $type);
  207. })->whereOr(function ($query2) use ($uids, $type) {
  208. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  209. });
  210. });
  211. $model = $model->group("FROM_UNIXTIME(b.add_time, '%Y-%m')");
  212. $model = $model->order('time desc');
  213. $model = $model->page($page, $limit);
  214. return $model->select();
  215. }
  216. /**
  217. * TODO 按月份查找用户记录
  218. * @param $uid $uid 用户编号
  219. * @param int $addTime $addTime 月份
  220. * @param string $category $category 记录类型
  221. * @param string $type $type 记录分类
  222. * @return mixed
  223. */
  224. public static function getRecordListDraw($uid, $addTime = 0, $category = 'now_money', $type = '')
  225. {
  226. if (!$uid) [];
  227. $model = new self;
  228. $model = $model->field("title,FROM_UNIXTIME(add_time, '%Y-%m-%d %H:%i') as time,number,pm");
  229. $model = $model->where('uid', $uid);
  230. $model = $model->where("FROM_UNIXTIME(add_time, '%Y-%m')= '{$addTime}'");
  231. $model = $model->where('category', $category);
  232. if (strlen(trim($type))) $model = $model->where('type', 'in', $type);
  233. $model = $model->order('add_time desc');
  234. $list = $model->select();
  235. if ($list) return $list->toArray();
  236. else [];
  237. }
  238. /**
  239. * TODO 获取订单返佣记录
  240. * @param $uid
  241. * @param int $addTime
  242. * @param string $category
  243. * @param string $type
  244. * @return mixed
  245. */
  246. public static function getRecordOrderListDraw($uid, $addTime = 0, $category = 'now_money', $type = 'brokerage')
  247. {
  248. if (!strlen(trim($uid))) return [];
  249. $uids = User::where('spread_uid', $uid)->column('uid');
  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. } else {
  289. $model = $model->where('pm', 1);
  290. }
  291. return $model->sum('number');
  292. }
  293. /**
  294. * TODO 获取订单返佣记录总数
  295. * @param $uid
  296. * @param string $category
  297. * @param string $type
  298. * @return mixed
  299. */
  300. public static function getRecordOrderCount($uid, $category = 'now_money', $type = 'brokerage')
  301. {
  302. if (!strlen(trim($uid))) return 0;
  303. $uids = User::where('spread_uid', $uid)->column('uid');
  304. $model = new self;
  305. $model = $model->alias('b');
  306. $model = $model->join('StoreOrder o', 'o.id=b.link_id');
  307. $model = $model->where('o.refund_status', 0);
  308. $model = $model->where(function ($query) use ($uid, $type, $uids) {
  309. $query->where(function ($query1) use ($uid, $type) {
  310. $query1->where('b.uid', $uid)->where('b.type', $type);
  311. })->whereOr(function ($query2) use ($uids, $type) {
  312. $query2->where('b.uid', 'in', $uids)->where('b.type', 'pay_money');
  313. });
  314. });
  315. $model = $model->where('b.category', $category);
  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. }