package com.jsc.oscm.controller;

import com.alibaba.fastjson.JSON;
import com.jsc.oscm.entity.CrawlNode;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.CrawlNodeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class CrawlNodeController {

    @Resource
    CrawlNodeService crawlNodeService;

    @PostMapping("/crawlnode")
    public String addCrawlNode(@RequestBody CrawlNode crawlNode) {
        ReturnT<String> returnT;
        if (crawlNodeService.addCrawlNodes(crawlNode)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "采集节点添加失败", "");
        }
        return JSON.toJSONString(returnT);
    }

    @DeleteMapping("/crawlnode")
    public String delCrawlNode(@RequestBody List<Integer> crawlNodeIds) {
        ReturnT<String> returnT;
        if (crawlNodeService.delCrawlNodes(crawlNodeIds)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "删除失败！", "");
        }
        return JSON.toJSONString(returnT);
    }

    @PutMapping("/crawlnode")
    public String updateNode(@RequestBody CrawlNode crawlNode) {
        ReturnT<String> returnT;
        if (crawlNodeService.updateCrawlNodes(crawlNode)) {
            returnT = new ReturnT<>(200, "", "");
        } else {
            returnT = new ReturnT<>(500, "采集节点更新失败", "");
        }
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/crawlnode")
    public String queryAllNodes() {
        ReturnT<List<CrawlNode>> returnT;
        List<CrawlNode> nodes = crawlNodeService.queryAllCrawlNodes();
        returnT = new ReturnT<>(200, "", nodes);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/crawlnode/{page}/{size}/{hostid}")
    public String queryAllNodePageable(@PathVariable(value = "page") int page, @PathVariable(value = "size") int size, @PathVariable(value = "hostid") Integer hostId) {
        ReturnT<List<CrawlNode>> returnT;
        List<CrawlNode> nodes = crawlNodeService.queryAllCrawlNodePageable(page, size, hostId);
        returnT = new ReturnT<>(200, "", nodes);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/crawlnode/count/{hostid}")
    public String queryAllNodeCount(@PathVariable(value = "hostid") Integer hostId) {
        ReturnT<Long> returnT;
        Long count = crawlNodeService.queryAllCrawlNodeCount(hostId);
        returnT = new ReturnT<>(200, "", count);
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/node/status")
    public String getNodeStatus(@RequestBody List<Integer> ids) {
        ReturnT<List<CrawlNode>> returnT;
        List<CrawlNode> nodes = crawlNodeService.queryNodesStatus(ids);
        if (nodes != null) {
            returnT = new ReturnT<>(200, "", nodes);
        } else {
            returnT = new ReturnT<>(500, "", null);
        }
        return JSON.toJSONString(returnT);
    }

}
