package com.yihu.hos.standard.service.adapter;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.yihu.hos.core.datatype.CollectionUtil;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.standard.model.adapter.AdapterDatasetModel;
import com.yihu.hos.standard.model.adapter.AdapterMetadataModel;
import com.yihu.hos.standard.model.adapter.AdapterSchemeModel;
import com.yihu.hos.standard.model.adapter.resultModel.AdapterMetadataResultDetailModel;
import com.yihu.hos.standard.model.adapter.resultModel.ApiMetadataResultDetailModel;
import com.yihu.hos.standard.model.standard.StdDataSetModel;
import com.yihu.hos.standard.model.standard.StdMetaDataModel;
import com.yihu.hos.standard.service.bo.AdapterVersion;
import com.yihu.hos.standard.service.bo.StandardVersion;
import com.yihu.hos.web.framework.constant.ErrorCode;
import com.yihu.hos.web.framework.constant.SqlConstants;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.exception.ApiException;
import com.yihu.hos.web.framework.model.DetailModelResult;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.sql.BeanTransformer;
import com.yihu.hos.web.framework.util.sql.SqlCreator;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.*;
@Transactional
@Service("AdapterMetadataService")
public class AdapterMetadataService extends SQLGeneralDAO {

    public static final String BEAN_ID = "AdapterMetadataService";

    public AdapterMetadataService() {
    }

    public void finalize() throws Throwable {
        super.finalize();
    }

    public Result add(String adapterVersion, String metadata) throws Exception {
        try {
            AdapterVersion version = new AdapterVersion(adapterVersion);
            AdapterMetadataModel adapterMetadataModel = objectMapper.readValue(metadata, AdapterMetadataModel.class);
            Session session = getCurrentSession();
            String sql = "select max(id) from " + version.getMetaDataTableName();
            Query query = session.createSQLQuery(sql);
            Object object = query.uniqueResult();
            adapterMetadataModel.setId(object == null ? 1 : Integer.parseInt(object.toString()) + 1);

            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(adapterMetadataModel));
            SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
            sql = sqlCreator.insertData(version.getMetaDataTableName(), jsonNode);
            query = getExeuteQuery(sqlCreator, sql);
            query.executeUpdate();
            adapterMetadataModel.setMessage("保存适配数据元成功");
            return adapterMetadataModel;
        } catch (Exception e) {
            return Result.error("保存适配数据元失败");
        }
    }

    public void delete(String adapterVersion, Integer metadataId) throws Exception {
        try {
            if (adapterVersion == null || adapterVersion.length() == 0) {
                throw new ApiException(ErrorCode.InvalidStdVersion);
            }
            AdapterVersion version = new AdapterVersion(adapterVersion);
            Session session = getCurrentSession();
            String sql = "delete from " + version.getMetaDataTableName() + " where id = :id";
            Query query = session.createSQLQuery(sql);
            query.setInteger("id", metadataId);
            query.executeUpdate();
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SaveMetaDataFailed);
        }
    }

    public Result modify(String adapterVersion, String metadata) throws Exception {
        try {
            AdapterVersion version = new AdapterVersion(adapterVersion);
            AdapterMetadataModel adapterMetadataModel = objectMapper.readValue(metadata, AdapterMetadataModel.class);
            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(adapterMetadataModel));

            SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
            String sql = sqlCreator.updateDataByTableKey(version.getMetaDataTableName(), jsonNode);
            Query query = getExeuteQuery(sqlCreator, sql);
            query.executeUpdate();
            adapterMetadataModel.setSuccessFlg(true);
            adapterMetadataModel.setMessage("修改适配数据元成功");
            return adapterMetadataModel;
        } catch (Exception e) {
            return Result.error("修改适配数据元失败");
        }
    }

    public Result get(String version, Integer dictEntryId) {
        try {
            AdapterVersion adapterVersion = new AdapterVersion(version);
            AdapterMetadataModel adapterMetadataModel = (AdapterMetadataModel) get(AdapterMetadataModel.class, adapterVersion.getDictTableName(), dictEntryId);
            adapterMetadataModel.setSuccessFlg(true);
            adapterMetadataModel.setMessage("获取适配数据元成功");
            return adapterMetadataModel;
        } catch (Exception e) {
            return Result.error("获取适配数据元失败");
        }
    }

    public DetailModelResult getDetailResult(String adapterVersion, String condition, String order, Integer rows, Integer page) {
        try {
            List dictEntryList = getMetadataList(AdapterMetadataModel.class, adapterVersion, condition, order, rows, page);
            Integer count = getMetadataCount(AdapterMetadataModel.class, adapterVersion, condition);
            DetailModelResult result = DetailModelResult.success("获取适配数据元列表成功");
            result.setDetailModelList(dictEntryList);
            result.setTotalCount(count);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return DetailModelResult.error("获取适配数据元列表失败");
        }
    }

    public List getMetadataList(Class tClass, String adapterVersion, String condition, String order, Integer limit, Integer offset) throws Exception {
        AdapterVersion version = new AdapterVersion(adapterVersion);
        String tableName = version.getMetaDataTableName();
        SqlCreator sqlCreator = new SqlCreator(tClass);

        if (!StringUtil.isEmpty(condition)) {
            try {
                JsonNode jsonNode = objectMapper.readTree(condition);
                String name = jsonNode.get("name").asText();
                sqlCreator.equalCondition("stdDatasetId", jsonNode.get("stdDatasetId").asInt());
                sqlCreator.likeOrCondition("stdMetadataCode", "stdMetadataName", name);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            if (!StringUtil.isEmpty(order)) {
                JsonNode jsonNode = objectMapper.readTree(order);
                Iterator<String> fieldNames = jsonNode.fieldNames();
                while (fieldNames.hasNext()) {
                    String fieldName = fieldNames.next();
                    String value = jsonNode.get(fieldName).asText().toUpperCase();
                    sqlCreator.order(fieldName, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String sql = sqlCreator.selectData(tableName);
        Query query = getQuery(sqlCreator, sql);
        if (limit != null) {
            query.setMaxResults(limit);
            if (offset != null) {
                query.setFirstResult((offset - 1) * limit);
            }
        }
        List<AdapterMetadataModel> modelList = query.list();

        List<Integer> idList = new ArrayList<>();
        Map<Integer, StdDataSetModel> stdDatasetModelMap = new HashMap<>();
        Integer schemeId = null;
        for (AdapterMetadataModel model : modelList) {
            if (model.getAdapterDatasetId() != null) {
                schemeId = model.getSchemeId();
                idList.add(model.getAdapterDatasetId());
            }
        }

        if (!CollectionUtil.isEmpty(idList)) {
            AdapterSchemeModel scheme = getEntity(AdapterSchemeModel.class, schemeId);
            StandardVersion standardVersion = new StandardVersion(scheme.getAdapterStdVersion());
            tableName = standardVersion.getDataSetTableName();
            sqlCreator = new SqlCreator(StdDataSetModel.class);
            sqlCreator.inCondition("id", idList);
            sql = sqlCreator.selectData(tableName);
            query = getQuery(sqlCreator, sql);
            List<StdDataSetModel> stdDatasetModelList = query.list();
            for (StdDataSetModel model : stdDatasetModelList) {
                stdDatasetModelMap.put(model.getId(), model);
            }
        }

        List<ApiMetadataResultDetailModel> detailModelList = new ArrayList<>();
        for (AdapterMetadataModel model : modelList) {
            ApiMetadataResultDetailModel detailModel = new ApiMetadataResultDetailModel();
            detailModel.setId(model.getId());
            detailModel.setSchemeId(model.getSchemeId());
            detailModel.setAdapterDatasetId(model.getAdapterDatasetId());
            detailModel.setAdapterDictId(model.getAdapterDictId());
            detailModel.setAdapterDataType(model.getAdapterDataType());
            detailModel.setAdapterMetadataId(model.getAdapterMetadataId());
            detailModel.setAdapterMetadataCode(model.getAdapterMetadataCode());
            detailModel.setAdapterMetadataName(model.getAdapterMetadataName());
            detailModel.setAdapterDictId(model.getAdapterDictId());
            detailModel.setStdDictId(model.getStdDictId());
            detailModel.setStdDatasetId(model.getStdDatasetId());
            detailModel.setStdMetadataId(model.getStdMetadataId());
            detailModel.setStdMetadataCode(model.getStdMetadataCode());
            detailModel.setStdMetadataName(model.getStdMetadataName());
            detailModel.setAdapterInfo(model.getAdapterInfo());
            if (stdDatasetModelMap.get(model.getAdapterDatasetId()) != null) {
                detailModel.setAdapterDatasetCode(stdDatasetModelMap.get(model.getAdapterDatasetId()).getCode());
                detailModel.setAdapterDatasetName(stdDatasetModelMap.get(model.getAdapterDatasetId()).getName());
            }
            detailModelList.add(detailModel);
        }
        return detailModelList;
    }

    public Integer getMetadataCount(Class tClass, String adapterVersion, String condition) throws Exception {
        AdapterVersion version = new AdapterVersion(adapterVersion);
        String tableName = version.getMetaDataTableName();
        SqlCreator sqlCreator = new SqlCreator(tClass);
        if (!StringUtil.isEmpty(condition)) {
            try {
                JsonNode jsonNode = objectMapper.readTree(condition);
                String name = jsonNode.get("name").asText();
                sqlCreator.equalCondition("stdDatasetId", jsonNode.get("stdDatasetId").asInt());
                sqlCreator.likeOrCondition("stdMetadataCode", "stdMetadataName", name);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        String sql = sqlCreator.countData(tableName);
        Query query = getCurrentSession().createSQLQuery(sql);
        for (String key : sqlCreator.getKeyValueMap().keySet()) {
            query.setParameter(key, sqlCreator.getKeyValueMap().get(key));
        }
        Integer count = Integer.parseInt(StringUtil.toString(query.list().get(0)));
        return count;
    }

    public List getInfoList(String adapterVersion, String condition, String order, Integer limit, Integer offset) throws Exception{
        AdapterVersion version = new AdapterVersion(adapterVersion);        List<AdapterMetadataModel> metadataModelList = getList(AdapterMetadataModel.class, version.getMetaDataTableName(), condition, order, limit, offset);
        List<Integer> idList = new ArrayList<>();
        Map<Integer, StdMetaDataModel> stdMetaDataModelMap = new HashMap<>();
        Integer schemeId = null;
        for (AdapterMetadataModel model : metadataModelList) {
            if (model.getAdapterMetadataId() != null) {
                idList.add(model.getAdapterMetadataId());
                schemeId = model.getSchemeId();
            }
        }

        if (!CollectionUtil.isEmpty(idList)) {
            AdapterSchemeModel scheme = getEntity(AdapterSchemeModel.class, schemeId);
            StandardVersion standardVersion = new StandardVersion(scheme.getAdapterStdVersion());
            String tableName = standardVersion.getMetaDataTableName();
            SqlCreator sqlCreator = new SqlCreator(StdMetaDataModel.class);
            sqlCreator.inCondition("id", idList);
            String sql = sqlCreator.selectData(tableName);
            Query query = getQuery(sqlCreator, sql);
            List<StdMetaDataModel> stdMetadataModelList = query.list();
            for (StdMetaDataModel model : stdMetadataModelList) {
                stdMetaDataModelMap.put(model.getId(), model);
            }
        }

        List<AdapterMetadataResultDetailModel> detailModelList = new ArrayList<>();
        for (AdapterMetadataModel model : metadataModelList) {
            if (StringUtils.isBlank(model.getAdapterMetadataCode()) || StringUtils.isBlank(model.getAdapterMetadataName())) {
                continue;
            }
            AdapterMetadataResultDetailModel detailModel = new AdapterMetadataResultDetailModel();
            detailModel.setId(model.getId());
            detailModel.setSchemeId(model.getSchemeId());
            detailModel.setAdapterDatasetId(model.getAdapterDatasetId());
            detailModel.setAdapterDictId(model.getAdapterDictId());
            detailModel.setAdapterDataType(model.getAdapterDataType());
            detailModel.setAdapterMetadataId(model.getAdapterMetadataId());
            detailModel.setAdapterMetadataCode(model.getAdapterMetadataCode());
            detailModel.setAdapterMetadataName(model.getAdapterMetadataName());
            detailModel.setAdapterDictId(model.getAdapterDictId());
            detailModel.setStdDictId(model.getStdDictId());
            detailModel.setStdDatasetId(model.getStdDatasetId());
            detailModel.setStdMetadataCode(model.getStdMetadataCode());
            detailModel.setStdMetadataName(model.getStdMetadataName());
            if (stdMetaDataModelMap.get(model.getAdapterMetadataId()) != null) {
                detailModel.setAdapterMetadataType(stdMetaDataModelMap.get(model.getAdapterMetadataId()).getColumnType());
            }
            detailModelList.add(detailModel);
        }
        return detailModelList;
    }

    public DetailModelResult updateMetadatas(String adapterVersion, String list) {
        try {
            AdapterVersion version = new AdapterVersion(adapterVersion);
            List<ApiMetadataResultDetailModel> adapterMetadataList = Arrays.asList(objectMapper.readValue(list, ApiMetadataResultDetailModel[].class));
            List<String> sqlList = new ArrayList<>();
            List<Integer> idList = new ArrayList<>();
            Map<Integer, ApiMetadataResultDetailModel> metadataResultMap = new HashMap<>();
            for (ApiMetadataResultDetailModel metadataModel : adapterMetadataList) {
                AdapterMetadataModel adapterMetadataModel = new AdapterMetadataModel();
                adapterMetadataModel.setId(metadataModel.getId());
                adapterMetadataModel.setSchemeId(metadataModel.getSchemeId());
                adapterMetadataModel.setAdapterDatasetId(metadataModel.getAdapterDatasetId());
                adapterMetadataModel.setAdapterDictId(metadataModel.getAdapterDictId());
                adapterMetadataModel.setAdapterDataType(metadataModel.getAdapterDataType());
                adapterMetadataModel.setAdapterMetadataId(metadataModel.getAdapterMetadataId());
                adapterMetadataModel.setAdapterMetadataCode(metadataModel.getAdapterMetadataCode());
                adapterMetadataModel.setAdapterMetadataName(metadataModel.getAdapterMetadataName());
                adapterMetadataModel.setAdapterDictId(metadataModel.getAdapterDictId());
                adapterMetadataModel.setStdDictId(metadataModel.getStdDictId());
                adapterMetadataModel.setStdMetadataId(metadataModel.getStdMetadataId());
                adapterMetadataModel.setStdDatasetId(metadataModel.getStdDatasetId());
                adapterMetadataModel.setStdMetadataCode(metadataModel.getStdMetadataCode());
                adapterMetadataModel.setStdMetadataName(metadataModel.getStdMetadataName());
                adapterMetadataModel.setAdapterInfo(metadataModel.getAdapterInfo());
                idList.add(metadataModel.getStdDatasetId());
                if (!StringUtil.isEmpty(metadataModel.getAdapterDatasetCode())){
                    metadataResultMap.put(metadataModel.getStdDatasetId(), metadataModel);
                }
                JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(adapterMetadataModel));
                SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
                String sql = sqlCreator.updateDataByTableKey(version.getMetaDataTableName(), jsonNode);
                sqlList.add(sql);
                Query query = getExeuteQuery(sqlCreator, sql);
                query.executeUpdate();
            }
            if (!CollectionUtil.isEmpty(idList)) {
                String tableName = version.getDataSetTableName();
                SqlCreator sqlCreator = new SqlCreator(AdapterDatasetModel.class);
                sqlCreator.inCondition("stdDatasetId", idList);
                String sql = sqlCreator.selectData(tableName);
                Query query = getQuery(sqlCreator, sql);
                List<AdapterDatasetModel> adapterDatasetModelList = query.list();
                for (AdapterDatasetModel model : adapterDatasetModelList) {
                    ApiMetadataResultDetailModel metadataModel = metadataResultMap.get(model.getStdDatasetId());
                    model.setAdapterDatasetId(metadataModel.getAdapterDatasetId());
                    model.setAdapterDatasetCode(metadataModel.getAdapterDatasetCode());
                    model.setAdapterDatasetName(metadataModel.getAdapterDatasetName());
                    JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(model));
                    sqlCreator = new SqlCreator(AdapterDatasetModel.class);
                    sql = sqlCreator.updateDataByTableKey(version.getDataSetTableName(), jsonNode);
                    sqlList.add(sql);
                    query = getExeuteQuery(sqlCreator, sql);
                    query.executeUpdate();
                }
//                 insertBatch( sqlList);
            }
            DetailModelResult detailModelResult = DetailModelResult.success("修改适配数据元成功");
            detailModelResult.setDetailModelList(adapterMetadataList);
            return detailModelResult;
        } catch (Exception e) {
            return DetailModelResult.error("修改适配数据元失败");
        }
    }

    public Result deleteMultiple(String adapterVersion, Integer metadataId, Integer stdDatasetId, Integer adapterDatasetId) {
        try {
            if (adapterVersion == null || adapterVersion.length() == 0) {
                return Result.error("无效适配版本");
            }
            AdapterVersion version = new AdapterVersion(adapterVersion);
            Session session = getCurrentSession();

            String sql = "delete from " + version.getMetaDataTableName() + " where id = :id";
            Query query = session.createSQLQuery(sql);
            query.setInteger("id", metadataId);
            query.executeUpdate();

            SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
            sqlCreator.equalCondition("stdDatasetId", stdDatasetId);
            sqlCreator.equalCondition("adapterDatasetId", adapterDatasetId);
            sql = sqlCreator.countData(version.getMetaDataTableName());
            query = getExeuteQuery(sqlCreator, sql);
            Integer count = Integer.parseInt(StringUtil.toString(query.list().get(0)));
            if (count == 0) {
                sql = "delete from " + version.getDataSetTableName() + " where std_dataset_id = :stdDatasetId and adapter_dataset_id = :adapterDatasetId";
                query = session.createSQLQuery(sql);
                query.setInteger("stdDatasetId", stdDatasetId);
                query.setInteger("adapterDatasetId", adapterDatasetId);
                query.executeUpdate();
            }
            return Result.success("删除适配数据元成功");
        } catch (Exception e) {
            return Result.error("删除适配数据元失败");
        }
    }

    public List getAdapterMetadataByDataset(String adapterVersion,Integer datasetId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("stdDatasetId", datasetId);
        String tableName="adapter_metadata_"+adapterVersion;
        try {
            String condition =objectMapper.writeValueAsString(map);
            List<AdapterMetadataModel> metadataModelList = getList(AdapterMetadataModel.class,tableName, condition, null, null, null);
            return metadataModelList;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;

    }

    public AdapterDatasetModel getAdapterDataset(String adapterVersion,Integer datasetId){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("stdDatasetId", datasetId);
        String tableName="adapter_dataset_"+adapterVersion;
        String condition = null;
        try {
            condition = objectMapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        List<AdapterDatasetModel> datasetModel = getList(AdapterDatasetModel.class,tableName, condition, null, null, null);
        return datasetModel.get(0);
    }
    public AdapterMetadataModel getAdapterMetadata(String version, Integer adapterMetadataId) {
        try {
            SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
            sqlCreator.equalCondition("id", adapterMetadataId);
            String sql = sqlCreator.selectData("adapter_metadata_"+version);
            Query query = getQuery(sqlCreator, sql);
            return (AdapterMetadataModel)query.uniqueResult();
        } catch (Exception e) {
            return null;
        }
    }

    public List getAdapterMetadataNotNullList(AdapterVersion adapterVersion, Integer stdDatasetId, String condition) {
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM ").append(adapterVersion.getMetaDataTableName());
            if (!StringUtil.isEmpty(condition)) {
                JsonNode jsonNode = objectMapper.readTree(condition);
                String column = jsonNode.get("column").asText();
                sql.append(" WHERE std_dataset_id = " + stdDatasetId + SqlConstants.AND);
                sql.append(column).append(" IS NOT NULL");
            }
            Query query = getCurrentSession().createSQLQuery(sql.toString());
            query.setResultTransformer(new BeanTransformer(AdapterMetadataModel.class));
            return query.list();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<AdapterMetadataModel> getAllAdaptMetadata(String adapter_version){
        AdapterVersion adapterVersion = new AdapterVersion(adapter_version);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.*").append(" FROM "+adapterVersion.getMetaDataTableName()).append(" m where m.adapter_metadata_id is not null");
        Query query = getCurrentSession().createSQLQuery(sql.toString());
        query.setResultTransformer(new BeanTransformer(AdapterMetadataModel.class));
        List<AdapterMetadataModel> list = query.list();
        return list;
    }

    public List<AdapterMetadataModel> getAllAdaptMetadata(String schemaName,String adapter_version){
        AdapterVersion adapterVersion = new AdapterVersion(adapter_version);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.*").append(" FROM "+adapterVersion.getMetaDataTableName()).append(" m where m.adapter_metadata_id is not null");

        String myCatAnnotation = "/*#mycat\\:schema=" + schemaName + "*/ ";
        String completeSql = myCatAnnotation + sql;

        Query query = getCurrentSession().createSQLQuery(completeSql);
        query.setResultTransformer(new BeanTransformer(AdapterMetadataModel.class));
        List<AdapterMetadataModel> list = query.list();
        return list;
    }

    public List<AdapterMetadataModel> getAllUnAdaptMetadata(String version){
        AdapterVersion adapterVersion = new AdapterVersion(version);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.*").append(" FROM "+adapterVersion.getMetaDataTableName()).append(" m where m.adapter_metadata_id is null");
        Query query = getCurrentSession().createSQLQuery(sql.toString());
        query.setResultTransformer(new BeanTransformer(AdapterMetadataModel.class));
        List<AdapterMetadataModel> list = query.list();
        return list;
    }

    public List<AdapterMetadataModel> getAllUnAdaptMetadata(String schemaName,String version){
        AdapterVersion adapterVersion = new AdapterVersion(version);
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT m.*").append(" FROM "+adapterVersion.getMetaDataTableName()).append(" m where m.adapter_metadata_id is null");

        String myCatAnnotation = "/*#mycat\\:schema=" + schemaName + "*/ ";
        String completeSql = myCatAnnotation + sql;


        Query query1 = getCurrentSession().createSQLQuery(sql.toString());
        query1.setResultTransformer(new BeanTransformer(AdapterMetadataModel.class));
        List<AdapterMetadataModel> list1 = query1.list();


        Query query = getCurrentSession().createSQLQuery(completeSql);
        query.setResultTransformer(new BeanTransformer(AdapterMetadataModel.class));
        List<AdapterMetadataModel> list = query.list();
        return list;
    }


    public void saveAdapt(AdapterMetadataModel strategyMetadata,StdMetaDataModel orgMetadata,String version,String adapterInfo) throws Exception {
        strategyMetadata.setAdapterDatasetId(orgMetadata.getDatasetId());
        strategyMetadata.setAdapterMetadataId(orgMetadata.getId());
        strategyMetadata.setAdapterMetadataCode(orgMetadata.getCode());
        strategyMetadata.setAdapterMetadataName(orgMetadata.getName());
        strategyMetadata.setAdapterInfo(adapterInfo);
        JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(strategyMetadata));
        SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
        String sql = sqlCreator.updateDataByTableKey(new AdapterVersion(version).getMetaDataTableName(), jsonNode);
        Query query = getExeuteQuery(sqlCreator, sql);
        query.executeUpdate();
    }

    public void saveAdapt(String schemaName,AdapterMetadataModel strategyMetadata,StdMetaDataModel orgMetadata,String version,String adapterInfo) throws Exception {
        strategyMetadata.setAdapterDatasetId(orgMetadata.getDatasetId());
        strategyMetadata.setAdapterMetadataId(orgMetadata.getId());
        strategyMetadata.setAdapterMetadataCode(orgMetadata.getCode());
        strategyMetadata.setAdapterMetadataName(orgMetadata.getName());
        strategyMetadata.setAdapterInfo(adapterInfo);
        JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(strategyMetadata));
        SqlCreator sqlCreator = new SqlCreator(AdapterMetadataModel.class);
        String sql = sqlCreator.updateDataByTableKey(new AdapterVersion(version).getMetaDataTableName(), jsonNode);

        String myCatAnnotation = "/*#mycat\\:schema=" + schemaName + "*/ ";
        String completeSql = myCatAnnotation + sql;

        Query query = getExeuteQuery(sqlCreator, completeSql);
        query.executeUpdate();
    }

    public String saveAdaptSql(AdapterMetadataModel strategyMetadata,StdMetaDataModel orgMetadata,String version,String adapterInfo) throws Exception {
        StringBuilder sqlBuffer = new StringBuilder();
        if(null != orgMetadata){
            sqlBuffer.append(SqlConstants.UPDATE + new AdapterVersion(version).getMetaDataTableName() + SqlConstants.SET);
            sqlBuffer.append(" adapter_dataset_id = " + orgMetadata.getDatasetId());
            sqlBuffer.append(",adapter_metadata_id = " + orgMetadata.getId());
            sqlBuffer.append(",adapter_metadata_code = '" + orgMetadata.getCode() + "'");
            sqlBuffer.append(",adapter_metadata_name = '" + orgMetadata.getName() + "'");
            if(null != adapterInfo){
                sqlBuffer.append(",adapter_info = " + adapterInfo);
            }
            sqlBuffer.append(" where id=" + strategyMetadata.getId());
        }
        return sqlBuffer.toString();
    }

    public void insertBatch(final List<String> insertSqlList) {
        super.insertBatch(insertSqlList);
    }
}