|
|
@@ -0,0 +1,184 @@
|
|
|
+package com.fs.app.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.fs.common.core.controller.BaseController;
|
|
|
+import com.fs.common.core.domain.R;
|
|
|
+import com.fs.framework.datasource.TenantDataSourceUtil;
|
|
|
+import com.fs.qw.domain.QwCompany;
|
|
|
+import com.fs.qw.domain.QwExternalContact;
|
|
|
+import com.fs.qw.domain.QwUser;
|
|
|
+import com.fs.qw.mapper.QwCompanyMapper;
|
|
|
+import com.fs.qw.mapper.QwExternalContactMapper;
|
|
|
+import com.fs.qwApi.domain.QwExternalContactAllListResult;
|
|
|
+import com.fs.qwApi.domain.QwUnassignedListResult;
|
|
|
+import com.fs.qwApi.domain.inner.ExternalContact;
|
|
|
+import com.fs.qwApi.domain.inner.ExternalContactInfo;
|
|
|
+import com.fs.qwApi.domain.inner.FollowInfo;
|
|
|
+import com.fs.qwApi.param.QwExternalListParam;
|
|
|
+import com.fs.qwApi.param.QwUnassignedListParam;
|
|
|
+import com.fs.qwApi.service.QwApiService;
|
|
|
+import com.fs.voice.utils.StringUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 企微外部联系人同步Controller(API端)
|
|
|
+ * 由Company端/Admin端通过HTTP远程调用
|
|
|
+ *
|
|
|
+ * @author fs
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/qw/externalContact")
|
|
|
+@Slf4j
|
|
|
+public class QwExternalContactController extends BaseController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QwApiService qwApiService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QwCompanyMapper qwCompanyMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private QwExternalContactMapper qwExternalContactMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TenantDataSourceUtil tenantDataSourceUtil;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步企微外部联系人(递归分页)
|
|
|
+ *
|
|
|
+ * @param qwUser 企微用户信息(JSON)
|
|
|
+ * @param cursor 分页游标
|
|
|
+ * @param tenantId 租户ID
|
|
|
+ */
|
|
|
+ @PostMapping("/syncExternalContact")
|
|
|
+ public R syncExternalContact(@RequestParam("qwUser") String qwUserJson,
|
|
|
+ @RequestParam(value = "cursor", required = false) String cursor,
|
|
|
+ @RequestParam("tenantId") Long tenantId) {
|
|
|
+ log.info("[QwExternalContact] 同步外部联系人,tenantId={}", tenantId);
|
|
|
+ return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
|
|
|
+ // 从JSON还原QwUser对象
|
|
|
+ QwUser qwUser = JSON.parseObject(qwUserJson, QwUser.class);
|
|
|
+ return doSyncExternalContact(qwUser, cursor);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归同步外部联系人
|
|
|
+ */
|
|
|
+ private R doSyncExternalContact(QwUser qwUser, String getNextCursor) {
|
|
|
+ String qwUserId = qwUser.getQwUserId();
|
|
|
+ String corpId = qwUser.getCorpId();
|
|
|
+ Long companyId = qwUser.getCompanyId();
|
|
|
+
|
|
|
+ QwExternalListParam param = new QwExternalListParam();
|
|
|
+ param.setLimit(100);
|
|
|
+ param.setUserid_list(Arrays.asList(qwUserId));
|
|
|
+ param.setCursor(getNextCursor);
|
|
|
+ QwCompany qwCompany = qwCompanyMapper.selectQwCompanyByCorpId(corpId);
|
|
|
+ QwExternalContactAllListResult list = qwApiService.getAllExternalcontactList(param, corpId, qwCompany);
|
|
|
+
|
|
|
+ log.info("批量获取客户详情 {}", list);
|
|
|
+
|
|
|
+ if (list.getErrcode() == 0) {
|
|
|
+ List<ExternalContactInfo> externalContactList = list.getExternal_contact_list();
|
|
|
+ for (ExternalContactInfo externalContactInfo : externalContactList) {
|
|
|
+ ExternalContact externalContact = externalContactInfo.getExternal_contact();
|
|
|
+ FollowInfo followInfo = externalContactInfo.getFollow_info();
|
|
|
+ QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactUserIdAndExternalIdAndCompanyId(externalContact.getExternal_userid(), qwUserId, corpId);
|
|
|
+ log.info("客户详情 {}", qwExternalContact);
|
|
|
+
|
|
|
+ if (qwExternalContact == null) {
|
|
|
+ qwExternalContact = new QwExternalContact();
|
|
|
+ qwExternalContact.setUserId(qwUserId);
|
|
|
+ qwExternalContact.setExternalUserId(externalContact.getExternal_userid());
|
|
|
+ qwExternalContact.setCorpId(corpId);
|
|
|
+ qwExternalContact.setCompanyId(companyId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置公共属性
|
|
|
+ qwExternalContact.setCompanyUserId(qwUser.getCompanyUserId());
|
|
|
+ qwExternalContact.setQwUserId(qwUser.getId());
|
|
|
+ qwExternalContact.setName(externalContact.getName());
|
|
|
+ qwExternalContact.setAvatar(externalContact.getAvatar());
|
|
|
+ qwExternalContact.setType(externalContact.getType());
|
|
|
+ qwExternalContact.setGender(externalContact.getGender());
|
|
|
+ qwExternalContact.setDescription(followInfo.getDescription());
|
|
|
+ qwExternalContact.setRemark(followInfo.getRemark());
|
|
|
+ qwExternalContact.setTagIds(JSON.toJSONString(followInfo.getTag_id()));
|
|
|
+ qwExternalContact.setRemarkMobiles(JSON.toJSONString(followInfo.getRemark_mobiles()));
|
|
|
+ qwExternalContact.setState(followInfo.getState());
|
|
|
+
|
|
|
+ if (followInfo.getState() != null && !followInfo.getState().isEmpty()) {
|
|
|
+ String s = "way:" + corpId + ":";
|
|
|
+ if (followInfo.getState().contains(s)) {
|
|
|
+ String wayId = followInfo.getState().substring(followInfo.getState().indexOf(s) + s.length());
|
|
|
+ qwExternalContact.setWayId(Long.parseLong(wayId));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ qwExternalContact.setRemarkCorpName(followInfo.getRemark_corp_name());
|
|
|
+ qwExternalContact.setAddWay(followInfo.getAdd_way());
|
|
|
+ qwExternalContact.setOperUserid(followInfo.getOper_userid());
|
|
|
+
|
|
|
+ // 根据是否存在记录进行更新或插入
|
|
|
+ if (qwExternalContact.getId() != null) {
|
|
|
+ qwExternalContactMapper.updateQwExternalContact(qwExternalContact);
|
|
|
+ } else {
|
|
|
+ qwExternalContactMapper.insertQwExternalContact(qwExternalContact);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return R.error("同步失败:" + list.getErrmsg());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!StringUtil.strIsNullOrEmpty(list.getNext_cursor())) {
|
|
|
+ return doSyncExternalContact(qwUser, list.getNext_cursor());
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步待转接客户(离职待分配)
|
|
|
+ * 调用企微API获取待转接客户列表,更新本地客户状态
|
|
|
+ *
|
|
|
+ * @param corpId 企业微信corpId
|
|
|
+ * @param tenantId 租户ID
|
|
|
+ */
|
|
|
+ @PostMapping("/addUnassigned")
|
|
|
+ public R addUnassigned(@RequestParam("corpId") String corpId,
|
|
|
+ @RequestParam("tenantId") Long tenantId) {
|
|
|
+ log.info("[QwExternalContact] 同步待转接客户,tenantId={}, corpId={}", tenantId, corpId);
|
|
|
+ return tenantDataSourceUtil.executeWithResult(tenantId, () -> {
|
|
|
+ QwUnassignedListParam qwUnassignedListParam = new QwUnassignedListParam();
|
|
|
+ qwUnassignedListParam.setPage_size(1000);
|
|
|
+ QwUnassignedListResult unassignedList = qwApiService.getUnassignedList(qwUnassignedListParam, corpId);
|
|
|
+
|
|
|
+ if (unassignedList.getErrcode() == 0) {
|
|
|
+ List<QwUnassignedListResult.InfoEntry> info = unassignedList.getInfo();
|
|
|
+ if (info != null) {
|
|
|
+ for (QwUnassignedListResult.InfoEntry infoEntry : info) {
|
|
|
+ QwExternalContact qwExternalContact = new QwExternalContact();
|
|
|
+ qwExternalContact.setExternalUserId(infoEntry.getExternal_userid());
|
|
|
+ qwExternalContact.setQwOpenUserId(infoEntry.getHandover_userid());
|
|
|
+ qwExternalContact.setStatus(0);
|
|
|
+ List<QwExternalContact> qwExternalContacts = qwExternalContactMapper.selectQwExternalContactList(qwExternalContact);
|
|
|
+ if (qwExternalContacts != null && qwExternalContacts.size() > 0) {
|
|
|
+ for (QwExternalContact externalContact : qwExternalContacts) {
|
|
|
+ QwExternalContact q = new QwExternalContact();
|
|
|
+ q.setId(externalContact.getId());
|
|
|
+ q.setStatus(1);
|
|
|
+ qwExternalContactMapper.updateQwExternalContact(q);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return R.ok();
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|