|
@@ -0,0 +1,248 @@
|
|
|
|
|
+package com.fs.company.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+import com.fs.common.utils.DateUtils;
|
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+import com.fs.company.mapper.CompanyExtensionBindMapper;
|
|
|
|
|
+import com.fs.company.mapper.CcExtNumMapper;
|
|
|
|
|
+import com.fs.company.domain.CompanyExtensionBind;
|
|
|
|
|
+import com.fs.company.param.BatchCreateExtensionParam;
|
|
|
|
|
+import com.fs.company.service.ICompanyExtensionBindService;
|
|
|
|
|
+import com.fs.aiSipCall.vo.CcExtNumVo;
|
|
|
|
|
+import com.fs.common.utils.StringUtils;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 公司分机绑定Service业务层处理
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author fs
|
|
|
|
|
+ * @date 2026-05-19
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class CompanyExtensionBindServiceImpl extends ServiceImpl<CompanyExtensionBindMapper, CompanyExtensionBind> implements ICompanyExtensionBindService {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(CompanyExtensionBindServiceImpl.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private CcExtNumMapper ccExtNumMapper;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询公司分机绑定
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param id 公司分机绑定主键
|
|
|
|
|
+ * @return 公司分机绑定
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CompanyExtensionBind selectCompanyExtensionBindById(Long id)
|
|
|
|
|
+ {
|
|
|
|
|
+ return baseMapper.selectCompanyExtensionBindById(id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询公司分机绑定列表
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param companyExtensionBind 公司分机绑定
|
|
|
|
|
+ * @return 公司分机绑定
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<CompanyExtensionBind> selectCompanyExtensionBindList(CompanyExtensionBind companyExtensionBind)
|
|
|
|
|
+ {
|
|
|
|
|
+ return baseMapper.selectCompanyExtensionBindList(companyExtensionBind);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增公司分机绑定
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param companyExtensionBind 公司分机绑定
|
|
|
|
|
+ * @return 结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int insertCompanyExtensionBind(CompanyExtensionBind companyExtensionBind)
|
|
|
|
|
+ {
|
|
|
|
|
+ companyExtensionBind.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
+ return baseMapper.insertCompanyExtensionBind(companyExtensionBind);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改公司分机绑定
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param companyExtensionBind 公司分机绑定
|
|
|
|
|
+ * @return 结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int updateCompanyExtensionBind(CompanyExtensionBind companyExtensionBind)
|
|
|
|
|
+ {
|
|
|
|
|
+ return baseMapper.updateCompanyExtensionBind(companyExtensionBind);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 批量删除公司分机绑定
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param ids 需要删除的公司分机绑定主键
|
|
|
|
|
+ * @return 结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int deleteCompanyExtensionBindByIds(Long[] ids)
|
|
|
|
|
+ {
|
|
|
|
|
+ return baseMapper.deleteCompanyExtensionBindByIds(ids);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除公司分机绑定信息
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param id 公司分机绑定主键
|
|
|
|
|
+ * @return 结果
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int deleteCompanyExtensionBindById(Long id)
|
|
|
|
|
+ {
|
|
|
|
|
+ return baseMapper.deleteCompanyExtensionBindById(id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<CcExtNumVo> createExtensionInEasycall(BatchCreateExtensionParam param, Long tenantId) {
|
|
|
|
|
+ int createNum = param.getCreateNum();
|
|
|
|
|
+ String password = param.getPassword();
|
|
|
|
|
+ Long companyId = param.getCompanyId();
|
|
|
|
|
+
|
|
|
|
|
+ CcExtNumVo lastExtNum = ccExtNumMapper.selectLastExtNum();
|
|
|
|
|
+ long currentMaxExtNum = (lastExtNum != null && lastExtNum.getExtNum() != null) ? lastExtNum.getExtNum() : 0;
|
|
|
|
|
+
|
|
|
|
|
+ List<CcExtNumVo> allNewExtNums = new ArrayList<>();
|
|
|
|
|
+ long nextExtNum = currentMaxExtNum + 1;
|
|
|
|
|
+ int userCodeSeq = 1;
|
|
|
|
|
+
|
|
|
|
|
+ for (int i = 0; i < createNum; i++) {
|
|
|
|
|
+ CcExtNumVo vo = new CcExtNumVo();
|
|
|
|
|
+ vo.setExtNum(nextExtNum + i);
|
|
|
|
|
+ vo.setExtPass(password);
|
|
|
|
|
+ vo.setUserCode(tenantId + "_" + companyId + "_" + userCodeSeq);
|
|
|
|
|
+ userCodeSeq++;
|
|
|
|
|
+ allNewExtNums.add(vo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int maxRetry = 10;
|
|
|
|
|
+ for (int retry = 0; retry < maxRetry; retry++) {
|
|
|
|
|
+ List<Long> extNumsToCheck = allNewExtNums.stream()
|
|
|
|
|
+ .map(CcExtNumVo::getExtNum)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ List<CcExtNumVo> existingExtNums = ccExtNumMapper.selectExtNumByExtNums(extNumsToCheck);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(existingExtNums)) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ List<Long> existingExtNumValues = existingExtNums.stream()
|
|
|
|
|
+ .map(CcExtNumVo::getExtNum)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+
|
|
|
|
|
+ long maxExisting = existingExtNumValues.stream().max(Long::compareTo).orElse(currentMaxExtNum);
|
|
|
|
|
+ nextExtNum = maxExisting + 1;
|
|
|
|
|
+
|
|
|
|
|
+ List<CcExtNumVo> replaceList = new ArrayList<>();
|
|
|
|
|
+ for (CcExtNumVo vo : allNewExtNums) {
|
|
|
|
|
+ if (existingExtNumValues.contains(vo.getExtNum())) {
|
|
|
|
|
+ vo.setExtNum(nextExtNum);
|
|
|
|
|
+ nextExtNum++;
|
|
|
|
|
+ replaceList.add(vo);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (CollectionUtils.isEmpty(replaceList)) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ ccExtNumMapper.batchInsertCcExtNum(allNewExtNums);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.warn("批量插入cc_ext_num失败,尝试逐条插入补偿", e);
|
|
|
|
|
+ List<CcExtNumVo> successList = new ArrayList<>();
|
|
|
|
|
+ for (CcExtNumVo vo : allNewExtNums) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ccExtNumMapper.insertCcExtNum(vo);
|
|
|
|
|
+ successList.add(vo);
|
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
|
+ log.warn("插入分机号{}失败,可能已被其他线程占用,尝试补偿", vo.getExtNum());
|
|
|
|
|
+ int compensateRetry = 0;
|
|
|
|
|
+ boolean compensated = false;
|
|
|
|
|
+ while (compensateRetry < 5 && !compensated) {
|
|
|
|
|
+ CcExtNumVo currentLast = ccExtNumMapper.selectLastExtNum();
|
|
|
|
|
+ long newExtNum = (currentLast != null && currentLast.getExtNum() != null) ? currentLast.getExtNum() + 1 : 1;
|
|
|
|
|
+ vo.setExtNum(newExtNum);
|
|
|
|
|
+ try {
|
|
|
|
|
+ ccExtNumMapper.insertCcExtNum(vo);
|
|
|
|
|
+ compensated = true;
|
|
|
|
|
+ } catch (Exception ex2) {
|
|
|
|
|
+ compensateRetry++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (compensated) {
|
|
|
|
|
+ successList.add(vo);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ allNewExtNums = successList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return allNewExtNums;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void bindExtensionToTenant(List<CcExtNumVo> extNums, Long companyId) {
|
|
|
|
|
+ List<CompanyExtensionBind> bindList = new ArrayList<>();
|
|
|
|
|
+ for (CcExtNumVo extNumVo : extNums) {
|
|
|
|
|
+ CompanyExtensionBind bind = new CompanyExtensionBind();
|
|
|
|
|
+ bind.setCompanyId(companyId);
|
|
|
|
|
+ bind.setExtensionNum(String.valueOf(extNumVo.getExtNum()));
|
|
|
|
|
+ bind.setExtensionPass(extNumVo.getExtPass());
|
|
|
|
|
+ bind.setExtId(extNumVo.getExtId());
|
|
|
|
|
+ bind.setUserCode(extNumVo.getUserCode());
|
|
|
|
|
+ bind.setCreateTime(DateUtils.getNowDate());
|
|
|
|
|
+ bindList.add(bind);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int batchSize = 100;
|
|
|
|
|
+ for (int i = 0; i < bindList.size(); i += batchSize) {
|
|
|
|
|
+ int end = Math.min(i + batchSize, bindList.size());
|
|
|
|
|
+ List<CompanyExtensionBind> batch = bindList.subList(i, end);
|
|
|
|
|
+ baseMapper.batchInsertCompanyExtensionBind(batch);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<CompanyExtensionBind> selectUnBindByCompanyId(Long companyId) {
|
|
|
|
|
+ return baseMapper.selectUnBindByCompanyId(companyId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<CompanyExtensionBind> selectUnBindAndSelfByCompanyId(Long companyId, Long companyUserId){
|
|
|
|
|
+ return baseMapper.selectUnBindAndSelfByCompanyId(companyId,companyUserId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updateBindByExtId(Long extId, Long companyUserId, String userCode) {
|
|
|
|
|
+ baseMapper.updateBindByExtId(extId, companyUserId, userCode);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CompanyExtensionBind selectUnBindByExtNum(String extensionNum, Long companyId) {
|
|
|
|
|
+ return baseMapper.selectByExtNumAndCompanyId(extensionNum, companyId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void clearBindByExtNum(String extensionNum, Long companyId,Long companyUserId) {
|
|
|
|
|
+ baseMapper.clearBindByExtNum(extensionNum, companyId,companyUserId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void updateBindByExtNum(String num, Long companyId, Long companyUserId, String userCode) {
|
|
|
|
|
+ baseMapper.updateBindByExtNum(num, companyId,companyUserId, userCode);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|