package com.malk.huagao.service.impl;

import com.malk.core.McProject;
import com.malk.huagao.service.HuaGaoService;
import com.malk.huagao.service.McProjectService;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilDateTime;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class McProjectServiceImpl implements McProjectService {
    @Autowired
    private Environment environment;

    @Autowired
    private HuaGaoService huaGaoService;

    @Override
    public void init() {
        MDC.put("MDC_KEY_PID","1001");
        McProject.addYida("1001",new String[] {"APP_BWPT6ET90UEE8IBEY9GJ","37766HD145ST2RBB9S4D37J2WMNU293GASC8M6Q"});//投入产出比看板
        McProject.addYida("1002",new String[] {"APP_YONR16SZSUIZCKXJUNSY","TNA66KB1AEDXKQHQ8IJBG53BDW7Q27TM33HDMADI"});//数据分析看板
        McProject.addYida("1003",new String[] {"APP_VQDMMWS6OR1VHL8VMFD3","XE766X81JHKXK4Z27SYOI86CPCLQ3J1LV7ODMH2"});//华高CRM
        log.info("项目加载完毕：1001,1002,1003");

        // 生产环境2专属逻辑
        if (isProdEnvironment()) {
            executeProductionMethod();
        }
    }

    private boolean isProdEnvironment() {
        String[] activeProfiles = environment.getActiveProfiles();
        for (String profile : activeProfiles) {
            if ("prod2".equals(profile)) {
                return true;
            }
        }
        return false;
    }
    @Profile("prod2")
    private void executeProductionMethod() {
        log.info("异常重启考勤同步-周度");
        LocalDateTime startTime = LocalDate.now().minusWeeks(1).atTime(LocalTime.MIN);
        LocalDateTime endTime = LocalDate.now().atTime(LocalTime.MIN);
        try {
            huaGaoService.syncKqData(startTime, endTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
