UserBill.php 12 KB

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