|
@@ -165,65 +165,87 @@ public class PcBladeSalesForecastMainServiceImpl extends BaseServiceImpl<PcBlade
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public R<String> batchAdd(PcBladeSalesForecastMain pcBladeSalesForecastMain) {
|
|
|
try {
|
|
|
+ // 1. 基础参数二次校验(确保年份、月份、客户ID存在)
|
|
|
if (pcBladeSalesForecastMain == null) {
|
|
|
- log.error("批量添加失败:主表数据不能为空");
|
|
|
- return R.fail("批量添加失败:主表数据不能为空");
|
|
|
+ return R.fail("主表数据不能为空");
|
|
|
}
|
|
|
-
|
|
|
- Long id = AuthUtil.getUserId();
|
|
|
- R<User> userInfo = userClient.userInfoById(id);
|
|
|
- if (userInfo.getData() == null || userInfo.getData().getCustomerId() == null) {
|
|
|
- return R.fail("用户不存在");
|
|
|
+ Integer year = pcBladeSalesForecastMain.getYear();
|
|
|
+ Integer month = pcBladeSalesForecastMain.getMonth();
|
|
|
+ Long customerId = pcBladeSalesForecastMain.getCustomerId();
|
|
|
+ if (year == null) {
|
|
|
+ return R.fail("年份为必填项");
|
|
|
+ }
|
|
|
+ if (month == null) {
|
|
|
+ return R.fail("月份为必填项");
|
|
|
+ }
|
|
|
+ if (customerId == null) {
|
|
|
+ return R.fail("客户ID缺失,请重新登录");
|
|
|
}
|
|
|
|
|
|
- if (pcBladeSalesForecastMain.getApprovalStatus() == null) {
|
|
|
- pcBladeSalesForecastMain.setApprovalStatus(0);
|
|
|
+ // 2. 第二次校验:事务内再次检查 客户ID+年份+月份 唯一性(极端并发防护)
|
|
|
+ boolean mainExists = checkMainDuplicate(year, month, customerId);
|
|
|
+ if (mainExists) {
|
|
|
+ String msg = String.format("提交失败:%d年%d月的销售预测记录已存在", year, month);
|
|
|
+ log.warn(msg + "(事务内二次校验拦截)");
|
|
|
+ return R.fail(msg);
|
|
|
}
|
|
|
|
|
|
- pcBladeSalesForecastMain.setCustomerId(userInfo.getData().getCustomerId());
|
|
|
- ViewCustomerSel zcrmViewCustomerSel = customerSelService.selectZcrmViewCustomerSelByCustomerId(pcBladeSalesForecastMain.getCustomerId());
|
|
|
- pcBladeSalesForecastMain.setCustomerCode(zcrmViewCustomerSel.getCustomerCode());
|
|
|
- pcBladeSalesForecastMain.setCustomerName(zcrmViewCustomerSel.getCustomerName());
|
|
|
+ // 3. 完善主表信息
|
|
|
+ R<User> userInfo = userClient.userInfoById(AuthUtil.getUserId());
|
|
|
+ if (userInfo.getData() == null) {
|
|
|
+ return R.fail("用户信息不存在");
|
|
|
+ }
|
|
|
+ // 查询客户详情
|
|
|
+ ViewCustomerSel customer = customerSelService.selectZcrmViewCustomerSelByCustomerId(customerId);
|
|
|
+ if (customer == null) {
|
|
|
+ return R.fail("客户信息不存在");
|
|
|
+ }
|
|
|
+ // 设置主表字段
|
|
|
+ pcBladeSalesForecastMain.setCustomerCode(customer.getCustomerCode());
|
|
|
+ pcBladeSalesForecastMain.setCustomerName(customer.getCustomerName());
|
|
|
+ pcBladeSalesForecastMain.setApprovalStatus(pcBladeSalesForecastMain.getApprovalStatus() == null ? 0 : pcBladeSalesForecastMain.getApprovalStatus());
|
|
|
|
|
|
+ // 4. 插入主表
|
|
|
int mainSaved = baseMapper.insert(pcBladeSalesForecastMain);
|
|
|
if (mainSaved == 0) {
|
|
|
- log.error("批量添加失败:主表数据保存失败");
|
|
|
- return R.fail("批量添加失败:主表数据保存失败");
|
|
|
+ log.error("主表插入失败:客户ID={}, 年份={}, 月份={}", customerId, year, month);
|
|
|
+ return R.fail("添加失败:主表数据保存失败");
|
|
|
+ }
|
|
|
+ Long mainId = pcBladeSalesForecastMain.getId();
|
|
|
+ if (mainId == null) {
|
|
|
+ log.error("主表ID生成失败:客户ID={}, 年份={}, 月份={}", customerId, year, month);
|
|
|
+ return R.fail("添加失败:主表ID生成失败");
|
|
|
}
|
|
|
|
|
|
+ // 5. 处理明细表(确保明细与主表的年份、月份一致)
|
|
|
List<PcBladeSalesForecastSummary> summaryList = pcBladeSalesForecastMain.getPcBladeSalesForecastSummaryList();
|
|
|
if (summaryList != null && !summaryList.isEmpty()) {
|
|
|
- Long mainId = pcBladeSalesForecastMain.getId();
|
|
|
- if (mainId == null) {
|
|
|
- log.error("批量添加失败:主表ID生成失败");
|
|
|
- return R.fail("批量添加失败:主表ID生成失败");
|
|
|
- }
|
|
|
-
|
|
|
for (PcBladeSalesForecastSummary summary : summaryList) {
|
|
|
+ // 强制明细的年份、月份与主表一致(防止数据不一致)
|
|
|
+ summary.setYear(year);
|
|
|
+ summary.setMonth(month);
|
|
|
summary.setForecastMainId(mainId);
|
|
|
- summary.setYear(pcBladeSalesForecastMain.getYear());
|
|
|
- summary.setMonth(pcBladeSalesForecastMain.getMonth());
|
|
|
- summary.setCustomerId(pcBladeSalesForecastMain.getCustomerId());
|
|
|
- summary.setCustomerCode(pcBladeSalesForecastMain.getCustomerCode());
|
|
|
- summary.setCustomerName(pcBladeSalesForecastMain.getCustomerName());
|
|
|
- if (summary.getApprovalStatus() == null) {
|
|
|
- summary.setApprovalStatus(0);
|
|
|
- }
|
|
|
+ summary.setCustomerId(customerId);
|
|
|
+ summary.setCustomerCode(customer.getCustomerCode());
|
|
|
+ summary.setCustomerName(customer.getCustomerName());
|
|
|
+ summary.setApprovalStatus(summary.getApprovalStatus() == null ? 0 : summary.getApprovalStatus());
|
|
|
}
|
|
|
|
|
|
boolean summarySaved = pcBladeSalesForecastSummaryService.saveBatch(summaryList);
|
|
|
if (!summarySaved) {
|
|
|
- log.error("批量添加失败:明细表数据保存失败");
|
|
|
- return R.fail("批量添加失败:明细表数据保存失败");
|
|
|
+ log.error("明细表插入失败:主表ID={}, 年份={}, 月份={}", mainId, year, month);
|
|
|
+ throw new RuntimeException("添加失败:明细表数据保存失败"); // 触发事务回滚
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
return R.data("添加成功");
|
|
|
} catch (Exception e) {
|
|
|
log.error("批量添加销售预测数据失败", e);
|
|
|
- return R.fail("批量添加销售预测数据失败" + e.getMessage());
|
|
|
+ return R.fail(e.getMessage() != null ? e.getMessage() : "添加失败:系统异常");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public boolean batchUpdate(PcBladeSalesForecastMain main) {
|