123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Bike;
- use App\Models\Statistic;
- use App\Models\WalletLog;
- use App\Utils\RedisKeys;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class StatisticsProfitCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'statistics:profit';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '每日收益统计';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- //
- $key = RedisKeys::PROFIT_STATISTICS_KEY;
- app()->redis->incr($key);
- $res = app()->redis->get($key);
- app()->redis->Expire($key, RedisKeys::STATISTICS_KEY_TIME);
- if ($res == 1) {
- $yestoday = Carbon::yesterday();
- $today = Carbon::today();
- $wallets = WalletLog::query()
- ->where('created_at', '>', $yestoday)
- ->where('created_at', '<', $today)
- ->whereIn('type', WalletLog::$subType)
- ->groupBy(['area_id'])
- ->select('area_id', DB::raw('SUM(money) as money_all'))
- ->get()
- ->toArray();
- // print_r($wallets);
- $profit_arr = [];
- $profit_total = 0;
- foreach ($wallets as $k => &$v) {
- $put_bikes = Bike::query()->where('put_status', Bike::PUT_STATUS_YES)->where('put_area_id', $v['area_id'])->count();
- $body = ['profit' => abs($v['money_all']), 'put_bikes' => $put_bikes];
- $v['body'] = json_encode($body);
- $v['name'] = Statistic::$statisticsMaps[Statistic::SLUG_PROFIT_STATIC];
- $v['slug'] = Statistic::SLUG_PROFIT_STATIC;
- $v['date'] = $yestoday;
- // $profit_arr[$v['area_id']] = $v['money_all'];
- $profit_total += $v['money_all'];
- unset($v['money_all']);
- }
- $profit_arr['area_id'] = config('statistic.all'); // 9999 表示总和
- $all_put_bikes = Bike::query()->where('put_status', Bike::PUT_STATUS_YES)->count();
- $all_body = ['profit' => abs($profit_total), 'put_bikes' => $all_put_bikes];
- $profit_arr['body'] = json_encode($all_body);
- $profit_arr['name'] = Statistic::$statisticsMaps[Statistic::SLUG_PROFIT_STATIC];
- $profit_arr['slug'] = Statistic::SLUG_PROFIT_STATIC;
- $profit_arr['date'] = $yestoday;
- array_push($wallets, $profit_arr);
- // print_r($profit_arr);
- $statistic = new Statistic();
- $statistic->insert($wallets);
- } else {
- return;
- }
- Log::info($yestoday . '收益统计已执行');
- }
- }
|