| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace App\Services\Common;
- use App\Models\OrderModel;
- use App\Models\MemberModel;
- use App\Models\WithdrawModel;
- use App\Models\AccountLogModel;
- use App\Services\BaseService;
- use Illuminate\Support\Facades\DB;
- /**
- * 财务服务
- */
- class FinancialService extends BaseService
- {
- /**
- * 获取数据统计
- */
- public function getStatistics()
- {
- try {
- // 获取当前时间范围
- $today = date('Y-m-d');
- $todayStart = strtotime($today . ' 00:00:00');
- $todayEnd = strtotime($today . ' 23:59:59');
-
- $monthStart = strtotime(date('Y-m-01 00:00:00'));
- $monthEnd = strtotime(date('Y-m-t 23:59:59'));
- // 1. 订单统计
- $orderStats = $this->getOrderStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
- // 2. 营业额统计
- $revenueStats = $this->getRevenueStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
- // 3. 退款统计
- $refundStats = $this->getRefundStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
- // 4. 结算统计
- $settlementStats = $this->getSettlementStatistics();
- // 5. 会员统计
- $memberStats = $this->getMemberStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'order' => $orderStats,
- 'revenue' => $revenueStats,
- 'refund' => $refundStats,
- 'settlement' => $settlementStats,
- 'member' => $memberStats
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '获取失败:' . $e->getMessage()
- ];
- }
- }
- /**
- * 订单统计
- */
- private function getOrderStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
- {
- $orderModel = new OrderModel();
- // 总订单数
- $total = $orderModel->where('mark', 1)->count();
- // 月订单数
- $month = $orderModel->where('mark', 1)
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->count();
- // 当日订单数
- $today = $orderModel->where('mark', 1)
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->count();
- return [
- 'total' => $total,
- 'month' => $month,
- 'today' => $today
- ];
- }
- /**
- * 营业额统计(使用账户明细表统计商城消费)
- */
- private function getRevenueStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
- {
- // 使用账户明细表统计商城消费(type=1)和充值缴费(type=2)
- $accountLogModel = new AccountLogModel();
- // 总营业额(商城消费 + 充值缴费)
- $total = $accountLogModel->where('mark', 1)
- ->where('status', 1) // 已完成
- ->whereIn('type', [1, 2]) // 1-商城消费,2-充值缴费
- ->sum('money');
- // 月营业额
- $month = $accountLogModel->where('mark', 1)
- ->where('status', 1)
- ->whereIn('type', [1, 2])
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->sum('money');
- // 当日营业额
- $today = $accountLogModel->where('mark', 1)
- ->where('status', 1)
- ->whereIn('type', [1, 2])
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->sum('money');
- return [
- 'total' => number_format($total, 2, '.', ''),
- 'month' => number_format($month, 2, '.', ''),
- 'today' => number_format($today, 2, '.', '')
- ];
- }
- /**
- * 退款统计(使用账户明细表统计商城退款)
- */
- private function getRefundStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
- {
- // 使用账户明细表统计商城退款(type=3)
- $accountLogModel = new AccountLogModel();
- // 总退款订单数和金额
- $totalRefund = $accountLogModel->where('mark', 1)
- ->where('type', 3) // 3-商城退款
- ->where('status', 1) // 已完成
- ->selectRaw('COUNT(*) as count, SUM(money) as amount')
- ->first();
- // 月退款订单数和金额
- $monthRefund = $accountLogModel->where('mark', 1)
- ->where('type', 3)
- ->where('status', 1)
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->selectRaw('COUNT(*) as count, SUM(money) as amount')
- ->first();
- // 当日退款订单数和金额
- $todayRefund = $accountLogModel->where('mark', 1)
- ->where('type', 3)
- ->where('status', 1)
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->selectRaw('COUNT(*) as count, SUM(money) as amount')
- ->first();
- return [
- 'total_count' => $totalRefund->count ?? 0,
- 'total_amount' => number_format($totalRefund->amount ?? 0, 2, '.', ''),
- 'month_count' => $monthRefund->count ?? 0,
- 'month_amount' => number_format($monthRefund->amount ?? 0, 2, '.', ''),
- 'today_count' => $todayRefund->count ?? 0,
- 'today_amount' => number_format($todayRefund->amount ?? 0, 2, '.', '')
- ];
- }
- /**
- * 结算统计(使用账户明细表统计佣金提现)
- */
- private function getSettlementStatistics()
- {
- // 使用账户明细表统计佣金提现(type=4)
- $accountLogModel = new AccountLogModel();
- // 已结算金额总数(已完成的佣金提现)
- $total = $accountLogModel->where('mark', 1)
- ->where('type', 4) // 4-佣金提现
- ->where('status', 1) // 已完成
- ->sum('money');
- return [
- 'total' => number_format($total, 2, '.', '')
- ];
- }
- /**
- * 会员统计
- */
- private function getMemberStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
- {
- $memberModel = new MemberModel();
- // 总会员数
- $total = $memberModel->where('mark', 1)->count();
- // 月新增会员
- $month = $memberModel->where('mark', 1)
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->count();
- // 当日新增会员
- $today = $memberModel->where('mark', 1)
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->count();
- return [
- 'total' => $total,
- 'month' => $month,
- 'today' => $today
- ];
- }
- /**
- * 获取统计列表(按年月日分组)
- */
- public function getStatisticsList($params)
- {
- $type = $params['type'] ?? 'order';
- $groupBy = $params['group_by'] ?? 'day';
- $startDate = $params['start_date'] ?? date('Y-m-d', strtotime('-30 days'));
- $endDate = $params['end_date'] ?? date('Y-m-d');
- $page = $params['page'] ?? 1;
- $limit = $params['limit'] ?? 20;
- $startTime = strtotime($startDate . ' 00:00:00');
- $endTime = strtotime($endDate . ' 23:59:59');
- try {
- switch ($type) {
- case 'order':
- return $this->getOrderList($groupBy, $startTime, $endTime, $page, $limit);
- case 'revenue':
- return $this->getRevenueList($groupBy, $startTime, $endTime, $page, $limit);
- case 'refund':
- return $this->getRefundList($groupBy, $startTime, $endTime, $page, $limit);
- case 'settlement':
- return $this->getSettlementList($groupBy, $startTime, $endTime, $page, $limit);
- case 'member':
- return $this->getMemberList($groupBy, $startTime, $endTime, $page, $limit);
- default:
- return ['code' => 1, 'msg' => '未知的统计类型'];
- }
- } catch (\Exception $e) {
- return ['code' => 1, 'msg' => '查询失败:' . $e->getMessage()];
- }
- }
- /**
- * 订单统计列表
- */
- private function getOrderList($groupBy, $startTime, $endTime, $page, $limit)
- {
- $dateFormat = $this->getDateFormat($groupBy);
-
- $query = OrderModel::where('mark', 1)
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count")
- ->groupBy('date')
- ->orderBy('date', 'desc');
- $total = $query->get()->count();
- $data = $query->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data,
- 'count' => $total
- ];
- }
- /**
- * 营业额统计列表
- */
- private function getRevenueList($groupBy, $startTime, $endTime, $page, $limit)
- {
- $dateFormat = $this->getDateFormat($groupBy);
-
- $query = AccountLogModel::where('mark', 1)
- ->where('status', 1)
- ->whereIn('type', [1, 2])
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, SUM(money) as amount")
- ->groupBy('date')
- ->orderBy('date', 'desc');
- $allResults = $query->get();
- $total = $allResults->count();
- $list = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
-
- foreach ($list as &$item) {
- $item['amount'] = number_format($item['amount'], 2, '.', '');
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list,
- 'count' => $total
- ];
- }
- /**
- * 退款统计列表
- */
- private function getRefundList($groupBy, $startTime, $endTime, $page, $limit)
- {
- $dateFormat = $this->getDateFormat($groupBy);
-
- $query = AccountLogModel::where('mark', 1)
- ->where('type', 3)
- ->where('status', 1)
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count, SUM(money) as amount")
- ->groupBy('date')
- ->orderBy('date', 'desc');
- $allResults = $query->get();
- $total = $allResults->count();
- $list = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
-
- foreach ($list as &$item) {
- $item['amount'] = number_format($item['amount'], 2, '.', '');
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list,
- 'count' => $total
- ];
- }
- /**
- * 结算统计列表
- */
- private function getSettlementList($groupBy, $startTime, $endTime, $page, $limit)
- {
- $dateFormat = $this->getDateFormat($groupBy);
-
- $query = AccountLogModel::where('mark', 1)
- ->where('type', 4)
- ->where('status', 1)
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, SUM(money) as amount")
- ->groupBy('date')
- ->orderBy('date', 'desc');
- $allResults = $query->get();
- $total = $allResults->count();
- $list = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
-
- foreach ($list as &$item) {
- $item['amount'] = number_format($item['amount'], 2, '.', '');
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list,
- 'count' => $total
- ];
- }
- /**
- * 会员统计列表
- */
- private function getMemberList($groupBy, $startTime, $endTime, $page, $limit)
- {
- $dateFormat = $this->getDateFormat($groupBy);
-
- $query = MemberModel::where('mark', 1)
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count")
- ->groupBy('date')
- ->orderBy('date', 'desc');
- $allResults = $query->get();
- $total = $allResults->count();
- $data = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $data,
- 'count' => $total
- ];
- }
- /**
- * 获取日期格式
- */
- private function getDateFormat($groupBy)
- {
- switch ($groupBy) {
- case 'year':
- return '%Y';
- case 'month':
- return '%Y-%m';
- case 'day':
- default:
- return '%Y-%m-%d';
- }
- }
- }
|