瀏覽代碼

bugfix:合班的预收款问题

yonge 1 周之前
父節點
當前提交
53873a5328

+ 3 - 10
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ClassGroupServiceImpl.java

@@ -15,6 +15,7 @@ import com.ym.mec.biz.dal.enums.*;
 import com.ym.mec.biz.dal.page.*;
 import com.ym.mec.biz.dal.wrapper.LiveGroupWrapper;
 import com.ym.mec.biz.service.*;
+import com.ym.mec.biz.util.SpanGroupMergePaymentValidator;
 import com.ym.mec.common.constant.CommonConstants;
 import com.ym.mec.common.controller.BaseController;
 import com.ym.mec.common.dal.BaseDAO;
@@ -4835,16 +4836,8 @@ public class ClassGroupServiceImpl extends BaseServiceImpl<Integer, ClassGroup>
         List<MusicGroupPaymentCalenderStudentDetail> calenderStudentDetails = paymentCalenderDto.getMusicGroupPaymentCalenderStudentDetails();
 
         paymentCalenderDto.setMusicGroupId(musicGroup.getId());
-        boolean containsAll = musicGroupPaymentCalenderStudentDetails.containsAll(calenderStudentDetails);
-        //给原价赋值,前端没传过来
-        for (MusicGroupPaymentCalenderStudentDetail e : calenderStudentDetails) {
-            BigDecimal subtract = e.getMasterSubCoursePrice().subtract(e.getSubCourseAmount());
-            if (subtract.compareTo(BigDecimal.ZERO) < 0) {
-                e.setCourseOriginalPrice(BigDecimal.ZERO);
-            } else {
-                e.setCourseOriginalPrice(subtract);
-            }
-        }
+        boolean containsAll = SpanGroupMergePaymentValidator.validateSubmission(
+                musicGroupPaymentCalenderStudentDetails, calenderStudentDetails, studentIds);
         if (!containsAll) {
             paymentCalenderDto.setStatus(AUDITING);
         } else {

+ 32 - 2
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ClassGroupStudentMapperServiceImpl.java

@@ -62,6 +62,8 @@ public class ClassGroupStudentMapperServiceImpl extends BaseServiceImpl<Long, Cl
     private ImUserFriendService imUserFriendService;
     @Resource
     private MusicGroupPaymentStudentCourseDetailDao musicGroupPaymentStudentCourseDetailDao;
+    @Resource
+    private MusicGroupPaymentCalenderStudentDetailDao musicGroupPaymentCalenderStudentDetailDao;
 
     private static String holidayUrl = "http://tool.bitefu.net/jiari/?d=";
 
@@ -645,8 +647,36 @@ public class ClassGroupStudentMapperServiceImpl extends BaseServiceImpl<Long, Cl
         BigDecimal divide = masterTotalPrice.divide(new BigDecimal(classGroupNotStartCourse.size()), 2, BigDecimal.ROUND_HALF_UP);
         BigDecimal decimal = divide.multiply(new BigDecimal(classGroupNotStartCourse.size()));
 
+        // 直接合班和审核通过均按已保存的最终缴费明细计算每名学员的预收款。
+        List<MusicGroupPaymentCalenderStudentDetail> paymentDetails = musicGroupPaymentCalenderStudentDetailDao.findByBatchNo(batchNo);
+        if (CollectionUtils.isEmpty(paymentDetails)) {
+            throw new BizException("操作失败:合班缴费明细不存在");
+        }
+        Map<Integer, BigDecimal> studentTotalPrices = new HashMap<>();
+        for (MusicGroupPaymentCalenderStudentDetail detail : paymentDetails) {
+            if (detail.getUserId() == null || detail.getMasterSubCoursePrice() == null
+                    || detail.getCourseOriginalPrice() == null || detail.getCourseCurrentPrice() == null) {
+                throw new BizException("操作失败:合班缴费金额不完整");
+            }
+            // 转入金额未单独落库;主班价值减原补缴金额即申请时确认的转入金额。
+            BigDecimal transferredPrice = detail.getMasterSubCoursePrice().subtract(detail.getCourseOriginalPrice());
+            if (transferredPrice.signum() < 0 || detail.getCourseCurrentPrice().signum() < 0) {
+                throw new BizException("操作失败:合班缴费金额不能为负数");
+            }
+            studentTotalPrices.merge(detail.getUserId(), transferredPrice.add(detail.getCourseCurrentPrice()), BigDecimal::add);
+        }
+        for (Integer studentId : studentIds) {
+            if (!studentTotalPrices.containsKey(studentId)) {
+                throw new BizException("操作失败:学员{}缺少合班缴费明细", studentId);
+            }
+        }
+
         //生成班级关联
         for (Integer studentId : studentIds) {
+            BigDecimal studentTotalPrice = studentTotalPrices.get(studentId).setScale(2, BigDecimal.ROUND_HALF_UP);
+            // 向下取分后在首节补齐尾差,避免小额多课时四舍五入导致首节出现负数。
+            BigDecimal expectPrice = studentTotalPrice.divide(new BigDecimal(classGroupNotStartCourse.size()), 2, BigDecimal.ROUND_DOWN);
+            BigDecimal expectPriceRemainder = studentTotalPrice.subtract(expectPrice.multiply(new BigDecimal(classGroupNotStartCourse.size())));
             ClassGroupStudentMapper classGroupStudent = classGroupStudentMapperDao.findClassGroupStudent(classGroupId.intValue(), studentId);
             if(classGroupStudent != null){
                 classGroupStudent.setStatus(ClassGroupStudentStatusEnum.NORMAL);
@@ -673,10 +703,10 @@ public class ClassGroupStudentMapperServiceImpl extends BaseServiceImpl<Long, Cl
                 cssp.setUserId(studentId);
                 if(i == 0){
                     cssp.setOriginalPrice(divide.add(masterTotalPrice.subtract(decimal)));
-                    cssp.setExpectPrice(divide.add(masterTotalPrice.subtract(decimal)));
+                    cssp.setExpectPrice(expectPrice.add(expectPriceRemainder));
                 }else {
                     cssp.setOriginalPrice(divide);
-                    cssp.setExpectPrice(divide);
+                    cssp.setExpectPrice(expectPrice);
                 }
                 cssp.setActualPrice(BigDecimal.ZERO);
                 courseScheduleStudentPayments.add(cssp);

+ 18 - 3
mec-biz/src/main/java/com/ym/mec/biz/service/impl/MusicGroupPaymentCalenderServiceImpl.java

@@ -19,6 +19,7 @@ import com.ym.mec.biz.dal.wrapper.MusicGroupPaymentCalenderAddressWrapper;
 import com.ym.mec.biz.event.source.GroupEventSource;
 import com.ym.mec.biz.event.source.SendSeoMessageSource;
 import com.ym.mec.biz.service.*;
+import com.ym.mec.biz.util.SpanGroupMergePaymentValidator;
 import com.ym.mec.common.api.CommonResult;
 import com.ym.mec.common.controller.BaseController;
 import com.ym.mec.common.dal.BaseDAO;
@@ -734,7 +735,8 @@ public class MusicGroupPaymentCalenderServiceImpl extends BaseServiceImpl<Long,
                 adjust.getClassGroupStudents(), studentIdList);
         List<MusicGroupPaymentCalenderStudentDetail> calenderStudentDetails = musicGroupPaymentCalenderDto.getMusicGroupPaymentCalenderStudentDetails();
 
-        boolean containsAll = musicGroupPaymentCalenderStudentDetails.containsAll(calenderStudentDetails);
+        boolean containsAll = SpanGroupMergePaymentValidator.validateSavedDetails(
+                musicGroupPaymentCalenderStudentDetails, calenderStudentDetails, studentIdList);
         if (!containsAll) {
             status = AUDITING;
         }
@@ -799,6 +801,13 @@ public class MusicGroupPaymentCalenderServiceImpl extends BaseServiceImpl<Long,
             List<Map> classGroupStudents = JSON.parseArray(adjust.getClassGroupStudents(), Map.class);
             List<Long> courseIds = JSON.parseArray(adjust.getSubLockCourseIds(), Long.class);
             List<Long> allLockCourseIds = JSON.parseArray(adjust.getAllLockCourseIds(), Long.class);
+            Map<Integer, List<MusicGroupPaymentCalenderStudentDetail>> studentDetails = calenderStudentDetails.stream()
+                    .collect(Collectors.groupingBy(MusicGroupPaymentCalenderStudentDetail::getUserId));
+            for (Map<Integer, String> classGroupStudent : classGroupStudents) {
+                Integer classGroupId = classGroupStudent.keySet().iterator().next();
+                studentRegistrationService.insertStudent(classGroupStudent.get(classGroupId),
+                        musicGroupDao.findByClassGroupId(classGroupId).getId(), musicGroupId, studentDetails, batchNo);
+            }
             classGroupService.spanGroupClassAdjustPass(adjust.getMasterClassGroupId()
                     , studentIdList, courseIds, classGroupStudents, allLockCourseIds, batchNo, adjust.getMasterTotalPrice());
         }
@@ -1094,16 +1103,22 @@ public class MusicGroupPaymentCalenderServiceImpl extends BaseServiceImpl<Long,
                 }
             } else if (calender.getPaymentType() == SPAN_GROUP_CLASS_ADJUST) {
                 List<MusicGroupPaymentCalenderStudentDetail> calenderStudentDetails = musicGroupPaymentCalenderStudentDetailDao.findByBatchNo(batchNo);
+                MusicGroupStudentClassAdjust adjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
+                if (adjust == null) {
+                    throw new BizException("操作失败:合班申请不存在");
+                }
+                List<Integer> studentIds = JSON.parseArray(adjust.getStudentIds(), Integer.class);
+                List<MusicGroupPaymentCalenderStudentDetail> verifiedDetails = classGroupService.queryStudentPaymentCalenders(
+                        adjust.getMasterClassGroupId(), adjust.getClassGroupStudents(), studentIds);
+                SpanGroupMergePaymentValidator.validateSavedDetails(verifiedDetails, calenderStudentDetails, studentIds);
                 //将学员加入新乐团、扣除原乐团剩余课程余额、补充到现有乐团
                 Map<Integer, List<MusicGroupPaymentCalenderStudentDetail>> collect = calenderStudentDetails.stream().collect(Collectors.groupingBy(e -> e.getUserId()));
-                MusicGroupStudentClassAdjust adjust = musicGroupStudentClassAdjustDao.findByBatchNo(batchNo);
                 List<Map> classGroupStudents = JSON.parseArray(adjust.getClassGroupStudents(), Map.class);
                 for (Map<Integer, String> classGroupStudent : classGroupStudents) {
                     Integer classGroupId = classGroupStudent.keySet().iterator().next();
                     studentRegistrationService.insertStudent(classGroupStudent.get(classGroupId), musicGroupDao.findByClassGroupId(classGroupId).getId(), musicGroup.getId(), collect,null);
                 }
                 //删除和解冻班级,课程信息
-                List<Integer> studentIds = JSON.parseArray(adjust.getStudentIds(), Integer.class);
                 List<Long> courseIds = JSON.parseArray(adjust.getSubLockCourseIds(), Long.class);
                 List<Long> allLockCourseIds = JSON.parseArray(adjust.getAllLockCourseIds(), Long.class);
                 //如果是跨团班级合并,添加学员

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRegistrationServiceImpl.java

@@ -1124,8 +1124,8 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
         //获取旧乐团学员注册信息
         List<StudentRegistration> studentRegistrations = studentRegistrationDao.queryByUserIdsAndMusicGroupId(studentIds, oldMusicGroupId);
         List<StudentCourseFeeDetail> studentCourseFeeDetails = new ArrayList<>();
-        BigDecimal amount = BigDecimal.ZERO;
         for (StudentRegistration studentRegistration : studentRegistrations) {
+            BigDecimal amount = BigDecimal.ZERO;
             List<MusicGroupPaymentCalenderStudentDetail> details = collect.get(studentRegistration.getUserId());
             BigDecimal masterTotalPrice = details.stream().map(e -> e.getCutAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
             if (masterTotalPrice.doubleValue() > studentRegistration.getSurplusCourseFee().doubleValue()) {

+ 102 - 0
mec-biz/src/main/java/com/ym/mec/biz/util/SpanGroupMergePaymentValidator.java

@@ -0,0 +1,102 @@
+package com.ym.mec.biz.util;
+
+import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderStudentDetail;
+import com.ym.mec.common.exception.BizException;
+
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/** 合班报价由后端核实,管理人员仅调整补缴金额。 */
+public final class SpanGroupMergePaymentValidator {
+    private SpanGroupMergePaymentValidator() {
+    }
+
+    public static boolean validateSubmission(List<MusicGroupPaymentCalenderStudentDetail> verified,
+                                             List<MusicGroupPaymentCalenderStudentDetail> submitted,
+                                             List<Integer> studentIds) {
+        return validate(verified, submitted, studentIds, false);
+    }
+
+    // 已保存的明细没有 subCourseAmount/cutAmount,用原补缴金额核对转入金额后恢复。
+    public static boolean validateSavedDetails(List<MusicGroupPaymentCalenderStudentDetail> verified,
+                                               List<MusicGroupPaymentCalenderStudentDetail> saved,
+                                               List<Integer> studentIds) {
+        return validate(verified, saved, studentIds, true);
+    }
+
+    private static boolean validate(List<MusicGroupPaymentCalenderStudentDetail> verified,
+                                    List<MusicGroupPaymentCalenderStudentDetail> submitted,
+                                    List<Integer> studentIds, boolean saved) {
+        if (verified == null || verified.isEmpty() || submitted == null || submitted.isEmpty()
+                || studentIds == null || studentIds.isEmpty()) {
+            throw new BizException("操作失败:合班学员缴费明细不能为空");
+        }
+        Map<String, MusicGroupPaymentCalenderStudentDetail> expected = index(verified);
+        Map<String, MusicGroupPaymentCalenderStudentDetail> actual = index(submitted);
+        Set<Integer> expectedStudents = new HashSet<>();
+        for (MusicGroupPaymentCalenderStudentDetail detail : verified) {
+            expectedStudents.add(detail.getUserId());
+        }
+        Set<Integer> requestedStudents = new HashSet<>(studentIds);
+        if (requestedStudents.size() != studentIds.size() || !expectedStudents.equals(requestedStudents)
+                || !expected.keySet().equals(actual.keySet())) {
+            throw new BizException("操作失败:合班学员或课程类型明细不一致,请重新提交");
+        }
+        boolean defaultPrice = true;
+        for (Map.Entry<String, MusicGroupPaymentCalenderStudentDetail> entry : expected.entrySet()) {
+            MusicGroupPaymentCalenderStudentDetail source = entry.getValue();
+            MusicGroupPaymentCalenderStudentDetail target = actual.get(entry.getKey());
+            BigDecimal masterPrice = source.getMasterSubCoursePrice();
+            BigDecimal transfer = source.getSubCourseAmount();
+            if (!nonNegative(masterPrice) || !nonNegative(transfer) || transfer.compareTo(masterPrice) > 0
+                    || !sameAmount(transfer, source.getCutAmount()) || !nonNegative(source.getCourseCurrentPrice())) {
+                throw new BizException("操作失败:后端合班金额异常,请检查课程金额和余额");
+            }
+            if (!nonNegative(target.getCourseCurrentPrice())) {
+                throw new BizException("操作失败:补缴金额不能为空或负数");
+            }
+            BigDecimal originalPrice = masterPrice.subtract(transfer);
+            if (!sameAmount(masterPrice, target.getMasterSubCoursePrice())
+                    || (saved && !sameAmount(originalPrice, target.getCourseOriginalPrice()))
+                    || (!saved && (!sameAmount(transfer, target.getSubCourseAmount())
+                    || !sameAmount(transfer, target.getCutAmount())))) {
+                throw new BizException("操作失败:合班课程价值或可转入金额已变化,请重新确认后提交");
+            }
+            defaultPrice &= sameAmount(source.getCourseCurrentPrice(), target.getCourseCurrentPrice());
+            // 不覆盖管理人员确认的 courseCurrentPrice,其余金额均使用后端核实值。
+            target.setMasterSubCoursePrice(masterPrice);
+            target.setSubCourseAmount(transfer);
+            target.setCutAmount(transfer);
+            target.setCourseOriginalPrice(originalPrice);
+            target.setCourseTime(source.getCourseTime());
+        }
+        return defaultPrice;
+    }
+
+    private static Map<String, MusicGroupPaymentCalenderStudentDetail> index(List<MusicGroupPaymentCalenderStudentDetail> details) {
+        Map<String, MusicGroupPaymentCalenderStudentDetail> result = new HashMap<>();
+        for (MusicGroupPaymentCalenderStudentDetail detail : details) {
+            if (detail == null || detail.getUserId() == null || detail.getClassGroupId() == null
+                    || detail.getCourseType() == null || detail.getCourseType().trim().isEmpty()) {
+                throw new BizException("操作失败:合班学员缴费明细不完整");
+            }
+            String key = detail.getUserId() + ":" + detail.getClassGroupId() + ":" + detail.getCourseType();
+            if (result.put(key, detail) != null) {
+                throw new BizException("操作失败:合班学员课程类型明细重复");
+            }
+        }
+        return result;
+    }
+
+    private static boolean nonNegative(BigDecimal value) {
+        return value != null && value.signum() >= 0;
+    }
+
+    private static boolean sameAmount(BigDecimal first, BigDecimal second) {
+        return first != null && second != null && first.compareTo(second) == 0;
+    }
+}