|
|
@@ -4,6 +4,7 @@ import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.fs.common.constant.Constants;
|
|
|
import com.fs.common.core.redis.RedisCacheT;
|
|
|
import com.fs.common.utils.PubFun;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
@@ -26,7 +27,6 @@ import com.fs.wxcid.service.FriendService;
|
|
|
import com.fs.wxcid.vo.AddContactVo;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -52,26 +52,25 @@ public class WxTaskService {
|
|
|
private final ICrmCustomerService crmCustomerService;
|
|
|
private final FriendService friendService;
|
|
|
private final CompanyVoiceRoboticMapper companyVoiceRoboticMapper;
|
|
|
- private final RedisCacheT<Long> redisCache;
|
|
|
-
|
|
|
- private static final String TASK_ID = "cid:task:";
|
|
|
- private static final String CELL_PHONE = "cellPhone";
|
|
|
- private static final String ADD_WX = "addWx";
|
|
|
- private static final String SEND_MSG = "sendMsg";
|
|
|
+ private final RedisCacheT<String> redisCache;
|
|
|
|
|
|
public void addWx(List<Long> accountIdList) {
|
|
|
+ log.info("==========执行加微信任务开始==========");
|
|
|
String json = sysConfigService.selectConfigByKey("wx.config");
|
|
|
WxConfig config = JSONUtil.toBean(json, WxConfig.class);
|
|
|
// 需要添加微信的列表
|
|
|
List<CompanyWxClient> list = companyWxClientService.getAddWxList(accountIdList);
|
|
|
+ log.info("需要添加微信的数量:{}", list.size());
|
|
|
if(list.isEmpty()) return;
|
|
|
List<CompanyWxClient> addList = new ArrayList<>();
|
|
|
Map<Long, CompanyWxClient> clientMap = PubFun.listToMapByGroupObject(list, CompanyWxClient::getAccountId);
|
|
|
List<CompanyWxAccount> accountList = new ArrayList<>(companyWxAccountService.listByIds(clientMap.keySet()));
|
|
|
+ log.info("查询加微的账号数量:{}", list.size());
|
|
|
List<CompanyWxAccount> addAccountList = accountList.stream().filter(e -> {
|
|
|
int newAddWxMinute = RandomUtil.randomInt(config.getNewAccountAddWxMin(), config.getNewAccountAddWxMax());
|
|
|
int addWxMinute = RandomUtil.randomInt(config.getAccountAddWxMin(), config.getAccountAddWxMax());
|
|
|
if (e.getLastAddWxTime() == null) {
|
|
|
+ log.info("加微时间为空,可以加微:{}", e.getWxNickName());
|
|
|
return true;
|
|
|
}
|
|
|
int minute = addWxMinute;
|
|
|
@@ -79,24 +78,35 @@ public class WxTaskService {
|
|
|
minute = newAddWxMinute;
|
|
|
}
|
|
|
long until = e.getLastAddWxTime().until(LocalDateTime.now(), ChronoUnit.MINUTES);
|
|
|
+ log.info("判断{}是否达到最低加微时间:上次加微时间:{},时间间隔:{},当前时间:{}", e.getWxNickName(), e.getLastAddWxTime(), until, LocalDateTime.now());
|
|
|
return until > minute;
|
|
|
}).collect(Collectors.toList());
|
|
|
+ log.info("实际加微的账号数量:{}", addAccountList.size());
|
|
|
addAccountList.forEach(e -> {
|
|
|
CompanyWxClient client = clientMap.get(e.getId());
|
|
|
if(client != null){
|
|
|
- CompanyWxDialog dialog = companyWxDialogService.getById(client.getDialogId());
|
|
|
- CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
- String newTxt = objectPlaceholderResolver.resolvePlaceholders(crmCustomer, dialog.getTemplateDetails());
|
|
|
- AddContactVo vo = friendService.addContact(e.getId(), crmCustomer.getMobile(), newTxt);
|
|
|
- if(vo.isSuccess()){
|
|
|
- e.setLastAddWxTime(LocalDateTime.now());
|
|
|
- e.setIsAddNum(e.getIsAddNum() + 1);
|
|
|
- client.setIsAdd(2);
|
|
|
- client.setAddTime(LocalDateTime.now());
|
|
|
- client.setWxV3(vo.getV3());
|
|
|
- client.setWxV4(vo.getV4());
|
|
|
- addList.add(client);
|
|
|
+ String task = redisCache.getCacheObject(Constants.TASK_ID + client.getRoboticId());
|
|
|
+ log.info("ROBOTIC-ID:{},CLIENT-ID:{},当前任务执行状态:{}", client.getRoboticId(), client.getId(), task);
|
|
|
+ if(StringUtils.isNotEmpty(task) && Constants.ADD_WX.equals(task)){
|
|
|
+ CompanyWxDialog dialog = companyWxDialogService.getById(client.getDialogId());
|
|
|
+ CrmCustomer crmCustomer = crmCustomerService.selectCrmCustomerById(client.getCustomerId());
|
|
|
+ String newTxt = objectPlaceholderResolver.resolvePlaceholders(crmCustomer, dialog.getTemplateDetails());
|
|
|
+ AddContactVo vo = friendService.addContact(e.getId(), crmCustomer.getMobile(), newTxt, client.getId());
|
|
|
+ log.info("ROBOTIC-ID:{},CLIENT-ID:{},执行加微:{},客户:{}-{},使用话术:{}", client.getRoboticId(), client.getId(), e.getId(), client.getCustomerId(), crmCustomer.getCustomerName(), dialog.getName());
|
|
|
+ if(vo.isSuccess()){
|
|
|
+ e.setLastAddWxTime(LocalDateTime.now());
|
|
|
+ e.setIsAddNum(e.getIsAddNum() + 1);
|
|
|
+ client.setIsAdd(2);
|
|
|
+ client.setAddTime(LocalDateTime.now());
|
|
|
+ client.setWxV3(vo.getV3());
|
|
|
+ client.setWxV4(vo.getV4());
|
|
|
+ addList.add(client);
|
|
|
+ }else{
|
|
|
+ log.error("ROBOTIC-ID:{},加微失败:{}", client.getRoboticId(), vo);
|
|
|
+ }
|
|
|
}
|
|
|
+ }else{
|
|
|
+ log.error("当前账号暂无需要添加微信:{}-{}", e.getId(), e.getWxNickName());
|
|
|
}
|
|
|
});
|
|
|
if(!addList.isEmpty()){
|
|
|
@@ -167,27 +177,28 @@ public class WxTaskService {
|
|
|
e.setNowTask(taskFlow.get(0));
|
|
|
log.info("ROBOTIC-ID:{},当前需要执行任务:{}", e.getId(), e.getNowTask());
|
|
|
});
|
|
|
- Function<CompanyVoiceRobotic, String> getKey = e -> TASK_ID + e.getId();
|
|
|
+ Function<CompanyVoiceRobotic, String> getKey = e -> Constants.TASK_ID + e.getId();
|
|
|
waitList.forEach(e -> {
|
|
|
if(redisCache.getCacheObject(getKey.apply(e)) != null){
|
|
|
log.info("ROBOTIC-ID:{},已有正在执行任务", e.getId());
|
|
|
return;
|
|
|
}
|
|
|
- redisCache.setCacheObject(getKey.apply(e), e.getTaskId());
|
|
|
+ redisCache.setCacheObject(getKey.apply(e), e.getNowTask());
|
|
|
try {
|
|
|
switch (e.getNowTask()) {
|
|
|
- case CELL_PHONE:
|
|
|
-
|
|
|
+ case Constants.CELL_PHONE:
|
|
|
+ companyVoiceRoboticService.addTask(e);
|
|
|
+ log.info("ROBOTIC-ID:{},打电话任务创建完成", e.getId());
|
|
|
break;
|
|
|
- case ADD_WX:
|
|
|
+ case Constants.ADD_WX:
|
|
|
|
|
|
break;
|
|
|
- case SEND_MSG:
|
|
|
+ case Constants.SEND_MSG:
|
|
|
|
|
|
break;
|
|
|
}
|
|
|
}catch (Exception exception){
|
|
|
- log.error("ROBOTIC-ID:{},任务执行失败", e.getId(), exception);
|
|
|
+ log.error("ROBOTIC-ID:{},任务执行失败:{}", e.getId(), e.getNowTask(), exception);
|
|
|
redisCache.deleteObject(getKey.apply(e));
|
|
|
}
|
|
|
});
|