package com.yida.data.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.constant.FebsConstant;
import com.yida.data.common.core.entity.system.EduHoliday;
import com.yida.data.system.mapper.EduHolidayMapper;
import com.yida.data.system.service.EduHolidayService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@AllArgsConstructor
@Transactional
@Service
public class EduHolidayServiceImpl extends ServiceImpl<EduHolidayMapper, EduHoliday>implements EduHolidayService {

    @Override
    public void initHoliday(Integer year){
        // 删除这一年的数据
        remove(Wrappers.<EduHoliday>lambdaQuery().eq(EduHoliday::getYear, year));
        // 法定假日数据
        JSONObject lawHoliday = JSONUtil.parseObj(HttpUtil.get(FebsConstant.LAW_HODIDAY_API + year)).getJSONObject("holiday");
        // 百度接口会查前后各一个月的数据，所以从2月开始查,到11月为止
        List<String> holidayList = new ArrayList<>();
        List<EduHoliday> saveHolidayList = new ArrayList<>();
        // 从2月开始查
        for (int i = 2; i < 12; i++) {
            String url = String.format(FebsConstant.BAIDU_DAY_API, year + "年" + i + "月");
            String res = HttpUtil.get(url);
            String s = res.split("\\(")[1].split("\\)")[0];
            JSONObject resJson = JSONUtil.parseObj(s);
            JSONArray dayList = resJson.getJSONArray("data").getJSONObject(0).getJSONArray("almanac");
            // 遍历所有的天
            for (Object o : dayList) {
                JSONObject dayJson = (JSONObject) o;
                // 改天为节假日才处理
                if ("1".equals(dayJson.getStr("status")) ||
                        ((!"2".equals(dayJson.getStr("status"))) && ("六日".contains(dayJson.getStr("cnDay"))))
                ) {
                    Integer month = Integer.valueOf(dayJson.getStr("month"));
                    Integer day = Integer.valueOf(dayJson.getStr("day"));
                    // 改日期已存在说明循环到下个月的数据
                    if (!holidayList.contains(month + "," + day)) {
                        holidayList.add(month + "," + day);
                        EduHoliday holiday = new EduHoliday();
                        holiday.setYear(year);
                        holiday.setDate(LocalDate.of(year, month, day));
                        // 该天为法定假日
                        if (lawHoliday.containsKey(LocalDateTimeUtil.format(holiday.getDate(), "MM-dd"))) {
                            holiday.setTerm(lawHoliday.getJSONObject(LocalDateTimeUtil.format(holiday.getDate(), "MM-dd")).get("name").toString());
                        } else {
                            holiday.setTerm("周末");
                        }
                        saveHolidayList.add(holiday);
                    }
                }
            }
        }
        saveHolidayList.sort((x, y) -> x.getDate().compareTo(y.getDate()));
        // 保存数据
        if (CollUtil.isNotEmpty(saveHolidayList)) {
            saveBatch(saveHolidayList);
        }
    }
}
