|  | @ -0,0 +1,510 @@
 | 
	
		
			
				|  |  | package com.yihu.ehr.standard.service.standard;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.JsonNode;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.common.dao.SQLGeneralDAO;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.constrant.Constants;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.constrant.ErrorCode;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.exception.ApiException;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.operator.CollectionUtil;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.operator.StringUtil;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.sql.BeanTransformer;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.sql.RequestParamTransformer;
 | 
	
		
			
				|  |  | import com.yihu.ehr.framework.util.sql.SqlCreator;
 | 
	
		
			
				|  |  | import com.yihu.ehr.standard.model.Select2;
 | 
	
		
			
				|  |  | import com.yihu.ehr.standard.model.standard.StandardVersionModel;
 | 
	
		
			
				|  |  | import com.yihu.ehr.standard.model.standard.StdDataSetModel;
 | 
	
		
			
				|  |  | import com.yihu.ehr.standard.model.standard.StdDictionaryModel;
 | 
	
		
			
				|  |  | import com.yihu.ehr.standard.model.standard.StdMetaDataModel;
 | 
	
		
			
				|  |  | import com.yihu.ehr.standard.service.bo.StandardVersion;
 | 
	
		
			
				|  |  | import jxl.Sheet;
 | 
	
		
			
				|  |  | import jxl.Workbook;
 | 
	
		
			
				|  |  | import org.hibernate.Query;
 | 
	
		
			
				|  |  | import org.hibernate.SQLQuery;
 | 
	
		
			
				|  |  | import org.hibernate.Session;
 | 
	
		
			
				|  |  | 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.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * 数据集管理器.
 | 
	
		
			
				|  |  |  *
 | 
	
		
			
				|  |  |  * @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";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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 StdDataSetModel add(String stdVersion, String dataset) {
 | 
	
		
			
				|  |  |         if (stdVersion.equals("{stdVersion}")) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetMissVersion);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         Boolean existFlg = false;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             StandardVersion standardVersion = new StandardVersion(stdVersion);
 | 
	
		
			
				|  |  |             ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |             StdDataSetModel stdDataSetModel = objectMapper.readValue(dataset, StdDataSetModel.class);
 | 
	
		
			
				|  |  |             existFlg = datasetCodeValidate(standardVersion, stdDataSetModel.getCode());
 | 
	
		
			
				|  |  |             if (!existFlg) {
 | 
	
		
			
				|  |  |                 this.add(standardVersion.getDataSetTableName(), stdDataSetModel);
 | 
	
		
			
				|  |  |                 return stdDataSetModel;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (existFlg) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.ExistStandardDatasetCode);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetSaveFailed);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public Boolean datasetCodeValidate(StandardVersion standardVersion, String code) {
 | 
	
		
			
				|  |  |         Map<String, String> query = new HashMap<>();
 | 
	
		
			
				|  |  |         Map<String, String> order = new HashMap<>();
 | 
	
		
			
				|  |  |         query.put("code", code);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<StdDataSetModel> list = this.getList(standardVersion, query, order, 1, 0);
 | 
	
		
			
				|  |  |         if (!CollectionUtil.isEmpty(list)) {
 | 
	
		
			
				|  |  |             return true;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return false;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public void delete(String stdVersion, Integer datasetId) {
 | 
	
		
			
				|  |  |         if (StringUtil.isEmpty(stdVersion)) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetMissVersion);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             StandardVersion standardVersion = new StandardVersion(stdVersion);
 | 
	
		
			
				|  |  |             if (datasetId != null) {
 | 
	
		
			
				|  |  |                 this.delete(standardVersion, datasetId);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetDeleteFailed);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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 Object update(String stdVersion, String dataset) {
 | 
	
		
			
				|  |  |         if (StringUtil.isEmpty(stdVersion)) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetMissVersion);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         Boolean existFlg = false;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             StandardVersion standardVersion = new StandardVersion(stdVersion);
 | 
	
		
			
				|  |  |             ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |             StdDataSetModel stdDataSetModel = objectMapper.readValue(dataset, StdDataSetModel.class);
 | 
	
		
			
				|  |  |             existFlg = datasetCodeValidate(standardVersion, stdDataSetModel.getId(), stdDataSetModel.getCode());
 | 
	
		
			
				|  |  |             if (!existFlg) {
 | 
	
		
			
				|  |  |                 this.update(standardVersion, stdDataSetModel);
 | 
	
		
			
				|  |  |                 return stdDataSetModel;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (existFlg) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.ExistStandardDatasetCode);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetUpdateFailed);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public StdDataSetModel get(String stdVersion, Integer datasetId) {
 | 
	
		
			
				|  |  |         if (StringUtil.isEmpty(stdVersion)) {
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.StandardDatasetMissVersion);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             StandardVersion standardVersion = new StandardVersion(stdVersion);
 | 
	
		
			
				|  |  |             if (datasetId != null) {
 | 
	
		
			
				|  |  |                 return this.get(standardVersion, datasetId);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         throw new ApiException(ErrorCode.StandardDatasetGetFailed);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     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 {
 | 
	
		
			
				|  |  |         ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |         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();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private void add(String dataSetTable, StdDataSetModel stdDataSetModel) throws IOException {
 | 
	
		
			
				|  |  |         ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |         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, String> 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);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return (List<StdDataSetModel>) sessionQuery.list();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List getDatasetList(Class tClass, String stdVersion, String condition, String order, Integer limit, Integer offset) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             StandardVersion standardVersion = new StandardVersion(stdVersion);
 | 
	
		
			
				|  |  |             String tableName = standardVersion.getDataSetTableName();
 | 
	
		
			
				|  |  |             SqlCreator sqlCreator = new SqlCreator(tClass);
 | 
	
		
			
				|  |  |             ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |             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);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             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 * limit);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             return query.list();
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             throw new ApiException(ErrorCode.GetDataSetListFailed);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public Integer getDatasetInt(Class tClass, String stdVersion, String condition) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             StandardVersion standardVersion = new StandardVersion(stdVersion);
 | 
	
		
			
				|  |  |             String tableName = standardVersion.getDataSetTableName();
 | 
	
		
			
				|  |  |             SqlCreator sqlCreator = new SqlCreator(tClass);
 | 
	
		
			
				|  |  |             ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |             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 List<Select2> getDatasetSelectList(String stdVersion, String condition, String order, Integer limit, Integer offset) {
 | 
	
		
			
				|  |  |         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() + Constants.COMMA + stdDataSetModel.getCode());
 | 
	
		
			
				|  |  |             select2.setText(stdDataSetModel.getName());
 | 
	
		
			
				|  |  |             detailModelList.add(select2);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return detailModelList;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public List getAllVersionDatasetList() {
 | 
	
		
			
				|  |  |         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 = Constants.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()) + Constants.UNION;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (!StringUtil.isEmpty(sql)) {
 | 
	
		
			
				|  |  |                 sql = sql.substring(0, sql.length() - Constants.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);
 | 
	
		
			
				|  |  |                         ObjectMapper objectMapper = new ObjectMapper();
 | 
	
		
			
				|  |  |                         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();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 | 
	
		
			
				|  |  | 
 |