| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- <?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
- {
- /**
- * 获取数据统计
- * @param int $storeId 商户ID,0表示平台管理员
- */
- public function getStatistics($storeId = 0)
- {
- 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, $storeId);
- // 2. 营业额统计(基于订单表的实付金额)
- $revenueStats = $this->getRevenueStatistics($todayStart, $todayEnd, $monthStart, $monthEnd, $storeId);
- // 3. 退款统计(基于订单表的退款状态)
- $refundStats = $this->getRefundStatistics($todayStart, $todayEnd, $monthStart, $monthEnd, $storeId);
- // 4. 结算统计(基于订单表的商家佣金)
- $settlementStats = $this->getSettlementStatistics($storeId);
- // 5. 会员统计(商户端不显示,返回0)
- $memberStats = $storeId > 0 ? [
- 'total' => 0,
- 'month' => 0,
- 'today' => 0
- ] : $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()
- ];
- }
- }
- /**
- * 订单统计(基于订单表)
- * @param int $storeId 商户ID,0表示平台管理员
- */
- private function getOrderStatistics($todayStart, $todayEnd, $monthStart, $monthEnd, $storeId = 0)
- {
- $orderModel = new OrderModel();
- // 总订单数
- $total = $orderModel->where('mark', 1)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->count();
- // 月订单数
- $month = $orderModel->where('mark', 1)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->count();
- // 当日订单数
- $today = $orderModel->where('mark', 1)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->count();
- return [
- 'total' => $total,
- 'month' => $month,
- 'today' => $today
- ];
- }
- /**
- * 营业额统计(基于订单表的实付金额pay_total)
- * @param int $storeId 商户ID,0表示平台管理员
- */
- private function getRevenueStatistics($todayStart, $todayEnd, $monthStart, $monthEnd, $storeId = 0)
- {
- $orderModel = new OrderModel();
- // 总营业额(已完成订单的实付金额)
- $total = $orderModel->where('mark', 1)
- ->where('status', 4) // 4-确认收货(已完成)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->sum('pay_total');
- // 月营业额
- $month = $orderModel->where('mark', 1)
- ->where('status', 4)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->sum('pay_total');
- // 当日营业额
- $today = $orderModel->where('mark', 1)
- ->where('status', 4)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->sum('pay_total');
- return [
- 'total' => number_format($total, 2, '.', ''),
- 'month' => number_format($month, 2, '.', ''),
- 'today' => number_format($today, 2, '.', '')
- ];
- }
- /**
- * 退款统计(基于订单表的退款状态refund_status=1)
- * @param int $storeId 商户ID,0表示平台管理员
- */
- private function getRefundStatistics($todayStart, $todayEnd, $monthStart, $monthEnd, $storeId = 0)
- {
- $orderModel = new OrderModel();
- // 总退款订单数和金额(refund_status=1表示已退款)
- $totalRefund = $orderModel->where('mark', 1)
- ->where('refund_status', 1) // 1-已退款
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->selectRaw('COUNT(*) as count, SUM(pay_total) as amount')
- ->first();
- // 月退款订单数和金额
- $monthRefund = $orderModel->where('mark', 1)
- ->where('refund_status', 1)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$monthStart, $monthEnd])
- ->selectRaw('COUNT(*) as count, SUM(pay_total) as amount')
- ->first();
- // 当日退款订单数和金额
- $todayRefund = $orderModel->where('mark', 1)
- ->where('refund_status', 1)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$todayStart, $todayEnd])
- ->selectRaw('COUNT(*) as count, SUM(pay_total) 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, '.', '')
- ];
- }
- /**
- * 结算统计(基于订单表的商家佣金bonus字段)
- * @param int $storeId 商户ID,0表示平台管理员
- */
- private function getSettlementStatistics($storeId = 0)
- {
- $orderModel = new OrderModel();
- // 已结算金额总数(已完成订单的商家佣金)
- $total = $orderModel->where('mark', 1)
- ->where('status', 4) // 4-确认收货(已完成)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->sum('bonus');
- 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;
- $storeId = $params['store_id'] ?? 0; // 商户ID,0表示平台管理员
- $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, $storeId);
- case 'revenue':
- return $this->getRevenueList($groupBy, $startTime, $endTime, $page, $limit, $storeId);
- case 'refund':
- return $this->getRefundList($groupBy, $startTime, $endTime, $page, $limit, $storeId);
- case 'settlement':
- return $this->getSettlementList($groupBy, $startTime, $endTime, $page, $limit, $storeId);
- case 'member':
- return $this->getMemberList($groupBy, $startTime, $endTime, $page, $limit, $storeId);
- default:
- return ['code' => 1, 'msg' => '未知的统计类型'];
- }
- } catch (\Exception $e) {
- return ['code' => 1, 'msg' => '查询失败:' . $e->getMessage()];
- }
- }
- /**
- * 订单统计列表
- * @param int $storeId 商户ID,0表示平台管理员查看全部数据
- */
- private function getOrderList($groupBy, $startTime, $endTime, $page, $limit, $storeId = 0)
- {
- $dateFormat = $this->getDateFormat($groupBy);
- $query = OrderModel::where('mark', 1)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->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
- ];
- }
- /**
- * 营业额统计列表(基于订单表的实付金额pay_total)
- * @param int $storeId 商户ID,0表示平台管理员查看全部数据
- */
- private function getRevenueList($groupBy, $startTime, $endTime, $page, $limit, $storeId = 0)
- {
- $dateFormat = $this->getDateFormat($groupBy);
- $query = OrderModel::where('mark', 1)
- ->where('status', 4) // 4-确认收货(已完成)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, SUM(pay_total) 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
- ];
- }
- /**
- * 退款统计列表(基于订单表的退款状态refund_status=1)
- * @param int $storeId 商户ID,0表示平台管理员查看全部数据
- */
- private function getRefundList($groupBy, $startTime, $endTime, $page, $limit, $storeId = 0)
- {
- $dateFormat = $this->getDateFormat($groupBy);
- $query = OrderModel::where('mark', 1)
- ->where('refund_status', 1) // 1-已退款
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count, SUM(pay_total) 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
- ];
- }
- /**
- * 结算统计列表(基于订单表的商家佣金bonus字段)
- * @param int $storeId 商户ID,0表示平台管理员查看全部数据
- */
- private function getSettlementList($groupBy, $startTime, $endTime, $page, $limit, $storeId = 0)
- {
- $dateFormat = $this->getDateFormat($groupBy);
- $query = OrderModel::where('mark', 1)
- ->where('status', 4) // 4-确认收货(已完成)
- ->when($storeId > 0, function ($query) use ($storeId) {
- return $query->where('store_id', $storeId);
- })
- ->whereBetween('create_time', [$startTime, $endTime])
- ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, SUM(bonus) 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
- ];
- }
- /**
- * 会员统计列表
- * @param int $storeId 商户ID,0表示平台管理员查看全部数据(会员数据商户端不显示)
- */
- private function getMemberList($groupBy, $startTime, $endTime, $page, $limit, $storeId = 0)
- {
- // 商户端不显示会员数据
- if ($storeId > 0) {
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [],
- 'count' => 0
- ];
- }
- $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';
- }
- }
- }
|