| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Common;
- use App\Models\AccountLogModel;
- use App\Models\ActionLogModel;
- use App\Services\BaseService;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- /**
- * 账户明细管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class AccountService
- * @package App\Services\Common
- */
- class AccountService extends BaseService
- {
- public static $instance = null;
- /**
- * 构造函数
- * @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)
- {
- $query = $this->getQuery($params);
- $model = clone $query;
- $counts = [
- 'count' => $model->count('a.id'),
- 'total' => $model->sum('a.money'),
- ];
- $list = $query->select(['a.*'])
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- $accountTypes = config('payment.accountTypes');
- foreach ($list['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : '';
- $type = isset($item['type']) ? intval($item['type']) : 0;
- $item['type_text'] = isset($item['remark']) ? trim($item['remark']) : '';
- $item['member'] = isset($item['member']) ? $item['member'] : [];
- if (empty($item['type_text'])) {
- $item['type_text'] = isset($accountTypes[$type]) ? $accountTypes[$type] : '收支明细';
- }
- }
- }
- return [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'counts' => $counts,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return \Illuminate\Database\Eloquent\Builder
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status']) ? $params['status'] : 0;
- $type = isset($params['type']) ? $params['type'] : 0;
- if ($status > 0) {
- $where['a.status'] = $status;
- }
- if ($type > 0) {
- $where['a.type'] = $type;
- }
- return $this->model->with(['member'])->from("account_logs as a")
- ->leftJoin('member as b', 'b.id', '=', 'a.user_id')
- ->where($where)
- ->where(function ($query) use ($params) {
- $keyword = isset($params['keyword']) ? $params['keyword'] : '';
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId) {
- $query->where('a.user_id', $userId);
- }
- if ($keyword) {
- $query->where(function ($query) use ($keyword) {
- $query->where('b.nickname', 'like', "%{$keyword}%")
- ->orWhere('b.mobile', 'like', "%{$keyword}%");
- });
- }
- $orderNo = isset($params['order_no']) ? trim($params['order_no']) : '';
- if ($orderNo) {
- $query->where(function ($query) use ($orderNo) {
- $query->where('a.source_order_no', 'like', "%{$orderNo}%");
- });
- }
- })
- ->where(function ($query) use ($params) {
- // 日期
- $date = isset($params['date']) ? $params['date'] : [];
- $start = isset($date[0]) ? $date[0] : '';
- $end = isset($date[1]) ? $date[1] : '';
- $end = $start >= $end ? '' : $end;
- if ($start) {
- $query->where('a.create_time', '>=', strtotime($start));
- }
- if ($end) {
- $query->where('a.create_time', '<=', strtotime($end));
- }
- });
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author laravel开发员
- */
- public function edit()
- {
- $data = request()->all();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 删除
- * @return array
- */
- public function delete()
- {
- // 设置日志标题
- ActionLogModel::setRecord(session('userId'), ['type' => 1, 'title' => "删除账户明细", 'content' => json_encode(request()->post(), 256), 'module' => 'admin']);
- ActionLogModel::record();
- $this->model->where('mark', 0)->where('update_time', '<=', time() - 7 * 86400)->delete();
- return parent::delete(); // TODO: Change the autogenerated stub
- }
- /**
- * 获取账户日志统计数据
- *
- * @param string $type 统计类型:today, yesterday, month, last_month, year, all
- * @param int|null $status 交易状态:1-已完成,2-待处理,3-失败/取消
- * @param int|null $start_time 开始时间戳
- * @param int|null $end_time 结束时间戳
- * @return array 统计结果
- */
- public function getStatistics($dateType = 'all', $type = null, $status = null, $start_time = null, $end_time = null)
- {
- // 基础查询
- $query = DB::table('account_logs');
- // 时间过滤
- if ($start_time && $end_time) {
- // 如果提供了开始和结束时间
- $query->whereBetween('date', [Carbon::createFromTimestamp($start_time), Carbon::createFromTimestamp($end_time)]);
- } else {
- // 根据不同的 `type` 筛选时间范围
- $currentDate = Carbon::now();
- switch ($dateType) {
- case 'today':
- $query->whereDate('date', '=', $currentDate->toDateString()); // 今日
- break;
- case 'yesterday':
- $query->whereDate('date', '=', $currentDate->subDay()->toDateString()); // 昨日
- break;
- case 'month':
- $query->whereMonth('date', '=', $currentDate->month) // 本月
- ->whereYear('date', '=', $currentDate->year);
- break;
- case 'last_month':
- $query->whereMonth('date', '=', $currentDate->subMonth()->month) // 上月
- ->whereYear('date', '=', $currentDate->year);
- break;
- case 'year':
- $query->whereYear('date', '=', $currentDate->year); // 本年
- break;
- case 'all':
- // 不进行时间过滤,返回所有数据
- break;
- default:
- break;
- }
- }
- // 如果传入了 status 参数,则加入状态过滤
- if ($status !== null) {
- $query->where('status', '=', $status);
- }
- // 如果传入了 status 参数,则加入状态过滤
- if ($type !== null) {
- $query->where('type', '=', $type);
- }
- // 获取统计数据
- return $query->selectRaw('
- count(*) as total_count,
- sum(money) as total_money
- ')->first();
- }
- public function getRevenueStats(array $params)
- {
- $dateType = $params['dateType'] ?? 'day';
- $vipType = $params['vip_type'] ?? null; // zg_vip | zsb_vip | video_vip
- $status = $params['status'] ?? null;
- $type = $params['type'] ?? null;
- $page = max(1, (int) ($params['page'] ?? 1));
- $limit = max(1, (int) ($params['limit'] ?? 10));
- $startTimestamp = !empty($params['start_time']) ? strtotime($params['start_time']) : strtotime('2000-01-01');
- $endTimestamp = !empty($params['end_time']) ? strtotime($params['end_time']) : time();
- if ($endTimestamp < $startTimestamp) {
- $endTimestamp = $startTimestamp;
- }
- // 日期分组
- switch ($dateType) {
- case 'year':
- $groupRaw = "FROM_UNIXTIME(lev_l.create_time,'%Y')";
- break;
- case 'month':
- $groupRaw = "FROM_UNIXTIME(lev_l.create_time,'%Y-%m')";
- break;
- case 'week':
- $groupRaw = "CONCAT(YEAR(FROM_UNIXTIME(lev_l.create_time)),'-',LPAD(WEEK(FROM_UNIXTIME(lev_l.create_time),1),2,'0'))";
- break;
- case 'day':
- default:
- $groupRaw = "FROM_UNIXTIME(lev_l.create_time,'%Y-%m-%d')";
- break;
- }
- $query = DB::table('account_logs as l')
- ->join('member as m', 'l.user_id', '=', 'm.id')
- ->selectRaw("
- {$groupRaw} as stat_date,
- COUNT(*) as total_count,
- SUM(lev_l.money) as total_money
- ")
- ->whereBetween('l.create_time', [$startTimestamp, $endTimestamp]);
- if ($status !== null) {
- $query->where('l.status', $status);
- }
- // VIP类型筛选
- if ($vipType == 'zg_vip') {
- $query->where('m.is_zg_vip', 1);
- } elseif ($vipType == 'zsb_vip') {
- $query->where('m.is_zsb_vip', 1);
- } elseif ($vipType == 'video_vip') {
- $query->where('m.is_video_vip', 1);
- }
- if ($type) {
- $query->where('l.type', $type);
- }
- $query->groupBy('stat_date')->orderBy('stat_date', 'desc');
- // 总条数
- $total = DB::table(DB::raw("({$query->toSql()}) as t"))
- ->mergeBindings($query)
- ->count();
- $list = $query->forPage($page, $limit)->get();
- return [
- 'list' => $list,
- 'total' => $total,
- 'page' => $page,
- 'limit' => $limit,
- ];
- }
- }
|