DividendsRecord.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 app\models\system\SystemGroupLevel;
  11. use app\models\system\SystemUserLevel;
  12. use think\facade\Cache;
  13. use crmeb\traits\ModelTrait;
  14. use crmeb\basic\BaseModel;
  15. use think\facade\Db;
  16. /**
  17. * TODO 用户消费新增金额明细 model
  18. * Class UserBill
  19. * @package app\models\user
  20. */
  21. class DividendsRecord extends BaseModel
  22. {
  23. /**
  24. * 数据表主键
  25. * @var string
  26. */
  27. protected $pk = 'id';
  28. /**
  29. * 模型名称
  30. * @var string
  31. */
  32. protected $name = 'dividends_record';
  33. use ModelTrait;
  34. public static function income($title, $uid, $type, $number , $mark = '', $status = 1)
  35. {
  36. $pm = 1;
  37. $add_time = time();
  38. return self::create(compact('title', 'uid', 'type', 'number', 'mark', 'status', 'pm', 'add_time'));
  39. }
  40. // public static function expend($title, $uid, $category, $type, $number, $link_id = 0, $balance = 0, $mark = '', $status = 1)
  41. // {
  42. // $pm = 0;
  43. // $add_time = time();
  44. // return self::create(compact('title', 'uid', 'link_id', 'category', 'type', 'number', 'balance', 'mark', 'status', 'pm', 'add_time'));
  45. // }
  46. public static function setWhereList($where)
  47. {
  48. $time['data'] = '';
  49. if ($where['start_time'] != '' && $where['end_time'] != '') {
  50. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  51. }
  52. $model = self::getModelTime($time, self::alias('A')
  53. ->join('user B', 'B.uid=A.uid')
  54. ->where('A.category', 'not in', 'integral')
  55. ->order('A.add_time desc'), 'A.add_time');
  56. // if (trim($where['type']) != '') {
  57. // $model = $model->where('A.type', $where['type']);
  58. // } else {
  59. // $model = $model->where('A.type', 'not in', 'gain,system_sub,deduction,sign');
  60. // }
  61. if ($where['nickname'] != '') {
  62. $model = $model->where('B.nickname|B.uid', 'like', "%$where[nickname]%");
  63. }
  64. return $model->field(['A.*', 'FROM_UNIXTIME(A.add_time,"%Y-%m-%d %H:%i:%s") as add_time', 'B.uid', 'B.nickname']);
  65. }
  66. /**
  67. * 积分/佣金 使用记录
  68. * @param $uid
  69. * @param $page
  70. * @param $limit
  71. * @param string $category
  72. * @return array|\think\Collection
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. * @throws \think\exception\DbException
  76. */
  77. public static function userBillList($uid, $page, $limit, $where = [])
  78. {
  79. // if ($page) {
  80. $list = self::where('uid', $uid)
  81. // ->where('category', $category)
  82. ->field('mark,pm,number,add_time')
  83. ->where('status', 1)
  84. ->where($where)
  85. ->order('add_time DESC')
  86. ->page((int)$page, (int)$limit)
  87. ->select();
  88. // } else {
  89. // $list = self::where('uid', $uid)
  90. // ->where('category', $category)
  91. // ->field('mark,pm,number,add_time')
  92. // ->where('status', 1)
  93. // ->where($where)
  94. // ->order('add_time DESC')
  95. // ->select();
  96. // }
  97. $list = count($list) ? $list->toArray() : [];
  98. foreach ($list as &$v) {
  99. $v['add_time'] = date('Y/m/d H:i', $v['add_time']);
  100. $v['number'] = floatval($v['number']);
  101. }
  102. return $list;
  103. }
  104. /*
  105. * 获取用户账单明细
  106. * @param int $uid 用户uid
  107. * @param int $page 页码
  108. * @param int $limit 展示多少条
  109. * @param int $type 展示类型
  110. * @return array
  111. * */
  112. public static function getUserBillList($uid, $page, $limit)
  113. {
  114. if (!$limit) return [];
  115. $model = self::where('uid', $uid)->order('add_time desc')->where('number', '<>', 0)
  116. ->field('FROM_UNIXTIME(add_time,"%Y-%m") as time,group_concat(id SEPARATOR ",") ids')->group('time');
  117. if ($page) $model = $model->page((int)$page, (int)$limit);
  118. $list = ($list = $model->select()) ? $list->toArray() : [];
  119. $data = [];
  120. foreach ($list as $item) {
  121. $value['time'] = $item['time'];
  122. $value['list'] = self::where('id', 'in', $item['ids'])->field('FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time,title,number,pm,status,mark')->order('add_time DESC')->select();
  123. array_push($data, $value);
  124. }
  125. return $data;
  126. }
  127. // 全网分红
  128. public static function wholeNetworkDividend(){
  129. $pool = sys_config('dividend_pool');
  130. if (!$pool|| $pool <= 0) return false;
  131. $user_list = Db::name('User')->where('level', '>', 0)
  132. ->whereOr('group_level', '>', 0)
  133. ->select();
  134. @file_put_contents('quanju.txt', json_encode($user_list) . "-分红用户列表\r\n", 8);
  135. foreach ($user_list as $user){
  136. $dividends=0;
  137. if ($user['level'] > 0){
  138. $level_grade = SystemUserLevel::where('id', $user['level'])->value('grade');
  139. $level_dividends = SystemUserLevel::where('grade', '<=', $level_grade)->sum('dividends');
  140. $dividends = bcadd($level_dividends, $dividends, 2);
  141. }
  142. if ($user['group_level'] > 0){
  143. $group_level_grade = SystemGroupLevel::where('id', $user['group_level'])->value('grade');
  144. $group_level_dividends = SystemGroupLevel::where('grade', '<=', $group_level_grade)->sum('dividends');
  145. $dividends = bcadd($group_level_dividends, $dividends, 2);
  146. }
  147. @file_put_contents('quanju.txt', $dividends . "-分红比例\r\n", 8);
  148. @file_put_contents('quanju.txt', $pool . "-分红奖池\r\n", 8);
  149. if ($dividends > 0){
  150. $brokerage = bcmul($pool, bcdiv($dividends, 100, 2), 2);
  151. $id = self::income('全网分红', $user['uid'], 'brokerage_price', $brokerage, '订单推荐佣金');
  152. $blance = bcadd($user['brokerage_price'], $brokerage, 2);
  153. UserBill::income('全网分红', $user['uid'], 'brokerage_price', 'dividend', $brokerage, $id, $blance, '全网分红奖励');
  154. User::where('uid', $user['uid'])->update(['brokerage_price' => $blance]);
  155. }
  156. }
  157. return true;
  158. }
  159. }