UserBalanceLog.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\weixin\model;
  3. use think\Model;
  4. class UserBalanceLog extends Model
  5. {
  6. protected $table = 'sg_user_balance_log';
  7. /**
  8. * 验证用户是否已经结算过对应类型收益
  9. * @param $userid 结算用户ID
  10. * @param $sourceUid 来源用户ID
  11. * @param $type 收益类型
  12. * @return mixed
  13. */
  14. public static function checkHasMarket($userid, $sourceUid, $type){
  15. return UserBalanceLog::where(['user_id'=> $userid, 'source_uid'=> $sourceUid,'type'=> $type, 'status'=> 1])->value('id');
  16. }
  17. /**
  18. * 验证用户是否已经结算过对应类型项目收益
  19. * @param $userid 结算用户ID
  20. * @param $sourceUid 来源用户ID
  21. * @param $type 收益类型
  22. * @return mixed
  23. */
  24. public static function checkHasMarketBySource($userid, $sourceUid, $sourceId, $type){
  25. $where = ['user_id'=> $userid, 'source_uid'=> $sourceUid,'source_id'=> $sourceId,'type'=> $type, 'status'=> 1];
  26. return UserBalanceLog::where($where)->value('id');
  27. }
  28. /**
  29. * 获取收益排行榜
  30. * @param int $pageSize 数量前多少名
  31. * @return \think\Paginator
  32. * @throws \think\exception\DbException
  33. */
  34. public static function getIncomeRankList($pageSize= 50, $userId=0, $type=0){
  35. $dataList = UserBalanceLog::alias('ub')
  36. ->leftJoin('user u','u.id=ub.user_id')
  37. ->where(['ub.status'=> 2,'u.agent_type'=> 1,'u.agent_status'=> 1, 'u.user_type'=> 2])
  38. ->where('type','>', 1)
  39. ->where('type','<', 30)
  40. ->where(function($query) use($type){
  41. })
  42. ->field('u.id,u.user_nickname,u.sex,u.avatar,'.db()->raw('sum(`ub`.`change`) as income'))
  43. ->group('user_id')
  44. ->order(db()->raw('sum(`ub`.`change`) desc'))
  45. ->order('ub.user_id asc')
  46. ->paginate($pageSize);
  47. $dataList = $dataList? $dataList->toArray() : [];
  48. $counts['rank_num'] = 0;
  49. if($dataList && $userId){
  50. foreach ($dataList['data'] as $k => $item){
  51. if($item['id'] == $userId){
  52. $counts['rank_num'] = $k+1;
  53. }
  54. }
  55. $dataList['counts'] = $counts;
  56. }
  57. return $dataList;
  58. }
  59. /**
  60. * 统计数量
  61. * @return float|int|string
  62. */
  63. public static function getRankCount(){
  64. return UserBalanceLog::alias('ub')
  65. ->leftJoin('user u','u.id=ub.user_id')
  66. ->where(['ub.status'=> 2,'u.agent_type'=> 1,'u.agent_status'=> 1, 'u.user_type'=> 2])
  67. ->where('type','>', 1)
  68. ->where('type','<', 30)
  69. ->field('u.id,u.user_nickname,u.avatar,'.db()->raw('sum(`ub`.`change`) as income'))
  70. ->group('user_id')
  71. ->order(db()->raw('sum(`ub`.`change`) desc'))
  72. ->order('ub.user_id asc')
  73. ->count('ub.user_id');
  74. }
  75. }