|
|
@@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.lang.Validator;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
@@ -83,6 +84,8 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
|
|
|
private final CouponCommonService couponCommonService;
|
|
|
|
|
|
+ private final CorpFollowActiveCodeMapper corpFollowActiveCodeMapper;
|
|
|
+
|
|
|
private static final String DEFAULT_MSG = "您好,欢迎加入银河录像局";
|
|
|
|
|
|
/**
|
|
|
@@ -105,7 +108,7 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- THREAD_POOL.execute(()->{
|
|
|
+ THREAD_POOL.execute(() -> {
|
|
|
//2.客户添加成员信息
|
|
|
List<FollowedUser> followedUsers = externalContact.getFollowedUsers();
|
|
|
//保存企微用户
|
|
|
@@ -313,7 +316,7 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
.ifPresent(corpUser -> {
|
|
|
if (corpUser.getUserId() == 0) {
|
|
|
Optional.ofNullable(userMapper.selectOne(Wrappers.lambdaQuery(User.class)
|
|
|
- .eq(User::getUnionid, corpUser.getUnionid()).last("limit 1")))
|
|
|
+ .eq(User::getUnionid, corpUser.getUnionid()).last("limit 1")))
|
|
|
.ifPresent(user -> {
|
|
|
corpUser.setUserId(user.getId());
|
|
|
corpUserMapper.updateById(corpUser);
|
|
|
@@ -414,8 +417,8 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
public CorpUser saveOrUpdateCorpUser(CorpXmlMessage message, ExternalContact corpUserInfo, List<FollowedUser> followedUsers) {
|
|
|
//查询客户是否存在
|
|
|
CorpUser corpUser = Optional.ofNullable(
|
|
|
- corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
|
|
|
- .eq(CorpUser::getUnionid, corpUserInfo.getUnionId())))
|
|
|
+ corpUserMapper.selectOne(Wrappers.lambdaQuery(CorpUser.class)
|
|
|
+ .eq(CorpUser::getUnionid, corpUserInfo.getUnionId())))
|
|
|
.orElseGet(() -> new CorpUser()
|
|
|
.setUnionid(corpUserInfo.getUnionId()));
|
|
|
|
|
|
@@ -423,32 +426,42 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
corpUser.setGender(corpUserInfo.getGender());
|
|
|
corpUser.setAvatar(corpUserInfo.getAvatar());
|
|
|
corpUser.setType(corpUserInfo.getType());
|
|
|
- //扫码来源
|
|
|
- corpUser.setState(message.getState());
|
|
|
- //打标签
|
|
|
- if (StrUtil.equals(message.getState(), Constant.DY_CS_CODE_KEY) && CollUtil.isNotEmpty(followedUsers)) {
|
|
|
- //查询对应标签是否存在
|
|
|
- CorpTag corpTag = corpTagMapper.selectOne(Wrappers.lambdaQuery(CorpTag.class)
|
|
|
- .eq(CorpTag::getName, Constant.DY_CS_CODE_KEY).last("limit 1"));
|
|
|
- if (corpTag == null) {
|
|
|
+ String state = message.getState();
|
|
|
+ CorpFollowActiveCode corpFollowActiveCode = null;
|
|
|
+ if (StrUtil.isNotBlank(state) && corpUser.getId() == null && state.contains(Constant.AC_CODE_KEY)) {
|
|
|
+ //查找对应的活动活码 记录
|
|
|
+ String acIdStr = StrUtil.subAfter(state, StrUtil.C_COLON, true);
|
|
|
+ if (Validator.isNumber(acIdStr)) {
|
|
|
+ Long acId = Long.parseLong(acIdStr);
|
|
|
+ corpFollowActiveCode = corpFollowActiveCodeMapper.selectById(acId);
|
|
|
+ if (corpFollowActiveCode != null) {
|
|
|
+ corpUser.setAcId(acId);
|
|
|
+ String welcomeStr = corpFollowActiveCode.getWelcomeStr();
|
|
|
+ //发送对应欢迎语
|
|
|
+ if (StrUtil.isNotBlank(corpFollowActiveCode.getWelcomeStr())) {
|
|
|
+ THREAD_POOL.execute(() -> {
|
|
|
+ sendCorpFollowActiveCode(message, welcomeStr);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
try {
|
|
|
- corpTag = addCorpTag(Constant.DY_CS_CODE_KEY, Constant.DY_CS_CODE_KEY, message.getToUserName());
|
|
|
+ List<CorpFollow> corpFollows = Jsons.parseList(corpFollowActiveCode.getFollowStr(), CorpFollow.class);
|
|
|
+ List<String> followIds = corpFollows.stream().map(CorpFollow::getUserid).collect(Collectors.toList());
|
|
|
+ //对客户进行标记
|
|
|
+ markAddUserTag(corpFollowActiveCode.getMarkTag(), message.getToUserName(), corpUserInfo.getExternalUserId(), followIds, followedUsers);
|
|
|
} catch (Exception e) {
|
|
|
}
|
|
|
- }
|
|
|
- if (corpTag != null) {
|
|
|
- List<String> dy_tag = List.of(corpTag.getTagId());
|
|
|
- followedUsers.forEach(follow -> {
|
|
|
- if (!Constant.DY_CUS.contains(follow.getUserId())) {
|
|
|
- return;
|
|
|
- }
|
|
|
- try {
|
|
|
- wxCorpOps.markTag(corpUserInfo.getExternalUserId(), follow.getUserId(), dy_tag.toArray(new String[dy_tag.size()]), null);
|
|
|
- } catch (Exception e) {
|
|
|
- }
|
|
|
- });
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+ //扫码来源
|
|
|
+ if (corpUser.getId() == null) {
|
|
|
+ corpUser.setState(state);
|
|
|
+ }
|
|
|
+ //打标签 抖音渠道活码 已有
|
|
|
+ if (StrUtil.equals(message.getState(), Constant.DY_CS_CODE_KEY) && CollUtil.isNotEmpty(followedUsers)) {
|
|
|
+ markAddUserTag(Constant.DY_CS_CODE_KEY, message.getToUserName(), corpUserInfo.getExternalUserId(), Constant.DY_CUS, followedUsers);
|
|
|
+ }
|
|
|
|
|
|
//新增修改客户信息
|
|
|
if (corpUser.getId() == null) {
|
|
|
@@ -465,6 +478,11 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
}
|
|
|
//添加固定客服发放优惠券
|
|
|
isJoinFixedCustomer(user.getId(), message.getUserId());
|
|
|
+
|
|
|
+ //活码 优惠券发送
|
|
|
+ if (corpFollowActiveCode != null && corpFollowActiveCode.getCouponId() > 0) {
|
|
|
+ couponCommonService.sendCouponToUser(user.getId(), corpFollowActiveCode.getCouponId(), 0l, 0l, 0l, CouponUser.Channel.corp_active_code);
|
|
|
+ }
|
|
|
}
|
|
|
corpUserMapper.insert(corpUser);
|
|
|
} else {
|
|
|
@@ -500,4 +518,64 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送添加活动活码客服 发送欢迎语
|
|
|
+ */
|
|
|
+ public void sendCorpFollowActiveCode(CorpXmlMessage message, String welcomeStr) {
|
|
|
+ try {
|
|
|
+ String welcomeCode = message.getWelcomeCode();
|
|
|
+ String followId = message.getUserId();
|
|
|
+ //新用户欢迎语
|
|
|
+ JSONObject params = Jsons.parseObject(welcomeStr, JSONObject.class);
|
|
|
+ params.putOpt("welcome_code", welcomeCode);
|
|
|
+ HttpResponse<byte[]> res = null;
|
|
|
+ res = J11HttpC.custom()
|
|
|
+ .ofPost()
|
|
|
+ .url(String.format("https://qyapi.weixin.qq.com/cgi-bin/externalcontact/send_welcome_msg?access_token=%s", wxCorpOps.getAccessToken(message.getToUserName())))
|
|
|
+ .body(HttpRequest.BodyPublishers.ofString(params.toString(), IoKit.Charsets.UTF_8.getCharset()))
|
|
|
+ .headers(J11HttpC.ReqType.raw_json)
|
|
|
+ .send(HttpResponse.BodyHandlers.ofByteArray());
|
|
|
+ JSONObject jsonObject = Jsons.parseObject(res.body(), JSONObject.class);
|
|
|
+ if (jsonObject.getInt("errcode") == 0) {
|
|
|
+ log.info("{}成员,发送欢迎语至客户{}成功", followId, message.getExternalUserId());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ log.info("{}成员,发送欢迎语至客户{}失败,msg:{}", followId, message.getExternalUserId(), jsonObject.get("errmsg"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.info("发送欢迎语过程异常,{}", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建标签 并标记客户
|
|
|
+ */
|
|
|
+ public void markAddUserTag(String tagName, String corpId, String externalUserId, List<String> relateFollowIds, List<FollowedUser> followedUsers) {
|
|
|
+ //对客户进行标记
|
|
|
+ //查询对应标签是否存在
|
|
|
+ CorpTag corpTag = corpTagMapper.selectOne(Wrappers.lambdaQuery(CorpTag.class)
|
|
|
+ .eq(CorpTag::getName, tagName).last("limit 1"));
|
|
|
+ if (corpTag == null) {
|
|
|
+ try {
|
|
|
+ corpTag = addCorpTag(tagName, tagName, corpId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (corpTag != null) {
|
|
|
+ List<String> tag = List.of(corpTag.getTagId());
|
|
|
+ try {
|
|
|
+ followedUsers.forEach(follow -> {
|
|
|
+ if (!relateFollowIds.contains(follow.getUserId())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ wxCorpOps.markTag(externalUserId, follow.getUserId(), tag.toArray(new String[tag.size()]), null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|