服务类
package com.djhu.ruleeditor.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.djhu.research.common.ResultResp;
import com.djhu.ruleeditor.api.SourceValueSearcher;
import com.djhu.ruleeditor.api.Transformer;
import com.djhu.ruleeditor.common.util.CheckUtil;
import com.djhu.ruleeditor.common.util.JsonUtils;
import com.djhu.ruleeditor.common.util.StringUtil;
import com.djhu.ruleeditor.core.TransformerInputValue;
import com.djhu.ruleeditor.core.ValueSearchMeta;
import com.djhu.ruleeditor.entity.TbRule;
import com.djhu.ruleeditor.entity.TbService;
import com.djhu.ruleeditor.entity.TbServiceRule;
import com.djhu.ruleeditor.common.constant.CommonConstant;
import com.djhu.ruleeditor.entity.dto.DynamicSearchDto;
import com.djhu.ruleeditor.entity.vo.SearchServiceVo;
import com.djhu.ruleeditor.service.ITbRuleService;
import com.djhu.ruleeditor.service.ITbServiceRuleService;
import com.djhu.ruleeditor.service.ITbServiceService;
import com.djhu.ruleeditor.service.search.DynamicSearchService;
import com.djhu.ruleeditor.tranformer.factory.TransformerFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
/**
-
@author cyf
-
@description
-
@create 2019-08-23 9:41
**/
@Data
@Slf4j
@Service
public class DynamicSearchServiceImpl implements DynamicSearchService {private String serviceId;
@Autowired
private ITbServiceService serviceService;@Autowired
private ITbServiceRuleService serviceRuleService;@Autowired
private TransformerFactory transformerFactory;@Autowired
private SourceValueSearcher sourceValueSearcher;@Autowired
private ITbRuleService tbRuleService;@ResponseBody
@Override
public ResultResp search(DynamicSearchDto searchDto) {
log.info(“动态生成服务传入的参数为 {}”, searchDto);
if (StringUtil.isEmptyStrs(searchDto.getSearchParam())) {
log.info(“searchParam参数为空!!!”);
return ResultResp.failed(-1, “searchParam参数为空!!!”);
}
checkService(serviceId);/** searchParam 为 hisId 或 cardNo */ String value = searchDto.getSearchParam(); String stime = searchDto.getStime(); String etime = searchDto.getEtime(); String field = getField(value); EntityWrapper<TbServiceRule> entityWrapper = new EntityWrapper<>(); entityWrapper.eq("SERVICE_ID", serviceId); List<TbServiceRule> tbServiceRules = serviceRuleService.selectList(entityWrapper); log.info("服务规则列表为 {}", JsonUtils.objectToJson(tbServiceRules)); List<SearchServiceVo> serviceVoList = new ArrayList(); if (CollectionUtils.isNotEmpty(tbServiceRules)) { for (TbServiceRule serviceRule : tbServiceRules) { String ruleId = serviceRule.getRuleId(); TbRule tbRule = tbRuleService.selectById(ruleId); if (Objects.isNull(tbRule)) { log.warn("规则为空 ruleId is {}", ruleId); continue; } SearchServiceVo serviceVo = new SearchServiceVo(); serviceVo.setRuleId(ruleId); serviceVo.setRuleName(tbRule.getRuleName()); serviceVo.setDataSetCode(tbRule.getDatasetcode()); serviceVo.setDataSetName(""); Map<String, Object> queryMap = new HashMap<>(); queryMap.put(field, value); if (StringUtil.isNotEmptyStrs(stime, etime)) { queryMap.put(CommonConstant.ADMIT_DATE, stime); queryMap.put(CommonConstant.ADMIT_DATE, etime); } List<Map> mapList = sourceValueSearcher.search(queryMap, new ValueSearchMeta(tbRule.getDatasetcode(), tbRule.getDataelementcode())); if (CollectionUtils.isEmpty(mapList)) { log.info("es查询结果为,查询参数为 {}", JsonUtils.objectToJson(queryMap), tbRule); continue; } Transformer transformer = transformerFactory.create(ruleId); List<SearchServiceVo.SearchServiceDataVo> datas = new ArrayList<>(); for (Map meta : mapList) { String srcContent = String.valueOf(meta.get(CommonConstant.SRC_CONTENT)); String hisId = String.valueOf(meta.get(CommonConstant.HIS_ID)); String documentDomainId = String.valueOf(meta.get(CommonConstant.DOCUMENT_DOMAIN_ID)); String documentUniqueId = String.valueOf(meta.get(CommonConstant.DOCUMENT_UNIQUE_ID)); Map metas = new HashMap(); metas.put(CommonConstant.HIS_ID, hisId); TransformerInputValue inputValue = new TransformerInputValue(); inputValue.setMetaInfo(metas); inputValue.setSourceDate(srcContent); String dstContent = String.valueOf(transformer.transform(inputValue)); SearchServiceVo.SearchServiceDataVo dataVo = new SearchServiceVo.SearchServiceDataVo(); dataVo.setHisId(hisId); dataVo.setSrcContent(srcContent); dataVo.setDstContent(StringUtils.isEmpty(dstContent) ? srcContent : dstContent); dataVo.setDocumentUniqueid(documentUniqueId); dataVo.setDocumentDomainId(documentDomainId); datas.add(dataVo); } serviceVo.setDatas(datas); serviceVoList.add(serviceVo); } } log.info("动态服务查询结果数量为 {},结果为 {}",serviceVoList.size(),JsonUtils.objectToJson(serviceVoList)); return ResultResp.ok(serviceVoList);
}
private void checkService(String serviceId) {
TbService tbService = serviceService.selectById(serviceId);
if (Objects.isNull(tbService)) {
log.error(“服务不存在,服务id为 {}”, serviceId);
throw new RuntimeException("服务不存在!!! id 为 " + serviceId);
}
}private String getField(String value) {
if (StringUtil.isEmptyStrs(value)) {
return “”;
}
return value.length() > CommonConstant.HIS_ID_OR_IDENTITY_NO_SIGN && StringUtils.isEmpty(CheckUtil.IdentityCardVerification(value))
? CommonConstant.IDENTITY_NO : CommonConstant.HIS_ID;
}
}
=======================
动态生成controller方法的类!
=======================
package com.djhu.ruleeditor.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.djhu.research.common.ResultResp;
import com.djhu.ruleeditor.api.SourceValueSearcher;
import com.djhu.ruleeditor.api.Transformer;
import com.djhu.ruleeditor.common.util.CheckUtil;
import com.djhu.ruleeditor.common.util.JsonUtils;
import com.djhu.ruleeditor.common.util.StringUtil;
import com.djhu.ruleeditor.core.TransformerInputValue;
import com.djhu.ruleeditor.core.ValueSearchMeta;
import com.djhu.ruleeditor.entity.TbRule;
import com.djhu.ruleeditor.entity.TbService;
import com.djhu.ruleeditor.entity.TbServiceRule;
import com.djhu.ruleeditor.common.constant.CommonConstant;
import com.djhu.ruleeditor.entity.dto.DynamicSearchDto;
import com.djhu.ruleeditor.entity.vo.SearchServiceVo;
import com.djhu.ruleeditor.service.ITbRuleService;
import com.djhu.ruleeditor.service.ITbServiceRuleService;
import com.djhu.ruleeditor.service.ITbServiceService;
import com.djhu.ruleeditor.service.search.DynamicSearchService;
import com.djhu.ruleeditor.tranformer.factory.TransformerFactory;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
/**
-
@author cyf
-
@description
-
@create 2019-08-23 9:41
**/
@Data
@Slf4j
@Service
public class DynamicSearchServiceImpl implements DynamicSearchService {private String serviceId;
@Autowired
private ITbServiceService serviceService;@Autowired
private ITbServiceRuleService serviceRuleService;@Autowired
private TransformerFactory transformerFactory;@Autowired
private SourceValueSearcher sourceValueSearcher;@Autowired
private ITbRuleService tbRuleService;@ResponseBody
@Override
public ResultResp search(DynamicSearchDto searchDto) {
log.info(“动态生成服务传入的参数为 {}”, searchDto);
if (StringUtil.isEmptyStrs(searchDto.getSearchParam())) {
log.info(“searchParam参数为空!!!”);
return ResultResp.failed(-1, “searchParam参数为空!!!”);
}
checkService(serviceId);/** searchParam 为 hisId 或 cardNo */ String value = searchDto.getSearchParam(); String stime = searchDto.getStime(); String etime = searchDto.getEtime(); String field = getField(value); EntityWrapper<TbServiceRule> entityWrapper = new EntityWrapper<>(); entityWrapper.eq("SERVICE_ID", serviceId); List<TbServiceRule> tbServiceRules = serviceRuleService.selectList(entityWrapper); log.info("服务规则列表为 {}", JsonUtils.objectToJson(tbServiceRules)); List<SearchServiceVo> serviceVoList = new ArrayList(); if (CollectionUtils.isNotEmpty(tbServiceRules)) { for (TbServiceRule serviceRule : tbServiceRules) { String ruleId = serviceRule.getRuleId(); TbRule tbRule = tbRuleService.selectById(ruleId); if (Objects.isNull(tbRule)) { log.warn("规则为空 ruleId is {}", ruleId); continue; } SearchServiceVo serviceVo = new SearchServiceVo(); serviceVo.setRuleId(ruleId); serviceVo.setRuleName(tbRule.getRuleName()); serviceVo.setDataSetCode(tbRule.getDatasetcode()); serviceVo.setDataSetName(""); Map<String, Object> queryMap = new HashMap<>(); queryMap.put(field, value); if (StringUtil.isNotEmptyStrs(stime, etime)) { queryMap.put(CommonConstant.ADMIT_DATE, stime); queryMap.put(CommonConstant.ADMIT_DATE, etime); } List<Map> mapList = sourceValueSearcher.search(queryMap, new ValueSearchMeta(tbRule.getDatasetcode(), tbRule.getDataelementcode())); if (CollectionUtils.isEmpty(mapList)) { log.info("es查询结果为,查询参数为 {}", JsonUtils.objectToJson(queryMap), tbRule); continue; } Transformer transformer = transformerFactory.create(ruleId); List<SearchServiceVo.SearchServiceDataVo> datas = new ArrayList<>(); for (Map meta : mapList) { String srcContent = String.valueOf(meta.get(CommonConstant.SRC_CONTENT)); String hisId = String.valueOf(meta.get(CommonConstant.HIS_ID)); String documentDomainId = String.valueOf(meta.get(CommonConstant.DOCUMENT_DOMAIN_ID)); String documentUniqueId = String.valueOf(meta.get(CommonConstant.DOCUMENT_UNIQUE_ID)); Map metas = new HashMap(); metas.put(CommonConstant.HIS_ID, hisId); TransformerInputValue inputValue = new TransformerInputValue(); inputValue.setMetaInfo(metas); inputValue.setSourceDate(srcContent); String dstContent = String.valueOf(transformer.transform(inputValue)); SearchServiceVo.SearchServiceDataVo dataVo = new SearchServiceVo.SearchServiceDataVo(); dataVo.setHisId(hisId); dataVo.setSrcContent(srcContent); dataVo.setDstContent(StringUtils.isEmpty(dstContent) ? srcContent : dstContent); dataVo.setDocumentUniqueid(documentUniqueId); dataVo.setDocumentDomainId(documentDomainId); datas.add(dataVo); } serviceVo.setDatas(datas); serviceVoList.add(serviceVo); } } log.info("动态服务查询结果数量为 {},结果为 {}",serviceVoList.size(),JsonUtils.objectToJson(serviceVoList)); return ResultResp.ok(serviceVoList);
}
private void checkService(String serviceId) {
TbService tbService = serviceService.selectById(serviceId);
if (Objects.isNull(tbService)) {
log.error(“服务不存在,服务id为 {}”, serviceId);
throw new RuntimeException("服务不存在!!! id 为 " + serviceId);
}
}private String getField(String value) {
if (StringUtil.isEmptyStrs(value)) {
return “”;
}
return value.length() > CommonConstant.HIS_ID_OR_IDENTITY_NO_SIGN && StringUtils.isEmpty(CheckUtil.IdentityCardVerification(value))
? CommonConstant.IDENTITY_NO : CommonConstant.HIS_ID;
}
}