package com.ningdatech.kqapi.scheduler.task;

import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.ningdatech.kqapi.common.constant.BizConst;
import com.ningdatech.kqapi.common.enumeration.CommonEnum;
import com.ningdatech.kqapi.common.helper.MatterCacheHelper;
import com.ningdatech.kqapi.zzsfw.entity.entity.ComponentsMatterEliminateEntity;
import com.ningdatech.kqapi.zzsfw.service.ComponentsMatterEliminateService;
import com.ningdatech.kqapi.zzsfw.entity.entity.DscSxAdsShareItemQltQlsxCommonIDVKq;
import com.ningdatech.kqapi.zzsfw.entity.entity.MatterKey;
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.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @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;

    @Autowired
    private IDscSxAdsShareItemQltQlsxCommonIDVKqService kqService;

    @Autowired
    private ComponentsMatterEliminateService componentsMatterEliminateService;

    protected LoadingCache<MatterKey, NdKqZzsfwMattersDeduplicate> mattersDupCache;

    private final String KQ_ZZS_BASE_URL = "https://www.zjzwfw.gov.cn/zjservice-fe/#/workguide?localInnerCode=&siteCode=330000000000";

    /**
     * 每天
     */
    @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());
    }

    public void synMenu() {
        //2. 事项
        List<NdKqZzsfwMenu> menus = menuService.list();
        for(NdKqZzsfwMenu menu : menus){
            String url = matterCacheHelper.getUrl(menu.getItemName());
            log.info("当前缓存的数据 : {},{}",menu.getItemName(),url);
            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()));
            }
        }
    }

    public String synEliminate(List<String> keys){
//        kqService.remove(new LambdaQueryWrapper<DscSxAdsShareItemQltQlsxCommonIDVKq>()
//                .in(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName, keys));
        // 添加剔除信息
        List<ComponentsMatterEliminateEntity> entityList = new ArrayList<>();
        keys.forEach(key ->{
            ComponentsMatterEliminateEntity one = componentsMatterEliminateService.getOne(Wrappers.lambdaQuery(ComponentsMatterEliminateEntity.class)
                    .eq(ComponentsMatterEliminateEntity::getQlName, key));
            if (Objects.isNull(one)){
                entityList.add(new ComponentsMatterEliminateEntity(key,"1"));
            }
            // 删除缓存
            DscSxAdsShareItemQltQlsxCommonIDVKq matter = getSynUrl(key);
            String url = KQ_ZZS_BASE_URL.replace("localInnerCode=", "localInnerCode=" + matter.getQlInnerCode());
            MatterKey matterKey = MatterKey.of(matter.getQlName(),url);
            try {
                mattersDupCache.invalidate(matterKey);
            }catch (Exception e){
                log.error("mattersDupCache is null", e.getMessage());
            }
        });
        componentsMatterEliminateService.saveBatch(entityList);

        // 删除menu表
        menuService.remove(new LambdaQueryWrapper<NdKqZzsfwMenu>()
                .in(NdKqZzsfwMenu::getItemName, keys));

        // 删除deduplicate表
        matterDeduplicateService.remove(new LambdaQueryWrapper<NdKqZzsfwMattersDeduplicate>()
                .in(NdKqZzsfwMattersDeduplicate::getQlName, keys));

        return "剔除成功";
    }
    
    public DscSxAdsShareItemQltQlsxCommonIDVKq getSynUrl(String key){
        DscSxAdsShareItemQltQlsxCommonIDVKq matter = kqService
                .getOne(Wrappers.lambdaQuery(DscSxAdsShareItemQltQlsxCommonIDVKq.class)
                        .select(DscSxAdsShareItemQltQlsxCommonIDVKq::getRowguid,
                                DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName,
                                DscSxAdsShareItemQltQlsxCommonIDVKq::getQlState,
                                DscSxAdsShareItemQltQlsxCommonIDVKq::getQlInnerCode,
                                DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl)
                        .eq(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlName, key)
                        .eq(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlState, "1")
                        .isNotNull(DscSxAdsShareItemQltQlsxCommonIDVKq::getWebapplyurl)
                        // 确保getQlInnerCodeItem字段不为null且不为""
                        .isNotNull(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlInnerCode)
                        .ne(DscSxAdsShareItemQltQlsxCommonIDVKq::getQlInnerCode, "")
                        .orderByDesc(DscSxAdsShareItemQltQlsxCommonIDVKq::getUpdateDate)
                        .last(BizConst.LIMIT_1));
        return matter;
    }

    public String synSingle(String key){
        DscSxAdsShareItemQltQlsxCommonIDVKq matter = getSynUrl(key);
        String url = KQ_ZZS_BASE_URL.replace("localInnerCode=", "localInnerCode=" + matter.getQlInnerCode());
        List<NdKqZzsfwMenu> menus = menuService.list(Wrappers.lambdaQuery(NdKqZzsfwMenu.class)
                .eq(NdKqZzsfwMenu::getItemName, key));
        menus.forEach(menu->{
            menuService.update(Wrappers.lambdaUpdate(NdKqZzsfwMenu.class)
                    .eq(NdKqZzsfwMenu::getId,menu.getId())
                    .set(NdKqZzsfwMenu::getWebapplyurl,url)
                    .set(NdKqZzsfwMenu::getHasUrl,CommonEnum.YES.getCode()));
        });
        // 更新缓存
        MatterKey matterKey = MatterKey.of(matter.getQlName(),url);
        mattersDupCache.invalidate(matterKey);
        mattersDupCache.put(matterKey,NdKqZzsfwMattersDeduplicate.of(matter.getQlName(),url));
        return "更新成功,rul:" + url;
    }
}
