AbstractAdmBaseServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. package com.persagy.proxy.adm.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.map.MapUtil;
  4. import cn.hutool.core.util.ReflectUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.fasterxml.jackson.databind.ObjectMapper;
  9. import com.fasterxml.jackson.databind.node.ArrayNode;
  10. import com.fasterxml.jackson.databind.node.JsonNodeFactory;
  11. import com.fasterxml.jackson.databind.node.ObjectNode;
  12. import com.persagy.dmp.basic.model.QueryCriteria;
  13. import com.persagy.dmp.common.helper.SpringHelper;
  14. import com.persagy.dmp.common.model.entity.BaseEntity;
  15. import com.persagy.dmp.common.utils.JsonHelper;
  16. import com.persagy.dmp.digital.client.DigitalObjectFacade;
  17. import com.persagy.dmp.digital.entity.ObjectRelation;
  18. import com.persagy.proxy.adm.annotations.CascadeColumn;
  19. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  20. import com.persagy.proxy.adm.request.AdmResponse;
  21. import com.persagy.proxy.adm.service.IAdmBaseService;
  22. import com.persagy.proxy.adm.service.IAdmRelationService;
  23. import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
  24. import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
  25. import com.persagy.proxy.common.client.DmpRwdClient;
  26. import com.persagy.proxy.common.entity.DmpResult;
  27. import com.persagy.proxy.common.entity.InstanceUrlParam;
  28. import lombok.extern.slf4j.Slf4j;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import java.io.IOException;
  31. import java.lang.reflect.Field;
  32. import java.lang.reflect.ParameterizedType;
  33. import java.util.*;
  34. /**
  35. * ADM 抽象实现类
  36. * @author Charlie Yu
  37. * @date 2021-08-16
  38. */
  39. @Slf4j
  40. public class AbstractAdmBaseServiceImpl<T> implements IAdmBaseService<T> {
  41. @Autowired
  42. private DmpRwdClient rwdClient;
  43. @Autowired
  44. private ObjectMapper objectMapper;
  45. /**
  46. * 查询
  47. * @param request
  48. * @return
  49. */
  50. @Override
  51. public AdmResponse doQuery(InstanceUrlParam context, AdmQueryCriteria request, Class<T> clazz) {
  52. // 转换为数据中台查询条件
  53. QueryCriteria dmpRequest = AdmQueryCriteriaHelper.toDmpCriteria(request);
  54. // 转换参数
  55. JSONObject para = null;
  56. try {
  57. para = JsonHelper.toJsonObject(dmpRequest);
  58. } catch (IOException e) {
  59. log.error(e.getMessage(), e);
  60. return null;
  61. }
  62. // 调用中台查询
  63. DmpResult<JSONArray> dmpResult = rwdClient.queryObject(context, para);
  64. List<T> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), request.getProjection(), clazz);
  65. // 级联查询
  66. processCascade(context, admVOs, request.getName(), request.getCascade());
  67. // 设置返回值
  68. AdmResponse response = AdmResponse.success(admVOs);
  69. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  70. if(request.isOnlyCount()){
  71. response.setTotal(null);
  72. response.setCount(total);
  73. }else {
  74. response.setTotal(total);
  75. }
  76. response.setPageNumber(request.getPageNumber());
  77. response.setPageSize(request.getPageSize());
  78. return response;
  79. }
  80. /**
  81. * 处理级联查询
  82. * @param context
  83. * @param admVOs
  84. * @param name
  85. * @param cascades
  86. */
  87. private void processCascade(InstanceUrlParam context, List admVOs, String name, List<AdmQueryCriteria> cascades) {
  88. if(CollUtil.isEmpty(admVOs) || CollUtil.isEmpty(cascades)) {
  89. return;
  90. }
  91. Map<String, Object> voMap = CollUtil.fieldValueMap(admVOs, BaseEntity.PROP_ID);
  92. Object simpleVO = admVOs.get(0);
  93. for(AdmQueryCriteria cascade:cascades) {
  94. if(StrUtil.isBlank(cascade.getName())){
  95. //处理 级联查询参数name首字母大写cascade": [ {"Name": "property"}]
  96. continue;
  97. }
  98. // 获取关联对象的Class
  99. Field casField = ReflectUtil.getField(simpleVO.getClass(), cascade.getName());
  100. CascadeColumn annotationInfo = casField.getAnnotation(CascadeColumn.class);
  101. // 没有扩展信息不处理
  102. if(annotationInfo == null) {
  103. continue;
  104. }
  105. Class clazz = getFieldRealClass(casField);
  106. // 获取关联对象的类型
  107. String objectType = getObjTypeByClass(clazz);
  108. if(StrUtil.isBlank(objectType)) {
  109. continue;
  110. }
  111. cascade.setName(objectType);
  112. // 转换为数据中台查询条件
  113. QueryCriteria dmpRequest = AdmQueryCriteriaHelper.toDmpCriteria(cascade);
  114. // 拼接关系条件,得到关系映射
  115. Map<String, List<String>> idMap = ensureRelationCond(context, dmpRequest.getCriteria(), voMap.keySet(), annotationInfo);
  116. if(MapUtil.isEmpty(idMap)) {
  117. continue;
  118. }
  119. // 查询结果
  120. List vos = queryByCondition(context, dmpRequest, cascade.getProjection(), clazz);
  121. if(CollUtil.isEmpty(vos)) {
  122. continue;
  123. }
  124. // 写回原对象中
  125. writeToVOs(voMap, idMap, vos, casField);
  126. // 递归继续处理
  127. processCascade(context, vos, objectType, cascade.getCascade());
  128. }
  129. }
  130. /**
  131. * 拼接关系条件
  132. * @param context
  133. * @param node
  134. * @param idList
  135. * @param annotationInfo
  136. * @return
  137. */
  138. private Map<String, List<String>> ensureRelationCond(InstanceUrlParam context, ObjectNode node, Set<String> idList, CascadeColumn annotationInfo) {
  139. if(CollUtil.isEmpty(idList)) {
  140. return null;
  141. }
  142. // 查询关系
  143. List<ObjectRelation> relations = queryRelations(context, idList, annotationInfo);
  144. ArrayNode array = node.putObject(BaseEntity.PROP_ID).putArray("$in");
  145. // 拼接查询条件
  146. if(CollUtil.isEmpty(relations)) {
  147. array.add("0");
  148. } else {
  149. List<String> relList = CollUtil.getFieldValues(relations, annotationInfo.selectToObj()?"objTo":"objFrom", String.class);
  150. relList.forEach(id -> array.add(id));
  151. }
  152. if(annotationInfo.selectToObj()){
  153. return groupMapList(relations, "objFrom", "objTo");
  154. }else{
  155. return groupMapList(relations, "objTo", "objFrom");
  156. }
  157. }
  158. /**
  159. * 将集合转换为Map
  160. * @param list 集合
  161. * @param keyField key属性名
  162. * @param valueField value属性名
  163. * @return
  164. */
  165. private static Map<String, List<String>> groupMapList(List<?> list, String keyField, String valueField) {
  166. if(CollUtil.isEmpty(list)) {
  167. return null;
  168. }
  169. Map<String, List<String>> groupMap = new HashMap<>();
  170. for(Object vo:list) {
  171. String key = (String) ReflectUtil.getFieldValue(vo, keyField);
  172. String value = (String) ReflectUtil.getFieldValue(vo, valueField);
  173. if(!StrUtil.isAllNotBlank(key, value)) {
  174. continue;
  175. }
  176. List<String> valueList = groupMap.get(key);
  177. if(CollUtil.isEmpty(valueList)) {
  178. valueList = new ArrayList<>();
  179. groupMap.put(key, valueList);
  180. }
  181. valueList.add(value);
  182. }
  183. return groupMap;
  184. }
  185. /**
  186. * 查询关系
  187. * @param context
  188. * @param idList
  189. * @param anno
  190. * @return
  191. */
  192. private List<ObjectRelation> queryRelations(InstanceUrlParam context, Set<String>idList, CascadeColumn anno) {
  193. // 如果没有任何配置,不处理
  194. if(StrUtil.isBlank(anno.graphCode()) && StrUtil.isBlank(anno.relCode()) &&
  195. StrUtil.isBlank(anno.relValue())) {
  196. return null;
  197. }
  198. // 查询关系
  199. QueryCriteria queryRequest = new QueryCriteria();
  200. ObjectNode criteria = JsonNodeFactory.instance.objectNode();
  201. queryRequest.setCriteria(criteria);
  202. boolean isSelectTo = anno.selectToObj();
  203. putString(criteria, "graphCode", anno.graphCode());
  204. putString(criteria, "relCode", anno.relCode());
  205. putString(criteria, "relValue", anno.relValue());
  206. ArrayNode array = criteria.putObject(isSelectTo?"objFrom":"objTo").putArray("$in");
  207. idList.forEach(id -> array.add(id));
  208. return SpringHelper.getBean(IAdmRelationService.class).queryByCondition(context, queryRequest);
  209. }
  210. /**
  211. * 添加字符串条件
  212. * @param node
  213. * @param key
  214. * @param value
  215. */
  216. private void putString(ObjectNode node, String key, String value) {
  217. if(StrUtil.isBlank(value)) {
  218. return;
  219. }
  220. node.put(key, value);
  221. }
  222. /**
  223. * 将子集写回到对象中
  224. * @param voMap 原始对象Map
  225. * @param idMap 主键映射<mainId, List<subId>>
  226. * @param vos 子对象
  227. * @param casField 子对象对应属性
  228. */
  229. private <V> void writeToVOs(Map<String, Object> voMap, Map<String, List<String>> idMap, List<V> vos, Field casField) {
  230. if(CollUtil.isEmpty(vos) || MapUtil.isEmpty(idMap)) {
  231. return;
  232. }
  233. // 转换为Map
  234. Map<String, V> subVOMap = CollUtil.fieldValueMap(vos, BaseEntity.PROP_ID);
  235. // 是否为集合
  236. boolean isList = List.class.isAssignableFrom(casField.getType());
  237. // 按对象循环
  238. for(String id:voMap.keySet()) {
  239. Object mainVO = voMap.get(id);
  240. List<String> subIdList = idMap.get(id);
  241. if(CollUtil.isEmpty(subIdList)) {
  242. continue;
  243. }
  244. // 如果是集合
  245. if(isList) {
  246. // 取出对应的对象集合
  247. Map<String, V> currVOMap = MapUtil.filter(subVOMap, subIdList.toArray(new String[0]));
  248. List<V> voList = CollUtil.newArrayList(currVOMap.values());
  249. ReflectUtil.setFieldValue(mainVO, casField, voList);
  250. } else {
  251. // 非集合取第一个即可
  252. String subId = subIdList.get(0);
  253. V vo = subVOMap.get(subId);
  254. ReflectUtil.setFieldValue(mainVO, casField, vo);
  255. }
  256. }
  257. }
  258. /**
  259. * 调用Client查询
  260. * @param context
  261. * @param criteria
  262. * @param projection
  263. * @param clazz
  264. * @return
  265. */
  266. private <V> List<V> queryByCondition(InstanceUrlParam context, QueryCriteria criteria, List<String> projection, Class<V> clazz) {
  267. // 转换参数
  268. if(criteria == null) {
  269. return null;
  270. }
  271. List<ObjectNode> objects = DigitalObjectFacade.query(context.getGroupCode(), context.getProjectId(),
  272. context.getAppId(), null, criteria);
  273. return AdmEntityTransferUtil.toAdmMultiEntity(objects, projection, clazz);
  274. }
  275. /**
  276. * 获取实际的Class
  277. * @param casField
  278. * @return
  279. */
  280. private Class getFieldRealClass(Field casField) {
  281. // 取到Field
  282. Class clazz = casField.getType();
  283. // 如果是集合,则取实际的对象
  284. if(List.class.isAssignableFrom(clazz)) {
  285. clazz = (Class) ((ParameterizedType) casField.getGenericType()).getActualTypeArguments()[0];
  286. }
  287. return clazz;
  288. }
  289. /**
  290. * 通过Class获取其对象类型
  291. * @param clazz
  292. * @return
  293. */
  294. private String getObjTypeByClass(Class clazz) {
  295. // 获取关联对象的类型
  296. String objectType = null;
  297. try {
  298. objectType = (String) ReflectUtil.getFieldValue(clazz.newInstance(), "objectType");
  299. } catch (Exception e) {
  300. log.error(e.getMessage(), e);
  301. }
  302. return objectType;
  303. }
  304. /**
  305. * 新增
  306. * @param context
  307. * @param clazz
  308. * @param voList
  309. * @return
  310. */
  311. @Override
  312. public List<T> doInsert(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  313. if(CollUtil.isEmpty(voList)) {
  314. return null;
  315. }
  316. ArrayNode vos = AdmEntityTransferUtil.toDmpMultiEntity(voList);
  317. // 调用中台新增
  318. DmpResult<JSONArray> response = rwdClient.createObject(context, JSONArray.parseArray(vos.toString()));
  319. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  320. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  321. }
  322. // 转换为结果
  323. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  324. }
  325. /**
  326. * 修改
  327. * @param context
  328. * @param clazz
  329. * @param voList
  330. * @return
  331. */
  332. @Override
  333. public List<T> doUpdate(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  334. if(CollUtil.isEmpty(voList)) {
  335. return null;
  336. }
  337. ArrayNode vos = AdmEntityTransferUtil.toDmpMultiEntity(voList);
  338. // 调用中台修改
  339. DmpResult<cn.hutool.json.JSONArray> response = rwdClient.updateObject(context, JSONArray.parseArray(vos.toString()));
  340. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  341. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  342. }
  343. // 转换为结果
  344. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  345. }
  346. /**
  347. * 删除
  348. * @param context
  349. * @param voList
  350. * @return
  351. */
  352. @Override
  353. public void doDelete(InstanceUrlParam context, List<T> voList) {
  354. if(CollUtil.isEmpty(voList)) {
  355. return;
  356. }
  357. List<String> idList = CollUtil.getFieldValues(voList, "id", String.class);
  358. // 调用中台修改
  359. DmpResult response = rwdClient.deleteObject(context, idList);
  360. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  361. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  362. }
  363. }
  364. /**
  365. * 根据物理世界查询条件查询对象信息
  366. * @param context
  367. * @param dmpRequest
  368. * @param clazz
  369. * @return
  370. */
  371. @Override
  372. public AdmResponse doQuery(InstanceUrlParam context, QueryCriteria dmpRequest, Class<T> clazz) {
  373. // 转换参数
  374. JSONObject para = null;
  375. try {
  376. para = JsonHelper.toJsonObject(dmpRequest);
  377. } catch (IOException e) {
  378. log.error(e.getMessage(), e);
  379. return null;
  380. }
  381. // 调用中台查询
  382. DmpResult<JSONArray> dmpResult = rwdClient.queryObject(context, para);
  383. List<T> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), null, clazz);
  384. // JSONArray data = dmpResult.getData();
  385. // List<T> admVOs = new ArrayList<>();
  386. // if(CollUtil.isNotEmpty(data)){
  387. // admVOs = data.toJavaList(clazz);
  388. // }
  389. // 设置返回值
  390. AdmResponse response = AdmResponse.success(admVOs);
  391. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  392. response.setTotal(total);
  393. return response;
  394. }
  395. }