package com.ningdatech.kqapi.scheduler.task;

import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.kqapi.common.enumeration.CommonEnum;
import com.ningdatech.kqapi.common.helper.MatterCacheHelper;
import com.ningdatech.kqapi.scheduler.contants.TaskContant;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMattersDeduplicate;
import com.ningdatech.kqapi.zzsfw.entity.entity.NdKqZzsfwMenu;
import com.ningdatech.kqapi.zzsfw.manage.MatterManage;
import com.ningdatech.kqapi.zzsfw.service.IDscSxAdsShareItemQltQlsxCommonIDVKqService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMatterDeduplicateService;
import com.ningdatech.kqapi.zzsfw.service.INdKqZzsfwMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @Classname SynTask
 * @Description
 * @Date 2024/1/4 9:04
 * @Author PoffyZhang
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class SynTask {
    @Value("${hostname}")
    public String HOST;

    @Value("${spring.profiles.active}")
    public String active;

    @Autowired
    private MatterCacheHelper matterCacheHelper;

    @Autowired
    private MatterManage matterManage;

    @Autowired
    private INdKqZzsfwMatterDeduplicateService matterDeduplicateService;

    @Autowired
    private INdKqZzsfwMenuService menuService;

    /**
     * 每天
     */
    @Scheduled(cron = "0 30 8 * * ?")
    public void synData() throws UnknownHostException {
//        if (!HOST.equals(InetAddress.getLocalHost().getHostName())) {
//            log.info("定时器没开启或者host不对! {}:{}",
//                    HOST,InetAddress.getLocalHost().getHostName());
//            return;
//        }
//        if(!TaskContant.PROD.equals(active)){
//            log.info("非正式环境不用运行同步任务!");
//            return;
//        }

        log.info("政务数据 ===================== 同步开始");
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        //1.处理 去重数据
        if(matterCacheHelper.refreshAll()){
            matterManage.removeAllDup();
            List<NdKqZzsfwMattersDeduplicate> allDup = matterCacheHelper.all();
            matterDeduplicateService.saveBatch(allDup);

            //2. 事项
            List<NdKqZzsfwMenu> menus = menuService.list();
            for(NdKqZzsfwMenu menu : menus){
                String url = matterCacheHelper.getUrl(menu.getItemName());
                if(StringUtils.isBlank(url)){
                    menuService.update(Wrappers.lambdaUpdate(NdKqZzsfwMenu.class)
                            .eq(NdKqZzsfwMenu::getId,menu.getId())
                            .set(NdKqZzsfwMenu::getWebapplyurl,null)
                            .set(NdKqZzsfwMenu::getHasUrl, CommonEnum.NO.getCode()));
                }else{
                    menuService.update(Wrappers.lambdaUpdate(NdKqZzsfwMenu.class)
                            .eq(NdKqZzsfwMenu::getId,menu.getId())
                            .set(NdKqZzsfwMenu::getWebapplyurl,url)
                            .set(NdKqZzsfwMenu::getHasUrl,CommonEnum.YES.getCode()));
                }
            }
        }

        stopWatch.stop();
        log.info("政务数据 ===================== 同步总共耗时 :{} s",stopWatch.getTotalTimeSeconds());
    }
}
