123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace App\Console\Commands;
- use App\Models\DepositOrder;
- use App\Models\DepositRefund;
- use App\Models\Statistic;
- use App\Utils\RedisKeys;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class TimeStatisticsDepositOrderCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'time_statistics:deposit_orders
- {time=2020-03-16 00:00:00}';
- /**
- * 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()
- {
- $time = $this->argument('time');
- $today = Carbon::make($time);
- $i = 1;
- for ($i; Carbon::today()->diffInDays($today) > 0; $i++) {
- $today = $today->addDay();
- $this->aa($today);
- }
- }
- // 测试
- public function bb(Carbon $yestoday)
- {
- $today = Carbon::make($yestoday)->addDay();
- Log::info($yestoday . '---' . $today . '---');
- }
- private function aa($yestoday)
- {
- //
- $today = Carbon::make($yestoday)->addDay();
- //押金充值记录
- $deposit_orders = DepositOrder::query()
- ->where('pay_status', DepositOrder::PAY_STATUS_OK)
- ->where('pay_time', '>', $yestoday)
- ->where('pay_time', '<', $today)
- ->groupBy(['area_id'])
- ->select('area_id', DB::raw('SUM(money) as money_all ,COUNT(id) as count_id'))
- ->get()
- ->toArray();
- $deposit_orders_arr = [];
- if (!empty($deposit_orders)) {
- foreach ($deposit_orders as $v1) {
- // 为了防止合并时 重新索引 将key重新定义
- $deposit_orders_arr['area_' . $v1['area_id']] = $v1;
- }
- }
- //押金退还记录
- $deposit_refunds = DepositRefund::query()
- ->where('pay_status', DepositRefund::PAY_STATUS_OK)
- ->where('pay_time', '>', $yestoday)
- ->where('pay_time', '<', $today)
- ->groupBy(['area_id'])
- ->select('area_id', DB::raw('SUM(money) as money_refund_all'))
- ->get()
- ->toArray();
- // print_r($deposit_refunds);
- $deposit_refund_arr = [];
- if (!empty($deposit_refunds)) {
- foreach ($deposit_refunds as $v2) {
- // 为了防止合并时 重新索引 将key重新定义
- $deposit_refund_arr['area_' . $v2['area_id']] = $v2;
- }
- }
- //合并
- $arr = array_merge_recursive($deposit_orders_arr, $deposit_refund_arr);
- $insert = [];
- $total_money_all = 0;
- $total_count_id = 0;
- $total_money_refund_all = 0;
- foreach ($arr as $ar) {
- $deposit_statistics = [];
- // 合并之后 会存在相同的area_id 组成数组 所以进行判断
- if (is_array($ar['area_id'])) {
- $key = $ar['area_id'][0];
- // $ar['total_deposit'] = $this->total_deposit($key);
- unset($ar['area_id']);
- $deposit_statistics['body'] = json_encode($ar);
- $deposit_statistics['area_id'] = $key;
- $deposit_statistics['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC];
- $deposit_statistics['slug'] = Statistic::SLUG_DEPOSIT_STATIC;
- $deposit_statistics['date'] = $yestoday;
- $insert[] = $deposit_statistics;
- } else {
- $key = $ar['area_id'];
- // $ar['total_deposit'] = $this->total_deposit($key);
- unset($ar['area_id']);
- $deposit_statistics['body'] = json_encode($ar);
- $deposit_statistics['area_id'] = $key;
- $deposit_statistics['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC];
- $deposit_statistics['slug'] = Statistic::SLUG_DEPOSIT_STATIC;
- $deposit_statistics['date'] = $yestoday;
- $insert[] = $deposit_statistics;
- }
- $total_money_all += $ar['money_all'] ?? 0;
- $total_count_id += $ar['count_id'] ?? 0;
- $total_money_refund_all += $ar['money_refund_all'] ?? 0;
- }
- //计算系统总押金
- $all['money_all'] = $total_money_all ?? 0;
- $all['count_id'] = $total_count_id ?? 0;
- $all['money_refund_all'] = $total_money_refund_all ?? 0;
- // $deposit_statistics['all']['total_deposit'] = $this->total_deposit("all");
- $insertAll['body'] = json_encode($all);
- $insertAll['area_id'] = config('statistic.all');
- $insertAll['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC];
- $insertAll['slug'] = Statistic::SLUG_DEPOSIT_STATIC;
- $insertAll['date'] = $yestoday;
- array_push($insert, $insertAll);
- // print_r($deposit_statistics);
- //存储
- $statistic = new Statistic();
- $statistic->insert($insert);
- Log::info($yestoday . '---' . $today . '押金统计已执行');
- }
- /**
- *
- * 统计每日总押金(数据大 会死掉)
- * */
- // private function total_deposit($area_id){
- // $total_deposit = DepositOrder::query()
- // ->where('pay_status',DepositRefund::PAY_STATUS_OK)
- // ->where('is_refund',DepositOrder::REFUND_NO);
- // if($area_id=='all'){
- // $total_deposit = $total_deposit
- // ->sum('money');
- // }else{
- // $total_deposit = $total_deposit
- // ->where('area_id',$area_id)
- // ->sum('money');
- // }
- // return $total_deposit;
- // }
- }
|