CreateOwnerReport.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\OwnerAreaReport;
  4. use App\OwnerBillReport;
  5. use App\OwnerReport;
  6. use App\Services\common\BatchUpdateService;
  7. use App\Services\LogService;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\DB;
  10. class CreateOwnerReport extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'createOwnerReport';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'create owner report';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * TODO 1号生成或修改上月报表 此处假设每月生成报表数 不足1000,超过此数可能在SQL执行上溢出
  35. *
  36. * @return void
  37. */
  38. public function handle()
  39. {
  40. //转化日期格式 取得上月日期与天数 & 两月前月份
  41. $year = (int)date('Y');
  42. $month = (int)date('m');
  43. if ($month == 1){
  44. $year--;
  45. $lastMonth = '12';
  46. }else $lastMonth = ($month-1) < 10 ? "0".($month-1) : ($month-1);
  47. $historyYear = $year;
  48. if (($month-1) == 1){
  49. $historyYear--;
  50. $historyMonth = '12';
  51. }else $historyMonth = ($month-2) < 10 ? "0".($month-2) : ($month-2);
  52. $lastDay = date("d",strtotime("$year-$lastMonth +1 month -1 day"));
  53. //获取上月面积与报表
  54. $areas = OwnerAreaReport::query()->select("id","owner_id","counting_month",DB::raw("SUM(accounting_area) total,YEAR(counting_month) y,MONTH(counting_month) m"))
  55. ->with(['owner'=>function($query){
  56. $query->select('id',"code");
  57. }])->where("counting_month","like",$year."-".$lastMonth."%")
  58. ->groupBy("y","m","owner_id")->get();
  59. $reports = OwnerReport::query()->where("counting_month","like",$year."-".$lastMonth."%")->orWhere("counting_month","like",$historyYear."-".$historyMonth."%")->get();
  60. $bills = OwnerBillReport::query()->where("counting_month","like",$year."-".$lastMonth."%")->get();
  61. //日均单量统计
  62. $query = DB::raw("select count(*) c,CUSTOMERID from DOC_ORDER_HEADER where EDITTIME >= to_date('".$year."-".$lastMonth."-01 00:00:00','yyyy-mm-dd hh24:mi:ss') and EDITTIME <= to_date('".$year."-".$lastMonth."-".$lastDay." 23:59:59','yyyy-mm-dd hh24:mi:ss') group by CUSTOMERID");
  63. $orderStatistic = DB::connection("oracle")->select($query);
  64. $map = [];
  65. foreach ($orderStatistic as $item){
  66. $map[$item->customerid] = $item->c;
  67. }
  68. //已存在的报表记录
  69. $reportMap = [];
  70. $historyReportMap = [];
  71. foreach ($reports as $report){
  72. if ($report->counting_month == ($historyYear."-".$historyMonth)){
  73. $historyReportMap[$report->owner_id."_".$report->counting_month] = $report->current_month_counting_area;
  74. continue;
  75. }
  76. $reportMap[$report->owner_id."_".$report->counting_month] = $report->id;
  77. }
  78. //组装账单记录
  79. $billMap = [];
  80. foreach ($bills as $index => $bill){
  81. $bill[$bill->owner_id."_".$bill->counting_month] = $index;
  82. }
  83. //组装报表记录数据
  84. $updateReports = [[
  85. "id","daily_average_order_amount","current_month_counting_area","owner_bill_report_id"
  86. ]];
  87. $createReports = [];
  88. foreach ($areas as $area){
  89. $total = $area->owner ? ($map[$area->owner->code] ?? 0) : 0;
  90. if ($reportMap[$area->owner_id."_".$area->counting_month] ?? false){
  91. $updateReports[] = [
  92. "id"=>$reportMap[$area->owner_id."_".$area->counting_month],
  93. "total" =>$total,
  94. "daily_average_order_amount"=>round($total / $lastDay,2),
  95. "current_month_counting_area"=>$area->total ?? 0,
  96. "owner_bill_report_id" => $billMap[$area->owner_id."_".$area->counting_month] ?? 0,
  97. ];
  98. }else $createReports[] = [
  99. "owner_id"=>$area->owner_id,
  100. "counting_month"=>$area->counting_month."-01",
  101. "total" =>$total,
  102. "daily_average_order_amount"=>round($total / $lastDay,2),
  103. "current_month_counting_area"=>$area->total ?? 0,
  104. "owner_bill_report_id" => $billMap[$area->owner_id."_".$area->counting_month] ?? 0,
  105. "last_month_counting_area" => $historyReportMap[$area->owner_id."_".($historyYear."-".$historyMonth)] ?? 0,
  106. ];
  107. }
  108. //执行生成或修改
  109. app(BatchUpdateService::class)->batchUpdate('owner_reports', $updateReports);
  110. app('LogService')->log(__METHOD__,"客户管理-修改原有货主报表",json_encode($updateReports));
  111. DB::table("owner_reports")->insert($createReports);
  112. app('LogService')->log(__METHOD__,"客户管理-生成货主报表",json_encode($createReports));
  113. }
  114. }