CreateOwnerReport.php 5.1 KB

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