| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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;
- $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'] : '';
- $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);
- }
- }
- }
|