wesmiler %!s(int64=2) %!d(string=hai) anos
pai
achega
fd03a86b10

+ 3 - 4
app/Http/Controllers/Api/v1/AccountController.php

@@ -38,10 +38,9 @@ class AccountController extends webApp
      */
     public function counts()
     {
-        $params =request()->post();
-        $pageSize = request()->post('pageSize', 15);
-        $params['user_id'] = isset($params['user_id'])? $params['user_id'] : $this->userId;
-        $datas = AccountLogService::make()->getCounts($params, $pageSize);
+        $coinType =request()->post('coin_type',1);
+        $userType =request()->post('user_type',1);
+        $datas = AccountLogService::make()->getCountsByType($this->userId, $coinType, $userType);
         return message(1010, true, $datas);
     }
 

+ 174 - 76
app/Services/Api/AccountLogService.php

@@ -13,6 +13,7 @@ namespace App\Services\Api;
 
 use App\Models\AccountLogModel;
 use App\Services\BaseService;
+use App\Services\RedisService;
 use Illuminate\Support\Facades\DB;
 
 /**
@@ -55,51 +56,51 @@ class AccountLogService extends BaseService
     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;
-        $userId = isset($params['user_id'])? $params['user_id'] : 0;
-        $merchId = isset($params['merch_id'])? $params['merch_id'] : 0;
-        if($status>0){
+        $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;
+        $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{
+        } else {
             $where['status'] = 1;
         }
 
-        if($type>0){
+        if ($type > 0) {
             $where['type'] = $type;
         }
-        if($coinType>0){
+        if ($coinType > 0) {
             $where['coin_type'] = $coinType;
         }
 
-        if($userId>0 && $merchId<=0){
+        if ($userId > 0 && $merchId <= 0) {
             $where['user_id'] = $userId;
         }
 
-        if($merchId>0){
+        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)){
+        $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,$year,$month){
-            if($userType>0){
+        $model = DB::table($table)->where($where)->where(function ($query) use ($userType, $year, $month) {
+            if ($userType > 0) {
                 $query->whereIn('user_type', [0, $userType]);
             }
 
-            $date = $year&&$month? "{$year}-{$month}" : '';
-            $date = $date? $date : date('Y-m');
-            if($month){
+            $date = $year && $month ? "{$year}-{$month}" : '';
+            $date = $date ? $date : date('Y-m');
+            if ($month) {
                 $query->where('date', 'like', "{$date}%");
-            }else{
+            } else {
                 $query->where('date', 'like', "{$year}%");
             }
         });
@@ -108,35 +109,132 @@ class AccountLogService extends BaseService
         $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),
+            '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')
+            ->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->create_time = $item->create_time? datetime($item->create_time,'Y-m-d H.i.s') : '';
-                $titles = ['','服务消费','商城消费','佣金结算','平台调整','余额充值','余额转账','退款','聊天付费','简历付费','代理佣金','账户余额提现','商户余额提现'];
-                $item->remark = $item->remark? $item->remark : (isset($titles[$item->remark])? $titles[$item->remark]:'其他');
+        $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->create_time = $item->create_time ? datetime($item->create_time, 'Y-m-d H.i.s') : '';
+                $titles = ['', '服务消费', '商城消费', '佣金结算', '平台调整', '余额充值', '余额转账', '退款', '聊天付费', '简历付费', '代理佣金', '账户余额提现', '商户余额提现'];
+                $item->remark = $item->remark ? $item->remark : (isset($titles[$item->remark]) ? $titles[$item->remark] : '其他');
             }
             unset($item);
         }
 
         return [
-            'pageSize'=> $pageSize,
-            'counts'=> $counts,
-            'date'=> $date,
-            'total'=>isset($list['total'])? $list['total'] : 0,
-            'list'=> isset($list['data'])? $list['data'] : []
+            '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 币种类型
+     * @param int $userType 用户账户类型
+     * @return \float[][]
+     */
+    public function getCountsByType($userId, $cointype = 1, $userType = 1)
+    {
+        $datas = [
+            'counts' => [
+                'day' => (float)$this->getCountDataByDate($userId, $cointype, $userType,1),
+                'yestday' => (float)$this->getCountDataByDate($userId, $cointype, $userType,2),
+                'total' => (float)$this->getCountDataByDate($userId, $cointype, $userType,0)
+            ],
+            'tables' => [
+                'gl_burn' => (float)$this->getCountDataByType($userId, $cointype, $userType,98),
+                'level_burn' => (float)$this->getCountDataByType($userId, $cointype, $userType,97),
+                'live' => (float)$this->getCountDataByType($userId, $cointype, $userType,1),
+                'tip' => (float)$this->getCountDataByType($userId, $cointype, $userType,11),
+                'global' => (float)$this->getCountDataByType($userId, $cointype, $userType,15),
+                'gl' => (float)$this->getCountDataByType($userId, $cointype, $userType,13),
+                'point' => (float)$this->getCountDataByType($userId, $cointype, $userType,14),
+                'invite' => (float)$this->getCountDataByType($userId, $cointype, $userType,12),
+            ]
+        ];
+
+
+
+        return $datas;
+    }
+
+    /**
+     * 统计
+     * @param $userId
+     * @param $coinType
+     * @param $userType
+     * @param string $field
+     * @return array|mixed
+     */
+    public function getCountDataByType($userId, $coinType, $userType, $type=0, $field='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='money')
+    {
+        $cacheKey = "caches:accounts:count_date_{$userId}_{$coinType}_{$userType}_{$dateType}";
+        $data = RedisService::get($cacheKey);
+        if($data){
+            return $data;
+        }
+
+        $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 $params
      * @param int $pageSize
      * @return array
@@ -144,84 +242,84 @@ class AccountLogService extends BaseService
     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){
+        $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{
+        } else {
             $where['status'] = 1;
         }
 
-        if($type>0){
+        if ($type > 0) {
             $where['type'] = $type;
         }
-        if($coinType>0){
+        if ($coinType > 0) {
             $where['coin_type'] = $coinType;
         }
-        if($userId>0 && $merchId<=0){
+        if ($userId > 0 && $merchId <= 0) {
             $where['user_id'] = $userId;
         }
 
-        if($merchId>0){
+        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)){
+        $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;
+        $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){
+        $model = $model->where($where)->where(function ($query) use ($userType, $countType, $year, $month) {
+            if ($userType > 0) {
                 $query->whereIn('user_type', [0, $userType]);
             }
 
-            if($countType == 1){
+            if ($countType == 1) {
                 $query->where('money', '<', 0);
-            }else{
+            } else {
                 $query->where('money', '>', 0);
             }
 
-            $date = $year&&$month? "{$year}-{$month}" : '';
-            $date = $date? $date : date('Y-m');
-            if($month){
+            $date = $year && $month ? "{$year}-{$month}" : '';
+            $date = $date ? $date : date('Y-m');
+            if ($month) {
                 $query->where('date', 'like', "{$date}%");
-            }else{
+            } else {
                 $query->where('date', 'like', "{$year}%");
             }
         });
 
         $model1 = clone $model;
-        $total = abs(moneyFormat($model->sum('money'),2));
+        $total = abs(moneyFormat($model->sum('money'), 2));
         $count = $model1->count('id');
         $list = [];
 
-        $sums = $model1->select(['type',DB::raw('sum(money) as total')])
+        $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){
+        $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),
+                    'id' => $k,
+                    'name' => $name,
+                    'total' => $sum,
+                    'rate' => round($sum / $total * 100, 2),
                 ];
             }
         }
         return [
-            'counts'=> ['total'=> $total,'count'=> $count],
-            'list'=> $list,
+            'counts' => ['total' => $total, 'count' => $count],
+            'list' => $list,
         ];
     }
 }