| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 | 
							- <?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];
 
-     }
 
- }
 
 
  |