FinancialService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. namespace App\Services\Common;
  3. use App\Models\OrderModel;
  4. use App\Models\MemberModel;
  5. use App\Models\WithdrawModel;
  6. use App\Models\AccountLogModel;
  7. use App\Services\BaseService;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 财务服务
  11. */
  12. class FinancialService extends BaseService
  13. {
  14. /**
  15. * 获取数据统计
  16. */
  17. public function getStatistics()
  18. {
  19. try {
  20. // 获取当前时间范围
  21. $today = date('Y-m-d');
  22. $todayStart = strtotime($today . ' 00:00:00');
  23. $todayEnd = strtotime($today . ' 23:59:59');
  24. $monthStart = strtotime(date('Y-m-01 00:00:00'));
  25. $monthEnd = strtotime(date('Y-m-t 23:59:59'));
  26. // 1. 订单统计
  27. $orderStats = $this->getOrderStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
  28. // 2. 营业额统计
  29. $revenueStats = $this->getRevenueStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
  30. // 3. 退款统计
  31. $refundStats = $this->getRefundStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
  32. // 4. 结算统计
  33. $settlementStats = $this->getSettlementStatistics();
  34. // 5. 会员统计
  35. $memberStats = $this->getMemberStatistics($todayStart, $todayEnd, $monthStart, $monthEnd);
  36. return [
  37. 'code' => 0,
  38. 'msg' => '获取成功',
  39. 'data' => [
  40. 'order' => $orderStats,
  41. 'revenue' => $revenueStats,
  42. 'refund' => $refundStats,
  43. 'settlement' => $settlementStats,
  44. 'member' => $memberStats
  45. ]
  46. ];
  47. } catch (\Exception $e) {
  48. return [
  49. 'code' => 1,
  50. 'msg' => '获取失败:' . $e->getMessage()
  51. ];
  52. }
  53. }
  54. /**
  55. * 订单统计
  56. */
  57. private function getOrderStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
  58. {
  59. $orderModel = new OrderModel();
  60. // 总订单数
  61. $total = $orderModel->where('mark', 1)->count();
  62. // 月订单数
  63. $month = $orderModel->where('mark', 1)
  64. ->whereBetween('create_time', [$monthStart, $monthEnd])
  65. ->count();
  66. // 当日订单数
  67. $today = $orderModel->where('mark', 1)
  68. ->whereBetween('create_time', [$todayStart, $todayEnd])
  69. ->count();
  70. return [
  71. 'total' => $total,
  72. 'month' => $month,
  73. 'today' => $today
  74. ];
  75. }
  76. /**
  77. * 营业额统计(使用账户明细表统计商城消费)
  78. */
  79. private function getRevenueStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
  80. {
  81. // 使用账户明细表统计商城消费(type=1)和充值缴费(type=2)
  82. $accountLogModel = new AccountLogModel();
  83. // 总营业额(商城消费 + 充值缴费)
  84. $total = $accountLogModel->where('mark', 1)
  85. ->where('status', 1) // 已完成
  86. ->whereIn('type', [1, 2]) // 1-商城消费,2-充值缴费
  87. ->sum('money');
  88. // 月营业额
  89. $month = $accountLogModel->where('mark', 1)
  90. ->where('status', 1)
  91. ->whereIn('type', [1, 2])
  92. ->whereBetween('create_time', [$monthStart, $monthEnd])
  93. ->sum('money');
  94. // 当日营业额
  95. $today = $accountLogModel->where('mark', 1)
  96. ->where('status', 1)
  97. ->whereIn('type', [1, 2])
  98. ->whereBetween('create_time', [$todayStart, $todayEnd])
  99. ->sum('money');
  100. return [
  101. 'total' => number_format($total, 2, '.', ''),
  102. 'month' => number_format($month, 2, '.', ''),
  103. 'today' => number_format($today, 2, '.', '')
  104. ];
  105. }
  106. /**
  107. * 退款统计(使用账户明细表统计商城退款)
  108. */
  109. private function getRefundStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
  110. {
  111. // 使用账户明细表统计商城退款(type=3)
  112. $accountLogModel = new AccountLogModel();
  113. // 总退款订单数和金额
  114. $totalRefund = $accountLogModel->where('mark', 1)
  115. ->where('type', 3) // 3-商城退款
  116. ->where('status', 1) // 已完成
  117. ->selectRaw('COUNT(*) as count, SUM(money) as amount')
  118. ->first();
  119. // 月退款订单数和金额
  120. $monthRefund = $accountLogModel->where('mark', 1)
  121. ->where('type', 3)
  122. ->where('status', 1)
  123. ->whereBetween('create_time', [$monthStart, $monthEnd])
  124. ->selectRaw('COUNT(*) as count, SUM(money) as amount')
  125. ->first();
  126. // 当日退款订单数和金额
  127. $todayRefund = $accountLogModel->where('mark', 1)
  128. ->where('type', 3)
  129. ->where('status', 1)
  130. ->whereBetween('create_time', [$todayStart, $todayEnd])
  131. ->selectRaw('COUNT(*) as count, SUM(money) as amount')
  132. ->first();
  133. return [
  134. 'total_count' => $totalRefund->count ?? 0,
  135. 'total_amount' => number_format($totalRefund->amount ?? 0, 2, '.', ''),
  136. 'month_count' => $monthRefund->count ?? 0,
  137. 'month_amount' => number_format($monthRefund->amount ?? 0, 2, '.', ''),
  138. 'today_count' => $todayRefund->count ?? 0,
  139. 'today_amount' => number_format($todayRefund->amount ?? 0, 2, '.', '')
  140. ];
  141. }
  142. /**
  143. * 结算统计(使用账户明细表统计佣金提现)
  144. */
  145. private function getSettlementStatistics()
  146. {
  147. // 使用账户明细表统计佣金提现(type=4)
  148. $accountLogModel = new AccountLogModel();
  149. // 已结算金额总数(已完成的佣金提现)
  150. $total = $accountLogModel->where('mark', 1)
  151. ->where('type', 4) // 4-佣金提现
  152. ->where('status', 1) // 已完成
  153. ->sum('money');
  154. return [
  155. 'total' => number_format($total, 2, '.', '')
  156. ];
  157. }
  158. /**
  159. * 会员统计
  160. */
  161. private function getMemberStatistics($todayStart, $todayEnd, $monthStart, $monthEnd)
  162. {
  163. $memberModel = new MemberModel();
  164. // 总会员数
  165. $total = $memberModel->where('mark', 1)->count();
  166. // 月新增会员
  167. $month = $memberModel->where('mark', 1)
  168. ->whereBetween('create_time', [$monthStart, $monthEnd])
  169. ->count();
  170. // 当日新增会员
  171. $today = $memberModel->where('mark', 1)
  172. ->whereBetween('create_time', [$todayStart, $todayEnd])
  173. ->count();
  174. return [
  175. 'total' => $total,
  176. 'month' => $month,
  177. 'today' => $today
  178. ];
  179. }
  180. /**
  181. * 获取统计列表(按年月日分组)
  182. */
  183. public function getStatisticsList($params)
  184. {
  185. $type = $params['type'] ?? 'order';
  186. $groupBy = $params['group_by'] ?? 'day';
  187. $startDate = $params['start_date'] ?? date('Y-m-d', strtotime('-30 days'));
  188. $endDate = $params['end_date'] ?? date('Y-m-d');
  189. $page = $params['page'] ?? 1;
  190. $limit = $params['limit'] ?? 20;
  191. $startTime = strtotime($startDate . ' 00:00:00');
  192. $endTime = strtotime($endDate . ' 23:59:59');
  193. try {
  194. switch ($type) {
  195. case 'order':
  196. return $this->getOrderList($groupBy, $startTime, $endTime, $page, $limit);
  197. case 'revenue':
  198. return $this->getRevenueList($groupBy, $startTime, $endTime, $page, $limit);
  199. case 'refund':
  200. return $this->getRefundList($groupBy, $startTime, $endTime, $page, $limit);
  201. case 'settlement':
  202. return $this->getSettlementList($groupBy, $startTime, $endTime, $page, $limit);
  203. case 'member':
  204. return $this->getMemberList($groupBy, $startTime, $endTime, $page, $limit);
  205. default:
  206. return ['code' => 1, 'msg' => '未知的统计类型'];
  207. }
  208. } catch (\Exception $e) {
  209. return ['code' => 1, 'msg' => '查询失败:' . $e->getMessage()];
  210. }
  211. }
  212. /**
  213. * 订单统计列表
  214. */
  215. private function getOrderList($groupBy, $startTime, $endTime, $page, $limit)
  216. {
  217. $dateFormat = $this->getDateFormat($groupBy);
  218. $query = OrderModel::where('mark', 1)
  219. ->whereBetween('create_time', [$startTime, $endTime])
  220. ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count")
  221. ->groupBy('date')
  222. ->orderBy('date', 'desc');
  223. $total = $query->get()->count();
  224. $data = $query->offset(($page - 1) * $limit)->limit($limit)->get()->toArray();
  225. return [
  226. 'code' => 0,
  227. 'msg' => '获取成功',
  228. 'data' => $data,
  229. 'count' => $total
  230. ];
  231. }
  232. /**
  233. * 营业额统计列表
  234. */
  235. private function getRevenueList($groupBy, $startTime, $endTime, $page, $limit)
  236. {
  237. $dateFormat = $this->getDateFormat($groupBy);
  238. $query = AccountLogModel::where('mark', 1)
  239. ->where('status', 1)
  240. ->whereIn('type', [1, 2])
  241. ->whereBetween('create_time', [$startTime, $endTime])
  242. ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, SUM(money) as amount")
  243. ->groupBy('date')
  244. ->orderBy('date', 'desc');
  245. $allResults = $query->get();
  246. $total = $allResults->count();
  247. $list = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
  248. foreach ($list as &$item) {
  249. $item['amount'] = number_format($item['amount'], 2, '.', '');
  250. }
  251. return [
  252. 'code' => 0,
  253. 'msg' => '获取成功',
  254. 'data' => $list,
  255. 'count' => $total
  256. ];
  257. }
  258. /**
  259. * 退款统计列表
  260. */
  261. private function getRefundList($groupBy, $startTime, $endTime, $page, $limit)
  262. {
  263. $dateFormat = $this->getDateFormat($groupBy);
  264. $query = AccountLogModel::where('mark', 1)
  265. ->where('type', 3)
  266. ->where('status', 1)
  267. ->whereBetween('create_time', [$startTime, $endTime])
  268. ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count, SUM(money) as amount")
  269. ->groupBy('date')
  270. ->orderBy('date', 'desc');
  271. $allResults = $query->get();
  272. $total = $allResults->count();
  273. $list = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
  274. foreach ($list as &$item) {
  275. $item['amount'] = number_format($item['amount'], 2, '.', '');
  276. }
  277. return [
  278. 'code' => 0,
  279. 'msg' => '获取成功',
  280. 'data' => $list,
  281. 'count' => $total
  282. ];
  283. }
  284. /**
  285. * 结算统计列表
  286. */
  287. private function getSettlementList($groupBy, $startTime, $endTime, $page, $limit)
  288. {
  289. $dateFormat = $this->getDateFormat($groupBy);
  290. $query = AccountLogModel::where('mark', 1)
  291. ->where('type', 4)
  292. ->where('status', 1)
  293. ->whereBetween('create_time', [$startTime, $endTime])
  294. ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, SUM(money) as amount")
  295. ->groupBy('date')
  296. ->orderBy('date', 'desc');
  297. $allResults = $query->get();
  298. $total = $allResults->count();
  299. $list = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
  300. foreach ($list as &$item) {
  301. $item['amount'] = number_format($item['amount'], 2, '.', '');
  302. }
  303. return [
  304. 'code' => 0,
  305. 'msg' => '获取成功',
  306. 'data' => $list,
  307. 'count' => $total
  308. ];
  309. }
  310. /**
  311. * 会员统计列表
  312. */
  313. private function getMemberList($groupBy, $startTime, $endTime, $page, $limit)
  314. {
  315. $dateFormat = $this->getDateFormat($groupBy);
  316. $query = MemberModel::where('mark', 1)
  317. ->whereBetween('create_time', [$startTime, $endTime])
  318. ->selectRaw("FROM_UNIXTIME(create_time, '{$dateFormat}') as date, COUNT(*) as count")
  319. ->groupBy('date')
  320. ->orderBy('date', 'desc');
  321. $allResults = $query->get();
  322. $total = $allResults->count();
  323. $data = $allResults->slice(($page - 1) * $limit, $limit)->values()->toArray();
  324. return [
  325. 'code' => 0,
  326. 'msg' => '获取成功',
  327. 'data' => $data,
  328. 'count' => $total
  329. ];
  330. }
  331. /**
  332. * 获取日期格式
  333. */
  334. private function getDateFormat($groupBy)
  335. {
  336. switch ($groupBy) {
  337. case 'year':
  338. return '%Y';
  339. case 'month':
  340. return '%Y-%m';
  341. case 'day':
  342. default:
  343. return '%Y-%m-%d';
  344. }
  345. }
  346. }