123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\admin\controller\base\other;
- use app\admin\controller\Base;
- use app\common\model\base\other\Festival as festivalModel;
- use app\common\model\base\other\Holiday as holidayModel;
- class Holiday extends Base
- {
- protected $noNeedAuth = ['getyearholiday'];
- protected $holidayModel = null;
- protected $festivalModel = null;
- public function initialize()
- {
- parent::initialize();
- $this->holidayModel = new holidayModel;
- $this->festivalModel = new festivalModel;
- }
-
- public function getList($pageNo=1,$pageSize=10)
- {
- $list = [];
-
- $years = $this->holidayModel->field(['year'])->group('year')->select()->toArray();
- $years = array_column($years,'year');
-
- $festivals = $this->festivalModel->order("weigh ASC")->select()->toArray();
- foreach($years as $year){
- $item = [];
- $item['year'] = $year;
-
- $holidays = $this->holidayModel->where('year',$year)->select()->toArray();
- $holidays = array_column($holidays,null,'festival_id');
- foreach($festivals as $festival){
- $holiday = isset($holidays[$festival['festival_id']])?$holidays[$festival['festival_id']]:[];
- $festival = array_merge($festival,$holiday);
- $item['festival_'.$festival['festival_id']] = $festival;
- }
- $list[] = $item;
- }
- $list = FieldConverList($list);
- return res(1,"获取成功",$list);
- }
-
- public function getYearHoliday($year="")
- {
- $list = [];
- $festivals = $this->festivalModel->order("weigh ASC")->select()->toArray();
- foreach($festivals as $festival){
- if(!empty($year)){
- $holiday = $this->holidayModel->where('festival_id',$festival['festival_id'])->where('year',$year)->find();
- if($holiday){
- $holiday = $holiday->toArray();
- $holiday['daterange'][] = $holiday['start_date'];
- $holiday['daterange'][] = $holiday['end_date'];
- }else{
- $holiday = [];
- $holiday['daterange'] = [];
- }
- $list[] = array_merge($festival,$holiday);
- }else{
- $list[] = $festival;
- }
- }
- return res(1,"获取成功",$list);
- }
-
- public function doEdit()
- {
- $data = $this->request->param();
- $year = $data['year'];
- $list = $data['list'];
- try{
- $saveData = [];
- foreach($list as $row){
- if(!empty($row['daterange'])){
- $rangeInfo = $this->getDaysListRange($row['daterange']);
- $row['start_date'] = date("Y-m-d",strtotime($rangeInfo['start']));
- $row['end_date'] = date("Y-m-d",strtotime($rangeInfo['end']));
- $row['days'] = $rangeInfo['days'];
- $row['year'] = $year;
- $saveData[] = $row;
- }
- }
- $this->holidayModel->replace()->saveAll($saveData);
- slog(1,"编辑了".$year."年法定节假日");
- return res(1,"保存成功",$data);
- }catch(Exception $e){
- return res(2,"保存失败".$e->getMessage());
- }
- }
-
- public function doDelete($year = "")
- {
- if(empty($year))return res(2,"参数错误");
- $this->holidayModel->where("year",$year)->delete();
- slog(1,"删除了".$year."年法定节假日");
- return res(1,"删除成功");
- }
-
- private function getDaysListRange($datetimerange=[])
- {
- $list = [];
- if(!empty($datetimerange) && 2==count($datetimerange)){
- $start = date('Y-m-d 00:00:00',strtotime($datetimerange[0]));
- $end = date('Y-m-d 23:59:59',strtotime($datetimerange[1]));
- }else{
- $start = date('Y-m-01 00:00:00');
- $end = date('Y-m-d 23:59:59');
- }
- $days = (strtotime($end) - strtotime($start)) / 86400;
- for($i=0;$i<$days;$i++){
- $day = date('d',strtotime($start." + $i day"));
- $month = date('m',strtotime($start." + $i day"));
- $year = date('Y',strtotime($start." + $i day"));
- $date = date('Y-m-d',strtotime($start." + $i day"));
- $list[] = [
- 'year' => $year,
- 'month' => $month,
- 'day' => $day,
- 'date' => $date
- ];
- }
- return ['start'=>$start,'end'=>$end,'list'=>$list,'days'=>$days];
- }
- }
|