AbstractAdmDictBaseServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package com.persagy.proxy.dictionary.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.constant.ValidEnum;
  11. import com.persagy.dmp.common.utils.JsonHelper;
  12. import com.persagy.dmp.define.entity.ObjectInfoCollect;
  13. import com.persagy.proxy.adm.constant.AdmCommonConstant;
  14. import com.persagy.proxy.adm.constant.AdmDictConstant;
  15. import com.persagy.proxy.object.model.AdmObjectInfo;
  16. import com.persagy.proxy.dictionary.model.AdmObjectType;
  17. import com.persagy.proxy.adm.request.AdmQueryCriteria;
  18. import com.persagy.proxy.adm.request.AdmResponse;
  19. import com.persagy.proxy.dictionary.service.IAdmDictBaseService;
  20. import com.persagy.proxy.adm.utils.AdmEntityTransferUtil;
  21. import com.persagy.proxy.adm.utils.AdmQueryCriteriaHelper;
  22. import com.persagy.proxy.common.client.DmpDicClient;
  23. import com.persagy.proxy.common.client.DmpRwdClient;
  24. import com.persagy.proxy.common.entity.DmpResult;
  25. import com.persagy.proxy.common.entity.InstanceUrlParam;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Value;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Set;
  34. /**
  35. * @ClassName AbstractAdmDictBaseServiceImpl
  36. * @Description: 字典查询
  37. * @Author linhuili
  38. * @Date 2021/8/31 18:21
  39. * @Version V1.0
  40. **/
  41. @Slf4j
  42. public class AbstractAdmDictBaseServiceImpl<T> implements IAdmDictBaseService<T> {
  43. private static final Set<String> TABLE_INFOS=CollUtil.newHashSet("id","code","localId",
  44. "localName","groupCode","projectId","objType","classCode","grouping","virtualCodes","createApp","updateApp",
  45. "creator","creationTime","modifier","modifiedTime","valid","floorSequenceID");
  46. @Autowired
  47. private DmpRwdClient rwdClient;
  48. @Autowired
  49. private DmpDicClient dicClient;
  50. @Value("${middleware.group.code}")
  51. private String groupCode;
  52. /**
  53. * 查询DMP字典数据
  54. * @param context
  55. * @param dmpRequest
  56. * @param clazz
  57. * @return
  58. */
  59. @Override
  60. public AdmResponse doQueryDictData(InstanceUrlParam context,QueryCriteria dmpRequest, Class<T> clazz) {
  61. // 转换参数
  62. JSONObject para = null;
  63. try {
  64. para = JsonHelper.toJsonObject(dmpRequest);
  65. } catch (IOException e) {
  66. log.error(e.getMessage(), e);
  67. return null;
  68. }
  69. // 调用中台查询
  70. DmpResult<JSONArray> dmpResult = dicClient.queryDictData(context, para);
  71. List<AdmObjectType> admVOs = dictDataVOTransfer(dmpResult.getData());
  72. // 设置返回值
  73. AdmResponse response = AdmResponse.success(admVOs);
  74. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  75. response.setTotal(total);
  76. return response;
  77. }
  78. /**
  79. * 查询DMP对象类型
  80. * @param context
  81. * @param dmpRequest
  82. * @param clazz
  83. * @return
  84. */
  85. @Override
  86. public AdmResponse doQueryObjectType(InstanceUrlParam context,QueryCriteria dmpRequest, Class<T> clazz) {
  87. // 转换参数
  88. JSONObject para = null;
  89. try {
  90. para = JsonHelper.toJsonObject(dmpRequest);
  91. } catch (IOException e) {
  92. log.error(e.getMessage(), e);
  93. return null;
  94. }
  95. // 调用中台查询
  96. DmpResult<JSONArray> dmpResult = rwdClient.queryObjectType(context, para);
  97. JSONArray data = dmpResult.getData();
  98. List<AdmObjectType> admVOs = new ArrayList<>();
  99. if(CollUtil.isNotEmpty(data)){
  100. admVOs = data.toJavaList(AdmObjectType.class);
  101. }
  102. //参数格式转换
  103. dictDefTypeTransfer(admVOs);
  104. // 设置返回值
  105. AdmResponse response = AdmResponse.success(admVOs);
  106. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  107. response.setTotal(total);
  108. return response;
  109. }
  110. /**
  111. * 查询对象信息点
  112. * @param request
  113. * @return
  114. */
  115. @Override
  116. public AdmResponse doQueryObjectInfo(InstanceUrlParam context, AdmQueryCriteria request,String fieldName, Class<T> clazz) {
  117. // 转换为数据中台查询条件
  118. QueryCriteria dmpRequest = toDmpCriteria(request,fieldName);
  119. // 转换参数
  120. JSONObject para = null;
  121. try {
  122. para = JsonHelper.toJsonObject(dmpRequest);
  123. } catch (IOException e) {
  124. log.error(e.getMessage(), e);
  125. return null;
  126. }
  127. // 调用中台查询
  128. DmpResult<JSONArray> dmpResult = rwdClient.queryObjectInfo(context, para);
  129. List<AdmObjectInfo> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), request.getProjection(), AdmObjectInfo.class);
  130. // BDTP信息点格式与ADM兼容处理
  131. handleObjectInfoResult(admVOs);
  132. // 设置返回值
  133. AdmResponse response = AdmResponse.success(admVOs);
  134. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  135. response.setTotal(total);
  136. response.setPageNumber(request.getPageNumber());
  137. response.setPageSize(request.getPageSize());
  138. return response;
  139. }
  140. /**
  141. * 修改
  142. * @param context
  143. * @param clazz
  144. * @param voList
  145. * @return
  146. */
  147. @Override
  148. public List<T> doUpdateObjectInfo(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  149. if(CollUtil.isEmpty(voList)) {
  150. return null;
  151. }
  152. ArrayNode vos = JsonNodeUtils.toArrayNode(voList, null, null);
  153. // 调用中台修改
  154. DmpResult<cn.hutool.json.JSONArray> response = rwdClient.batchUpdateObjectInfo(context, JSONArray.parseArray(vos.toString()));
  155. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  156. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  157. }
  158. // 转换为结果
  159. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  160. }
  161. /**
  162. * 删除信息点
  163. * @param context
  164. * @param voList
  165. * @return
  166. */
  167. @Override
  168. public void doDeleteObjectInfo(InstanceUrlParam context, List<T> voList) {
  169. if(CollUtil.isEmpty(voList)) {
  170. return;
  171. }
  172. List<String> idList = CollUtil.getFieldValues(voList, "id", String.class);
  173. // 调用中台修改
  174. DmpResult response = rwdClient.batchDeleteObjectInfo(context, idList);
  175. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  176. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  177. }
  178. }
  179. /**
  180. * 信息点格式与ADM兼容处理
  181. * @param admVOs
  182. */
  183. private void handleObjectInfoResult(List<AdmObjectInfo> admVOs){
  184. if(CollUtil.isEmpty(admVOs)){
  185. return;
  186. }
  187. for (AdmObjectInfo admVO : admVOs) {
  188. admVO.setFirstName(admVO.getFirstTag());
  189. admVO.setSecondName(admVO.getSecondTag());
  190. admVO.setMultiple(admVO.getIsMultiple());
  191. admVO.setRegion(admVO.getIsRegion());
  192. admVO.setGroupCode(groupCode);
  193. admVO.setSort(admVO.getShowOrder());
  194. //path路径处理
  195. // if(StringUtils.isEmpty(admVO.getFirstName())
  196. // || AdmDictConstant.FIRST_BASE_TAG.equals(admVO.getFirstName())
  197. // || AdmDictConstant.HIS_FIRST_BASE_TAG.equals(admVO.getFirstName())
  198. // || AdmDictConstant.HIS_FIRST_BASE_TAG_TWO.equals(admVO.getFirstName())){
  199. // admVO.setPath(admVO.getCode());
  200. // }else{
  201. // admVO.setPath(AdmDictConstant.INFOS_EX+admVO.getCode());
  202. // }
  203. if (TABLE_INFOS.contains(admVO.getCode())){
  204. admVO.setPath(admVO.getCode());
  205. }else {
  206. admVO.setPath(AdmDictConstant.INFOS_EX+admVO.getCode());
  207. }
  208. //信息点是否可见设置默认值
  209. if(admVO.getVisible() == null){
  210. admVO.setVisible(false);
  211. }
  212. }
  213. }
  214. /**
  215. * 字典数据格式与ADM兼容
  216. * @param array
  217. */
  218. private List<AdmObjectType> dictDataVOTransfer(JSONArray array){
  219. List<AdmObjectType> result = new ArrayList<>();
  220. if(CollUtil.isEmpty(array)){
  221. return result;
  222. }
  223. ArrayNode dataNode = JsonNodeUtils.toArrayNode(array, null, null);
  224. for (JsonNode dicData : dataNode) {
  225. AdmObjectType objectType = new AdmObjectType();
  226. objectType.setCode(dicData.get("code").textValue());
  227. objectType.setName(dicData.get("label").textValue());
  228. objectType.setAliasCode(dicData.get("code").textValue());
  229. objectType.setAliasName(dicData.get("label").textValue());
  230. objectType.setObjType(dicData.get("type").textValue());
  231. result.add(objectType);
  232. }
  233. return result;
  234. }
  235. /**
  236. * 将ADM的查询条件转换为DMP的
  237. * 调用前注意:
  238. * - 调用前将name赋值,才会将条件中adm的属性替换为dmp的属性
  239. * - 统计查询时将isOnlyCount设置为true
  240. * 调用后注意(需要调用者自行处理的):
  241. * - 不处理级联,级联应在调用者里处理级联调用转换后查询赋值
  242. * - 不处理projection,查询后自行根据projection筛选返回属性。(dmp只能指定扩展属性,而adm的是针对所有属性的)
  243. * @param admCriteria
  244. * @return
  245. */
  246. public static QueryCriteria toDmpCriteria(AdmQueryCriteria admCriteria,String fieldName) {
  247. QueryCriteria dmpCriteria = new QueryCriteria();
  248. // 拷贝分页
  249. AdmQueryCriteriaHelper.copyPageInfo(admCriteria, dmpCriteria);
  250. // 处理filters
  251. dmpCriteria.setCriteria(AdmQueryCriteriaHelper.toCriteria(admCriteria.getName(), admCriteria.getFilters()));
  252. if(StrUtil.isNotEmpty(admCriteria.getName())){
  253. // 处理name
  254. dmpCriteria.getCriteria().put(fieldName, admCriteria.getName());
  255. }
  256. dmpCriteria.getCriteria().put(AdmCommonConstant.VALID, ValidEnum.TRUE.getType());
  257. // 处理排序
  258. dmpCriteria.setOrders(AdmQueryCriteriaHelper.toOrderItem(admCriteria.getOrders()));
  259. // 处理统计
  260. dmpCriteria.setOnlyCount(admCriteria.isOnlyCount());
  261. return dmpCriteria;
  262. }
  263. /**
  264. * 字典类型返回结构与ADM兼容
  265. * @param data
  266. */
  267. private List<AdmObjectType> dictDefTypeTransfer(List<AdmObjectType> data){
  268. if(CollUtil.isEmpty(data)){
  269. return new ArrayList<>();
  270. }
  271. for (AdmObjectType datum : data) {
  272. String projectId = datum.getProjectId();
  273. String groupCode = datum.getGroupCode();
  274. if(StringUtils.isNotEmpty(groupCode) && !"0".equals(groupCode)){
  275. //集团级
  276. datum.setType("group");
  277. }else if(StringUtils.isNotEmpty(projectId) && !"0".equals(projectId)){
  278. //项目级
  279. datum.setType("project");
  280. }else{
  281. //平台级
  282. datum.setType("common");
  283. }
  284. }
  285. return data;
  286. }
  287. /**
  288. * 查询对象信息点
  289. * @param request
  290. * @return
  291. */
  292. @Override
  293. public AdmResponse doQueryObjectInfoCollect(InstanceUrlParam context, AdmQueryCriteria request,String fieldName, Class<T> clazz) {
  294. // 转换为数据中台查询条件
  295. QueryCriteria dmpRequest = toDmpCriteria(request,fieldName);
  296. // 转换参数
  297. JSONObject para = null;
  298. try {
  299. para = JsonHelper.toJsonObject(dmpRequest);
  300. } catch (IOException e) {
  301. log.error(e.getMessage(), e);
  302. return null;
  303. }
  304. // 调用中台查询
  305. log.info(para.toString());
  306. DmpResult<JSONArray> dmpResult = rwdClient.queryObjectInfoCollect(context, para);
  307. List<ObjectInfoCollect> admVOs = AdmEntityTransferUtil.toAdmMultiEntity(dmpResult.getData(), request.getProjection(), ObjectInfoCollect.class);
  308. // 设置返回值
  309. AdmResponse response = AdmResponse.success(admVOs);
  310. Long total = dmpResult.getCount() == null ? null : dmpResult.getCount().longValue();
  311. response.setTotal(total);
  312. response.setPageNumber(request.getPageNumber());
  313. response.setPageSize(request.getPageSize());
  314. return response;
  315. }
  316. /**
  317. * 设置采集信息
  318. * @param context
  319. * @param clazz
  320. * @param voList
  321. * @return
  322. */
  323. @Override
  324. public List<T> doSetObjectInfoCollect(InstanceUrlParam context, Class<T> clazz, List<T> voList) {
  325. if(CollUtil.isEmpty(voList)) {
  326. return null;
  327. }
  328. ArrayNode vos = JsonNodeUtils.toArrayNode(voList, null, null);
  329. // 调用中台修改
  330. DmpResult<cn.hutool.json.JSONArray> response = rwdClient.batchUpdateObjectInfoCollect(context, JSONArray.parseArray(vos.toString()));
  331. if(!DmpResult.SUCCESS.equals(response.getResult())) {
  332. throw new RuntimeException("调用中台查询接口出错:" + response.getMessage());
  333. }
  334. // 转换为结果
  335. return AdmEntityTransferUtil.toAdmMultiEntity(response.getData(), null, clazz);
  336. }
  337. }