UserBill.php 12 KB

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