AbstractAdmDictBaseServiceImpl.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package com.persagy.proxy.adm.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.fasterxml.jackson.databind.JsonNode;
  7. import com.fasterxml.jackson.databind.node.ArrayNode;
  8. import com.persagy.dmp.basic.model.QueryCriteria;
  9. import com.persagy.dmp.basic.utils.JsonNodeUtils;
  10. import com.persagy.dmp.common.utils.JsonHelper;
  11. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  12. import com.persagy.proxy.adm.constant.AdmDictConstant;
  13. import com.persagy.proxy.adm.constant.enums.ValidEnum;
  14. import com.persagy.proxy.adm.model.AdmObjectInfo;
  15. import com.persagy.proxy.adm.model.AdmObjectType;
  16. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  17. import com.persagy.proxy.adm.request.AdmResponse;
  18. import com.persagy.proxy.adm.service.IAdmDictBaseService;
  19. import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
  20. import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
  21. import com.persagy.proxy.common.client.DmpDicClient;
  22. import com.persagy.proxy.common.client.DmpRwdClient;
  23. import com.persagy.proxy.common.entity.DmpResult;
  24. import com.persagy.proxy.common.entity.InstanceUrlParam;
  25. import lombok.extern.slf4j.Slf4j;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import java.io.IOException;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * @ClassName AbstractAdmDictBaseServiceImpl
  33. * @Description: 字典查询
  34. * @Author linhuili
  35. * @Date 2021/8/31 18:21
  36. * @Version V1.0
  37. **/
  38. @Slf4j
  39. public class AbstractAdmDictBaseServiceImpl<T> implements IAdmDictBaseService<T> {
  40. @Autowired
  41. private DmpRwdClient rwdClient;
  42. @Autowired
  43. private DmpDicClient dicClient;
  44. /**
  45. * 查询DMP字典数据
  46. * @param context
  47. * @param dmpRequest
  48. * @param clazz
  49. * @return
  50. */
  51. @Override
  52. public AdmResponse doQueryDictData(InstanceUrlParam context,QueryCriteria dmpRequest, Class<T> clazz) {
  53. // 转换参数
  54. JSONObject para = null;
  55. try {
  56. para = JsonHelper.toJSONObject(dmpRequest);
  57. } catch (IOException e) {
  58. log.error(e.getMessage(), e);
  59. return null;
  60. }
  61. // 调用中台查询
  62. DmpResult<JSONArray> dmpResult = dicClient.queryDictData(context, para);
  63. List<AdmObjectType> admVOs = dictDataVOTransfer(dmpResult.getData());
  64. // 设置返回值
  65. AdmResponse response = AdmResponse.success(admVOs);
  66. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  67. response.setTotal(total);
  68. return response;
  69. }
  70. /**
  71. * 查询DMP对象类型
  72. * @param context
  73. * @param dmpRequest
  74. * @param clazz
  75. * @return
  76. */
  77. @Override
  78. public AdmResponse doQueryObjectType(InstanceUrlParam context,QueryCriteria dmpRequest, Class<T> clazz) {
  79. // 转换参数
  80. JSONObject para = null;
  81. try {
  82. para = JsonHelper.toJSONObject(dmpRequest);
  83. } catch (IOException e) {
  84. log.error(e.getMessage(), e);
  85. return null;
  86. }
  87. // 调用中台查询
  88. DmpResult<JSONArray> dmpResult = rwdClient.queryObjectType(context, para);
  89. JSONArray data = dmpResult.getData();
  90. List<T> admVOs = data.toJavaList(clazz);
  91. // 设置返回值
  92. AdmResponse response = AdmResponse.success(admVOs);
  93. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  94. response.setTotal(total);
  95. return response;
  96. }
  97. /**
  98. * 查询对象信息点
  99. * @param request
  100. * @return
  101. */
  102. @Override
  103. public AdmResponse doQueryObjectInfo(InstanceUrlParam context, AdmQueryCriteria request,String fieldName, Class<T> clazz) {
  104. // 转换为数据中台查询条件
  105. QueryCriteria dmpRequest = toDmpCriteria(request,fieldName);
  106. // 转换参数
  107. JSONObject para = null;
  108. try {
  109. para = JsonHelper.toJSONObject(dmpRequest);
  110. } catch (IOException e) {
  111. log.error(e.getMessage(), e);
  112. return null;
  113. }
  114. // 调用中台查询
  115. DmpResult<JSONArray> dmpResult = rwdClient.queryObjectInfo(context, para);
  116. List<AdmObjectInfo> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), request.getProjection(), AdmObjectInfo.class);
  117. // BDTP信息点格式与ADM兼容处理
  118. handleObjectInfoResult(admVOs);
  119. // 设置返回值
  120. AdmResponse response = AdmResponse.success(admVOs);
  121. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  122. response.setTotal(total);
  123. response.setPageNumber(request.getPageNumber());
  124. response.setPageSize(request.getPageSize());
  125. return response;
  126. }
  127. /**
  128. * 修改
  129. * @param context
  130. * @param clazz
  131. * @param voList
  132. * @return
  133. */
  134. @Override
  135. public List<T> doUpdateObjectInfo(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  136. if(CollUtil.isEmpty(voList)) {
  137. return null;
  138. }
  139. ArrayNode vos = JsonNodeUtils.toArrayNode(voList, null, null);
  140. // 调用中台修改
  141. DmpResult<cn.hutool.json.JSONArray> response = rwdClient.batchUpdateObjectInfo(context, JSONArray.parseArray(vos.toString()));
  142. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  143. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  144. }
  145. // 转换为结果
  146. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  147. }
  148. /**
  149. * 删除信息点
  150. * @param context
  151. * @param voList
  152. * @return
  153. */
  154. @Override
  155. public void doDeleteObjectInfo(InstanceUrlParam context, List<T> voList) {
  156. if(CollUtil.isEmpty(voList)) {
  157. return;
  158. }
  159. List<String> idList = CollUtil.getFieldValues(voList, "id", String.class);
  160. // 调用中台修改
  161. DmpResult response = rwdClient.batchDeleteObjectInfo(context, idList);
  162. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  163. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  164. }
  165. }
  166. /**
  167. * 信息点格式与ADM兼容处理
  168. * @param admVOs
  169. */
  170. private void handleObjectInfoResult(List<AdmObjectInfo> admVOs){
  171. for (AdmObjectInfo admVO : admVOs) {
  172. admVO.setFirstName(admVO.getFirstTag());
  173. admVO.setSecondName(admVO.getSecondTag());
  174. admVO.setMultiple(admVO.getIsMultiple());
  175. admVO.setRegion(admVO.getIsRegion());
  176. //path路径处理
  177. if(StringUtils.isEmpty(admVO.getFirstName()) || AdmDictConstant.FIRST_BASE_TAG.equals(admVO.getFirstName())
  178. || AdmDictConstant.HIS_FIRST_BASE_TAG.equals(admVO.getFirstName())){
  179. admVO.setPath(admVO.getCode());
  180. }else{
  181. admVO.setPath(AdmDictConstant.INFOS_EX+admVO.getCode());
  182. }
  183. }
  184. }
  185. /**
  186. * 字典数据格式与ADM兼容
  187. * @param array
  188. */
  189. private List<AdmObjectType> dictDataVOTransfer(JSONArray array){
  190. List<AdmObjectType> result = new ArrayList<>();
  191. if(CollUtil.isEmpty(array)){
  192. return result;
  193. }
  194. ArrayNode dataNode = JsonNodeUtils.toArrayNode(array, null, null);
  195. for (JsonNode dicData : dataNode) {
  196. AdmObjectType objectType = new AdmObjectType();
  197. objectType.setCode(dicData.get("code").textValue());
  198. objectType.setName(dicData.get("label").textValue());
  199. objectType.setAliasCode(dicData.get("code").textValue());
  200. objectType.setAliasName(dicData.get("label").textValue());
  201. objectType.setObjType(dicData.get("type").textValue());
  202. result.add(objectType);
  203. }
  204. return result;
  205. }
  206. /**
  207. * 将ADM的查询条件转换为DMP的
  208. * 调用前注意:
  209. * - 调用前将name赋值,才会将条件中adm的属性替换为dmp的属性
  210. * - 统计查询时将isOnlyCount设置为true
  211. * 调用后注意(需要调用者自行处理的):
  212. * - 不处理级联,级联应在调用者里处理级联调用转换后查询赋值
  213. * - 不处理projection,查询后自行根据projection筛选返回属性。(dmp只能指定扩展属性,而adm的是针对所有属性的)
  214. * @param admCriteria
  215. * @return
  216. */
  217. public static QueryCriteria toDmpCriteria(AdmQueryCriteria admCriteria,String fieldName) {
  218. QueryCriteria dmpCriteria = new QueryCriteria();
  219. // 拷贝分页
  220. AdmQueryCriteriaHelper.copyPageInfo(admCriteria, dmpCriteria);
  221. // 处理filters
  222. dmpCriteria.setCriteria(AdmQueryCriteriaHelper.toCriteria(admCriteria.getName(), admCriteria.getFilters()));
  223. if(StrUtil.isNotEmpty(admCriteria.getName())){
  224. // 处理name
  225. dmpCriteria.getCriteria().put(fieldName, admCriteria.getName());
  226. }
  227. dmpCriteria.getCriteria().put(AdmCommonConstant.VALID,ValidEnum.TRUE.getValue());
  228. // 处理排序
  229. dmpCriteria.setOrders(AdmQueryCriteriaHelper.toOrderItem(admCriteria.getOrders()));
  230. // 处理统计
  231. dmpCriteria.setOnlyCount(admCriteria.isOnlyCount());
  232. return dmpCriteria;
  233. }
  234. }