CreateOwnerReport.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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()->with(['owner'=>function($query){
  55. $query->select('id',"code");
  56. }])->where("counting_month","like",$year."-".$lastMonth."%")->get();
  57. $reports = OwnerReport::query()->where("counting_month","like",$year."-".$lastMonth."%")->orWhere("counting_month","like",$historyYear."-".$historyMonth."%")->get();
  58. $bills = OwnerBillReport::query()->where("counting_month","like",$year."-".$lastMonth."%")->get();
  59. //日均单量统计
  60. $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");
  61. $orderStatistic = DB::connection("oracle")->select($query);
  62. $map = [];
  63. foreach ($orderStatistic as $item){
  64. $map[$item->customerid] = round($item->c / $lastDay,2);
  65. }
  66. //已存在的报表记录
  67. $reportMap = [];
  68. $historyReportMap = [];
  69. foreach ($reports as $report){
  70. if ($report->counting_month == ($historyYear."-".$historyMonth)){
  71. $historyReportMap[$report->owner_id."_".$report->counting_month] = $report->current_month_counting_area;
  72. continue;
  73. }
  74. $reportMap[$report->owner_id."_".$report->counting_month] = $report->id;
  75. }
  76. //组装账单记录
  77. $billMap = [];
  78. foreach ($bills as $index => $bill){
  79. $bill[$bill->owner_id."_".$bill->counting_month] = $index;
  80. }
  81. //组装报表记录数据
  82. $updateReports = [[
  83. "id","daily_average_order_amount","current_month_counting_area","owner_bill_report_id"
  84. ]];
  85. $createReports = [];
  86. foreach ($areas as $area){
  87. if ($reportMap[$area->owner_id."_".$area->counting_month] ?? false){
  88. $updateReports[] = [
  89. "id"=>$reportMap[$area->owner_id."_".$area->counting_month],
  90. "daily_average_order_amount"=>$area->owner ? ($map[$area->owner->code] ?? 0) : 0,
  91. "current_month_counting_area"=>$area->accounting_area ?? 0,
  92. "owner_bill_report_id" => $billMap[$area->owner_id."_".$area->counting_month] ?? 0,
  93. ];
  94. }else $createReports[] = [
  95. "owner_id"=>$area->owner_id,
  96. "counting_month"=>$area->counting_month."-01",
  97. "daily_average_order_amount"=>$area->owner ? ($map[$area->owner->code] ?? 0) : 0,
  98. "current_month_counting_area"=>$area->accounting_area ?? 0,
  99. "owner_bill_report_id" => $billMap[$area->owner_id."_".$area->counting_month] ?? 0,
  100. "last_month_counting_area" => $historyReportMap[$area->owner_id."_".($historyYear."-".$historyMonth)] ?? 0,
  101. ];
  102. }
  103. //执行生成或修改
  104. app('LogService')->log(__METHOD__,"生成货主报表",json_encode($createReports));
  105. app(BatchUpdateService::class)->batchUpdate('owner_reports', $updateReports);
  106. DB::table("owner_reports")->insert($createReports);
  107. }
  108. }