CreateWeightStatistic.php 1.6 KB

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