// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AccountLogModel; use App\Services\BaseService; 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; $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,$year,$month){ if($userType>0){ $query->whereIn('user_type', [0, $userType]); } $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->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'] : [] ]; } /** * @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, ]; } }