Ver Fonte

扣款逻辑校验优化,加入行锁避免并发重复扣除

yjwang há 2 semanas atrás
pai
commit
c437a05e06

+ 16 - 3
fs-admin/src/main/java/com/fs/his/controller/FsCompanyDeductController.java

@@ -166,9 +166,9 @@ public class FsCompanyDeductController extends BaseController
     @RepeatSubmit
     public R audit(@RequestBody CompanyDeduct param)
     {
-        CompanyDeduct deduct=companyDeductService.selectCompanyDeductById(param.getDeductId());
+        CompanyDeduct deduct=companyDeductService.selectCompanyDeductByIdForUpdate(param.getDeductId());
         if(deduct.getIsAudit()!=0){
-            return R.error("非法操作");
+            return R.error("该扣款记录已审核,请勿重复操作");
         }
         deduct.setIsAudit(param.getIsAudit());
         deduct.setRemark(param.getRemark());
@@ -176,9 +176,22 @@ public class FsCompanyDeductController extends BaseController
         if(loginUser.getUser().getDeptId() == null){
             return R.error("审核失败,当前审核账号未绑定部门!");
         }
+
+
         if(deduct.getIsAudit()==1){
             Company company=companyService.selectCompanyByIdForUpdate(deduct.getCompanyId());
-            company.setMoney(company.getMoney().subtract(deduct.getMoney()));
+            BigDecimal deductMoney = deduct.getMoney();
+            BigDecimal companyMoney = company.getMoney();
+            if(companyMoney == null){
+                companyMoney = BigDecimal.ZERO;
+            }
+            if(deductMoney == null){
+                deductMoney = BigDecimal.ZERO;
+            }
+            if(companyMoney.compareTo(deductMoney) < 0){
+                return R.error("审核失败,销售公司余额不足!当前余额:" + companyMoney + "元,扣款金额:" + deductMoney + "元");
+            }
+            company.setMoney(companyMoney.subtract(deductMoney));
             deduct.setBalance(company.getMoney());
             companyService.updateCompany(company);
             //生成流水

+ 9 - 0
fs-service/src/main/java/com/fs/company/mapper/CompanyDeductMapper.java

@@ -23,6 +23,15 @@ public interface CompanyDeductMapper
      */
     public CompanyDeduct selectCompanyDeductById(Long deductId);
 
+    /**
+     * 查询扣款(加行锁,用于并发控制)
+     *
+     * @param deductId 扣款ID
+     * @return 扣款
+     */
+    @Select("SELECT * FROM company_deduct WHERE deduct_id = #{deductId} FOR UPDATE")
+    public CompanyDeduct selectCompanyDeductByIdForUpdate(Long deductId);
+
     /**
      * 查询扣款列表
      *

+ 8 - 0
fs-service/src/main/java/com/fs/company/service/ICompanyDeductService.java

@@ -21,6 +21,14 @@ public interface ICompanyDeductService
      */
         public CompanyDeduct selectCompanyDeductById(Long deductId);
 
+    /**
+     * 查询扣款(加行锁,用于并发控制)
+     *
+     * @param deductId 扣款ID
+     * @return 扣款
+     */
+    public CompanyDeduct selectCompanyDeductByIdForUpdate(Long deductId);
+
     /**
      * 查询扣款列表
      *

+ 12 - 0
fs-service/src/main/java/com/fs/company/service/impl/CompanyDeductServiceImpl.java

@@ -34,6 +34,18 @@ public class CompanyDeductServiceImpl implements ICompanyDeductService
         return companyDeductMapper.selectCompanyDeductById(deductId);
     }
 
+    /**
+     * 查询扣款(加行锁,用于并发控制)
+     *
+     * @param deductId 扣款ID
+     * @return 扣款
+     */
+    @Override
+    public CompanyDeduct selectCompanyDeductByIdForUpdate(Long deductId)
+    {
+        return companyDeductMapper.selectCompanyDeductByIdForUpdate(deductId);
+    }
+
     /**
      * 查询扣款列表
      *