Commit f5dffa4f by fudahua

企业为西标签

parent f641a17b
package com.gic.haoban.manage.api.service; package com.gic.haoban.manage.api.service;
import com.gic.haoban.base.api.common.ServiceResponse;
import com.gic.haoban.manage.api.dto.QywxTagCallbackDTO; import com.gic.haoban.manage.api.dto.QywxTagCallbackDTO;
import com.gic.haoban.manage.api.dto.QywxTagInfoDTO; import com.gic.haoban.manage.api.dto.QywxTagInfoDTO;
import com.gic.haoban.manage.api.dto.QywxTagItemDTO; import com.gic.haoban.manage.api.dto.QywxTagItemDTO;
...@@ -72,6 +73,6 @@ public interface QywxTagApiService { ...@@ -72,6 +73,6 @@ public interface QywxTagApiService {
* @param enterpriseId * @param enterpriseId
* @param externalUserId * @param externalUserId
*/ */
public void syncFriendTagToQywxByExternalUserId(String wxEnterpriseId, String enterpriseId, String externalUserId, String tagItemId); public ServiceResponse syncFriendTagToQywxByExternalUserId(String wxEnterpriseId, String enterpriseId, String externalUserId, List<String> tagItemIds);
} }
...@@ -67,4 +67,13 @@ public interface QywxTagItemMapper { ...@@ -67,4 +67,13 @@ public interface QywxTagItemMapper {
* @return * @return
*/ */
TabQywxTagItem getByQywxTagKey(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("qywxTagKey") String qywxTagKey); TabQywxTagItem getByQywxTagKey(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("qywxTagKey") String qywxTagKey);
/**
* 根据id 获取标签项信息
*
* @param wxEnterpriseId
* @param ids
* @return
*/
List<TabQywxTagItem> listByQywxTagItemsIds(@Param("wxEnterpriseId") String wxEnterpriseId, @Param("ids") List<String> ids);
} }
\ No newline at end of file
...@@ -156,6 +156,14 @@ public interface QywxTagService { ...@@ -156,6 +156,14 @@ public interface QywxTagService {
*/ */
public TabQywxTagRelation getQywxTagRelationByQywxKey(String wxEnterpriseId, String key, Integer qywxTagRelationType); public TabQywxTagRelation getQywxTagRelationByQywxKey(String wxEnterpriseId, String key, Integer qywxTagRelationType);
/**
* 根据企业微信的标签项获取 标签项列表
*
* @param wxEnterpriseId
* @param ids
* @return
*/
public List<TabQywxTagItem> listQywxTagItemByIds(String wxEnterpriseId, List<String> ids);
} }
...@@ -205,4 +205,9 @@ public class QywxTagServiceImpl implements QywxTagService { ...@@ -205,4 +205,9 @@ public class QywxTagServiceImpl implements QywxTagService {
return qywxTagRelationMapper.getQywxTagByQywxTagItemId(wxEnterpriseId, qywxTagItem.getQywxTagItemId()); return qywxTagRelationMapper.getQywxTagByQywxTagItemId(wxEnterpriseId, qywxTagItem.getQywxTagItemId());
} }
} }
@Override
public List<TabQywxTagItem> listQywxTagItemByIds(String wxEnterpriseId, List<String> ids) {
return qywxTagItemMapper.listByQywxTagItemsIds(wxEnterpriseId, ids);
}
} }
package com.gic.haoban.manage.service.service.out.impl; package com.gic.haoban.manage.service.service.out.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.gic.commons.util.ToolUtil; import com.gic.commons.util.ToolUtil;
import com.gic.haoban.base.api.common.ServiceResponse;
import com.gic.haoban.common.utils.EntityUtil; import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.manage.api.dto.*; import com.gic.haoban.manage.api.dto.*;
import com.gic.haoban.manage.api.enums.QywxTagRelationTypeEnum; import com.gic.haoban.manage.api.enums.QywxTagRelationTypeEnum;
...@@ -12,6 +14,7 @@ import com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise; ...@@ -12,6 +14,7 @@ import com.gic.haoban.manage.service.entity.TabHaobanWxEnterprise;
import com.gic.haoban.manage.service.entity.TabQywxTag; import com.gic.haoban.manage.service.entity.TabQywxTag;
import com.gic.haoban.manage.service.entity.TabQywxTagItem; import com.gic.haoban.manage.service.entity.TabQywxTagItem;
import com.gic.haoban.manage.service.entity.TabQywxTagRelation; import com.gic.haoban.manage.service.entity.TabQywxTagRelation;
import com.gic.haoban.manage.service.exception.WxApiLimitException;
import com.gic.haoban.manage.service.service.QywxTagService; import com.gic.haoban.manage.service.service.QywxTagService;
import com.gic.haoban.manage.service.service.WxEnterpriseService; import com.gic.haoban.manage.service.service.WxEnterpriseService;
import com.gic.wechat.api.dto.qywx.QywxTagDTO; import com.gic.wechat.api.dto.qywx.QywxTagDTO;
...@@ -20,6 +23,7 @@ import com.gic.wechat.api.dto.qywx.response.QywxGetCorpTagListDTO; ...@@ -20,6 +23,7 @@ import com.gic.wechat.api.dto.qywx.response.QywxGetCorpTagListDTO;
import com.gic.wechat.api.dto.qywx.response.QywxResponseDTO; import com.gic.wechat.api.dto.qywx.response.QywxResponseDTO;
import com.gic.wechat.api.service.qywx.QywxSuiteApiService; import com.gic.wechat.api.service.qywx.QywxSuiteApiService;
import com.gic.wechat.api.service.qywx.QywxUserApiService; import com.gic.wechat.api.service.qywx.QywxUserApiService;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -27,9 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -27,9 +31,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.slf4j.LoggerFactory.getLogger; import static org.slf4j.LoggerFactory.getLogger;
...@@ -58,6 +60,9 @@ public class QywxTagApiServiceImpl implements QywxTagApiService { ...@@ -58,6 +60,9 @@ public class QywxTagApiServiceImpl implements QywxTagApiService {
@Autowired @Autowired
private DealSyncOperationApiService dealSyncOperationApiService; private DealSyncOperationApiService dealSyncOperationApiService;
@Autowired
private QywxUserApiService qywxUserApiService;
@Override @Override
public void pullQywxTag(String wxEnterpriseId) { public void pullQywxTag(String wxEnterpriseId) {
WxEnterpriseDTO wxEnterpriseDTO = wxEnterpriseService.selectById(wxEnterpriseId); WxEnterpriseDTO wxEnterpriseDTO = wxEnterpriseService.selectById(wxEnterpriseId);
...@@ -432,7 +437,103 @@ public class QywxTagApiServiceImpl implements QywxTagApiService { ...@@ -432,7 +437,103 @@ public class QywxTagApiServiceImpl implements QywxTagApiService {
} }
@Override @Override
public void syncFriendTagToQywxByExternalUserId(String wxEnterpriseId, String enterpriseId, String externalUserId, String tagItemId) { public ServiceResponse syncFriendTagToQywxByExternalUserId(String wxEnterpriseId, String enterpriseId, String externalUserId, List<String> tagItemIds) {
ServiceResponse resp = new ServiceResponse();
List<TabQywxTagRelation> relations = qywxTagService.listAllQywxRelation(wxEnterpriseId);
if (CollectionUtils.isEmpty(relations)) {
resp.setCode(2);
resp.setMessage("没有需要同步的标签");
return resp;
}
List<String> syncTagItemsIds = relations.stream()
.filter(dto -> dto.getRelationType() == QywxTagRelationTypeEnum.TAG_ITEM.getType() && tagItemIds.contains(dto.getTagItemId()))
.map(dto -> dto.getQywxTagItemId()).collect(Collectors.toList());
if (CollectionUtils.isEmpty(syncTagItemsIds)) {
resp.setCode(3);
resp.setMessage("没有同步的标签项");
return resp;
}
//同步的标签项
List<TabQywxTagItem> tagItems = qywxTagService.listQywxTagItemByIds(wxEnterpriseId, syncTagItemsIds);
Set<String> qywxTagKes = tagItems.stream().map(dto -> dto.getQywxTagKey()).collect(Collectors.toSet());
WxEnterpriseDTO wxEnterprise = wxEnterpriseService.selectById(wxEnterpriseId);
//查询外部联系人
String externalUseridInfo = qywxUserApiService.getExternalUseridInfo(wxEnterprise.getCorpid(), config.getWxSuiteid(), externalUserId);
if (StringUtils.isBlank(externalUseridInfo)) {
logger.info("好友不存在");
resp.setCode(3);
resp.setMessage("好友不存在");
return resp;
} else if (externalUseridInfo.equals("1")) {
logger.info("限制次数");
resp.setCode(45033);
resp.setMessage("getExternalUseridInfo接口次数限制");
return resp;
}
Map<String, Set<String>> externalTagMap = getExternalTagMap(externalUseridInfo);
if (null == externalTagMap) {
logger.info("没有好友需要同步");
resp.setCode(5);
resp.setMessage("没有好友需要同步");
return resp;
}
//同步好友
externalTagMap.forEach((wxUserId, haveTagKes) -> {
//获取需要打的标签
Sets.SetView<String> needSetTags = Sets.difference(qywxTagKes, haveTagKes);
//todo 获取需要删除的标签
Set<String> needDelTags = new HashSet<>();
if (CollectionUtils.isEmpty(needDelTags) && CollectionUtils.isEmpty(needSetTags)) {
logger.info("不需要");
return;
}
QywxResponseDTO responseDTO = qywxUserApiService.markTag(wxEnterprise.getCorpid(), config.getWxSuiteid(), wxUserId, externalUserId, needSetTags, needDelTags);
if (responseDTO.getErrcode() != 0) {
logger.info("权限不对");
resp.setMessage(responseDTO.getErrmsg());
resp.setCode(responseDTO.getErrcode());
return;
}
});
return resp;
}
private Map<String, Set<String>> getExternalTagMap(String externalUseridInfo) {
Map<String, Set<String>> ret = new HashMap<>();
JSONObject jsonObject = JSONObject.parseObject(externalUseridInfo);
if (!jsonObject.containsKey("external_contact")) {
logger.info("用户信息不存在");
return null;
}
JSONObject externalContact = jsonObject.getJSONObject("external_contact");
if (!externalContact.containsKey("follow_user")) {
logger.info("没有相关的好友");
return null;
}
JSONArray followUser = externalContact.getJSONArray("follow_user");
for (int i = 0; i < followUser.size(); i++) {
JSONObject midUser = followUser.getJSONObject(i);
String wxUserId = midUser.getString("userid");
Set<String> midTagIds = new HashSet<>();
if (!midUser.containsKey("tags")) {
ret.put(wxUserId, midTagIds);
continue;
}
JSONArray tags = midUser.getJSONArray("tags");
for (int n = 0; n < tags.size(); n++) {
JSONObject tagMid = tags.getJSONObject(n);
String tagId = tagMid.getString("tag_id");
String tagType = tagMid.getString("type");
if (tagType.equals("1")) {
midTagIds.add(tagId);
}
}
ret.put(wxUserId, midTagIds);
}
return ret;
} }
} }
...@@ -187,4 +187,16 @@ ...@@ -187,4 +187,16 @@
and status_flag=1 and status_flag=1
and wx_enterprise_id=#{wxEnterpriseId} and wx_enterprise_id=#{wxEnterpriseId}
</select> </select>
<select id="listByQywxTagItemsIds" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from tab_haoban_qywx_tag_item
where qywx_tag_item_id in
<foreach collection="ids" open="(" close=")" separator="," item="item">
#{item}
</foreach>
and status_flag=1
and wx_enterprise_id=#{wxEnterpriseId}
</select>
</mapper> </mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment