package com.hz.pm.api.scheduler.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.hz.pm.api.organization.model.entity.ProvincialGovBusinessStrip;
import com.hz.pm.api.organization.model.vo.ProvincialGovBusinessStripVO;
import com.hz.pm.api.organization.service.IProvincialGovBusinessStripService;
import com.hz.pm.api.provincial.service.IJoinReviewProvincialBureauService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;
import java.util.stream.Collectors;

/**
 * @author ZPF
 * @since 2023/08/31 18:16
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class SynProvinceOrgTask {

    @Value("${hostname}")
    public String HOST;

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

    @Autowired
    private IProvincialGovBusinessStripService provincialGovBusinessStripService;

    @Autowired
    private IJoinReviewProvincialBureauService joinReviewProvincialBureauService;

    /**
     *
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void doTask() throws UnknownHostException {
        if (!HOST.equals(InetAddress.getLocalHost().getHostName())) {
            log.info("定时器没开启或者host不对! {}:{}",
                    HOST,InetAddress.getLocalHost().getHostName());
            return;
        }

        StopWatch stopWatch = new StopWatch();
        stopWatch.start();

        log.info("同步省局升级单位数据任务开始====={}s",stopWatch.getTotalTimeSeconds());

        List<ProvincialGovBusinessStripVO> res = Lists.newArrayList();
        for(int i = 0;i < 10;i++){
            res = joinReviewProvincialBureauService.searchGovUnits();
            if(CollUtil.isNotEmpty(res)){
                log.info("请求到了数据 :{}",res.size());
                break;
            }
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        if(CollUtil.isEmpty(res)){
            log.info("一直都没请求到数据 任务结束");
            return;
        }


        log.info("数据同步任务开始之前 先删除前置机上的所有数据");
        provincialGovBusinessStripService.remove(Wrappers.lambdaQuery(ProvincialGovBusinessStrip.class));

        List<ProvincialGovBusinessStrip> saves = res.stream().map(r -> {
            ProvincialGovBusinessStrip strip = new ProvincialGovBusinessStrip();
            strip.setBusinessStripCode(r.getBusinessStripCode());
            strip.setBusinessStripName(r.getBusinessStripName());
            return strip;
        }).collect(Collectors.toList());
        provincialGovBusinessStripService.saveBatch(saves);

        stopWatch.stop();
        log.info("同步省局升级单位数据任务结束====={}s",stopWatch.getTotalTimeSeconds());
    }
}
