package com.jsc.oscm.service;

import com.jsc.oscm.dao.CrawlNodeRepository;
import com.jsc.oscm.entity.CrawlNode;
import com.jsc.oscm.util.HttpUtil;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class CrawlNodeService {

    @Resource
    CrawlNodeRepository crawlNodeRepository;

    static final ExecutorService executorService = Executors.newFixedThreadPool(4);

    public boolean addCrawlNodes(CrawlNode crawlNode) {
        crawlNode.setNodeStatus(0);
        crawlNodeRepository.save(crawlNode);
        return true;
    }

    public boolean delCrawlNodes(List<Integer> ids) {
        for (int id : ids) {
            crawlNodeRepository.deleteById(id);
        }
        return true;
    }

    /*
     * add by pangq 2021/2/5
     * 增加通过hostid删除节点
     */
    public boolean delCrawlNodesByHostIds(List<Integer> hostids) {
        try {
            crawlNodeRepository.deleteNodesByHostId(hostids);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public boolean updateCrawlNodes(CrawlNode crawlNode) {
        crawlNodeRepository.save(crawlNode);
        return true;
    }

    public List<CrawlNode> queryAllCrawlNodes() {
        return crawlNodeRepository.findAll();
    }

    public List<CrawlNode> queryAllCrawlNodePageable(int page, int size, Integer hostId) {
        List<CrawlNode> crawlNodes = new ArrayList<>();
        Pageable pageable = PageRequest.of(page, size);
        CrawlNode exampleCrawlNode = new CrawlNode();
        exampleCrawlNode.setHostId(hostId);
        Example<CrawlNode> example = Example.of(exampleCrawlNode);
        Page<CrawlNode> nodePage = crawlNodeRepository.findAll(example, pageable);
        if (nodePage.hasContent()) {
            crawlNodes = nodePage.getContent();
        }
        return crawlNodes;
    }

    public long queryAllCrawlNodeCount(Integer hostId) {
        CrawlNode exampleCrawlNode = new CrawlNode();
        exampleCrawlNode.setHostId(hostId);
        Example<CrawlNode> example = Example.of(exampleCrawlNode);
        return crawlNodeRepository.count(example);
    }


    public List<CrawlNode> queryNodesStatus(List<Integer> ids) {
        executorService.execute(() -> {
            List<CrawlNode> nodes = crawlNodeRepository.findAll();
            for (CrawlNode cloudNode : nodes) {
                String serverAddress = String.format("http://%s", cloudNode.getNodeIp());
                boolean isChanged = false;
                if (HttpUtil.get(serverAddress) != null) {
                    if (cloudNode.getNodeStatus() == 0) {
                        isChanged = true;
                    }
                } else {
                    if (cloudNode.getNodeStatus() == 1) {
                        isChanged = true;
                    }
                }
                if (isChanged) {//edit by pangq@2021/2/5  节点状态发生变化时才需要更新数据库，且更新前检查该节点是否仍在数据库里
                    cloudNode.setNodeStatus(cloudNode.getNodeStatus() == 0 ? 1 : 0);
                    Optional<CrawlNode> nodeTemp = crawlNodeRepository.findById(cloudNode.getId());
                    crawlNodeRepository.save(cloudNode);
                }
            }
            //crawlNodesRepository.saveAll(nodes);
        });

        return crawlNodeRepository.findAllById(ids);
    }

}
