package com.yinet.wsyy.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.yinet.common.utils.DateUtils;
import com.yinet.common.utils.http.HttpUtils;
import com.yinet.system.domain.Holiday;
import com.yinet.system.mapper.HolidayMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yinet.wsyy.mapper.WsyySettingMapper;
import com.yinet.wsyy.domain.WsyySetting;
import com.yinet.wsyy.service.IWsyySettingService;
import com.yinet.common.core.text.Convert;

/**
 * 网上预约配置Service业务层处理
 *
 * @author yinet
 * @date 2019-09-02
 */
@Service
public class WsyySettingServiceImpl implements IWsyySettingService {
    @Autowired
    private WsyySettingMapper wsyySettingMapper;
    @Autowired
    private HolidayMapper holidayMapper;

    /**
     * 查询网上预约配置
     *
     * @return 网上预约配置
     */
    @Override
    public WsyySetting selectWsyySetting() {
        return wsyySettingMapper.selectWsyySetting();
    }


    /**
     * 修改网上预约配置
     *
     * @param wsyySetting 网上预约配置
     * @return 结果
     */
    @Override
    public int updateWsyySetting(WsyySetting wsyySetting) {
        return wsyySettingMapper.updateWsyySetting(wsyySetting);
    }

    @Override
    public int updateWsyyHoliday() {
        List<String> nowYearAllDay = getNowYearAllDay();
        List<Holiday> holidays = new ArrayList<>();
        for (String date : nowYearAllDay) {
            String s = HttpUtils.sendGet("http://timor.tech/api/holiday/info/" + date, null);
            Map map = new Gson().fromJson(s, Map.class);
            //判断周末
            int i = new Double((Double) ((Map) map.get("type"))
                    .get("type")).intValue();
             i = (i != 0 || i != 3) ? 1 : 0;
            holidays.add(new Holiday(date, i));
        }
        holidayMapper.truncateHoliday();
        for (Holiday ho : holidays) {
            holidayMapper.insertHoliday(ho);
        }
        return 0;
    }
    public static List<String> getNowYearAllDay() {
        List<String> resultList = new ArrayList<String>();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int i = aCalendar.get(Calendar.YEAR);
        for (int j = 1; j < 13; j++) {
            resultList.addAll(getAllMonthDays(i + "-" + j));
        }
        return resultList;
    }

    /**
     * [
     * 获取某一月份的所有日期
     * date 年月;格式:"2018-10"
     *
     * @return
     */
    public static List<String> getAllMonthDays(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5));
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int days = a.get(Calendar.DATE);
        List<String> resultList = new ArrayList<String>();
        for (int i = 1; i <= days; i++) {
            String day = String.valueOf(year) + "-" + fillZero(month) + "-" + fillZero(i);
            resultList.add(day);
        }
        return resultList;
    }

    public static String fillZero(int num) {
        if (num < 10) {
            return "0" + num;
        } else {
            return String.valueOf(num);
        }
    }
}
