使用RequestMappingHandlerMapping自动生成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;
    }
    }
    =======================
    动态生成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;
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值