|
|
@@ -0,0 +1,120 @@
|
|
|
+package com.uas.sso.crawler.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.uas.sso.common.util.HttpUtil;
|
|
|
+import com.uas.sso.core.ICallable;
|
|
|
+import com.uas.sso.crawler.service.CrawlerUserSpaceService;
|
|
|
+import com.uas.sso.entity.App;
|
|
|
+import com.uas.sso.entity.SyncLog;
|
|
|
+import com.uas.sso.entity.Userspace;
|
|
|
+import com.uas.sso.logging.LoggerManager;
|
|
|
+import com.uas.sso.logging.SyncBufferedLogger;
|
|
|
+import com.uas.sso.service.AppService;
|
|
|
+import com.uas.sso.service.UserspaceService;
|
|
|
+import com.uas.sso.support.SyncFail;
|
|
|
+import com.uas.sso.util.ExecuteUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wangmh
|
|
|
+ * @create 2018-12-17 17:07
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+public class CrawlerUserSpaceServiceImpl implements CrawlerUserSpaceService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserspaceService userspaceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AppService appService;
|
|
|
+
|
|
|
+ private SyncBufferedLogger syncLogger = LoggerManager.getLogger(SyncBufferedLogger.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void completeUserspaceInfo(String json) {
|
|
|
+ // 1.解析json
|
|
|
+ List<Userspace> newSpaceList = JSON.parseArray(json, Userspace.class);
|
|
|
+ // 2.1企业uu号集合,方便查询
|
|
|
+ List<Long> spaceUUList = new ArrayList<>();
|
|
|
+ // 2.2存储企业信息,方便根据uu号查询
|
|
|
+ Map<Long, Userspace> userSpaceMap = new LinkedHashMap<>(newSpaceList.size());
|
|
|
+ // 企业uu号,临时变量
|
|
|
+ Long spaceUU;
|
|
|
+
|
|
|
+ // 3.数据整理,将数据放入map和list中
|
|
|
+ for (Userspace newSpace : newSpaceList) {
|
|
|
+ if (StringUtils.isEmpty(spaceUU = newSpace.getSpaceUU()) || userSpaceMap.containsKey(spaceUU)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ userSpaceMap.put(spaceUU, newSpace);
|
|
|
+ spaceUUList.add(spaceUU);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4.根据uu号查询企业信息
|
|
|
+ List<Userspace> oldSpaceList = userspaceService.findAll(spaceUUList);
|
|
|
+
|
|
|
+ // 5.批量修改企业信息
|
|
|
+ for (Userspace oldSpace : oldSpaceList) {
|
|
|
+ Userspace newSpace = userSpaceMap.get(oldSpace.getSpaceUU());
|
|
|
+ // 修改数据 - 旧企业数据为空,新企业数据不为空则更新
|
|
|
+ if (StringUtils.isEmpty(oldSpace.getBusinessCode()) && !StringUtils.isEmpty(newSpace.getBusinessCode())) {
|
|
|
+ oldSpace.setRegAddress(newSpace.getBusinessCode());
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(oldSpace.getCorporation()) && !StringUtils.isEmpty(newSpace.getCorporation())) {
|
|
|
+ oldSpace.setRegAddress(newSpace.getCorporation());
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(oldSpace.getProfession()) && !StringUtils.isEmpty(newSpace.getProfession())) {
|
|
|
+ oldSpace.setRegAddress(newSpace.getProfession());
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(oldSpace.getTags()) && !StringUtils.isEmpty(newSpace.getTags())) {
|
|
|
+ oldSpace.setRegAddress(newSpace.getTags());
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(oldSpace.getRegAddress()) && !StringUtils.isEmpty(newSpace.getRegAddress())) {
|
|
|
+ oldSpace.setRegAddress(newSpace.getRegAddress());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ oldSpaceList = userspaceService.save(oldSpaceList);
|
|
|
+
|
|
|
+ String msg = "爬虫修改企业信息";
|
|
|
+ syncUserspaceInfo(oldSpaceList, msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步企业信息到其他应用(阻塞、慢速)
|
|
|
+ * @param userspaceList 企业信息列表
|
|
|
+ * @param msg 同步信息
|
|
|
+ */
|
|
|
+ private void syncUserspaceInfo(List<Userspace> userspaceList, String msg) {
|
|
|
+ List<App> apps = appService.findDefaultUseApp();
|
|
|
+ for (Userspace userspace : userspaceList) {
|
|
|
+ ExecuteUtils.execute((ICallable<Void, App>) app -> {
|
|
|
+ String url, appId, spaceStr;
|
|
|
+ HttpUtil.ResponseWrap res;
|
|
|
+ if (app != null && !StringUtils.isEmpty(url = app.getBackSpaceUrl())) {
|
|
|
+ appId = app.getUid();
|
|
|
+ JSONObject formData = JSON.parseObject(spaceStr = JSON.toJSONString(userspace));
|
|
|
+ try {
|
|
|
+ res = HttpUtil.doPost(url, formData, 30000);
|
|
|
+ if (!res.isSuccess()) {
|
|
|
+ SyncLog syncLog = syncLogger.error(appId, msg + ",同步企业信息失败", spaceStr, res.getContent());
|
|
|
+ SyncFail.add(syncLog.getId(), formData, url, app.getUid());
|
|
|
+ } else {
|
|
|
+ syncLogger.info(appId, msg + ",同步企业信息成功", spaceStr);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ syncLogger.error(appId, msg + ",同步企业信息失败", spaceStr, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }, apps);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|