StatisticService.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2023/11/23 20:57
  7. */
  8. namespace App\Http\Services\Admin\Finance;
  9. use App\Http\Services\BaseService;
  10. use App\Models\Distributor;
  11. use App\Models\Menu;
  12. use App\Models\StatisticIncome;
  13. use Illuminate\Support\Facades\DB;
  14. class StatisticService extends BaseService
  15. {
  16. public function income($data)
  17. {
  18. $model = $this->queryCondition(StatisticIncome::query(), $data);
  19. if (!empty($data['province'])) {
  20. $model->where('province', $data['province']);
  21. }
  22. if (!empty($data['city'])) {
  23. $model->where('city', $data['city']);
  24. }
  25. $amountModel = clone $model;
  26. $model->select(['province', 'city', 'district']);
  27. $selectColumn = "SUM(order_count) as order_count,SUM(pay_price) as pay_price,SUM(artificer_income) as artificer_income,SUM(promotion_income) as promotion_income,SUM(total_income) as total_income,SUM(platform_income) as platform_income,SUM(agent_income) as agent_income,SUM(market_income) as market_income";
  28. $amountModel->selectRaw($selectColumn);
  29. if (!empty($data['type']) && $data['type'] !== '0') {
  30. // $model->selectRaw("SUM(order_count) as order_count,SUM(pay_price) as pay_price,SUM(artificer_income) as artificer_income,SUM(promotion_income) as promotion_income,SUM(total_income) as total_income,SUM(platform_income) as platform_income,SUM(agent_income) as agent_income,SUM(market_income) as market_income");
  31. $model->selectRaw($selectColumn);
  32. // 按月查询
  33. if ($data['type'] === '1') {
  34. $model->selectRaw("DATE_FORMAT(`time`, '%Y-%m') as month")
  35. ->groupBy(['province', 'city', 'district', 'month'])
  36. // ->latest('month');
  37. ->orderByDesc('month');
  38. if (!empty($data['month'])) {
  39. $where = [
  40. ['time', 'like', "%${data['month']}%"]
  41. ];
  42. // $model->where('time', 'like', "%${data['month']}%");
  43. $model->where($where);
  44. $amountModel->where($where);
  45. }
  46. }
  47. // 按年查询
  48. if ($data['type'] === '2') {
  49. $model->selectRaw("DATE_FORMAT(`time`, '%Y') as year")
  50. ->groupBy(['province', 'city', 'district', 'year'])
  51. // ->latest('year');
  52. ->orderByDesc('year');
  53. if (!empty($data['year'])) {
  54. $where = [
  55. ['time', 'like', "%${data['year']}%"]
  56. ];
  57. $model->where($where);
  58. $amountModel->where($where);
  59. // $model->where('time', 'like', "%${data['year']}%");
  60. }
  61. }
  62. } else {
  63. $model->selectRaw('time as date,order_count,pay_price,artificer_income,promotion_income,total_income,platform_income,agent_income,market_income')
  64. // ->latest('id');
  65. ->orderByDesc('date');
  66. if (!empty($data['date'])) {
  67. $where = [
  68. ['time', 'like', "%${data['date']}%"]
  69. ];
  70. $model->where($where);
  71. $amountModel->where($where);
  72. // $model->where('time', 'like', "%${data['date']}%");
  73. }
  74. }
  75. $amountData = $amountModel->first()->toArray();
  76. $list = $model->orderBy('province')
  77. ->orderBy('city')
  78. ->orderBy('district')
  79. ->paginate($data['pageSize'])
  80. ->toArray();
  81. return $this->apiSuccess('', [
  82. 'list' => $list['data'],
  83. 'total' => $list['total'],
  84. 'amountData' => $amountData
  85. ]);
  86. }
  87. }