Prechádzať zdrojové kódy

fix:添加锁,防止重复提交

ly 1 rok pred
rodič
commit
c99b91a27a

+ 58 - 34
src/main/java/cn/ezhizao/project/business/reportTax/controller/BizFinancialReportTaxNonZeroController.java

@@ -4,6 +4,8 @@ import cn.ezhizao.common.exception.ServiceException;
 import cn.ezhizao.common.utils.poi.ExcelUtil;
 import cn.ezhizao.framework.aspectj.lang.annotation.Log;
 import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
+import cn.ezhizao.framework.lock.DistributionLock;
+import cn.ezhizao.framework.lock.LockKey;
 import cn.ezhizao.framework.web.controller.BaseController;
 import cn.ezhizao.framework.web.domain.AjaxResult;
 import cn.ezhizao.framework.web.page.TableDataInfo;
@@ -149,44 +151,66 @@ public class BizFinancialReportTaxNonZeroController extends BaseController {
     @ResponseBody
     @Transactional(rollbackFor = Exception.class)
     public AjaxResult saveDetail(@RequestBody BizFinancialReportTaxDetail detail) {
-        detail.setVerifierTime(new Date());
-        detail.setVerifierId(getUserId());
-        boolean bool = financialReportTaxDetaiIsZerolService.saveOrUpdate(detail);
-        Map<String, Object> conditions = new HashMap<>(1);
-        conditions.put("financialReportTaxDetailId", detail.getId());
-        detail.setVerifierId(getUserId());
-        financialReportTaxDetailReportIsZeroService.physicallyDelete(conditions);
-        detail.getReportFiles().forEach(l -> {
-            l.setFinancialReportTaxId(detail.getFinancialReportTaxId());
-            l.setFinancialReportTaxDetailId(detail.getId());
-        });
-        bool = bool && (detail.getReportFiles().isEmpty() || financialReportTaxDetailReportIsZeroService.saveBatch(detail.getReportFiles()));
+        String join = LockKey.join(LockKey.REPORTTAXNONZERODETAIL,detail.getFinancialReportTaxId(),detail.getYear(),detail.getMonth(),detail.getCompanyId());
+        DistributionLock lock = new DistributionLock(join);
+        try {
+            lock.lock();
+            if(null == detail.getId()){
+                Long count = financialReportTaxDetaiIsZerolService
+                        .lambdaQuery()
+                        .eq(BizFinancialReportTaxDetail::getDeleted,0)
+                        .eq(BizFinancialReportTaxDetail::getYear,detail.getYear())
+                        .eq(BizFinancialReportTaxDetail::getMonth,detail.getMonth())
+                        .eq(BizFinancialReportTaxDetail::getFinancialReportTaxId,detail.getFinancialReportTaxId())
+                        .count();
+                if(null != count && count > 0) {
+                    return error("已经保存过数据,不可重复提交");
+                }
+            }
+            detail.setVerifierTime(new Date());
+            detail.setVerifierId(getUserId());
+            boolean bool = financialReportTaxDetaiIsZerolService.saveOrUpdate(detail);
+            Map<String, Object> conditions = new HashMap<>(1);
+            conditions.put("financialReportTaxDetailId", detail.getId());
+            detail.setVerifierId(getUserId());
+            financialReportTaxDetailReportIsZeroService.physicallyDelete(conditions);
+            detail.getReportFiles().forEach(l -> {
+                l.setFinancialReportTaxId(detail.getFinancialReportTaxId());
+                l.setFinancialReportTaxDetailId(detail.getId());
+            });
+            bool = bool && (detail.getReportFiles().isEmpty() || financialReportTaxDetailReportIsZeroService.saveBatch(detail.getReportFiles()));
 
-        Map<String, Object> delCondition = new HashMap<>();
-        delCondition.put("financialReportTaxDetailId", detail.getId());
-        financialMergeService.physicallyDelete(delCondition);
+            Map<String, Object> delCondition = new HashMap<>();
+            delCondition.put("financialReportTaxDetailId", detail.getId());
+            financialMergeService.physicallyDelete(delCondition);
 
-        bool = bool && checkDeduction(detail);
+            bool = bool && checkDeduction(detail);
 
-        SysFileStorage delConditions = new SysFileStorage();
-        delConditions.setMasterId(detail.getId());
-        delConditions.setMasterTableName("biz_financial_report_tax_detail");
-        fileStorageService.physicalDelete(delConditions);
-        List<SysFileStorage> fileStorages = detail.getFiles();
+            SysFileStorage delConditions = new SysFileStorage();
+            delConditions.setMasterId(detail.getId());
+            delConditions.setMasterTableName("biz_financial_report_tax_detail");
+            fileStorageService.physicalDelete(delConditions);
+            List<SysFileStorage> fileStorages = detail.getFiles();
 
-        fileStorages.forEach(l -> {
-            l.setMasterId(detail.getId());
-            l.setMasterTableName("biz_financial_report_tax_detail");
-        });
-        BizHistory bizHistory = new BizHistory();
-        bizHistory.setTableName("biz_financial_report_tax");
-        bizHistory.setCompanyId(detail.getCompanyId());
-        bizHistory.setBelongPeriod(detail.getYear() + "-" + String.format("%02d", Integer.valueOf(detail.getMonth())) );
-        bizHistory.setEvent("保存");
-        bizHistory.setTenantId(getTenantId());
-        bizHistoryService.save(bizHistory);
-        bool = bool && (fileStorages.isEmpty() || fileStorageService.saveOrUpdateBatch(fileStorages));
-        return bool ? success(detail) : error();
+            fileStorages.forEach(l -> {
+                l.setMasterId(detail.getId());
+                l.setMasterTableName("biz_financial_report_tax_detail");
+            });
+            BizHistory bizHistory = new BizHistory();
+            bizHistory.setTableName("biz_financial_report_tax");
+            bizHistory.setCompanyId(detail.getCompanyId());
+            bizHistory.setBelongPeriod(detail.getYear() + "-" + String.format("%02d", Integer.valueOf(detail.getMonth())) );
+            bizHistory.setEvent("保存");
+            bizHistory.setTenantId(getTenantId());
+            bizHistoryService.save(bizHistory);
+            bool = bool && (fileStorages.isEmpty() || fileStorageService.saveOrUpdateBatch(fileStorages));
+            return bool ? success(detail) : error();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return error("保存失败");
+        } finally {
+            lock.unlock();
+        }
     }
 
     /**

+ 58 - 33
src/main/java/cn/ezhizao/project/business/reportTaxIsZero/controller/BizFinancialReportTaxController.java

@@ -4,6 +4,8 @@ import cn.ezhizao.common.exception.ServiceException;
 import cn.ezhizao.common.utils.poi.ExcelUtil;
 import cn.ezhizao.framework.aspectj.lang.annotation.Log;
 import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
+import cn.ezhizao.framework.lock.DistributionLock;
+import cn.ezhizao.framework.lock.LockKey;
 import cn.ezhizao.framework.web.controller.BaseController;
 import cn.ezhizao.framework.web.domain.AjaxResult;
 import cn.ezhizao.framework.web.page.TableDataInfo;
@@ -129,44 +131,67 @@ public class BizFinancialReportTaxController extends BaseController {
     @Transactional(rollbackFor = Exception.class)
     @ResponseBody
     public AjaxResult saveDetail(@RequestBody BizFinancialReportTaxDetail detail) {
-        detail.setVerifierId(getUserId());
-        detail.setVerifierTime(new Date());
-        boolean bool = financialReportTaxDetailService.saveOrUpdate(detail);
-        Map<String, Object> conditions = new HashMap<>(1);
-        conditions.put("financialReportTaxDetailId", detail.getId());
-        financialReportTaxDetailReportService.physicallyDelete(conditions);
-        detail.getReportFiles().forEach(l -> {
-            l.setFinancialReportTaxId(detail.getFinancialReportTaxId());
-            l.setFinancialReportTaxDetailId(detail.getId());
-        });
-        bool = bool && (detail.getReportFiles().isEmpty() || financialReportTaxDetailReportService.saveBatch(detail.getReportFiles()));
+        String join = LockKey.join(LockKey.REPORTTAXZERODETAIL,detail.getFinancialReportTaxId(),detail.getYear(),detail.getMonth(),detail.getCompanyId());
+        DistributionLock lock = new DistributionLock(join);
+        try {
+            lock.lock();
+            if(null == detail.getId()){
+                Long count = financialReportTaxDetailService
+                        .lambdaQuery()
+                        .eq(BizFinancialReportTaxDetail::getDeleted,0)
+                        .eq(BizFinancialReportTaxDetail::getYear,detail.getYear())
+                        .eq(BizFinancialReportTaxDetail::getMonth,detail.getMonth())
+                        .eq(BizFinancialReportTaxDetail::getFinancialReportTaxId,detail.getFinancialReportTaxId())
+                        .count();
+                if(null != count && count > 0) {
+                    return error("已经保存过数据,不可重复提交");
+                }
+            }
+            detail.setVerifierId(getUserId());
+            detail.setVerifierTime(new Date());
+            boolean bool = financialReportTaxDetailService.saveOrUpdate(detail);
+            Map<String, Object> conditions = new HashMap<>(1);
+            conditions.put("financialReportTaxDetailId", detail.getId());
+            financialReportTaxDetailReportService.physicallyDelete(conditions);
+            detail.getReportFiles().forEach(l -> {
+                l.setFinancialReportTaxId(detail.getFinancialReportTaxId());
+                l.setFinancialReportTaxDetailId(detail.getId());
+            });
+            bool = bool && (detail.getReportFiles().isEmpty() || financialReportTaxDetailReportService.saveBatch(detail.getReportFiles()));
 
-        Map<String, Object> delCondition = new HashMap<>();
-        delCondition.put("financialReportTaxDetailId", detail.getId());
-        financialMergeService.physicallyDelete(delCondition);
+            Map<String, Object> delCondition = new HashMap<>();
+            delCondition.put("financialReportTaxDetailId", detail.getId());
+            financialMergeService.physicallyDelete(delCondition);
 
 //        bool = bool && checkDeduction(detail);
 
-        SysFileStorage delConditions = new SysFileStorage();
-        delConditions.setMasterId(detail.getId());
-        delConditions.setMasterTableName("biz_financial_report_tax_detail");
-        fileStorageService.physicalDelete(delConditions);
-        List<SysFileStorage> fileStorages = detail.getFiles();
+            SysFileStorage delConditions = new SysFileStorage();
+            delConditions.setMasterId(detail.getId());
+            delConditions.setMasterTableName("biz_financial_report_tax_detail");
+            fileStorageService.physicalDelete(delConditions);
+            List<SysFileStorage> fileStorages = detail.getFiles();
+
+            fileStorages.forEach(l -> {
+                l.setMasterId(detail.getId());
+                l.setMasterTableName("biz_financial_report_tax_detail");
+            });
+            //零申报
+            BizHistory bizHistory = new BizHistory();
+            bizHistory.setTableName("biz_financial_report_tax_zero");
+            bizHistory.setCompanyId(detail.getCompanyId());
+            bizHistory.setBelongPeriod(detail.getYear() + "-" + String.format("%02d", Integer.valueOf(detail.getMonth())) );
+            bizHistory.setEvent("保存");
+            bizHistory.setTenantId(getTenantId());
+            bizHistoryService.save(bizHistory);
+            bool = bool && (fileStorages.isEmpty() || fileStorageService.saveOrUpdateBatch(fileStorages));
+            return bool ? success(detail) : error();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return error("保存失败");
+        } finally {
+            lock.unlock();
+        }
 
-        fileStorages.forEach(l -> {
-            l.setMasterId(detail.getId());
-            l.setMasterTableName("biz_financial_report_tax_detail");
-        });
-        //零申报
-        BizHistory bizHistory = new BizHistory();
-        bizHistory.setTableName("biz_financial_report_tax_zero");
-        bizHistory.setCompanyId(detail.getCompanyId());
-        bizHistory.setBelongPeriod(detail.getYear() + "-" + String.format("%02d", Integer.valueOf(detail.getMonth())) );
-        bizHistory.setEvent("保存");
-        bizHistory.setTenantId(getTenantId());
-        bizHistoryService.save(bizHistory);
-        bool = bool && (fileStorages.isEmpty() || fileStorageService.saveOrUpdateBatch(fileStorages));
-        return bool ? success(detail) : error();
     }
 
     /**