作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

@@ -86,21 +86,19 @@ class MonthAllCount extends Command @@ -86,21 +86,19 @@ class MonthAllCount extends Command
86 $end = date('Y-m-t', strtotime($v['month'])); 86 $end = date('Y-m-t', strtotime($v['month']));
87 $arr['project_id'] = $project_id; 87 $arr['project_id'] = $project_id;
88 $res = $this->inquiry($url,$v['month']); 88 $res = $this->inquiry($url,$v['month']);
89 - $arr['total'] = $arr['month_total'] = 0; 89 + $arr['total'] = $arr['month_total'] = 0;
90 if(isset($res['data']['count'])){ 90 if(isset($res['data']['count'])){
91 echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL; 91 echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
92 - $arr['month_total'] = $res['data']['count'];  
93 - //获取上一个的count  
94 - $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));  
95 - $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);  
96 - if($previousInfo === false){  
97 - $arr['total'] = $arr['month_total'];  
98 - }else{  
99 - $arr['total'] = $res['data']['count'] + ($previousInfo['total'] ?? 0);  
100 - } 92 + $arr['month_total'] = $res['data']['count'] + InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);
  93 + }
  94 + //获取上一个的count
  95 + $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));
  96 + $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
  97 + if($previousInfo === false){
  98 + $arr['total'] = $arr['month_total'];
  99 + }else{
  100 + $arr['total'] = $arr['month_total'] + ($previousInfo['total'] ?? 0);
101 } 101 }
102 - //获取当月的其他询盘  
103 - $arr['total'] += InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);  
104 $country = []; 102 $country = [];
105 if(isset($res['data']['data'])){ 103 if(isset($res['data']['data'])){
106 $country = $res['data']['data']; 104 $country = $res['data']['data'];
@@ -112,18 +112,16 @@ class MonthCount extends Command @@ -112,18 +112,16 @@ class MonthCount extends Command
112 $arr['total'] = $arr['month_total'] = 0; 112 $arr['total'] = $arr['month_total'] = 0;
113 if(isset($res['data']['count'])){ 113 if(isset($res['data']['count'])){
114 echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL; 114 echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
115 - $arr['month_total'] = $res['data']['count'];  
116 - //获取上一个的count  
117 - $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));  
118 - $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);  
119 - if($previousInfo === false){  
120 - $arr['total'] = $arr['month_total'];  
121 - }else{  
122 - $arr['total'] = $res['data']['count'] + ($previousInfo['total'] ?? 0);  
123 - } 115 + $arr['month_total'] = $res['data']['count'] + InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);
  116 + }
  117 + //获取上一个的count
  118 + $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));
  119 + $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
  120 + if($previousInfo === false){
  121 + $arr['total'] = $arr['month_total'];
  122 + }else{
  123 + $arr['total'] = $arr['month_total'] + ($previousInfo['total'] ?? 0);
124 } 124 }
125 - //获取当月的其他询盘  
126 - $arr['total'] += InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);  
127 $country = []; 125 $country = [];
128 if(isset($res['data']['data'])){ 126 if(isset($res['data']['data'])){
129 $country = $res['data']['data']; 127 $country = $res['data']['data'];
@@ -145,7 +143,6 @@ class MonthCount extends Command @@ -145,7 +143,6 @@ class MonthCount extends Command
145 $firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month")); 143 $firstDayOfNextMonth = date('Y-m-01 01:00:00', strtotime("$selectedDate +1 month"));
146 $arr['created_at'] = $firstDayOfNextMonth; 144 $arr['created_at'] = $firstDayOfNextMonth;
147 $arr['updated_at'] = $firstDayOfNextMonth; 145 $arr['updated_at'] = $firstDayOfNextMonth;
148 -// echo date('Y-m-d H:i:s') . '数据:'.json_encode($arr) . PHP_EOL;  
149 $monthCountModel->insert($arr); 146 $monthCountModel->insert($arr);
150 }else{ 147 }else{
151 $monthCountModel->edit($arr,['id'=>$info['id']]); 148 $monthCountModel->edit($arr,['id'=>$info['id']]);
@@ -18,6 +18,7 @@ use App\Models\Project\Project; @@ -18,6 +18,7 @@ use App\Models\Project\Project;
18 use App\Models\Visit\Visit; 18 use App\Models\Visit\Visit;
19 use App\Services\ProjectServer; 19 use App\Services\ProjectServer;
20 use Illuminate\Console\Command; 20 use Illuminate\Console\Command;
  21 +use Illuminate\Support\Carbon;
21 use Illuminate\Support\Facades\DB; 22 use Illuminate\Support\Facades\DB;
22 use App\Models\HomeCount\Count; 23 use App\Models\HomeCount\Count;
23 24
@@ -61,35 +62,40 @@ class MonthProjectCount extends Command @@ -61,35 +62,40 @@ class MonthProjectCount extends Command
61 * @time :2024/1/8 9:05 62 * @time :2024/1/8 9:05
62 */ 63 */
63 public function count($project_id,$url){ 64 public function count($project_id,$url){
  65 + $data = [];
64 $list = DB::connection('custom_mysql')->table('gl_customer_visit') 66 $list = DB::connection('custom_mysql')->table('gl_customer_visit')
65 ->select(DB::raw('DATE_FORMAT(updated_date, "%Y-%m") as month')) 67 ->select(DB::raw('DATE_FORMAT(updated_date, "%Y-%m") as month'))
  68 + ->orderBy('month', 'asc')
66 ->groupBy('month')->get()->toArray(); 69 ->groupBy('month')->get()->toArray();
67 - foreach ($list as $k=>$v){ 70 + foreach ($list as $v){
  71 + $data[] = $v->month;
  72 + }
  73 + $list = $this->fillMissingMonths($data);
  74 + foreach ($list as $v){
68 $arr = []; 75 $arr = [];
69 - $v = (array)$v;  
70 $monthCountModel = new MonthCount(); 76 $monthCountModel = new MonthCount();
71 - $info = $monthCountModel->read(['month'=>$v['month'],'project_id'=>$project_id]); 77 + $info = $monthCountModel->read(['month'=>$v,'project_id'=>$project_id]);
72 // 获取当月开始时间 78 // 获取当月开始时间
73 - $start = date('Y-m-01', strtotime($v['month'])); 79 + $start = date('Y-m-01', strtotime($v));
74 // 获取当月结束时间 80 // 获取当月结束时间
75 - $end = date('Y-m-t', strtotime($v['month'])); 81 + $end = date('Y-m-t', strtotime($v));
76 $arr['project_id'] = $project_id; 82 $arr['project_id'] = $project_id;
77 - $res = $this->inquiry($url,$v['month']); 83 + $res = $this->inquiry($url,$v);
  84 + echo date('Y-m-d H:i:s') . '月份:'.$v. PHP_EOL;
78 $arr['total'] = $arr['month_total'] = 0; 85 $arr['total'] = $arr['month_total'] = 0;
79 if(isset($res['data']['count'])){ 86 if(isset($res['data']['count'])){
80 echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL; 87 echo date('Y-m-d H:i:s') . '数据:'.$res['data']['count'] . PHP_EOL;
81 - $arr['month_total'] = $res['data']['count'];  
82 - //获取上一个的count  
83 - $previousMonth = date('Y-m', strtotime($v['month'] . ' -1 month'));  
84 - $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);  
85 - if($previousInfo === false){  
86 - $arr['total'] = $arr['month_total'];  
87 - }else{  
88 - $arr['total'] = $res['data']['count'] + ($previousInfo['total'] ?? 0);  
89 - } 88 + $arr['month_total'] = $res['data']['count'] + InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']);
90 } 89 }
91 - //获取当月的其他询盘  
92 - $arr['total'] += InquiryFormData::getCount([$start.' 00:00:00',$end.' 00:00:00']); 90 + //获取上一个的count
  91 + $previousMonth = date('Y-m', strtotime($v . ' -1 month'));
  92 + $previousInfo = $monthCountModel->read(['month'=>$previousMonth,'project_id'=>$project_id]);
  93 + if($previousInfo === false){
  94 + $arr['total'] = $arr['month_total'];
  95 + }else{
  96 + $arr['total'] = $arr['month_total'] + ($previousInfo['total'] ?? 0);
  97 + }
  98 + echo date('Y-m-d H:i:s') . '加上其他询盘:'.$arr['total'] . PHP_EOL;
93 $country = []; 99 $country = [];
94 if(isset($res['data']['data'])){ 100 if(isset($res['data']['data'])){
95 $country = $res['data']['data']; 101 $country = $res['data']['data'];
@@ -103,7 +109,7 @@ class MonthProjectCount extends Command @@ -103,7 +109,7 @@ class MonthProjectCount extends Command
103 } 109 }
104 } 110 }
105 $arr['country'] = json_encode($country); 111 $arr['country'] = json_encode($country);
106 - $arr['month'] = $v['month']; 112 + $arr['month'] = $v;
107 $arr = $this->pv_ip($arr,$start,$end,$project_id); 113 $arr = $this->pv_ip($arr,$start,$end,$project_id);
108 $arr = $this->sourceCount($arr,$start,$end); 114 $arr = $this->sourceCount($arr,$start,$end);
109 if($info === false){ 115 if($info === false){
@@ -191,4 +197,37 @@ class MonthProjectCount extends Command @@ -191,4 +197,37 @@ class MonthProjectCount extends Command
191 return $res; 197 return $res;
192 } 198 }
193 199
  200 + /**
  201 + * @remark :补齐月份
  202 + * @name :fillMissingMonths
  203 + * @author :lyh
  204 + * @method :post
  205 + * @time :2024/8/14 11:11
  206 + */
  207 + public function fillMissingMonths($dates) {
  208 + // 将字符串日期转换为 Carbon 对象
  209 + $carbonDates = array_map(function($date) {
  210 + return Carbon::createFromFormat('Y-m', $date);
  211 + }, $dates);
  212 + // 排序日期,确保列表按时间顺序排列
  213 + usort($carbonDates, function($a, $b) {
  214 + return $a->gt($b);
  215 + });
  216 + // 用于存储完整日期的数组
  217 + $completeDates = [];
  218 + // 遍历日期列表,补齐中间缺失的月份
  219 + for ($i = 0; $i < count($carbonDates) - 1; $i++) {
  220 + $current = $carbonDates[$i];
  221 + $next = $carbonDates[$i + 1];
  222 + // 将当前月份加入完整日期数组
  223 + array_push($completeDates, $current->format('Y-m'));
  224 + // 循环补齐中间缺失的月份
  225 + while ($current->addMonth()->lt($next)) {
  226 + array_push($completeDates, $current->format('Y-m'));
  227 + }
  228 + }
  229 + // 加入最后一个月份
  230 + array_push($completeDates, $carbonDates[count($carbonDates) - 1]->format('Y-m'));
  231 + return $completeDates;
  232 + }
194 } 233 }
@@ -64,10 +64,13 @@ class MonthReportController extends BaseController @@ -64,10 +64,13 @@ class MonthReportController extends BaseController
64 if(!empty($info['country'])){ 64 if(!empty($info['country'])){
65 $info['country'] = json_decode($info['country']); 65 $info['country'] = json_decode($info['country']);
66 } 66 }
67 - // 获取上个月的开始时间  
68 - $startTime = Carbon::now()->subMonth()->startOfMonth()->toDateString();  
69 - // 获取上个月的结束时间  
70 - $endTime = Carbon::now()->subMonth()->endOfMonth()->toDateString(); 67 + // 创建指定年月的 DateTime 对象
  68 + $date = $this->map['month'];
  69 + $start = new \DateTime("$date-01 00:00:00");
  70 + // 克隆 $start 对象并设置到下个月的第一天,减去1秒得到该月的最后一秒
  71 + $end = (clone $start)->modify('last day of this month')->setTime(23, 59, 59);
  72 + $startTime = $start->format('Y-m-d H:i:s');
  73 + $endTime = $end->format('Y-m-d H:i:s');
71 $param = [ 74 $param = [
72 'date' => ['between',[$startTime,$endTime]], 75 'date' => ['between',[$startTime,$endTime]],
73 'project_id' => $this->user['project_id'] 76 'project_id' => $this->user['project_id']