MonthTotal.php
3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace App\Console\Commands;
use App\Modules\Models\GuideRecord\GuideRecord;
use App\Modules\Models\Production\Production;
use App\Modules\Models\Settlement\Finance;
//use App\Modules\Models\Settlement\Settlement;
use App\Modules\Models\Spot\Spot;
use Illuminate\Console\Command;
class MonthTotal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'MonthTotal {--day=}';
/**
* 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->option('day');
// if( empty($time) ){
$time = date('Y-m');
// }
$deal_time = date("Ym", strtotime("$time -1 month"));
$start_time = date("Y-m-01 00:00:00", strtotime("$time -1 month"));
$end_time = date("Y-m-01 00:00:00", strtotime("$time"));
$orders = Production::select('rent.spot_id','rent.business_id','rent.real_total','production.return_time')
->leftJoin('rent', 'production.rent_id', '=', 'rent.id')
->where('production.return_time', '>=', $start_time)
->where('production.return_time','<=',$end_time)
->where('production.is_return',1)
->get()
->toArray();
$res=GuideRecord::select('guide_record.spot_id','guide_record.business_id','guide_record.real_total')
->where('guide_record.pay_time', '>=', $start_time)
->where('guide_record.pay_time','<=',$end_time)
->where('guide_record.is_pay',1)
->get()
->toArray();
$income_data = [];
$date = [ ];
if (!empty($orders)) {
for ($i = 0; $i < count($orders); $i++) {
if (!isset($income_data[$orders[$i]['spot_id']]['real_total'])) {
$income_data[$orders[$i]['spot_id']]['real_total'] = 0;
}
$income_data[$orders[$i]['spot_id']]['real_total'] += $orders[$i]['real_total'];
}
}
if(!empty($res)){
for($i=0;$i<count($res);$i++){
if(!isset($date[$res[$i]['spot_id']]['real_total'])){
$date[$res[$i]['spot_id']]['real_total'] = 0;
}
$date[$res[$i]['spot_id']]['real_total'] += $res[$i]['real_total'];
}
}
$insert_data = [];
$spots = Spot::select('*')->get()->toArray();
foreach ($spots as $k => $v) {
$insert_data[$k]['business_id'] = (int)$v['business_id'];
$insert_data[$k]['spot_id'] = (int)$v['id'];
$insert_data[$k]['month'] = (int)$deal_time;
$insert_data[$k]['total'] = empty($income_data[$v['id']]['real_total']) ? 0 : $income_data[$v['id']]['real_total'];
$insert_data[$k]['guide_total'] = empty($date[$v['id']]['real_total']) ? 0 : $date[$v['id']]['real_total'];
$insert_data[$k]['created_at'] =date("Y-m-d H:i:s");
$insert_data[$k]['updated_at'] =date("Y-m-d H:i:s");
}
$res = Finance::insert($insert_data);
}
}