CreateWeightStatistic.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Order;
  4. use App\OrderBin;
  5. use App\OrderPackageCountingRecord;
  6. use App\Services\BatchService;
  7. use App\Services\common\BatchUpdateService;
  8. use App\Services\DocWaveHeaderService;
  9. use App\Services\LogService;
  10. use App\ValueStore;
  11. use Carbon\Carbon;
  12. use Illuminate\Console\Command;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. class CreateWeightStatistic extends Command
  16. {
  17. protected $signature = 'create:weightStatistic';
  18. protected $description = 'every day create weight statistic data';
  19. public function handle()
  20. {
  21. $yesterday = date("Y-m-d",strtotime("-1 day"));
  22. $sql = <<<sql
  23. SELECT DATE_FORMAT(created_at,'%Y-%m-%d') date,
  24. SUM(CASE WHEN weighed_at IS NULL THEN 1 ELSE 0 END) AS count,
  25. COUNT(1) total FROM order_packages WHERE created_at BETWEEN '{$yesterday} 00:00:00' AND '{$yesterday} 23:59:59' GROUP BY date
  26. sql;
  27. $result = DB::selectOne(DB::raw($sql));
  28. if (!$result)$obj = [
  29. "targeted_at" => $yesterday,
  30. "un_weigh_count" => 0,
  31. "total_count" => 0
  32. ]; else $obj = [
  33. "targeted_at" => $result->date,
  34. "un_weigh_count" => $result->count,
  35. "total_count" => $result->total,
  36. ];
  37. /** @var \stdClass $model */
  38. $model = OrderPackageCountingRecord::query()->create($obj);
  39. Cache::put("weight.".$yesterday,[
  40. "date"=>$yesterday,
  41. "total"=>$model->total_count,
  42. "count"=>$model->un_weigh_count,
  43. "value"=>$model->total_count ? round($model->un_weigh_count/$model->total_count,2)*100 : 0
  44. ]);
  45. }
  46. }