// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AccountCountModel; use App\Models\AccountLogModel; use App\Models\MemberModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 交易管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class AccountLogService * @package App\Services\Common */ class AccountLogService extends BaseService { /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * AccountService constructor. */ public function __construct() { $this->model = new AccountLogModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; $type = isset($params['type']) ? $params['type'] : 0; $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0; $userType = isset($params['user_type']) ? $params['user_type'] : 0; $tradeType = isset($params['trade_type']) ? $params['trade_type'] : 0; $userId = isset($params['user_id']) ? $params['user_id'] : 0; if ($status > 0) { $where['status'] = $status; } else { $where['status'] = 1; } if ($type > 0) { $where['type'] = $type; } if ($coinType > 0) { $where['coin_type'] = $coinType; } if ($userId > 0) { $where['user_id'] = $userId; } $year = isset($params['year']) && $params['year'] ? $params['year'] : ''; $month = isset($params['month']) && $params['month'] ? $params['month'] : ''; $date = $year && $month ? "{$year}-{$month}" : ''; $date = $date ? $date : date('Y'); if (!$table = $this->model->getTable($year)) { return false; } $model = DB::table($table)->where($where)->where(function ($query) use ($userType, $tradeType, $year, $month) { if ($userType > 0) { $query->whereIn('user_type', [0, $userType]); } if($tradeType == 1){ $query->where('actual_money','>',0); }else if($tradeType==2){ $query->where('actual_money','<',0); } $date = $year && $month ? "{$year}-{$month}" : ''; $date = $date ? $date : date('Y-m'); if ($month) { $query->where('date', 'like', "{$date}%"); } else { $query->where('date', 'like', "{$year}%"); } }); // 统计 $countModel = clone $model; $countModel1 = clone $model; $counts = [ 'expend' => moneyFormat(abs($countModel->where('money', '<', 0)->sum('money')), 2), 'income' => moneyFormat($countModel1->where('money', '>', 0)->sum('money'), 2), ]; $list = $model->select(['*']) ->orderBy('create_time', 'desc') ->orderBy('id', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item->time_text = $item->create_time ? dateFormat($item->create_time, 'm月d日 H:i') : date('Y年m月'); $item->pay_at = $item->create_time ? datetime($item->create_time, 'Y-m-d H:i:s') : ''; $item->actual_money = moneyFormat($item->actual_money, 4); $item->money = moneyFormat($item->money, 4); if($item->user_type == 1){ $item->member = MemberService::make()->getCacheInfo(['id'=>$item->user_id], ['id','nickname','username']); }else if($item->user_type == 2){ $item->member = MerchantService::make()->getCacheInfo(['id'=>$item->user_id], ['id','name','user_id']); }else if($item->user_type == 3){ $item->member = AcceptorService::make()->getCacheInfo(['id'=>$item->user_id], ['id','relname','user_id']); }else if($item->user_type == 4){ $item->member = ['id'=>0,'nickname'=>'平台']; } } unset($item); } return [ 'pageSize' => $pageSize, 'counts' => $counts, 'date' => $date, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 积分统计 * @param $userId 用户ID * @param int $coinType 币种类型 * @return \float[][] */ public function getScoreCounts($userId=0, int $coinType=4) { $counts = [ 'day_total' => $this->getScoreCountByDate(4, $coinType, 23,3), 'award_total' => $this->getScoreCountByDate(1, $coinType, 9,3), ]; $xlTotal = ConfigService::make()->getConfigByCode('xd_total', 100000000); $xlTotal = $xlTotal>0? $xlTotal : 100000000; $counts['xl_total'] = $xlTotal; $counts['remain_total'] = moneyFormat($xlTotal - ($counts['day_total'] + $counts['award_total']),2); $counts['total'] = moneyFormat($counts['day_total'] + $counts['award_total'],2); return $counts; } /** * 分类账户统计 * @param $userId 用户ID * @param int $coinType 币种类型 * @param int $userType 用户账户类型 * @return \float[][] */ public function getCountsByType($userId, $coinType = 1, $userType = 1, $type=1) { $datas = [ 'counts' => [ 'today' => $this->getCountDataByDate($userId, $coinType, $userType,1), 'yestday' => $this->getCountDataByDate($userId, $coinType, $userType,2), 'total' => $this->getCountDataByDate($userId, $coinType, $userType,0) ], ]; if($type == 1){ $datas['tables'] = [ 'gl_burn' => $this->getCountDataByType($userId, $coinType, $userType,98), 'level_burn' => $this->getCountDataByType($userId, $coinType, $userType,97), 'live' => $this->getCountDataByType($userId, $coinType, $userType,1), 'tip' => $this->getCountDataByType($userId, $coinType, $userType,11), 'global' => $this->getCountDataByType($userId, $coinType, $userType,15), 'gl' => $this->getCountDataByType($userId, $coinType, $userType,13), 'point' => $this->getCountDataByType($userId, $coinType, $userType,14), 'invite' => $this->getCountDataByType($userId, $coinType, $userType,12), ]; $datas['tables'] = array_values($datas['tables']); } return $datas; } /** * 统计 * @param $userId * @param $coinType * @param $userType * @param string $field * @return array|mixed */ public function getCountDataByType($userId, $coinType, $userType, $type=0, $field='actual_money') { $cacheKey = "caches:accounts:count_type_{$userId}_{$coinType}_{$userType}_{$type}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = $this->model->where(['user_id'=> $userId,'type'=> $type,'coin_type'=> $coinType,'user_type'=>$userType,'status'=>1,'mark'=>1]) ->where($field,'>',0) ->sum($field); $data = $data? moneyFormat($data, 2) : 0.00; if($data){ RedisService::set($cacheKey, $data, rand(3,5)); } return $data; } /** * 统计 * @param $userId * @param $coinType * @param $userType * @param string $field * @return array|mixed */ public function getCountDataByDate($userId, $coinType, $userType, $dateType=1, $field='actual_money') { $cacheKey = "caches:accounts:count_date_{$userId}_{$coinType}_{$userType}_{$dateType}"; $data = RedisService::get($cacheKey); if($data){ return moneyFormat($data, 2); } $data = $this->model->where(['user_id'=> $userId,'coin_type'=> $coinType,'user_type'=>$userType,'status'=>1,'mark'=>1]) ->where(function($query) use($dateType){ // 今日 if($dateType == 1){ $query->where('date','>=', date('Y-m-d')); } // 昨日 else if ($dateType == 2){ $query->where('date','<', date('Y-m-d')); $query->where('date','>=', date('Y-m-d', strtotime('-1 day'))); } }) ->where($field,'>',0) ->sum($field); $data = $data? moneyFormat($data, 2) : 0.00; if($data){ RedisService::set($cacheKey, $data, rand(3,5)); } return $data; } /** * 积分统计 * @param $userId * @param $coinType * @param $userType * @param string $field * @return array|mixed */ public function getScoreCountByDate($userType = 1, $coinType, $type=23, $dateType=1, $field='actual_money') { $cacheKey = "caches:accounts:score_date_{$userType}_{$coinType}_{$type}_{$dateType}"; $data = RedisService::get($cacheKey); if($data){ return moneyFormat($data, 2); } $where = ['coin_type'=> $coinType,'status'=>1,'mark'=>1]; if($userType){ $where['user_type'] = $userType; } if($type>0){ $where['type'] = $type; } $data = $this->model->where($where) ->where(function($query) use($dateType){ // 今日 if($dateType == 1){ $query->where('date','>=', date('Y-m-d')); } // 昨日 else if($dateType == 2){ $query->where('date','<', date('Y-m-d')) ->where('date','>=', date('Y-m-d', strtotime('-1 day'))); } // 截止昨日 else if ($dateType == 3){ $query->where('date','<', date('Y-m-d')); } }) ->where($field,'>',0) ->sum($field); $data = $data? moneyFormat($data, 2) : 0.00; if($data){ RedisService::set($cacheKey, $data, rand(3,5)); } return $data; } /** * 个人每日业绩 * @param $userId * @param $date * @param int $type * @return array|mixed */ public function getTotal($userId, $date, $type = 2) { $cacheKey = "caches:members:account:total_{$userId}_{$date}_{$type}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = AccountCountModel::where(['user_id'=> $userId,'date'=>$date,'type'=> $type]) ->sum('money'); if($data){ RedisService::set($cacheKey, 5, 10); } return $data; } /** * 团队每日业绩 * @param $userId * @param $date * @param int $type * @return array|mixed */ public function getTeamTotal($userId, $date, $type = 2) { $cacheKey = "caches:members:account:tram_total_{$userId}_{$date}_{$type}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $data = AccountCountModel::from('account_counts as a') ->leftJoin('member as b','b.id','=','a.user_id') ->where(function($query) use($userId){ $query->where('b.id',$userId)->orWhereRaw('FIND_IN_SET(?,b.parents)', $userId); }) ->where(['a.date'=>$date,'a.type'=> $type]) ->sum('a.money'); if($data){ RedisService::set($cacheKey, $data, rand(5,10)); } return $data; } /** * @param $params * @param int $pageSize * @return array */ public function getCounts($params, $pageSize = 15) { $where = ['mark' => 1]; $status = isset($params['status']) ? $params['status'] : 0; $type = isset($params['type']) ? $params['type'] : 0; $coinType = isset($params['coin_type']) ? $params['coin_type'] : 0; $countType = isset($params['count_type']) ? $params['count_type'] : 0; $userId = isset($params['user_id']) ? $params['user_id'] : 0; $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0; if ($status > 0) { $where['status'] = $status; } else { $where['status'] = 1; } if ($type > 0) { $where['type'] = $type; } if ($coinType > 0) { $where['coin_type'] = $coinType; } if ($userId > 0 && $merchId <= 0) { $where['user_id'] = $userId; } if ($merchId > 0) { $where['merch_id'] = $merchId; } $year = isset($params['year']) && $params['year'] ? $params['year'] : ''; $month = isset($params['month']) && $params['month'] ? $params['month'] : ''; $types = [1 => '服务消费', 2 => '商城消费', 3 => '佣金结算', 4 => '平台调整', 5 => '余额充值', 6 => '余额转账', 7 => '退款', 8 => '聊天付费', 9 => '简历付费', 11 => '账户余额提现', 99 => '其他']; if (!$table = $this->model->getTable($year)) { return false; } $userType = isset($params['user_type']) ? $params['user_type'] : 0; $model = new AccountLogModel($table); $model = $model->where($where)->where(function ($query) use ($userType, $countType, $year, $month) { if ($userType > 0) { $query->whereIn('user_type', [0, $userType]); } if ($countType == 1) { $query->where('money', '<', 0); } else { $query->where('money', '>', 0); } $date = $year && $month ? "{$year}-{$month}" : ''; $date = $date ? $date : date('Y-m'); if ($month) { $query->where('date', 'like', "{$date}%"); } else { $query->where('date', 'like', "{$year}%"); } }); $model1 = clone $model; $total = abs(moneyFormat($model->sum('money'), 2)); $count = $model1->count('id'); $list = []; $sums = $model1->select(['type', DB::raw('sum(money) as total')]) ->groupBy('type') ->get()->keyBy('type'); $sums = $sums ? $sums->toArray() : []; foreach ($types as $k => $name) { $sum = isset($sums[$k]['total']) ? abs(moneyFormat($sums[$k]['total'], 2)) : 0; if ($sum > 0) { $list[] = [ 'id' => $k, 'name' => $name, 'total' => $sum, 'rate' => round($sum / $total * 100, 2), ]; } } return [ 'counts' => ['total' => $total, 'count' => $count], 'list' => $list, ]; } /** * 更新账户统计 * @param $userId 用户ID * @param $money 金额 * @param int $type 类型 * @return mixed */ public function saveCount($userId, $money, $type=1) { $date = date('Y-m-d'); $id = AccountCountModel::where(['user_id'=> $userId,'date'=> $date,'type'=> $type])->value('id'); if(!$id){ $data = ['user_id'=>$userId,'date'=> $date,'type'=>$type,'money'=>$money,'create_time'=>time(),'update_time'=> time()]; return AccountCountModel::insertGetId($data); }else{ $data = ['money'=>DB::raw("money + {$money}"),'update_time'=>time()]; return AccountCountModel::where(['id'=> $id])->update($data); } } }