cqp 11 ماه پیش
والد
کامیت
231bdc1614
3فایلهای تغییر یافته به همراه160 افزوده شده و 0 حذف شده
  1. 13 0
      app/Model/CalendarYear.php
  2. 13 0
      app/Model/CalendarYearDays.php
  3. 134 0
      app/Service/CalendarService.php

+ 13 - 0
app/Model/CalendarYear.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class CalendarYear extends Model
+{
+    protected $table = "calendar_year"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+}

+ 13 - 0
app/Model/CalendarYearDays.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+class CalendarYearDays extends Model
+{
+    protected $table = "calendar_year_days"; //指定表
+    const CREATED_AT = null;
+    const UPDATED_AT = null;
+    protected $dateFormat = 'U';
+}

+ 134 - 0
app/Service/CalendarService.php

@@ -0,0 +1,134 @@
+<?php
+
+namespace App\Service;
+
+use App\Model\CalendarYear;
+use App\Model\CalendarYearDays;
+use Illuminate\Support\Facades\DB;
+
+class CalendarService extends Service
+{
+    public function createCalendar($year = 0){
+        if(empty($year)) $year = date("Y");
+
+        try {
+            DB::beginTransaction();
+
+            $year_model = CalendarYear::where('year',$year)->first();
+            if(empty($year_model)){
+                $year_model = new CalendarYear();
+                list($status, $msg) = $this->getHoliday($year);
+                $days_stamp = $this->generateTimestampsForYear($year,$msg);
+
+                if(! empty($days_stamp)){
+                    //子表更新
+                    CalendarYearDays::insert($days_stamp);
+
+                    $year_model->is_update_holidays = 1;
+                }
+
+                //主表更新
+                $year_model->year = $year;
+                $year_model->save();
+            }else{
+                if($year_model->is_update_holidays > 0) return [false, '已更新' . $year . '年的节假日信息'];
+
+                list($status, $msg) = $this->getHoliday($year);
+                if(empty($msg)) return [false, '未获取到'. $year . '年的节假日信息'];
+
+                CalendarYearDays::whereIn('time_stamp', array_keys($msg))->delete();
+                CalendarYearDays::insert(array_values($msg));
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){dd($exception->getMessage(),$exception->getLine());
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ''];
+    }
+
+   function generateTimestampsForYear($year, $holidays){
+        // 创建一个 DateTime 对象,设置为当年的第一天
+        $date = new \DateTime("$year-01-01");
+        //设置时间到午夜
+        $date->setTime(0, 0, 0);
+        // 获取当年最后一天
+        $endOfYear = new \DateTime("$year-12-31");
+        // 创建一个空数组来保存时间戳
+        $timestamps = []; // 循环直到到达当年的最后一天
+
+        while ($date <= $endOfYear){
+           if(isset($holidays[$date->getTimestamp()])){
+               $t = $holidays[$date->getTimestamp()] ?? [];
+               $timestamps[] = [
+                   'time_stamp' => $date->getTimestamp(),
+                   'is_holiday' => $t['is_holiday'],
+                   'holiday_title' => $t['holiday_title']
+               ];
+           }else{
+               $timestamps[] = [
+                   'time_stamp' => $date->getTimestamp(),
+                   'is_holiday' => 0,
+                   'holiday_title' => ''
+               ];
+           }
+           $date->modify('+1 day');
+        }
+
+        return $timestamps;
+    }
+
+   function getHoliday($year){
+        $return = [];
+
+        $url = 'http://api.jiejiariapi.com/v1/holidays/' . $year;
+        list($status, $msg) = $this->get_helper($url);
+
+        if(! $status) return [false, $return];
+        if(! is_array($msg) || $msg == 'Year not found') return [false, $return];
+
+        foreach ($msg as $value){
+            $time =  strtotime($value['date'] . "00:00:00");
+            $return[$time] = [
+                'time_stamp' => strtotime($value['date'] . "00:00:00"),
+                'is_holiday' => $value['isOffDay'] ? 1 : 0,
+                'holiday_title' => $value['name']
+            ];
+        }
+
+        return [true, $return];
+   }
+
+    public function get_helper($url, $header=[], $timeout = 20){
+        $ch = curl_init();
+        curl_setopt_array($ch, array(
+            CURLOPT_URL => $url,
+            CURLOPT_RETURNTRANSFER => true,
+            CURLOPT_ENCODING => '',
+            CURLOPT_MAXREDIRS => 10,
+            CURLOPT_TIMEOUT => $timeout,
+            CURLOPT_FOLLOWLOCATION => true,
+            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+            CURLOPT_CUSTOMREQUEST => 'GET',
+            CURLOPT_SSL_VERIFYPEER => false,
+            CURLOPT_HTTPHEADER => $header,
+        ));
+        $r = curl_exec($ch);
+
+        if ($r === false) {
+            // 获取错误号
+            $errorNumber = curl_errno($ch);
+            // 获取错误信息
+            $errorMessage = curl_error($ch);
+            $message = "cURL Error #{$errorNumber}: {$errorMessage}";
+            return [false, $message];
+        }
+
+        curl_close($ch);
+        $return = json_decode($r, true);
+
+        return [true, $return];
+    }
+}