package com.ebts.generator.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ebts.common.utils.ServerResult;
import com.ebts.common.utils.StringUtils;
import com.ebts.generator.dao.QueryDao;
import com.ebts.generator.dao.RelationalMapDao;
import com.ebts.generator.dao.TopSearchTableDao;
import com.ebts.generator.entity.RelationalMap;
import com.ebts.generator.service.TopQueryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Clay
 * @date: 2021/7/23 21:51
 * @description：TopQueryServiceImpl
 * @email: clay@hchyun.com
 */
@Service
public class TopQueryServiceImpl implements TopQueryService {
    private final Logger logger = LoggerFactory.getLogger(TopQueryServiceImpl.class);

    @Autowired
    private TopSearchTableDao topSearchTableDao;
    @Autowired
    private RelationalMapDao relationalMapDao;
    @Autowired
    private QueryDao queryDao;

    @Override
    public Map<String, Object> selectTopSearchTables() {
        Map<String, Object> modeMap = new HashMap<>();
        modeMap.put("tables", topSearchTableDao.selectTopSearchTables());
        modeMap.put("relationalMap", relationalMapDao.selectRelationalMapList(new RelationalMap()));
        return modeMap;
    }

    @Override
    public ServerResult<List<Map<String, Object>>> preview(String jsonData) {
        try {

            JSONObject graphData = JSON.parseObject(jsonData);
            JSONArray nodes = graphData.getJSONArray("nodes");
            JSONArray edges = graphData.getJSONArray("edges");
            JSONObject node = nodes.getJSONObject(0);

            StringBuffer sql = new StringBuffer("select ");

            JSONArray attrs = node.getJSONArray("attrs");
            boolean start = false;
            for (int i = 0; i < attrs.size(); i++) {
                JSONObject column = attrs.getJSONObject(i);
                if (column.getInteger("isUse") == 1) {
                    if (start) {
                        sql.append(", ");
                    }
                    sql.append(column.getString("columnName"));
                    if (StringUtils.isNotNull(column.getString("columnComment"))){
                        sql.append(" as ");
                        sql.append("'" + column.getString("columnComment") + "'");
                    }
                    start=true;
                }
            }
            sql.append(" from " + node.getString("table"));
            System.out.println(sql.toString());
            List<Map<String, Object>> dataMap = queryDao.UniQuery(sql.toString());
            return new ServerResult<>(true, dataMap);
        } catch (RuntimeException e) {
            logger.error(e.getMessage());
            return new ServerResult<>(false, e.getMessage());
        }
    }


}
