UserBill.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\user;
  12. use app\wap\model\special\Special;
  13. use basic\ModelBasic;
  14. use traits\ModelTrait;
  15. class UserBill extends ModelBasic
  16. {
  17. use ModelTrait;
  18. protected $insert = ['add_time'];
  19. protected function setAddTimeAttr()
  20. {
  21. return time();
  22. }
  23. public static function income($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1, $get_uid = 0)
  24. {
  25. $pm = 1;
  26. return self::set(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'get_uid'));
  27. }
  28. public static function expend($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1)
  29. {
  30. $pm = 0;
  31. return self::set(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm'));
  32. }
  33. public static function getSginDay($year, $month, $uid)
  34. {
  35. $model = self::where('uid', $uid)->where(['category' => 'integral', 'type' => 'sign', 'status' => 1, 'pm' => 1]);
  36. if (!$year && !$month) {
  37. $model->whereTime('add_time', 'm');
  38. } else {
  39. $t = date('t', strtotime($year . '-' . $month));
  40. $model->whereTime('add_time', 'between', [strtotime($year . '-' . $month), strtotime($year . '-' . $month . '-' . $t)]);
  41. }
  42. $list = $model->field(['from_unixtime(add_time,\'%d\') as time'])->order('time asc')->select();
  43. count($list) && $list = $list->toArray();
  44. foreach ($list as &$item) {
  45. $item['day'] = ltrim($item['time'], '\0');
  46. }
  47. return $list;
  48. }
  49. /**
  50. * 获取提现记录或者佣金记录
  51. * @param $where arrat 查询条件
  52. * @param $uid int 用户uid
  53. * @return array
  54. *
  55. * */
  56. public static function getSpreadList($where, $uid)
  57. {
  58. $uids = User::where('spread_uid', $uid)->column('uid');
  59. $uids1 = User::where('spread_uid', 'in', $uids)->group('uid')->column('uid');
  60. $model = self::where('a.uid', $uid)->alias('a')->join('__USER__ u', 'u.uid=a.uid')->where('a.link_id', 'neq', 0)->order('a.add_time desc');
  61. switch ((int)$where['type']) {
  62. case 0:
  63. $model=$model->join('store_order o', 'o.id = a.link_id')->whereIn('o.uid', $uids);
  64. $model = $model->where('a.category', 'now_money')->where('a.type', 'in', ['brokerage','brokerage_return']);
  65. break;
  66. case 1:
  67. $model=$model->join('store_order o', 'o.id = a.link_id')->whereIn('o.uid', $uids1);
  68. $model = $model->where('a.category', 'now_money')->where('a.type', 'in', ['brokerage','brokerage_return']);
  69. break;
  70. case 2:
  71. $model = $model->where('a.category', 'now_money')->where('a.type','in','extract,extract_success,extract_fail');
  72. break;
  73. }
  74. if ($where['data']) {
  75. $where['data'] = str_replace(['年', '月'], ['-', ''], $where['data']);
  76. $starttime = strtotime($where['data']);
  77. $dayArray = explode('-', $where['data']);
  78. $day = self::DaysInMonth($dayArray[0], $dayArray[1]);
  79. $endtime = bcadd(strtotime($where['data']), bcmul($day, 86400, 0), 0);
  80. $model = $model->where('a.add_time', 'between', [$starttime, $endtime]);
  81. }
  82. $list = $model->field(['a.get_uid', 'a.mark', 'a.title', 'a.number', 'a.category', 'a.type','FROM_UNIXTIME(a.add_time,"%Y-%c-%d %H:%i:%s") as add_time', 'a.pm', 'a.link_id'])
  83. ->page((int)$where['page'], (int)$where['limit'])->select();
  84. $list = count($list) ? $list->toArray() : [];
  85. foreach ($list as &$item) {
  86. $item['nickname'] = '';
  87. if ((int)$where['type'] == 0 || (int)$where['type'] == 1) {
  88. $item['nickname'] = User::where('uid', $item['get_uid'])->value('nickname');
  89. $item['title'] = Special::PreWhere()->where('id', self::getDb('store_order')->where('id', $item['link_id'])->value('cart_id'))->value('title');
  90. }
  91. if ((int)$where['type'] == 2 && $item['category']=='now_money' && $item['type']=='extract_success' && $item['pm']==0) {
  92. $item['pm']=3;
  93. $item['number']='提现成功';
  94. }
  95. }
  96. $page = $where['page'] + 1;
  97. return compact('list', 'page');
  98. }
  99. /*
  100. * 获得某年某月的天数
  101. * */
  102. public static function DaysInMonth($month, $year)
  103. {
  104. return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
  105. }
  106. /*
  107. * 获取总佣金
  108. * */
  109. public static function getBrokerage($uid,$category = 'now_money',$type='brokerage',$where)
  110. {
  111. return self::getModelTime($where,self::where('uid','in',$uid)->where('category',$category)
  112. ->where('type',$type)->where('pm',1)->where('status',1))->sum('number');
  113. }
  114. /*
  115. * 获取返还佣金
  116. * */
  117. public static function getReturnBrokerage($uid,$category = 'now_money',$type='brokerage_return',$where)
  118. {
  119. return self::getModelTime($where,self::where('uid','in',$uid)->where('category',$category)
  120. ->where('type',$type)->where('pm',0)->where('status',1))->sum('number');
  121. }
  122. /**获取用户佣金金额
  123. * @param int $uid
  124. */
  125. public static function getCommissionAmount($uid=0){
  126. $brokerage=self::where('uid','in',$uid)->where('category','now_money')
  127. ->where('type','brokerage')->where('pm',1)->where('status',1)->sum('number');
  128. $brokerage_return=self::where('uid','in',$uid)->where('category','now_money')
  129. ->where('type','brokerage_return')->where('pm',0)->where('status',1)->sum('number');
  130. $commission=bcsub($brokerage,$brokerage_return,2);
  131. return $commission;
  132. }
  133. public static function getUserGoldBill(array $where,$page = 0,$limit = 10)
  134. {
  135. $model = self::where('status',1);
  136. if ($where){
  137. $list = $model->where($where);
  138. }
  139. $list = $model->order('add_time desc')->page((int)$page,(int)$limit)->select();
  140. $list = count($list) ? $list->toArray() : [];
  141. foreach ($list as &$item){
  142. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  143. }
  144. $page--;
  145. return ['list'=>$list,'page'=> $page];
  146. }
  147. }