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

import com.fasterxml.jackson.databind.JsonNode;
import com.yihu.hos.web.framework.dao.SQLGeneralDAO;
import com.yihu.hos.web.framework.constant.ErrorCode;
import com.yihu.hos.web.framework.constant.SqlConstants;
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.core.datatype.CollectionUtil;
import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.web.framework.util.sql.BeanTransformer;
import com.yihu.hos.web.framework.util.sql.RequestParamTransformer;
import com.yihu.hos.web.framework.util.sql.SqlCreator;
import com.yihu.hos.standard.model.Select2;
import com.yihu.hos.standard.model.standard.*;
import com.yihu.hos.standard.model.standard.resultModel.StandardParamResultModel;
import com.yihu.hos.standard.service.bo.StandardVersion;
import jxl.Sheet;
import jxl.Workbook;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 数据集管理器.
 *
 * @author Sand
 * @version 1.0
 * @created 30-6月-2015 16:19:05
 */
@Transactional
@Service("StdDatasetService")
public class StdDatasetService extends SQLGeneralDAO {

    public static final String BEAN_ID = "StdDatasetService";
    @Autowired
    StandardService standardService;

    public StdDatasetService() {
    }

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

    public List<StdDataSetModel> getList(String stdVersion, String condition, String order, Integer limit, Integer offset) {
        try {
            Map queryMap = RequestParamTransformer.parseJsonToMap(condition);
            Map orderMap = RequestParamTransformer.parseJsonToMap(order);
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            return this.getList(standardVersion, queryMap, orderMap, limit, offset);
        } catch (Exception e) {
            e.printStackTrace();
        }
        throw new ApiException(ErrorCode.StandardDatasetGetListFailed);
    }

    public Result add(String stdVersion, String dataset) {
        if (StringUtil.isEmpty(stdVersion)) {
            return Result.error("并无该标准版本");
        }
        Boolean existFlg = false;
        try {
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            StdDataSetModel stdDataSetModel = objectMapper.readValue(dataset, StdDataSetModel.class);
            existFlg = datasetCodeValidate(standardVersion, stdDataSetModel.getCode());
            if (!existFlg) {
                this.add(standardVersion.getDataSetTableName(), stdDataSetModel);
                stdDataSetModel.setSuccessFlg(true);
                stdDataSetModel.setMessage("保存数据集成功");
                return stdDataSetModel;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (existFlg) {
            return Result.error("已存在标准数据集编码");
        } else {
            return Result.error("保存数据集失败");
        }
    }

    public Boolean datasetCodeValidate(StandardVersion standardVersion, String code) {
        Map<String, Object> query = new HashMap<>();
        Map<String, String> order = new HashMap<>();
        query.put("code", code);

        List<StdDataSetModel> list = this.getList(standardVersion, query, order, 1, 1);
        if (!CollectionUtil.isEmpty(list)) {
            return true;
        }
        return false;
    }

    public Result delete(String stdVersion, Integer datasetId) {
        if (StringUtil.isEmpty(stdVersion)) {
            return Result.error("并无该标准版本");
        }
        try {
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            if (datasetId != null) {
                this.delete(standardVersion, datasetId);
            }
            return Result.success("删除数据集成功");
        } catch (Exception e) {
            return Result.success("删除数据集失败");
        }
    }

    public Boolean datasetCodeValidate(StandardVersion standardVersion, Integer datasetId,  String code) {
        SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
        sqlCreator.equalCondition("code", code);
        sqlCreator.neCondition("id", datasetId);
        String sql = sqlCreator.selectData(standardVersion.getDataSetTableName());
        Query sessionQuery = getQuery(sqlCreator, sql);
        List<StdDataSetModel> list = sessionQuery.list();
        if (!CollectionUtil.isEmpty(list)) {
            return true;
        }
        return false;
    }

    public Result update(String stdVersion, String dataset) {
        if (StringUtil.isEmpty(stdVersion)) {
            return Result.error("并无该标准版本");
        }
        Boolean existFlg = false;
        try {
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            StdDataSetModel stdDataSetModel = objectMapper.readValue(dataset, StdDataSetModel.class);
            existFlg = datasetCodeValidate(standardVersion, stdDataSetModel.getId(), stdDataSetModel.getCode());
            if (!existFlg) {
                this.update(standardVersion, stdDataSetModel);
                stdDataSetModel.setSuccessFlg(true);
                stdDataSetModel.setMessage("修改数据集成功");
                return stdDataSetModel;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (existFlg) {
            return Result.error("已存在标准数据集编码");
        } else {
            return Result.error("修改数据集失败");
        }
    }

    public Result get(String stdVersion, Integer datasetId) {
        if (StringUtil.isEmpty(stdVersion)) {
            return Result.error("并无该数据集信息");
        }

        try {
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            if (datasetId != null) {
                StdDataSetModel stdDataSetModel = this.get(standardVersion, datasetId);
                stdDataSetModel.setSuccessFlg(true);
                stdDataSetModel.setMessage("获取标准数据集成功");
                return stdDataSetModel;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return Result.error("获取标准数据集失败");
    }

    private StdDataSetModel get(StandardVersion standardVersion, Integer datasetId) {
        String dataSetTable = standardVersion.getDataSetTableName();
        SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
        sqlCreator.equalCondition("id", datasetId);
        String sql = sqlCreator.selectData(dataSetTable);
        Query query = getQuery(sqlCreator, sql);
        return (StdDataSetModel) query.uniqueResult();
    }

    private void update(StandardVersion standardVersion, StdDataSetModel stdDataSetModel) throws IOException {
        JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(stdDataSetModel));
        SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
        String dataSetTable = standardVersion.getDataSetTableName();
        String sql = sqlCreator.updateDataByTableKey(dataSetTable, jsonNode);
        Query query = getExeuteQuery(sqlCreator, sql);
        query.executeUpdate();
    }

    private void delete(StandardVersion standardVersion, Integer datasetId) {
        Session session = getCurrentSession();
        String sql = "delete from " + standardVersion.getDataSetTableName() + " where id = :id";
        Query query = session.createSQLQuery(sql);
        query.setInteger("id", datasetId);
        query.executeUpdate();
        sql = "delete from " + standardVersion.getMetaDataTableName() + " where dataset_id = :datasetId";
        query = session.createSQLQuery(sql);
        query.setInteger("datasetId", datasetId);
        query.executeUpdate();
    }

    public void add(String dataSetTable, StdDataSetModel stdDataSetModel) throws IOException {
        stdDataSetModel.setId(getMaxId(dataSetTable));

        JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(stdDataSetModel));
        SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
        String sql = sqlCreator.insertData(dataSetTable, jsonNode);
        Query query = getExeuteQuery(sqlCreator, sql);
        query.executeUpdate();
    }

    private List<StdDataSetModel> getList(StandardVersion standardVersion, Map<String, Object> query, Map<String, String> order, Integer limit, Integer offset) {
        SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
        for (String key : query.keySet()) {
            sqlCreator.equalCondition(key, query.get(key));
        }
        for (String key : order.keySet()) {
            sqlCreator.order(key, order.get(key));
        }

        String sql = sqlCreator.selectData(standardVersion.getDataSetTableName());
        Query sessionQuery = getQuery(sqlCreator, sql);
        if (limit != null) {
            sessionQuery.setMaxResults(limit);
            if (offset != null) {
                sessionQuery.setFirstResult((offset - 1) * limit);
            }
        }
        return (List<StdDataSetModel>) sessionQuery.list();
    }

    public DetailModelResult getDetailModelList(String stdVersion, String condition, String order, Integer rows, Integer page) {
        try {
            List<StdDataSetModel> dataSetModelList = getDatasetList(StdDataSetModel.class, stdVersion, condition, order, rows, page);
            List<StdDataSetModel> totalList = getDatasetList(StdDataSetModel.class, stdVersion, condition, order, null, null);
            Integer count = totalList.size();
//            Integer count = getDatasetInt(StdDataSetModel.class, stdVersion, condition);
            DetailModelResult result = DetailModelResult.success("获取标准数据集列表成功");
            result.setDetailModelList(dataSetModelList);
            result.setTotalCount(count);
            return result;
        } catch (Exception e) {
            return DetailModelResult.error("获取标准数据集列表失败");
        }
    }

    public List getDatasetList(Class tClass, String stdVersion, String condition, String order, Integer limit, Integer offset) throws IOException {
        StandardVersion standardVersion = new StandardVersion(stdVersion);
        String tableName = standardVersion.getDataSetTableName();
        SqlCreator sqlCreator = new SqlCreator(tClass);
        if (!StringUtil.isEmpty(condition)) {
            JsonNode jsonNode = objectMapper.readTree(condition);
            if(jsonNode.get("initVal")!=null && !jsonNode.get("initVal").asText().isEmpty()){
                String initName = jsonNode.get("initVal").asText();
                sqlCreator.neCondition("code",initName);
            }else if(jsonNode.get("name")!=null && !jsonNode.get("name").asText().isEmpty()){
                String name = jsonNode.get("name").asText();
                sqlCreator.likeOrCondition("code", "name", name);
            } else if (jsonNode.get("stdId") != null && !jsonNode.get("stdId").asText().isEmpty()) {
                String[] stdLs = jsonNode.get("stdId").asText().split(",");
                List<Integer> stdIdLs = new ArrayList<>();
                for (String s : stdLs) {
                    stdIdLs.add(Integer.valueOf(s));
                }
                sqlCreator.inCondition("id", stdIdLs);
            }
        }
        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);
            }
        }
        String sql = sqlCreator.selectData(tableName);
        Query query = getQuery(sqlCreator, sql);
        if (limit != null) {
            query.setMaxResults(limit);
            if (offset != null) {
                query.setFirstResult((offset - 1) * limit);
            }
        }
        return query.list();
    }

    public Integer getDatasetInt(Class tClass, String stdVersion, String condition) {
        try {
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            String tableName = standardVersion.getDataSetTableName();
            SqlCreator sqlCreator = new SqlCreator(tClass);
            if (!StringUtil.isEmpty(condition)) {
                JsonNode jsonNode = objectMapper.readTree(condition);
                String name = jsonNode.get("name").asText();
                sqlCreator.likeOrCondition("code", "name", name);
            }
            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;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ApiException(ErrorCode.GetDataSetListFailed);
        }
    }

    public DetailModelResult getDatasetSelectList(String stdVersion, String condition, String order, Integer limit, Integer offset) {
        try {
            List<StdDataSetModel> datasetList = getDatasetList(StdDataSetModel.class, stdVersion, condition, order, limit, offset);
            List<Select2> detailModelList = new ArrayList<>();
            for (StdDataSetModel stdDataSetModel : datasetList) {
                Select2 select2 = new Select2();
                select2.setId(stdDataSetModel.getId() + SqlConstants.COMMA + stdDataSetModel.getCode());
                select2.setText(stdDataSetModel.getName());
                detailModelList.add(select2);
            }
            DetailModelResult detailModelResult = DetailModelResult.success("获取数据集下拉列表成功");
            detailModelResult.setDetailModelList(detailModelList);
            detailModelResult.setTotalCount(detailModelList.size());
            return detailModelResult;
        } catch (Exception e) {
            return DetailModelResult.error("获取数据集下拉列表失败");
        }
    }

    public List getAllVersionDatasetList() {
        // 导入的数据集列表应该只取集成标准已发布各版本的并集去重 @modify by yingjie chen
        Criteria criteria = getCurrentSession().createCriteria(StandardModel.class);
        StandardParamResultModel standardParam = standardService.getStandardParam(SqlConstants.STANDARD);
        String code = standardParam.getCode();
        criteria.add(Restrictions.eq("code", code));
        List<StandardModel> standardModelList = criteria.list();

        List<Integer> idList = standardModelList.stream().map(StandardModel::getId).collect(Collectors.toList());
        try {
            List<StandardVersionModel> versionModelList = new ArrayList<>();
            if (!CollectionUtil.isEmpty(idList)) {
                String hql = "FROM StandardVersionModel WHERE publishTime is not null AND standardId IN :idList order by publishTime";
                Query query = getCurrentSession().createQuery(hql);
                query.setParameterList("idList", idList);
                versionModelList = query.list();
            }
            String sql = SqlConstants.EMPTY;
            BeanTransformer transformer = new BeanTransformer(StdDataSetModel.class);
            for (StandardVersionModel versionModel : versionModelList) {
                SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
                StandardVersion version = new StandardVersion(versionModel.getVersion());
                sql = sql + sqlCreator.selectData(version.getDataSetTableName()) + SqlConstants.UNION;
            }

            if (!StringUtil.isEmpty(sql)) {
                sql = sql.substring(0, sql.length() - SqlConstants.UNION.length());
                SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
                sqlQuery.setResultTransformer(transformer);
                List<StdDataSetModel> modelList = sqlQuery.list();
                List<StdDataSetModel> distinctModelList = new ArrayList<>();
                Map<String, StdDataSetModel> datasetModelMap = new HashMap<>();
                for (StdDataSetModel model : modelList) {
                    datasetModelMap.put(model.getCode(), model);
                }
                for (String key : datasetModelMap.keySet()) {
                    distinctModelList.add(datasetModelMap.get(key));
                }
                return distinctModelList;
            } else {
                return new ArrayList<>();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ApiException(ErrorCode.GetDataSetListFailed);
        }
    }

    //从excel导入数据集、数据元
    public void importFromExcel(String filePath, String stdVersion){
        String datasetTable;
        String metadataTable;
        String dictTable;
        if (StringUtil.isEmpty(stdVersion)) {
            datasetTable = "std_dataset";
            metadataTable = "std_metadata";
            dictTable = "std_dictionary";
        } else {
            StandardVersion standardVersion = new StandardVersion(stdVersion);
            datasetTable = standardVersion.getDataSetTableName();
            metadataTable = standardVersion.getMetaDataTableName();
            dictTable = standardVersion.getDictTableName();
        }

        SqlCreator sqlCreator = new SqlCreator(StdDictionaryModel.class);
        String sql = sqlCreator.selectData(dictTable);
        Query sessionQuery = getQuery(sqlCreator, sql);
        List<StdDictionaryModel> stdDictList = sessionQuery.list();
        Map<String, Integer> stdDictMap = new HashMap<>();
        for (StdDictionaryModel dict : stdDictList) {
            stdDictMap.put(dict.getCode(), dict.getId());
        }

        try {
            InputStream is = new FileInputStream(filePath);
            Workbook rwb = Workbook.getWorkbook(is);
            Sheet[] sheets = rwb.getSheets();
            for (Sheet sheet : sheets) {
                StdDataSetModel stdDataSetModel = new StdDataSetModel();
                //获取数据集信息
                String sheetName = sheet.getName(); //sheet名字
                String dataSetNname = sheet.getCell(1, 0).getContents();//名称
                String dataSetCode = sheet.getCell(1, 1).getContents();//标识
                String reference = sheet.getCell(1, 2).getContents();//参考
                String summary = sheet.getCell(1, 3).getContents();//备注

                //todo：test--测试时备注做区别，方便删除测试，summary变量区别
                //summary="测试excel导入";
                //todo：test--测试时code区别，否则测试不成功，因为code唯一
                //dataSetCode = dataSetCode+"excel";

                if (dataSetNname==null || dataSetNname.equals("")){
                    throw new Exception(sheetName+"数据集名称不能为空，请检查！");
                }
                if (dataSetCode==null || dataSetCode.equals("")){
                    throw new Exception(sheetName +"数据集标识不能为空，请检查！");
                }

                //插入数据集信息
                stdDataSetModel.setCode(dataSetCode);//code唯一
                stdDataSetModel.setName(dataSetNname);
                stdDataSetModel.setstandardId(2);

                //获取数据元信息
                Set<String> set = new HashSet<String>();
                List<StdMetaDataModel> metaDataList = new ArrayList<>();
                int rows = sheet.getRows();
                for (int j = 0; j < rows - 5; j++) {
                    StdMetaDataModel metaData = new StdMetaDataModel();
                    int row = j + 5;
                    String innerCode = sheet.getCell(1, row).getContents();//内部标识
                    String code = sheet.getCell(2, row).getContents();//数据元编码
                    String name = sheet.getCell(3, row).getContents();//数据元名称
                    String definition = sheet.getCell(4, row).getContents();//数据元定义
                    String type = sheet.getCell(5, row).getContents();//数据类型
                    String format = sheet.getCell(6, row).getContents();//表示形式
                    String dictCode = sheet.getCell(7, row).getContents();//术语范围值
                    String columnName = sheet.getCell(8, row).getContents();//列名
                    String columnType = sheet.getCell(9, row).getContents();//列类型
                    String columnLength = sheet.getCell(10, row).getContents();//列长度
                    String primaryKey = sheet.getCell(11, row).getContents();//主键
                    String nullable = sheet.getCell(12, row).getContents();//可为空
                    Integer pk = 0;
                    Integer isNull = 1;
                    if (!StringUtil.isEmpty(primaryKey)) {
                        pk = Integer.parseInt(primaryKey);
                    }
                    if (!StringUtil.isEmpty(nullable)) {
                        isNull = Integer.parseInt(nullable);
                    }

                    //todo：test--测试时备注做区别，方便删除测试，definition变量区别
                    //definition="测试excel导入";

                    //数据元的校验，一个不通过则全部不保存
                    if (innerCode==null || innerCode.equals("")){
                        throw new Exception(sheetName+"第"+(row+1)+"行内部标识不能为空，请检查！");
                    }else{
                        //innerCode要唯一
                        set.add(innerCode);
                        if (set.toArray(new String[set.size()]).length != j+1){
                            //innerCode重复
                            throw new Exception(sheetName+"第"+(row+1)+"行内部标识已存在，请检查！");
                        }
                    }
                    if (StringUtil.isEmpty(code)){
                        throw new Exception(sheetName+"第"+(row+1)+"行数据元编码不能为空，请检查！");
                    }
                    if (StringUtil.isEmpty(name)){
                        throw new Exception(sheetName+"第"+(row+1)+"行数据元名称不能为空，请检查！");
                    }
                    if (StringUtil.isEmpty(columnName)){
                        throw new Exception(sheetName+"第"+(row+1)+"行列名不能为空，请检查！");
                    }
                    if (pk == 1 && isNull == 1){
                        throw new Exception(sheetName+"第"+(row+1)+"行主键不能为空，请检查！");
                    }
                    //数据类型与列类型一致 S、L、N、D、DT、T、BY
                    if ((type.contains("S") && !columnType.contains("VARCHAR")) || (type.equals("D") && !columnType.equals("DATE"))  || (type.equals("DT") && !columnType.equals("DATETIME"))) {
                        throw new Exception(sheetName+"第"+(row+1)+"行数据类型与列类型不匹配，请检查！");
                    }

                    //插入数据元信息
                    metaData.setId(0);//为0内部自增
                    metaData.setCode(innerCode);
                    metaData.setName(name);
                    metaData.setDeCode(code);
                    metaData.setType(type);
                    if (!StringUtil.isEmpty(dictCode)) {
                        metaData.setDictId(stdDictMap.get(dictCode));
                    }
                    metaData.setFormat(format);
                    metaData.setDefinition(definition);
                    metaData.setColumnName(columnName);
                    metaData.setColumnLength(columnLength);
                    metaData.setColumnType(columnType);
                    metaData.setPrimaryKey(0);
                    metaData.setPrimaryKey(pk);
                    metaData.setNullable(isNull);
                    metaDataList.add(metaData);
                    //metaDataManager.saveMetaData(dataSet, metaData);//保存数据元
                }
                //数据集入库
                add(datasetTable, stdDataSetModel);//保存数据集信息
                Integer metaDataMaxId = getMaxId(metadataTable);

                //数据元入库
                if (metaDataList.size()>0){
                    for (StdMetaDataModel metaData : metaDataList) {
                        sqlCreator = new SqlCreator(StdMetaDataModel.class);
                        metaData.setDatasetId(stdDataSetModel.getId());
                        metaData.setId(metaDataMaxId++);
                        metaData.setStandardId(2);
                        JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(metaData));
                        sql = sqlCreator.insertData(metadataTable, jsonNode);
                        Query query = getExeuteQuery(sqlCreator, sql);
                        query.executeUpdate();
                    }
                }
            }

            //关闭
            rwb.close();
            is.close();

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    public List datasetVersionListByName(String rsResourceImportDatasetDialogName) {

        try {
            String hql = "FROM StandardVersionModel WHERE publishTime is not null order by publishTime";
            Query query = getCurrentSession().createQuery(hql);
            List<StandardVersionModel> versionModelList = query.list();
            String sql = SqlConstants.EMPTY;
            BeanTransformer transformer = new BeanTransformer(StdDataSetModel.class);
            for (StandardVersionModel versionModel : versionModelList) {
                SqlCreator sqlCreator = new SqlCreator(StdDataSetModel.class);
                StandardVersion version = new StandardVersion(versionModel.getVersion());
                sql = sql + sqlCreator.selectData(version.getDataSetTableName())  + SqlConstants.UNION;
            }

            if (!StringUtil.isEmpty(sql)) {
                sql = sql.substring(0, sql.length() - SqlConstants.UNION.length());
                SQLQuery sqlQuery = getCurrentSession().createSQLQuery(sql);
                sqlQuery.setResultTransformer(transformer);
                List<StdDataSetModel> modelList = sqlQuery.list();
                List<StdDataSetModel> distinctModelList = new ArrayList<>();
                Map<String, StdDataSetModel> datasetModelMap = new HashMap<>();
                for (StdDataSetModel model : modelList) {
                    datasetModelMap.put(model.getCode(), model);
                }
                for (String key : datasetModelMap.keySet()) {
                    StdDataSetModel s=datasetModelMap.get(key);
                    if(s.getName().contains(rsResourceImportDatasetDialogName)){
                        distinctModelList.add(datasetModelMap.get(key));
                    }
                }
                return distinctModelList;
            } else {
                return new ArrayList<>();
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new ApiException(ErrorCode.GetDataSetListFailed);
        }
    }
}

