package com.palmnest.service.estateserve.impl;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.DateTypeDto;
import com.palmnest.model.DateTypeInfo;
import com.palmnest.service.estateserve.DateTypeInfoManager;
import com.palmnest.service.estateserve.DateTypeInfoService;
import com.palmnest.service.estateserve.SmsAuthCodeManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Service("dateTypeInfoService")
public class DateTypeInfoServiceImpl implements DateTypeInfoService {


	@Autowired
	private DateTypeInfoManager dateTypeInfoManager;
	@Autowired
	private SmsAuthCodeManager  smsAuthCodeManager;

	@Override
	public String saveDateType(String year, String month) {

		log.info("begin ------saveDateType ----");

		String           res = "SUCCESS";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar         now = Calendar.getInstance();
		if (!StringUtil.isNotEmpty(year)) {
			year = now.get(Calendar.YEAR) + "";
		}
		if (!StringUtil.isNotEmpty(month)) {
			month = (now.get(Calendar.MONTH) + 1) + "";
		}
		log.info(" ------saveDateType ----year:" + year + ";month:" + month + "--------");
		try {
			List<DateTypeDto> list = new ArrayList<DateTypeDto>();
			//获取近期的节假日
			List<DateTypeDto> holidayList = DateUtils.getHoliday(year + "-" + month);
			//获取本月所有的日历
			List<DateTypeDto> dateList = DateUtils.getDateTypeInfo(Integer.parseInt(year), Integer.parseInt(month));
			if (dateList != null && dateList.size() > 0) {
				log.info("------saveDateType ----dateList");
				for (int i = 0; i < dateList.size(); i++) {
					DateTypeDto dto          = dateList.get(i);
					int         dateInfoType = -1;
					for (int k = 0; k < holidayList.size(); k++) {
						DateTypeDto holidayDto = holidayList.get(k);
						//如果相等，代表着需要将假期中的值替换到list中
						if (dto.getDate().equals(holidayDto.getDate())) {
							dateInfoType = holidayDto.getType();
						}
					}
					if (dateInfoType != -1) {
						dto.setType(dateInfoType);
					}
					list.add(dto);
				}
			}
			//集合不为空且个数大于0时需要将值保存到数据库中
			//首先判断集合中的值是否在数据库中存在，如果存在，就修改，如果不存在就添加
			if (list != null && list.size() > 0) {
				log.info("------saveDateType ----setSize:" + list.size());
				int count = 0;
				for (DateTypeDto dto : list) {
					DateTypeInfo dateInfo = dateTypeInfoManager.getDateTypeInfoList(dto.getDate());
					log.info("------saveDateType ----dateInfo:" + dateInfo);
					if (dto.getType() == -1) {
						count++;
					}
					if (dateInfo != null) {
						dateInfo.setModifyTime(new Date());
						dateInfo.setType(dto.getType());
						dateTypeInfoManager.updateModel(dateInfo);
					} else {
						dateInfo = new DateTypeInfo();
						dateInfo.setCreateTime(new Date());
						dateInfo.setDate(sdf.parse(dto.getDate()));
						dateInfo.setType(dto.getType());
						dateTypeInfoManager.save(dateInfo);
					}
				}
				//预警
				if (count > 0) {
					log.info("发送短信号码：134xxxx4033");
					String result = smsAuthCodeManager.smsSendByOrgId("10", "存在异常日期格式，请排查！", "13423994033", "", CodeConstants.SMS_DINNER_TYPE);
					log.info("isMessage：" + result);
				}
			}
		} catch (Exception e) {
			log.error("更新日期类型失败:" + e.getMessage());
			e.printStackTrace();
			res = "FAIL";
		}
		log.info("end ------saveDateType--------");
		return res;
	}

	@Override
	public String saveDateTypeAddOne(String year, String month) {
		log.info("begin ------saveDateTypeAddOne ----");
		String           res = "SUCCESS";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar         now = Calendar.getInstance();
		if (!StringUtil.isNotEmpty(year)) {
			year = now.get(Calendar.YEAR) + "";
		}
		if (!StringUtil.isNotEmpty(month)) {
			month = (now.get(Calendar.MONTH) + 1) + "";
		}

		if ("12".equals(month)) {
			log.info(" ------saveDateTypeAddOne ----year:" + year + ";month:" + month + "--------");
			year = (Integer.parseInt(year) + 1) + "";
			month = "1";
			log.info(" ------saveDateTypeAddOne ----year:" + year + ";month:" + month + "--------");
		}
		log.info(" ------saveDateTypeAddOne ----year:" + year + ";month:" + month + "--------");
		try {
			List<DateTypeDto> list = new ArrayList<DateTypeDto>();
			//获取近期的节假日
			List<DateTypeDto> holidayList = DateUtils.getHoliday(year + "-" + month);
			//获取本月所有的日历
			List<DateTypeDto> dateList = DateUtils.getDateTypeInfo(Integer.parseInt(year), Integer.parseInt(month));
			if (dateList != null && dateList.size() > 0) {
				log.info("------saveDateTypeAddOne ----dateList");
				for (int i = 0; i < dateList.size(); i++) {
					DateTypeDto dto          = dateList.get(i);
					int         dateInfoType = -1;
					for (int k = 0; k < holidayList.size(); k++) {
						DateTypeDto holidayDto = holidayList.get(k);
						//如果相等，代表着需要将假期中的值替换到list中
						if (dto.getDate().equals(holidayDto.getDate())) {
							dateInfoType = holidayDto.getType();
						}
					}
					if (dateInfoType != -1) {
						dto.setType(dateInfoType);
					}
					list.add(dto);
				}
			}
			//集合不为空且个数大于0时需要将值保存到数据库中
			//首先判断集合中的值是否在数据库中存在，如果存在，就修改，如果不存在就添加
			if (list != null && list.size() > 0) {
				log.info("------saveDateTypeAddOne ----setSize:" + list.size());
				int count = 0;
				for (DateTypeDto dto : list) {
					DateTypeInfo dateInfo = dateTypeInfoManager.getDateTypeInfoList(dto.getDate());
					if (dto.getType() == -1) {
						count++;
					}
					if (dateInfo != null) {
						dateInfo.setModifyTime(new Date());
						dateInfo.setType(dto.getType());
						dateTypeInfoManager.updateModel(dateInfo);
					} else {
						dateInfo = new DateTypeInfo();
						dateInfo.setCreateTime(new Date());
						dateInfo.setDate(sdf.parse(dto.getDate()));
						dateInfo.setType(dto.getType());
						dateTypeInfoManager.save(dateInfo);
					}
				}
				//预警
				if (count > 0) {
					log.info("发送短信号码：134xxxx4033");
					String result = smsAuthCodeManager.smsSendByOrgId("10", "存在异常日期格式，请排查！", "13423994033", "", CodeConstants.SMS_DINNER_TYPE);
					log.info("isMessage：" + result);
				}
			}
		} catch (Exception e) {
			log.error("更新日期类型失败:" + e.getMessage());
			e.printStackTrace();
			res = "FAIL";
		}
		log.info("end ------saveDateTypeAddOne--------");
		return res;
	}
}
