|
|
|
@ -11,12 +11,17 @@ import com.baiye.constant.DefaultNumberConstants;
|
|
|
|
|
import com.baiye.exception.BadRequestException;
|
|
|
|
|
import com.baiye.feign.SourceClueClient;
|
|
|
|
|
import com.baiye.http.CommonResponse;
|
|
|
|
|
import com.baiye.model.dto.CallClueDTO;
|
|
|
|
|
import com.baiye.model.dto.ClueDto;
|
|
|
|
|
import com.baiye.model.dto.UserDto;
|
|
|
|
|
import com.baiye.model.enums.CallStatusEnum;
|
|
|
|
|
import com.baiye.modules.agent.repository.ChannelCustomRepository;
|
|
|
|
|
import com.baiye.modules.platform.domain.CallDeduct;
|
|
|
|
|
import com.baiye.modules.platform.domain.Clue;
|
|
|
|
|
import com.baiye.modules.platform.domain.Company;
|
|
|
|
|
import com.baiye.modules.platform.repository.CallDeductRepository;
|
|
|
|
|
import com.baiye.modules.platform.service.CompanyService;
|
|
|
|
|
import com.baiye.modules.system.domain.User;
|
|
|
|
|
import com.baiye.modules.system.service.UserService;
|
|
|
|
|
import com.baiye.modules.telemarkting.dao.*;
|
|
|
|
|
import com.baiye.modules.telemarkting.entity.*;
|
|
|
|
@ -70,6 +75,9 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
@Resource
|
|
|
|
|
private ApplicationContext applicationContext;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private ChannelCustomRepository channelCustomRepository;
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public CommonResponse<TelephoneCallStopDTO> doubleCallReq(TelephoneCallReqDTO doubleCallReq, Long companyId) {
|
|
|
|
|
String requestId = RandomUtil.randomString(10);
|
|
|
|
@ -124,8 +132,10 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
callClueRepository.updateByStatus(status, userDate);
|
|
|
|
|
allCallInfo.setStatus(status);
|
|
|
|
|
allCallInfoRepository.save(allCallInfo);
|
|
|
|
|
UserDto user = userService.findById(allCallInfo.getMemberId());
|
|
|
|
|
Long whichUserId = user.getWhichUserId();
|
|
|
|
|
//实时扣费
|
|
|
|
|
callCostCount(allCallInfo.getMemberId(), allCallInfo.getClueId(), allCallInfo.getClueType(), allCallInfo.getDuration(), DefaultNumberConstants.TWO_NUMBER);
|
|
|
|
|
callCostCount(allCallInfo.getMemberId(), allCallInfo.getClueId(), allCallInfo.getClueType(), allCallInfo.getDuration(), DefaultNumberConstants.TWO_NUMBER, whichUserId);
|
|
|
|
|
//更新资源通话状态
|
|
|
|
|
CompletableFuture.runAsync(() -> updateSourceCallStatus(userDate, DefaultNumberConstants.TWO_NUMBER, allCallInfo.getClueType()));
|
|
|
|
|
} else {
|
|
|
|
@ -175,6 +185,13 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public CommonResponse<TelephoneCallStopDTO> rollCallReq(TelephoneCallReqDTO telephoneCallReqDTO, Long companyId) {
|
|
|
|
|
//检查余量
|
|
|
|
|
UserDto user = userService.findById(telephoneCallReqDTO.getMemberId());
|
|
|
|
|
Long whichUserId = user.getWhichUserId();
|
|
|
|
|
Integer surplusPhoneBillByUserId = channelCustomRepository.findSurplusPhoneBillByUserId(whichUserId);
|
|
|
|
|
if (surplusPhoneBillByUserId == null || surplusPhoneBillByUserId <= 0) {
|
|
|
|
|
return CommonResponse.createByErrorMessage("话费不足");
|
|
|
|
|
}
|
|
|
|
|
String requestId = RandomUtil.randomString(10);
|
|
|
|
|
telephoneCallReqDTO.setRequestId(requestId);
|
|
|
|
|
if (telephoneCallReqDTO.getDisplay() == null || telephoneCallReqDTO.getTelA() == null) {
|
|
|
|
@ -219,9 +236,9 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取被叫号
|
|
|
|
|
Clue body;
|
|
|
|
|
CallClueDTO body;
|
|
|
|
|
try {
|
|
|
|
|
body = sourceClueClient.queryDetails(Long.parseLong(telephoneCallReqDTO.getUserData())).getBody();
|
|
|
|
|
body = sourceClueClient.queryDetailsByClueType(Long.parseLong(telephoneCallReqDTO.getUserData()), telephoneCallReqDTO.getClueType()).getBody();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("获取被叫号错误:{},time:{} ", e.getMessage(), DateUtil.now());
|
|
|
|
|
throw new BadRequestException("呼叫失败");
|
|
|
|
@ -230,6 +247,9 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
log.error("未获取到号码");
|
|
|
|
|
return CommonResponse.createByErrorMessage("未获取到号码");
|
|
|
|
|
}
|
|
|
|
|
if (body.getMemberId() == null) {
|
|
|
|
|
return CommonResponse.createByErrorMessage("线索未分配,请刷新后重试");
|
|
|
|
|
}
|
|
|
|
|
telephoneCallReqDTO.setTelB("474" + telephoneCallReqDTO.getDisplay() + body.getNid());
|
|
|
|
|
//请求呼叫
|
|
|
|
|
String reqId = rollCallReq.startReq(telephoneCallReqDTO);
|
|
|
|
@ -273,6 +293,8 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
allCallInfoRepository.updateByRecord(DefaultNumberConstants.ONE_NUMBER, rollCallBackDTO.getRecord_file_url(), allCallInfo.getId());
|
|
|
|
|
} else {
|
|
|
|
|
log.info("被叫回调-点呼:otherLeg:{},详情:{}", otherLeg, rollCallBackDTO);
|
|
|
|
|
UserDto user = userService.findById(allCallInfo.getMemberId());
|
|
|
|
|
Long whichUserId = user.getWhichUserId();
|
|
|
|
|
boolean status;
|
|
|
|
|
//拨打线索号的回调
|
|
|
|
|
if (StrUtil.isNotBlank(rollCallBackDTO.getCallee_answer_time())) {
|
|
|
|
@ -282,7 +304,7 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
//更新资源通话状态
|
|
|
|
|
CompletableFuture.runAsync(() -> updateSourceCallStatus(allCallInfo.getClueId(), DefaultNumberConstants.TWO_NUMBER, allCallInfo.getClueType()));
|
|
|
|
|
//实时扣除话费
|
|
|
|
|
callCostCount(allCallInfo.getMemberId(), allCallInfo.getClueId(), allCallInfo.getClueType(), Integer.valueOf(rollCallBackDTO.getDuration()), 2);
|
|
|
|
|
callCostCount(allCallInfo.getMemberId(), allCallInfo.getClueId(), allCallInfo.getClueType(), Integer.valueOf(rollCallBackDTO.getDuration()), 2, whichUserId);
|
|
|
|
|
status = true;
|
|
|
|
|
} else {
|
|
|
|
|
//更新资源通话状态
|
|
|
|
@ -290,7 +312,7 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
status = false;
|
|
|
|
|
}
|
|
|
|
|
if (Arrays.asList(ClueTypeConstants.TOKER_TYPE).contains(allCallInfo.getClueType())) {
|
|
|
|
|
ClueBoostDTO clueBoostDTO = new ClueBoostDTO().addClueBoostDTO(allCallInfo.getClueId(), allCallInfo.getMemberId(), allCallInfo.getClueType(), null);
|
|
|
|
|
ClueBoostDTO clueBoostDTO = new ClueBoostDTO().addClueBoostDTO(allCallInfo.getClueId(), allCallInfo.getMemberId(), whichUserId, allCallInfo.getClueType(), null);
|
|
|
|
|
clueBoostDTO.setStatus(status);
|
|
|
|
|
applicationContext.publishEvent(clueBoostDTO);
|
|
|
|
|
}
|
|
|
|
@ -323,73 +345,74 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
* @param duration 通话时长(秒)
|
|
|
|
|
* @param callType 呼叫类型 1-双呼 2-点呼
|
|
|
|
|
*/
|
|
|
|
|
public void callCostCount(Long memberId, Long clueId, Integer clueType, Integer duration, Integer callType) {
|
|
|
|
|
// Long companyId = SecurityUtils.getCompanyId();
|
|
|
|
|
public void callCostCount(Long memberId, Long clueId, Integer clueType, Integer duration, Integer callType, Long whichUserId) {
|
|
|
|
|
Long companyId = userService.findUserInfo(memberId).getCompanyId();
|
|
|
|
|
Company company = companyService.findCompanyInfo(companyId);
|
|
|
|
|
|
|
|
|
|
double minDuration = dealDuration(duration);
|
|
|
|
|
// Company company = companyService.findById(companyId);
|
|
|
|
|
int minDuration = dealDuration(duration);
|
|
|
|
|
CallDeduct callDeduct = new CallDeduct();
|
|
|
|
|
callDeduct.setDeductDuration((int) minDuration);
|
|
|
|
|
callDeduct.setDeductDuration(minDuration);
|
|
|
|
|
callDeduct.setClueId(clueId);
|
|
|
|
|
callDeduct.setMemberId(memberId);
|
|
|
|
|
callDeduct.setCompanyId(companyId);
|
|
|
|
|
callDeduct.setStatus(false);
|
|
|
|
|
callDeduct.setCreateTime(new Date());
|
|
|
|
|
callDeduct.setVersion(company.getVersion());
|
|
|
|
|
callDeduct.setVersion(0);
|
|
|
|
|
callDeduct.setType(DefaultNumberConstants.ONE_NUMBER);
|
|
|
|
|
|
|
|
|
|
//模板费用
|
|
|
|
|
double fee = 0;
|
|
|
|
|
String typeStr;
|
|
|
|
|
if (callType == DefaultNumberConstants.ONE_NUMBER) {
|
|
|
|
|
//双呼
|
|
|
|
|
if (company.getDoubleCallFee() != null) {
|
|
|
|
|
fee = company.getDoubleCallFee();
|
|
|
|
|
}
|
|
|
|
|
typeStr = "双呼套餐";
|
|
|
|
|
} else {
|
|
|
|
|
//点呼
|
|
|
|
|
if (clueType == DefaultNumberConstants.THREE_NUMBER) {
|
|
|
|
|
if (company.getTiktokCallFee() != null) {
|
|
|
|
|
fee = company.getTiktokCallFee();
|
|
|
|
|
}
|
|
|
|
|
typeStr = "抖音套餐";
|
|
|
|
|
} else if (clueType == DefaultNumberConstants.FOUR_NUMBER) {
|
|
|
|
|
if (company.getDeliveryCallFee() != null) {
|
|
|
|
|
fee = company.getDeliveryCallFee();
|
|
|
|
|
}
|
|
|
|
|
typeStr = "投流套餐";
|
|
|
|
|
} else if (clueType == DefaultNumberConstants.FIVE_NUMBER) {
|
|
|
|
|
if (company.getTalkCallFee() != null) {
|
|
|
|
|
fee = company.getTalkCallFee();
|
|
|
|
|
}
|
|
|
|
|
typeStr = "拓客套餐";
|
|
|
|
|
} else {
|
|
|
|
|
if (company.getRollCallFee() != null) {
|
|
|
|
|
fee = company.getRollCallFee();
|
|
|
|
|
}
|
|
|
|
|
typeStr = "crm套餐";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// double fee = 0;
|
|
|
|
|
// String typeStr;
|
|
|
|
|
// if (callType == DefaultNumberConstants.ONE_NUMBER) {
|
|
|
|
|
// //双呼
|
|
|
|
|
// if (company.getDoubleCallFee() != null) {
|
|
|
|
|
// fee = company.getDoubleCallFee();
|
|
|
|
|
// }
|
|
|
|
|
// typeStr = "双呼套餐";
|
|
|
|
|
// } else {
|
|
|
|
|
// //点呼
|
|
|
|
|
// if (clueType == DefaultNumberConstants.THREE_NUMBER) {
|
|
|
|
|
// if (company.getTiktokCallFee() != null) {
|
|
|
|
|
// fee = company.getTiktokCallFee();
|
|
|
|
|
// }
|
|
|
|
|
// typeStr = "抖音套餐";
|
|
|
|
|
// } else if (clueType == DefaultNumberConstants.FOUR_NUMBER) {
|
|
|
|
|
// if (company.getDeliveryCallFee() != null) {
|
|
|
|
|
// fee = company.getDeliveryCallFee();
|
|
|
|
|
// }
|
|
|
|
|
// typeStr = "投流套餐";
|
|
|
|
|
// } else if (clueType == DefaultNumberConstants.FIVE_NUMBER) {
|
|
|
|
|
// if (company.getTalkCallFee() != null) {
|
|
|
|
|
// fee = company.getTalkCallFee();
|
|
|
|
|
// }
|
|
|
|
|
// typeStr = "拓客套餐";
|
|
|
|
|
// } else {
|
|
|
|
|
// if (company.getRollCallFee() != null) {
|
|
|
|
|
// fee = company.getRollCallFee();
|
|
|
|
|
// }
|
|
|
|
|
// typeStr = "crm套餐";
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
double mul = NumberUtil.mul(minDuration, fee);
|
|
|
|
|
callDeduct.setDeductAmount(mul);
|
|
|
|
|
callDeduct.setComboType(typeStr);
|
|
|
|
|
// double mul = NumberUtil.mul(minDuration, fee);
|
|
|
|
|
// callDeduct.setDeductAmount(mul);
|
|
|
|
|
callDeduct.setDeductAmount(0.0);
|
|
|
|
|
callDeduct.setComboType("话费");
|
|
|
|
|
|
|
|
|
|
CallDeduct save = callDeductRepository.save(callDeduct);
|
|
|
|
|
updateBalance(mul, companyId, company.getVersion(), save);
|
|
|
|
|
// updateBalance(mul, companyId, company.getVersion(), save, whichUserId);
|
|
|
|
|
updateBalance(save, whichUserId, minDuration);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateBalance(Double mul, Long companyId, Integer version, CallDeduct save) {
|
|
|
|
|
int num = companyService.updateBalanceOptimistic(mul, companyId, version);
|
|
|
|
|
private void updateBalance(CallDeduct save, Long whichUserId, Integer bill) {
|
|
|
|
|
// int num = companyService.updateBalanceOptimistic(mul, companyId, version);
|
|
|
|
|
int num = channelCustomRepository.updateSurplusBill(whichUserId, bill);
|
|
|
|
|
if (num > 0) {
|
|
|
|
|
save.setVersion(version);
|
|
|
|
|
save.setStatus(true);
|
|
|
|
|
callDeductRepository.save(save);
|
|
|
|
|
} else {
|
|
|
|
|
updateBalance(mul, companyId, version + 1, save);
|
|
|
|
|
}
|
|
|
|
|
// else {
|
|
|
|
|
// updateBalance(mul, companyId, version + 1, save);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -398,9 +421,9 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
* @param duration
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private double dealDuration(int duration) {
|
|
|
|
|
private Integer dealDuration(int duration) {
|
|
|
|
|
if (duration <= DefaultNumberConstants.SIXTY) {
|
|
|
|
|
return 1.00;
|
|
|
|
|
return 1;
|
|
|
|
|
} else {
|
|
|
|
|
//余数
|
|
|
|
|
int i = duration % 60;
|
|
|
|
@ -408,7 +431,7 @@ public class TelephoneCallServiceImpl implements TelephoneCallService {
|
|
|
|
|
int q = duration / 60;
|
|
|
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
return NumberUtil.add(q, 1);
|
|
|
|
|
return q + 1;
|
|
|
|
|
} else {
|
|
|
|
|
return q;
|
|
|
|
|
}
|
|
|
|
|