AbstractAdmBaseServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. // 获取关联对象的Class
  95. Field casField = ReflectUtil.getField(simpleVO.getClass(), cascade.getName());
  96. CascadeColumn annotationInfo = casField.getAnnotation(CascadeColumn.class);
  97. // 没有扩展信息不处理
  98. if(annotationInfo == null) {
  99. continue;
  100. }
  101. Class clazz = getFieldRealClass(casField);
  102. // 获取关联对象的类型
  103. String objectType = getObjTypeByClass(clazz);
  104. if(StrUtil.isBlank(objectType)) {
  105. continue;
  106. }
  107. cascade.setName(objectType);
  108. // 转换为数据中台查询条件
  109. QueryCriteria dmpRequest = AdmQueryCriteriaHelper.toDmpCriteria(cascade);
  110. // 拼接关系条件,得到关系映射
  111. Map<String, List<String>> idMap = ensureRelationCond(context, dmpRequest.getCriteria(), voMap.keySet(), annotationInfo);
  112. if(MapUtil.isEmpty(idMap)) {
  113. continue;
  114. }
  115. // 查询结果
  116. List vos = queryByCondition(context, dmpRequest, cascade.getProjection(), clazz);
  117. if(CollUtil.isEmpty(vos)) {
  118. continue;
  119. }
  120. // 写回原对象中
  121. writeToVOs(voMap, idMap, vos, casField);
  122. // 递归继续处理
  123. processCascade(context, vos, objectType, cascade.getCascade());
  124. }
  125. }
  126. /**
  127. * 拼接关系条件
  128. * @param context
  129. * @param node
  130. * @param idList
  131. * @param annotationInfo
  132. * @return
  133. */
  134. private Map<String, List<String>> ensureRelationCond(InstanceUrlParam context, ObjectNode node, Set<String> idList, CascadeColumn annotationInfo) {
  135. if(CollUtil.isEmpty(idList)) {
  136. return null;
  137. }
  138. // 查询关系
  139. List<ObjectRelation> relations = queryRelations(context, idList, annotationInfo);
  140. ArrayNode array = node.putObject(BaseEntity.PROP_ID).putArray("$in");
  141. // 拼接查询条件
  142. if(CollUtil.isEmpty(relations)) {
  143. array.add("0");
  144. } else {
  145. List<String> relList = CollUtil.getFieldValues(relations, annotationInfo.selectToObj()?"objTo":"objFrom", String.class);
  146. relList.forEach(id -> array.add(id));
  147. }
  148. if(annotationInfo.selectToObj()){
  149. return groupMapList(relations, "objFrom", "objTo");
  150. }else{
  151. return groupMapList(relations, "objTo", "objFrom");
  152. }
  153. }
  154. /**
  155. * 将集合转换为Map
  156. * @param list 集合
  157. * @param keyField key属性名
  158. * @param valueField value属性名
  159. * @return
  160. */
  161. private static Map<String, List<String>> groupMapList(List<?> list, String keyField, String valueField) {
  162. if(CollUtil.isEmpty(list)) {
  163. return null;
  164. }
  165. Map<String, List<String>> groupMap = new HashMap<>();
  166. for(Object vo:list) {
  167. String key = (String) ReflectUtil.getFieldValue(vo, keyField);
  168. String value = (String) ReflectUtil.getFieldValue(vo, valueField);
  169. if(!StrUtil.isAllNotBlank(key, value)) {
  170. continue;
  171. }
  172. List<String> valueList = groupMap.get(key);
  173. if(CollUtil.isEmpty(valueList)) {
  174. valueList = new ArrayList<>();
  175. groupMap.put(key, valueList);
  176. }
  177. valueList.add(value);
  178. }
  179. return groupMap;
  180. }
  181. /**
  182. * 查询关系
  183. * @param context
  184. * @param idList
  185. * @param anno
  186. * @return
  187. */
  188. private List<ObjectRelation> queryRelations(InstanceUrlParam context, Set<String>idList, CascadeColumn anno) {
  189. // 如果没有任何配置,不处理
  190. if(StrUtil.isBlank(anno.graphCode()) && StrUtil.isBlank(anno.relCode()) &&
  191. StrUtil.isBlank(anno.relValue())) {
  192. return null;
  193. }
  194. // 查询关系
  195. QueryCriteria queryRequest = new QueryCriteria();
  196. ObjectNode criteria = JsonNodeFactory.instance.objectNode();
  197. queryRequest.setCriteria(criteria);
  198. boolean isSelectTo = anno.selectToObj();
  199. putString(criteria, "graphCode", anno.graphCode());
  200. putString(criteria, "relCode", anno.relCode());
  201. putString(criteria, "relValue", anno.relValue());
  202. ArrayNode array = criteria.putObject(isSelectTo?"objFrom":"objTo").putArray("$in");
  203. idList.forEach(id -> array.add(id));
  204. return SpringHelper.getBean(IAdmRelationService.class).queryByCondition(context, queryRequest);
  205. }
  206. /**
  207. * 添加字符串条件
  208. * @param node
  209. * @param key
  210. * @param value
  211. */
  212. private void putString(ObjectNode node, String key, String value) {
  213. if(StrUtil.isBlank(value)) {
  214. return;
  215. }
  216. node.put(key, value);
  217. }
  218. /**
  219. * 将子集写回到对象中
  220. * @param voMap 原始对象Map
  221. * @param idMap 主键映射<mainId, List<subId>>
  222. * @param vos 子对象
  223. * @param casField 子对象对应属性
  224. */
  225. private <V> void writeToVOs(Map<String, Object> voMap, Map<String, List<String>> idMap, List<V> vos, Field casField) {
  226. if(CollUtil.isEmpty(vos) || MapUtil.isEmpty(idMap)) {
  227. return;
  228. }
  229. // 转换为Map
  230. Map<String, V> subVOMap = CollUtil.fieldValueMap(vos, BaseEntity.PROP_ID);
  231. // 是否为集合
  232. boolean isList = List.class.isAssignableFrom(casField.getType());
  233. // 按对象循环
  234. for(String id:voMap.keySet()) {
  235. Object mainVO = voMap.get(id);
  236. List<String> subIdList = idMap.get(id);
  237. if(CollUtil.isEmpty(subIdList)) {
  238. continue;
  239. }
  240. // 如果是集合
  241. if(isList) {
  242. // 取出对应的对象集合
  243. Map<String, V> currVOMap = MapUtil.filter(subVOMap, subIdList.toArray(new String[0]));
  244. List<V> voList = CollUtil.newArrayList(currVOMap.values());
  245. ReflectUtil.setFieldValue(mainVO, casField, voList);
  246. } else {
  247. // 非集合取第一个即可
  248. String subId = subIdList.get(0);
  249. V vo = subVOMap.get(subId);
  250. ReflectUtil.setFieldValue(mainVO, casField, vo);
  251. }
  252. }
  253. }
  254. /**
  255. * 调用Client查询
  256. * @param context
  257. * @param criteria
  258. * @param projection
  259. * @param clazz
  260. * @return
  261. */
  262. private <V> List<V> queryByCondition(InstanceUrlParam context, QueryCriteria criteria, List<String> projection, Class<V> clazz) {
  263. // 转换参数
  264. if(criteria == null) {
  265. return null;
  266. }
  267. List<ObjectNode> objects = DigitalObjectFacade.query(context.getGroupCode(), context.getProjectId(),
  268. context.getAppId(), null, criteria);
  269. return AdmEntityTransferUtil.toAdmMultiEntity(objects, projection, clazz);
  270. }
  271. /**
  272. * 获取实际的Class
  273. * @param casField
  274. * @return
  275. */
  276. private Class getFieldRealClass(Field casField) {
  277. // 取到Field
  278. Class clazz = casField.getType();
  279. // 如果是集合,则取实际的对象
  280. if(List.class.isAssignableFrom(clazz)) {
  281. clazz = (Class) ((ParameterizedType) casField.getGenericType()).getActualTypeArguments()[0];
  282. }
  283. return clazz;
  284. }
  285. /**
  286. * 通过Class获取其对象类型
  287. * @param clazz
  288. * @return
  289. */
  290. private String getObjTypeByClass(Class clazz) {
  291. // 获取关联对象的类型
  292. String objectType = null;
  293. try {
  294. objectType = (String) ReflectUtil.getFieldValue(clazz.newInstance(), "objectType");
  295. } catch (Exception e) {
  296. log.error(e.getMessage(), e);
  297. }
  298. return objectType;
  299. }
  300. /**
  301. * 新增
  302. * @param context
  303. * @param clazz
  304. * @param voList
  305. * @return
  306. */
  307. @Override
  308. public List<T> doInsert(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  309. if(CollUtil.isEmpty(voList)) {
  310. return null;
  311. }
  312. ArrayNode vos = AdmEntityTransferUtil.toDmpMultiEntity(voList);
  313. // 调用中台新增
  314. DmpResult<JSONArray> response = rwdClient.createObject(context, JSONArray.parseArray(vos.toString()));
  315. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  316. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  317. }
  318. // 转换为结果
  319. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  320. }
  321. /**
  322. * 修改
  323. * @param context
  324. * @param clazz
  325. * @param voList
  326. * @return
  327. */
  328. @Override
  329. public List<T> doUpdate(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  330. if(CollUtil.isEmpty(voList)) {
  331. return null;
  332. }
  333. ArrayNode vos = AdmEntityTransferUtil.toDmpMultiEntity(voList);
  334. // 调用中台修改
  335. DmpResult<cn.hutool.json.JSONArray> response = rwdClient.updateObject(context, JSONArray.parseArray(vos.toString()));
  336. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  337. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  338. }
  339. // 转换为结果
  340. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  341. }
  342. /**
  343. * 删除
  344. * @param context
  345. * @param voList
  346. * @return
  347. */
  348. @Override
  349. public void doDelete(InstanceUrlParam context, List<T> voList) {
  350. if(CollUtil.isEmpty(voList)) {
  351. return;
  352. }
  353. List<String> idList = CollUtil.getFieldValues(voList, "id", String.class);
  354. // 调用中台修改
  355. DmpResult response = rwdClient.deleteObject(context, idList);
  356. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  357. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  358. }
  359. }
  360. /**
  361. * 根据物理世界查询条件查询对象信息
  362. * @param context
  363. * @param dmpRequest
  364. * @param clazz
  365. * @return
  366. */
  367. @Override
  368. public AdmResponse doQuery(InstanceUrlParam context, QueryCriteria dmpRequest, Class<T> clazz) {
  369. // 转换参数
  370. JSONObject para = null;
  371. try {
  372. para = JsonHelper.toJsonObject(dmpRequest);
  373. } catch (IOException e) {
  374. log.error(e.getMessage(), e);
  375. return null;
  376. }
  377. // 调用中台查询
  378. DmpResult<JSONArray> dmpResult = rwdClient.queryObject(context, para);
  379. JSONArray data = dmpResult.getData();
  380. List<T> admVOs = new ArrayList<>();
  381. if(CollUtil.isNotEmpty(data)){
  382. admVOs = data.toJavaList(clazz);
  383. }
  384. // 设置返回值
  385. AdmResponse response = AdmResponse.success(admVOs);
  386. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  387. response.setTotal(total);
  388. return response;
  389. }
  390. }