package com.bonus.system.att.tasks;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.system.att.dao.AttSourceDataDao;
import com.bonus.system.att.entity.Holiday;
import com.bonus.system.att.entity.OrgChangeBean;
import com.bonus.system.att.service.OrgChangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.*;

/**
 * @author zys
 * 节假日定时器
 *
 */
@Configuration
@EnableScheduling
@Slf4j
@EnableAsync
public class HolidayTasks {

    @Resource(name = "attSourceDataDao")
    private AttSourceDataDao attSourceDataDao;

    @Resource(name = "OrgChangeService")
    private OrgChangeService orgChangeService;

    @Scheduled(cron = "0 0 5 26 11 *")//每天12月26日凌晨5点
    @Async
    public void getAttTasks(){
        getHoliday();
    }

    public void getHoliday(){
        try {
            String year = DateUtils.getNextYear();
            String data = HttpUtil.get("http://timor.tech/api/holiday/year/" + year + "?type=Y&week=Y");
            String code = JSONObject.parseObject(data).getString("code");
            if(code.equals("0")){
                JSONObject jsonObject = JSONObject.parseObject(data).getJSONObject("holiday");
                Set<String> keySet = jsonObject.keySet();
                for (String key : keySet) {
                    JSONObject jsonObjectData = jsonObject.getJSONObject(key);
                    Holiday holiday = new Holiday();
                    holiday.setName(jsonObjectData.getString("name"));
                    holiday.setDate(jsonObjectData.getString("date"));
                    if(jsonObjectData.getBoolean("holiday")){
                        holiday.setType("1");
                    }else{
                        holiday.setType("2");
                    }
                    attSourceDataDao.insertHoliday(holiday);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 考勤组变更更新数据
     */
//    @Scheduled(fixedDelay = 60000 * 60)
    @Async
    public void getOrgChange(){
        OrgChangeBean bean = new OrgChangeBean();
        bean.setIsCheck("1");
        //查询审核通过的
        List<OrgChangeBean> list = orgChangeService.selectOrgChangeList(bean);
        list.stream().filter(item -> "0".equals(item.getIsApply())).forEach(item -> {
            Date changeEffectiveDate = item.getChangeEffectiveDate();
            // 将 Date 转换为 Instant
            Instant changeEffectiveInstant = changeEffectiveDate.toInstant();
            // 获取当前时间的 Instant
            Instant now = Instant.now();
            // 判断 changeEffectiveDate 是否大于当前时间
            if (!changeEffectiveInstant.isAfter(now)) {
                orgChangeService.updateOrgByUserId(item);
                if ("1".equals(item.getIsChangeAttGroup())){
                    orgChangeService.updateAttGroupByUserId(item);
                }
                item.setIsApply("1");
                orgChangeService.updateOrgChangeIsApply(item);
            }
        });
    }

}
