提交 4e84dfe7 作者: 卢宇

发送客服消息适配小程序

上级 a8a134cb
......@@ -20,10 +20,7 @@ import com.yaoyaozw.customer.mapper.CustomerGraphicsDelayMapper;
import com.yaoyaozw.customer.mapper.CustomerGraphicsMapper;
import com.yaoyaozw.customer.mapper.KanbanCommonMapper;
import com.yaoyaozw.customer.mapper.MaterialCommonMapper;
import com.yaoyaozw.customer.service.CrowdPackageConditionMatchService;
import com.yaoyaozw.customer.service.CustomerServiceCommonService;
import com.yaoyaozw.customer.service.ReferralEntityService;
import com.yaoyaozw.customer.service.RegisterUserEntityService;
import com.yaoyaozw.customer.service.*;
import com.yaoyaozw.customer.vo.AuthInfoVO;
import com.yaoyaozw.customer.vo.kanban.CommonOptionResponseVO;
import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO;
......@@ -42,6 +39,8 @@ import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import static com.yaoyaozw.customer.constants.CustomerCommonConstant.CUSTOMER_TYPE_VALUE_MINI_PROGRAM;
/**
* @author darker
* @date 2022/9/28 15:16
......@@ -78,6 +77,8 @@ public class CustomerServiceCommonAsyncComponent {
private CustomerGraphicsDelayMapper customerGraphicsDelayMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private CustomerGraphicsMiniProgramService miniProgramService;
@Async("myExecutor")
......@@ -151,7 +152,12 @@ public class CustomerServiceCommonAsyncComponent {
LOCAL_LOG.info("当前包含书城: {} 个", storeGroupMap.size());
// 删除之前设置人群包的时候获取链接生成的数据
referralEntityService.ultimateDeleteReferrals(materialId, "and account_id is not null");
if (customerGraphics.getType().equals(CUSTOMER_TYPE_VALUE_MINI_PROGRAM)){
referralEntityService.ultimateDeleteReferrals(materialId,null );
}else {
referralEntityService.ultimateDeleteReferrals(materialId, "and account_id is not null");
}
for (ReferralEntity referralEntity : referralEntityList) {
......@@ -160,6 +166,7 @@ public class CustomerServiceCommonAsyncComponent {
LOCAL_LOG.info("异步获取图文客服链接");
getMessageAuthListLink(storeGroupMap, customerGraphics, referralEntity);
} else {
miniProgramService.upload(storeGroupMap, customerGraphics, referralEntity);
LOCAL_LOG.info("newsType: {}, 不需要获取链接", referralEntity.getNewsType());
}
}
......
......@@ -26,6 +26,9 @@ public class CustomerMessageMiniProgramSaveDTO implements Serializable {
@ApiModelProperty("客服消息类型(图文,文本,小程序)")
private String type;
@ApiModelProperty("图片")
private String coverUrl;
@ApiModelProperty("小程序标题")
private String mpTitle;
......
......@@ -6,6 +6,11 @@ import com.yaoyaozw.customer.common.GenericsResult;
import com.yaoyaozw.customer.dto.customer.CustomerMessageMiniProgramSaveDTO;
import com.yaoyaozw.customer.dto.customer.CustomerMessageTextSaveDTO;
import com.yaoyaozw.customer.entity.CustomerGraphics;
import com.yaoyaozw.customer.entity.ReferralEntity;
import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO;
import java.util.List;
import java.util.Map;
public interface CustomerGraphicsMiniProgramService extends IService<CustomerGraphics> {
/**
......@@ -21,4 +26,12 @@ public interface CustomerGraphicsMiniProgramService extends IService<CustomerGra
* @return
*/
GenericsResult<CustomerMessageMiniProgramSaveDTO> getCustomerTextDetail(Long id);
/**
* 生成链接
* @param storeGroupMap
* @param customerGraphics
* @param referralEntity
*/
void upload(Map<String, List<CrowdPackageUserVO>> storeGroupMap, CustomerGraphics customerGraphics, ReferralEntity referralEntity);
}
......@@ -3,6 +3,8 @@ package com.yaoyaozw.customer.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaoyaozw.customer.common.BaseResult;
......@@ -11,21 +13,31 @@ import com.yaoyaozw.customer.components.SnowflakeComponent;
import com.yaoyaozw.customer.components.TokenManager;
import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.dto.customer.CustomerMessageMiniProgramSaveDTO;
import com.yaoyaozw.customer.entity.CommonReferralBody;
import com.yaoyaozw.customer.entity.CustomerGraphics;
import com.yaoyaozw.customer.entity.ReferralEntity;
import com.yaoyaozw.customer.exception.BaseException;
import com.yaoyaozw.customer.mapper.CustomerGraphicsMapper;
import com.yaoyaozw.customer.mapper.KanbanCommonMapper;
import com.yaoyaozw.customer.service.CustomerGraphicsMiniProgramService;
import com.yaoyaozw.customer.service.CustomerServiceCommonService;
import com.yaoyaozw.customer.service.ReferralEntityService;
import com.yaoyaozw.customer.vo.customer.CustomerMessageDetailVO;
import com.yaoyaozw.customer.utils.HttpClientUtil;
import com.yaoyaozw.customer.vo.AuthInfoVO;
import com.yaoyaozw.customer.vo.customer.CrowdPackageUserVO;
import com.yaoyaozw.customer.vo.kanban.CommonOptionResponseVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class CustomerGraphicsMiniProgramServiceImpl extends ServiceImpl<CustomerGraphicsMapper, CustomerGraphics> implements CustomerGraphicsMiniProgramService {
......@@ -41,6 +53,13 @@ public class CustomerGraphicsMiniProgramServiceImpl extends ServiceImpl<Customer
@Autowired
private ReferralEntityService referralEntityService;
@Autowired
private KanbanCommonMapper kanbanCommonMapper;
@Autowired
private CustomerServiceCommonService customerServiceCommonService;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Override
@Transactional(rollbackFor = Exception.class)
public BaseResult insertCustomerMessage(CustomerMessageMiniProgramSaveDTO saveDto) {
......@@ -60,6 +79,21 @@ public class CustomerGraphicsMiniProgramServiceImpl extends ServiceImpl<Customer
super.saveOrUpdate(customerGraphics);
// 处理活动数据
List<ReferralEntity> list = referralEntityService.list(new QueryWrapper<ReferralEntity>().eq("material_graphics_id", customerGraphics.getId()).eq("is_deleted", 0));
if (CollectionUtil.isNotEmpty(list)) {
list.forEach(c->{
BeanUtil.copyProperties(saveDto, c);
// 获取name模板
String nameModel = CustomerCommonConstant.getLinkNameModel(c.getNewsType());
LOCAL_LOG.info("获取name模板: {}", nameModel);
if (StringUtils.isNotBlank(nameModel)) {
c.setName(nameModel);
}
// 保存链接数据
referralEntityService.saveOrUpdate(c);
});
return new BaseResult().success();
}
ReferralEntity referralEntity = new ReferralEntity();
BeanUtil.copyProperties(saveDto, referralEntity);
referralEntity.setMaterialGraphicsId(customerGraphics.getId());
......@@ -100,4 +134,72 @@ public class CustomerGraphicsMiniProgramServiceImpl extends ServiceImpl<Customer
return new GenericsResult<>(miniProgramDTO);
}
@Override
public void upload(Map<String, List<CrowdPackageUserVO>> storeGroupMap, CustomerGraphics customerGraphics, ReferralEntity customerReferral) {
Map<String, String> storeEntityMap = getStoreEntityMap(storeGroupMap.keySet());
storeGroupMap.forEach((storeType, userVoList) -> {
LOCAL_LOG.info("当前处理书城: {}", storeType);
// 去重提取公众号
Set<String> accountSet = userVoList.stream().map(CrowdPackageUserVO::getAccountId).collect(Collectors.toSet());
List<AuthInfoVO> authInfoList = customerServiceCommonService.getAuthInfoList(accountSet);
// 定义
List<ReferralEntity> referralEntityList = new ArrayList<>(accountSet.size());
for (AuthInfoVO authInfoVo : authInfoList) {
ReferralEntity referralEntity = new ReferralEntity();
BeanUtil.copyProperties(customerReferral, referralEntity);
// 重新生成链接主键id
referralEntity.setId(snowflakeComponent.snowflakeId());
referralEntity.setAccountId(authInfoVo.getAccountId());
referralEntity.setStoreTypeName(storeEntityMap.get(storeType));
uploadImg(referralEntity,customerGraphics.getCoverUrl(),authInfoVo.getAppId());
referralEntityList.add(referralEntity);
}
// 批量新增
referralEntityService.saveBatch(referralEntityList);
});
}
private void uploadImg(ReferralEntity referralEntity, String imgUrl, String appId) {
if (StringUtils.isBlank(imgUrl)) {
return;
}
// 在这里生成新的media_id和url
byte[] picBytes = HttpClientUtil.readInputStream(imgUrl);
String suffix = null;
if (imgUrl.contains(".")) {
suffix = imgUrl.substring(imgUrl.lastIndexOf(".") + 1);
}
Map<String, String> wxTokenMap = new HashMap<>();
Object tokenObj = redisTemplate.opsForHash().get("AUTH_ACCESS_TOKEN", appId);
if (ObjectUtil.isNull(tokenObj)) {
return;
}
wxTokenMap.put("access_token", tokenObj.toString());
wxTokenMap.put("type", "image");
String json = HttpClientUtil.uploadFileByte("https://api.weixin.qq.com" + "/cgi-bin/material/add_material", picBytes, wxTokenMap, suffix);
JSONObject jsonObject = JSON.parseObject(json);
if (StringUtils.isNotBlank(jsonObject.getString("errcode"))) {
throw new BaseException(jsonObject.getString("errmsg"));
}
String newUrlMediaId = jsonObject.getString("media_id");
referralEntity.setMediaId(newUrlMediaId);
}
private Map<String, String> getStoreEntityMap(Set<String> storeKeySet) {
if (CollectionUtil.isEmpty(storeKeySet)) {
return new HashMap<>(4);
}
List<CommonOptionResponseVO> storeTypeEntity = kanbanCommonMapper.getStoreTypeEntity(storeKeySet);
return storeTypeEntity.stream().collect(Collectors.toMap(CommonOptionResponseVO::getKey, CommonOptionResponseVO::getName));
}
}
......@@ -267,6 +267,7 @@ public class CustomerGraphicsServiceImpl extends ServiceImpl<CustomerGraphicsMap
Map<String, List<ReferralEntityVo>> referralMap=new HashMap<>();
if (customerGraphics.getType().equals(CUSTOMER_TYPE_VALUE_MINI_PROGRAM)){
referralList=referralEntityService.findReferralMiniByCustomerGraphicsId(customerGraphics.getId());
referralMap = referralList.stream().collect(Collectors.groupingBy(ReferralEntityVo::getAppid));
}else {
referralList = referralEntityService.findReferralByCustomerGraphicsId(customerGraphics.getId());
referralMap = referralList.stream().filter(a->StringUtils.isNotBlank(a.getReferral())).collect(Collectors.groupingBy(ReferralEntityVo::getAppid));
......@@ -278,22 +279,15 @@ public class CustomerGraphicsServiceImpl extends ServiceImpl<CustomerGraphicsMap
String appid = usersEntry.getKey();
try { //获取该号的链接实体
if (customerGraphics.getType().equals(CUSTOMER_TYPE_VALUE_MINI_PROGRAM)){
//获取该号的openid
List<CrowdPackageUserVO> packageUserVo = usersEntry.getValue();
singleUserCount += packageUserVo.size();
weChatService.sendCustomerMessage(appid,customerGraphics,packageUserVo,referralList);
}else {
List<ReferralEntityVo> referralEntityVo = referralMap.get(appid);
//获取该号的openid
List<CrowdPackageUserVO> packageUserVo = usersEntry.getValue();
singleUserCount += packageUserVo.size();
weChatService.sendCustomerMessage(appid,customerGraphics,packageUserVo,referralEntityVo);
}
List<ReferralEntityVo> referralEntityVo = referralMap.get(appid);
//获取该号的openid
List<CrowdPackageUserVO> packageUserVo = usersEntry.getValue();
singleUserCount += packageUserVo.size();
weChatService.sendCustomerMessage(appid,customerGraphics,packageUserVo,referralEntityVo);
}catch (Exception e){
LOCAL_LOG.error("{} send CustomerMessage failed for {}",appid,e.getStackTrace()[0]);
}
}
LOCAL_LOG.info("id {} has user list size {}", customerGraphics.getId(), singleUserCount);
//客服状态修改
......
......@@ -13,6 +13,7 @@
<select id="getAuthInfoList" resultType="com.yaoyaozw.customer.vo.AuthInfoVO">
select
id, account_id as accountId,
appId,
nick_name as accountName,
store_type as storeType
from authorizer_info
......
......@@ -49,11 +49,13 @@
</select>
<select id="findReferralMiniByCustomerGraphicsId" resultType="com.yaoyaozw.customer.vo.referral.ReferralEntityVo">
select material_graphics_id ,referral.account_id,referral.referral,referral.sort,referral.mp_title,referral.mp_app_id,referral.mp_path,
select material_graphics_id ,referral.account_id,authInfo.appid,referral.mp_title,referral.mp_app_id,referral.mp_path,
referral.media_id
from referral_entity referral
from referral_entity referral left join authorizer_info authInfo
on referral.account_id=authInfo.account_id
where referral.material_graphics_id=#{graphicsId} and referral.is_deleted=0
where referral.material_graphics_id=#{graphicsId} and referral.is_deleted=0 and referral.account_id is not null
</select>
<delete id="ultimateDeleteReferralsBatch">
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论