/*package com.wx.application.nebula.graph.service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.wx.application.adapter.dto.qo.GraphTaskExecuteQ;
import com.wx.application.adapter.dto.qo.GraphTaskFileExecuteQ;
import com.wx.application.core.entity.GraphTask;
import com.wx.application.core.entity.GraphTaskFile;
import com.wx.application.core.service.GraphTaskFileService;
import com.wx.application.core.service.GraphTaskService;
import com.wx.application.nebula.graph.bean.NebulaEdgeLine;
import com.wx.application.nebula.graph.bean.NebulaVertex;
import com.wx.application.nebula.graph.enums.DataType;
import com.wx.application.nebula.graph.enums.IndexType;
import com.wx.application.util.MD5Util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import lombok.extern.slf4j.Slf4j;

*//**
 * 图谱数据导入
 *//*
@Slf4j
@Service
public class ImportGraphInExcelService {

    public final static String YYYYMMDD = "yyyy-MM-dd";

    @Value("${upload.graph.path}")
    private String graphFilePath;

    @Autowired
    GraphTaskService graphTaskService;

    @Autowired
    GraphTaskFileService graphTaskFileService;

    @Autowired
    NebulaOperateService nebulaOperateService;

    *//**
     * 上传单个文件
     *//*
    public GraphTask createTask(MultipartFile multipartFile,
                                String space, String name) {
        GraphTask graphTask = null;
        try {

            String tempDir = DateUtil.format(new Date(), YYYYMMDD) + File.separator;
            String mkdir = graphFilePath + tempDir;
            if (!FileUtil.exist(mkdir)) {
                FileUtil.mkdir(mkdir);
            }

            String originalFilename = multipartFile.getOriginalFilename();

            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            originalFilename = originalFilename.replaceAll(suffix, "");

            String fileName = MD5Util.getMD5(multipartFile.getInputStream()) + suffix;
            String path = mkdir + fileName;
            path = path.replaceAll("\\\\", "/");

            FileUtil.writeBytes(multipartFile.getBytes(), path);
            String relativePath = tempDir + fileName;

            ExcelReader reader = ExcelUtil.getReader(path);
            List<Object> headerRow = reader.readRow(0);

            graphTask = new GraphTask();
            graphTask.setName(name);
            graphTask.setSpace(space);
            graphTaskService.create(graphTask);

            GraphTaskFile taskFile = new GraphTaskFile();
            taskFile.setTaskId(graphTask.getId());
            taskFile.setOriginalFilename(originalFilename);
            taskFile.setSuffix(suffix);
            taskFile.setPath(relativePath);
            taskFile.setHeaderRow(JSONUtil.toJsonStr(headerRow));

            graphTaskFileService.create(taskFile);


        } catch (Exception e) {
            log.info("{}", e);
        }

        return graphTask;
    }

    *//**
     * 分析任务模型
     *//*
    public GraphTask findTaskById(Long taskId) {


        GraphTask graphTask = graphTaskService.getOne(taskId);

        Map fQ = new HashMap<>();
        fQ.put("EQL_taskId", taskId);
        List<GraphTaskFile> tfs = graphTaskFileService.queryList(fQ);

        *//**
         * 查询所有的tag
         *//*
        List<JSONObject> tags = nebulaOperateService.showTag(graphTask.getSpace());


        *//**
         * 查询所有的edge
         *//*
        List<JSONObject> edges = nebulaOperateService.showEdge(graphTask.getSpace());

        Map<String, JSONObject> tagMap = new HashMap<>();

        for (JSONObject tg : tags) {
            String tag = tg.getStr("Name");
            List<JSONObject> fields = nebulaOperateService.descTag(graphTask.getSpace(), tag);
            fields.add(0, createField("vid", DataType.STRING, "唯一id"));
            tg.set("fields", fields);
            tagMap.put(tag, tg);
        }

        Map<String, JSONObject> edgeMap = new HashMap<>();
        for (JSONObject eg : edges) {
            String edge = eg.getStr("Name");
            List<JSONObject> fields = nebulaOperateService.descTag(graphTask.getSpace(), edge);
            fields.add(0, createField("srcvid", DataType.STRING, "开始id"));
            fields.add(1, createField("dstvid", DataType.STRING, "结束id"));
            eg.set("fields", fields);
            edgeMap.put(edge, eg);
        }


        for (GraphTaskFile fn : tfs) {
            String originalFilename = fn.getOriginalFilename();
            if (tagMap.containsKey(originalFilename)) {
                fn.setType(IndexType.TAG);
            }

            if (edgeMap.containsKey(originalFilename)) {
                fn.setType(IndexType.EDGE);
            }

            if (fn.getType() != null) {
                fn.setTypename(originalFilename);
            }

 
        }


        graphTask.setTags(tags);
        graphTask.setEdges(edges);
        graphTask.setTfs(tfs);

        return graphTask;
    }

    *//**
     * 读取文件执行导入
     *
     * @param gtQ
     *//*
    public void graphTaskExecute(GraphTaskExecuteQ gtQ) {

        GraphTask graphTask = graphTaskService.getOne(gtQ.getTaskId());

        Map fQ = new HashMap<>();
        fQ.put("EQL_taskId", gtQ.getTaskId());
        List<GraphTaskFile> tfs = graphTaskFileService.queryList(fQ);

        Map<Long, String> mvMap = tfs.stream().collect(Collectors.toMap(GraphTaskFile::getId, GraphTaskFile::getPath));

        List<GraphTaskFileExecuteQ> taskFiles = gtQ.getTaskFiles();

        for (GraphTaskFileExecuteQ fn : taskFiles) {
            final String relativePath = mvMap.get(fn.getTaskFileId());
            final String path = graphFilePath + relativePath;

            *//**
             * 执行excel读取
             *//*

            if (fn.getCorrespond() == null
                    || fn.getCorrespond().size() == 0) {
                continue;
            }

            if (IndexType.TAG.equals(fn.getType())) {

                if (!fn.getCorrespond().containsKey("vid")) {
                    continue;
                }

                ExcelUtil.readBySax(path, 0, analysisTag(graphTask.getSpace(), fn));
            }

            *//**
             * 执行读取
             *//*
            if (IndexType.EDGE.equals(fn.getType())) {

                if (!fn.getCorrespond().containsKey("srcvid")
                        || !fn.getCorrespond().containsKey("dstvid")) {
                    continue;
                }

                ExcelUtil.readBySax(path, 0, analysisEdge(graphTask.getSpace(), fn));
            }
        }
    }


    private RowHandler analysisTag(String space, GraphTaskFileExecuteQ taskFileQ) {

        JSONObject correspond = taskFileQ.getCorrespond();

        List<String> columns = new ArrayList<>(correspond.keySet());
        List<String> header = new ArrayList<>();
        List<NebulaVertex> vts = new ArrayList<>();

        RowHandler rowHandler = new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {

                if (rowIndex > 0) {

                    JSONObject ob = new JSONObject();

                    for (String feild : correspond.keySet()) {

                        *//**
                         * 获得属性对应的文件字段
                         *//*
                        String fh = correspond.getStr(feild);

                        if (StringUtils.isBlank(fh)) {
                            continue;
                        }

                        int i = header.indexOf(fh);
                        if (i < 0) {
                            continue;
                        }
                        Object value = rowlist.get(i);
                        ob.set(feild, value);
                    }

                    NebulaVertex e = new NebulaVertex();
                    e.setOb(ob);
                    vts.add(e);

                    if (vts.size() >= 500) {
                        nebulaOperateService.insertTagAll(space, taskFileQ.getName(), columns, vts);
                        vts.clear();
                    }

                } else {
                    for (Object h : rowlist) {
                        header.add(h.toString());
                    }
                }
            }
        };

        if (vts.size() >= 0) {
            nebulaOperateService.insertTagAll(space, taskFileQ.getName(), columns, vts);
            vts.clear();

            GraphTaskFile file = new GraphTaskFile();
            file.setId(taskFileQ.getTaskFileId());
            file.setOver(true);
            graphTaskFileService.modify(file);
        }

        return rowHandler;
    }


    private RowHandler analysisEdge(String space, GraphTaskFileExecuteQ taskFileQ) {

        JSONObject correspond = taskFileQ.getCorrespond();

        List<String> columns = new ArrayList<>(correspond.keySet());
        List<String> header = new ArrayList<>();
        List<NebulaEdgeLine> vts = new ArrayList<>();

        RowHandler rowHandler = new RowHandler() {
            @Override
            public void handle(int sheetIndex, long rowIndex, List<Object> rowlist) {

                if (rowIndex > 0) {

                    JSONObject ob = new JSONObject();

                    for (String feild : correspond.keySet()) {

                        *//**
                         * 获得属性对应的文件字段
                         *//*
                        String fh = correspond.getStr(feild);

                        if (StringUtils.isBlank(fh)) {
                            continue;
                        }

                        int i = header.indexOf(fh);
                        if (i < 0) {
                            continue;
                        }
                        Object value = rowlist.get(i);
                        ob.set(feild, value);
                    }

                    NebulaEdgeLine e = new NebulaEdgeLine();
                    e.setOb(ob);
                    vts.add(e);

                    if (vts.size() >= 500) {
                        nebulaOperateService.insertEdgeLineAll(space, taskFileQ.getName(), columns, vts);
                        vts.clear();
                    }
                } else {
                    for (Object h : rowlist) {
                        header.add(h.toString());
                    }
                }
            }
        };

        if (vts.size() >= 0) {
            nebulaOperateService.insertEdgeLineAll(space, taskFileQ.getName(), columns, vts);
            vts.clear();
            GraphTaskFile file = new GraphTaskFile();
            file.setId(taskFileQ.getTaskFileId());
            file.setOver(true);
            graphTaskFileService.modify(file);
        }

        return rowHandler;
    }


    *//**
     * 获得字段属性
     *//*
    private static JSONObject createField(String name, DataType type, String comment) {
        JSONObject ot = new JSONObject();
        ot.set("field", name);
        ot.set("type", type);
        ot.set("comment", comment);
        return ot;
    }
    
}
*/