|
|
@@ -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;
|
|
|
@@ -25,6 +26,7 @@ import com.cyksj.model.response.FollowedUser;
|
|
|
import com.cyksj.model.response.corp.CorpExternalContactInfo;
|
|
|
import com.cyksj.model.response.corp.CorpUserExternalTagGroupInfo;
|
|
|
import com.cyksj.model.response.corp.CorpUserExternalTagGroupList;
|
|
|
+import com.cyksj.service.corp.CorpService;
|
|
|
import com.cyksj.service.corp.WxCorpOps;
|
|
|
import com.cyksj.service.corp.msg.CorpEventActionService;
|
|
|
import com.cyksj.service.mange.coupon.CouponCommonService;
|
|
|
@@ -86,6 +88,10 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
|
|
|
private final OceanengineService oceanengineService;
|
|
|
|
|
|
+ private final CorpFollowActiveCodeMapper corpFollowActiveCodeMapper;
|
|
|
+
|
|
|
+ private final CorpService corpService;
|
|
|
+
|
|
|
private static final String DEFAULT_MSG = "您好,欢迎加入银河录像局";
|
|
|
|
|
|
/**
|
|
|
@@ -108,12 +114,12 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- THREAD_POOL.execute(()->{
|
|
|
+ THREAD_POOL.execute(() -> {
|
|
|
//2.客户添加成员信息
|
|
|
List<FollowedUser> followedUsers = externalContact.getFollowedUsers();
|
|
|
//保存企微用户
|
|
|
CorpUser corpUser = saveOrUpdateCorpUser(message, corpUserInfo, followedUsers);
|
|
|
- if (StrUtil.isNotBlank(message.getState())) {
|
|
|
+ if (StrUtil.isNotBlank(state)) {
|
|
|
try {
|
|
|
followedUsers = wxCorpOps.getExternalContact(message.getToUserName(), message.getExternalUserId()).getFollowedUsers();
|
|
|
} catch (Exception e) {
|
|
|
@@ -324,7 +330,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);
|
|
|
@@ -425,8 +431,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()));
|
|
|
|
|
|
@@ -434,8 +440,40 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
corpUser.setGender(corpUserInfo.getGender());
|
|
|
corpUser.setAvatar(corpUserInfo.getAvatar());
|
|
|
corpUser.setType(corpUserInfo.getType());
|
|
|
+ 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 welcomeContent = corpFollowActiveCode.getText();
|
|
|
+ String attachmentsStr = corpFollowActiveCode.getAttachmentsStr();
|
|
|
+ //发送对应欢迎语
|
|
|
+ if (StrUtil.isNotBlank(welcomeContent) || StrUtil.isNotBlank(attachmentsStr)) {
|
|
|
+ THREAD_POOL.execute(() -> {
|
|
|
+ sendCorpFollowActiveCode(message, welcomeContent, attachmentsStr);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<String> followIds = Jsons.parseList(corpFollowActiveCode.getFollowStr(), String.class);
|
|
|
+ //对客户进行标记
|
|
|
+ markAddUserTag(corpFollowActiveCode.getMarkTag(), message.getToUserName(), corpUserInfo.getExternalUserId(), followIds, followedUsers);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
//扫码来源
|
|
|
- corpUser.setState(message.getState());
|
|
|
+ if (corpUser.getId() == null) {
|
|
|
+ corpUser.setState(state);
|
|
|
+ }
|
|
|
+
|
|
|
+ //打标签 抖音渠道活码 已有 兼容之前的临时
|
|
|
if (StrUtil.isNotBlank(message.getState())) {
|
|
|
//打标签
|
|
|
SysConfig one = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
|
|
|
@@ -478,6 +516,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 {
|
|
|
@@ -518,4 +561,80 @@ public class CorpEventActionServiceImpl implements CorpEventActionService {
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送添加活动活码客服 发送欢迎语
|
|
|
+ */
|
|
|
+ public void sendCorpFollowActiveCode(CorpXmlMessage message, String content, String attachmentsStr) {
|
|
|
+ try {
|
|
|
+ String welcomeCode = message.getWelcomeCode();
|
|
|
+ String followId = message.getUserId();
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ //新用户欢迎语
|
|
|
+ if (StrUtil.isNotBlank(content)) {
|
|
|
+ JSONObject text = new JSONObject();
|
|
|
+ text.putOpt("content", content);
|
|
|
+ params.putOpt("text", text);
|
|
|
+ }
|
|
|
+ //附件
|
|
|
+ LinkedList<Attachments> attachments;
|
|
|
+ if (StrUtil.isNotBlank(attachmentsStr)) {
|
|
|
+ List<Attachments> dbAttachments = Jsons.parseList(attachmentsStr, Attachments.class);
|
|
|
+ attachments = new LinkedList<>();
|
|
|
+ for (Attachments dbAttachment : dbAttachments) {
|
|
|
+ attachments.add(corpService.updateMaterial(dbAttachment, message.getToUserName(), 1));
|
|
|
+ }
|
|
|
+ params.putOpt("attachments", attachments);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|