| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace app\weixin\model;
- use think\Model;
- class UserBalanceLog extends Model
- {
- protected $table = 'sg_user_balance_log';
- /**
- * 验证用户是否已经结算过对应类型收益
- * @param $userid 结算用户ID
- * @param $sourceUid 来源用户ID
- * @param $type 收益类型
- * @return mixed
- */
- public static function checkHasMarket($userid, $sourceUid, $type){
- return UserBalanceLog::where(['user_id'=> $userid, 'source_uid'=> $sourceUid,'type'=> $type, 'status'=> 1])->value('id');
- }
- /**
- * 验证用户是否已经结算过对应类型项目收益
- * @param $userid 结算用户ID
- * @param $sourceUid 来源用户ID
- * @param $type 收益类型
- * @return mixed
- */
- public static function checkHasMarketBySource($userid, $sourceUid, $sourceId, $type){
- $where = ['user_id'=> $userid, 'source_uid'=> $sourceUid,'source_id'=> $sourceId,'type'=> $type, 'status'=> 1];
- return UserBalanceLog::where($where)->value('id');
- }
- /**
- * 获取收益排行榜
- * @param int $pageSize 数量前多少名
- * @return \think\Paginator
- * @throws \think\exception\DbException
- */
- public static function getIncomeRankList($pageSize= 50, $userId=0, $type=0){
- $dataList = UserBalanceLog::alias('ub')
- ->leftJoin('user u','u.id=ub.user_id')
- ->where(['ub.status'=> 2,'u.agent_type'=> 1,'u.agent_status'=> 1, 'u.user_type'=> 2])
- ->where('type','>', 1)
- ->where('type','<', 30)
- ->where(function($query) use($type){
- })
- ->field('u.id,u.user_nickname,u.sex,u.avatar,'.db()->raw('sum(`ub`.`change`) as income'))
- ->group('user_id')
- ->order(db()->raw('sum(`ub`.`change`) desc'))
- ->order('ub.user_id asc')
- ->paginate($pageSize);
- $dataList = $dataList? $dataList->toArray() : [];
- $counts['rank_num'] = 0;
- if($dataList && $userId){
- foreach ($dataList['data'] as $k => $item){
- if($item['id'] == $userId){
- $counts['rank_num'] = $k+1;
- }
- }
- $dataList['counts'] = $counts;
- }
- return $dataList;
- }
- /**
- * 统计数量
- * @return float|int|string
- */
- public static function getRankCount(){
- return UserBalanceLog::alias('ub')
- ->leftJoin('user u','u.id=ub.user_id')
- ->where(['ub.status'=> 2,'u.agent_type'=> 1,'u.agent_status'=> 1, 'u.user_type'=> 2])
- ->where('type','>', 1)
- ->where('type','<', 30)
- ->field('u.id,u.user_nickname,u.avatar,'.db()->raw('sum(`ub`.`change`) as income'))
- ->group('user_id')
- ->order(db()->raw('sum(`ub`.`change`) desc'))
- ->order('ub.user_id asc')
- ->count('ub.user_id');
- }
- }
|