<?php
namespace app\admin\controller\base\other;
/**
 * @title: 每年法定节假日
 */
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;
    }


    /**
     * @title: 获取各年度节日假期
     * @param {int} {pageNo} {非必填,默认值为1} {页码}
     * @param {int} {pageSize} {非必填,默认值为10} {每页数量}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:20
     */
    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);
    }


    /**
     * @title: 获取某一年度的假期情况
     * @param {string} {year} {非必填} {年}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:21
     */
    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);
    }


    /**
     * @title: 一年法定节假日编辑
     * @param array
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:23
     */
    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());
        }
    }


    /**
     * @title: 一年法定节假日删除
     * @param {string} {year} {非必填} {年}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:24
     */
    public function doDelete($year = "")
    {
        if(empty($year))return res(2,"参数错误");
        $this->holidayModel->where("year",$year)->delete();
        slog(1,"删除了".$year."年法定节假日");
        return res(1,"删除成功");
    }


    /**
     * @title: 根据用户所传时间范围,列出范围内的每一天
     * @param {array} {datetimerange} {非必填} {时间区间}
     * @return array
     * @Author: wangkewei
     * @Date: 2021/5/18 9:25
     */
    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];
    }
}