|
|
@@ -14,6 +14,7 @@ import com.fs.company.domain.CompanyTrafficRecord;
|
|
|
import com.fs.company.domain.CompanyTrafficRecordLog;
|
|
|
import com.fs.company.mapper.CompanyTrafficRecordMapper;
|
|
|
import com.fs.company.param.CompanyTrafficRecordChargeParam;
|
|
|
+import com.fs.company.param.CompanyTrafficRecordDeductParam;
|
|
|
import com.fs.company.param.CompanyTrafficRecordQueryParam;
|
|
|
import com.fs.company.service.ICompanyService;
|
|
|
import com.fs.company.service.ICompanyTrafficRecordLogService;
|
|
|
@@ -249,6 +250,139 @@ public class CompanyTrafficRecordServiceImpl extends ServiceImpl<CompanyTrafficR
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 扣减场景下的公司有效性校验(与 validCompany(charge) 逻辑一致,仅 param 类型不同)
|
|
|
+ */
|
|
|
+ private boolean validCompany(CompanyTrafficRecordDeductParam record) {
|
|
|
+ Company company = new Company();
|
|
|
+ company.setCompanyId(record.getCompanyId());
|
|
|
+ company.setIsDel(0);
|
|
|
+ List<Company> companies = companyService.selectCompanyList(company);
|
|
|
+ if (companies != null && companies.size() == 1) {
|
|
|
+ record.setCompany(companies.get(0));
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 经销商流量扣减主逻辑
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean deduct(CompanyTrafficRecordDeductParam record) {
|
|
|
+ if (record.getCompanyId() == null || !validCompany(record)) {
|
|
|
+ throw new IllegalArgumentException("销售公司异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (record.getDeductValue() == null || record.getDeductValue() <= 0) {
|
|
|
+ throw new BaseException("扣减流量必须大于 0");
|
|
|
+ }
|
|
|
+ long ratio;
|
|
|
+ long unitLimit;
|
|
|
+ String unit = record.getUnit() == null ? "" : record.getUnit().toUpperCase();
|
|
|
+ switch (unit) {
|
|
|
+ case "KB":
|
|
|
+ ratio = 1L;
|
|
|
+ unitLimit = 1024L;
|
|
|
+ break;
|
|
|
+ case "MB":
|
|
|
+ ratio = 1024L;
|
|
|
+ unitLimit = 1024L;
|
|
|
+ break;
|
|
|
+ case "GB":
|
|
|
+ ratio = 1024L * 1024L;
|
|
|
+ unitLimit = 1024L;
|
|
|
+ break;
|
|
|
+ case "TB":
|
|
|
+ ratio = 1024L * 1024L * 1024L;
|
|
|
+ unitLimit = 10L;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new BaseException("非法的扣减单位,仅支持 KB/MB/GB/TB");
|
|
|
+ }
|
|
|
+ if (record.getDeductValue() > unitLimit) {
|
|
|
+ throw new BaseException("单次扣减数值不能超过 " + unitLimit + " " + unit + ",请分次操作");
|
|
|
+ }
|
|
|
+ long deductKB = record.getDeductValue() * ratio;
|
|
|
+ if (deductKB <= 0) {
|
|
|
+ throw new BaseException("扣减流量必须大于 0");
|
|
|
+ }
|
|
|
+
|
|
|
+ CompanyTrafficRecord companyRecord = baseMapper.selectOne(new LambdaQueryWrapper<CompanyTrafficRecord>()
|
|
|
+ .eq(CompanyTrafficRecord::getCompanyId, record.getCompanyId()));
|
|
|
+ if (companyRecord == null) {
|
|
|
+ throw new BaseException("公司流量记录不存在,无法扣减");
|
|
|
+ }
|
|
|
+ long beforeBalance = companyRecord.getBalance() == null ? 0L : companyRecord.getBalance();
|
|
|
+ if (beforeBalance < deductKB) {
|
|
|
+ throw new BaseException("公司流量余额不足,扣减失败");
|
|
|
+ }
|
|
|
+ long afterBalance = beforeBalance - deductKB;
|
|
|
+
|
|
|
+ CompanyTrafficRecord update = CompanyTrafficRecord.builder()
|
|
|
+ .id(companyRecord.getId())
|
|
|
+ .balance(afterBalance)
|
|
|
+ .updateTime(new Date())
|
|
|
+ .updateBy(record.getUserId())
|
|
|
+ .build();
|
|
|
+ baseMapper.updateById(update);
|
|
|
+
|
|
|
+ boolean refundToDept = !Boolean.TRUE.equals(record.getIsAdmin())
|
|
|
+ && record.getDeptId() != null
|
|
|
+ && !record.getDeptId().equals(1L);
|
|
|
+ if (refundToDept) {
|
|
|
+ SysDeptConfig deptConfig = sysDeptConfigService.getDeptConfig(record.getDeptId());
|
|
|
+ if (deptConfig == null) {
|
|
|
+ throw new BaseException("部门配置不存在,无法返还流量");
|
|
|
+ }
|
|
|
+ long oldFlow = deptConfig.getFlowNum() == null ? 0L : deptConfig.getFlowNum();
|
|
|
+ long newFlow = oldFlow + deductKB;
|
|
|
+ deptConfig.setFlowNum(newFlow);
|
|
|
+ deptConfig.setUpdateBy(record.getUserName());
|
|
|
+ deptConfig.setUpdateTime(new Date());
|
|
|
+ sysDeptConfigService.updateById(deptConfig);
|
|
|
+ sysDeptConfigLogService.addLog(
|
|
|
+ deptConfig.getDeptId(),
|
|
|
+ 1,
|
|
|
+ 0,
|
|
|
+ String.valueOf(deductKB),
|
|
|
+ String.valueOf(oldFlow),
|
|
|
+ String.valueOf(newFlow),
|
|
|
+ record.getUserName(),
|
|
|
+ "经销商流量扣减返还,公司ID=" + record.getCompanyId()
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ Long cacheDeptId = record.getCompany() != null && record.getCompany().getDeptId() != null
|
|
|
+ ? record.getCompany().getDeptId() : companyRecord.getDeptId();
|
|
|
+ if (cacheDeptId != null) {
|
|
|
+ redisCache.incr(CompanyTrafficConstants.CACHE_KEY + ":" + cacheDeptId + ":" + record.getCompanyId(),
|
|
|
+ -deductKB);
|
|
|
+ redisCache.incr(CompanyTrafficConstants.CACHE_KEY + ":" + cacheDeptId, -deductKB);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("扣减流量后 Redis 缓存更新异常,companyId={}, deductKB={}, err={}",
|
|
|
+ record.getCompanyId(), deductKB, e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ String userRemark = record.getRemark() == null ? "" : record.getRemark();
|
|
|
+ String tag = refundToDept ? "[非公司部门操作,已返还部门]" : "[公司部门操作,不返还部门]";
|
|
|
+ return companyTrafficRecordLogService.save(CompanyTrafficRecordLog.builder()
|
|
|
+ .companyId(record.getCompanyId())
|
|
|
+ .userId(record.getUserId())
|
|
|
+ .userName(record.getUserName())
|
|
|
+ .operationType(2)
|
|
|
+ .trafficAmount(deductKB)
|
|
|
+ .chargeAmount(null)
|
|
|
+ .balance(afterBalance)
|
|
|
+ .remark(userRemark + tag)
|
|
|
+ .createTime(new Date())
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 更新Redis缓存
|
|
|
*
|