Skip to content
Snippets Groups Projects

0

Merged jiangxiaoming requested to merge jiangxiaoming into dev
Compare and
2 files
+ 99
112
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -51,6 +51,7 @@ import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -69,11 +70,11 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @Author jxm
@@ -87,8 +88,6 @@ public class EntryCommandServiceImpl implements EntryCommandService {
@Value("${pdfResource}")
private String pdfResource;
@Autowired
ThreadPoolExecutor threadPoolExecutor;
@Autowired
IncomeRepository incomeRepository;
@Autowired
EntryBillRepository entryBillRepository;
@@ -118,6 +117,8 @@ public class EntryCommandServiceImpl implements EntryCommandService {
RedisTemplate redisTemplate;
@Autowired
RedissonClient redissonClient;
@Autowired
private ThreadPoolExecutor threadPoolExecutor;
//未分配、待认领、需要补充但未提交补充信息的这三部分回单可以暂存入账 作废 全走storageTempAll去了
/*@Transactional
@@ -1063,30 +1064,16 @@ public class EntryCommandServiceImpl implements EntryCommandService {
Boolean isBatch = false;
@Override
public void storageTempAll(Long[] longList) {
String lockKey = "lock:storageTempAll";
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
List<VchDetailEntity> detailListAll = new ArrayList<>();
List<IncomeAggregate> incomeAggregates = incomeRepository.findByIds(longList);
Map<Long,IncomeAggregate> map = new HashMap<>();
UserInfoOut infoOut = userInfoUtil.getLoginInfoOut();
// 1 入账+生成入账申请单 有事务
storageTemp(incomeAggregates,map,detailListAll);
//2 推送凭证 异步
new Thread(()->{
pushVoucher(detailListAll,new AtomicBoolean(true),map,infoOut);
}).start();
}catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
}finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
List<VchDetailEntity> detailListAll = Collections.synchronizedList(new ArrayList<>());
List<IncomeAggregate> incomeAggregates = incomeRepository.findByIds(longList);
Map<Long,IncomeAggregate> map = new HashMap<>();
UserInfoOut infoOut = userInfoUtil.getLoginInfoOut();
// 1 入账+生成入账申请单 有事务
storageTemp(incomeAggregates,map,detailListAll);
//2 推送凭证 异步
new Thread(()->{
pushVoucher(detailListAll,new AtomicBoolean(true),map,infoOut);
}).start();
}
@Transactional
@@ -1095,55 +1082,79 @@ public class EntryCommandServiceImpl implements EntryCommandService {
//1 查询所有待认领的回单
Long batchId = IdUtil.getSnowflakeNextId();
this.batchId = batchId;
List<CompletableFuture<Void>> futures = new ArrayList<>();
int i = 0;
for(IncomeAggregate incomeAggregate:incomeAggregates) {
EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(incomeAggregate.getIncomeId());
VoucherAggregate agg = voucherRepository.findByOutOrderNo(incomeAggregate.getIncomeId());
// 查询凭证 是否有申请中的
if(agg!=null && agg.getVoucherState().equals(VoucherStateApplyEnum.申请中.getValue())){
throw new RuntimeException("当前回单入账在途中");
}
if(entryBillAggregate!=null && entryBillAggregate.getVoucherState().getValue().equals(VoucherStateEnum.已入账.getValue())
&& agg.getVoucherState().equals(VoucherStateApplyEnum.已入账.getValue())
){
throw new RuntimeException("已经入账的回单不允许再次入账");
}
if(incomeAggregate.getReceiptBillVo().getIncomeTypeName()!=null &&
incomeAggregate.getReceiptBillVo().getIncomeTypeName().equals("已线下入账")){
throw ExceptionUtil.getException(null,"已线下入账不允许暂存入账:"+incomeAggregate.getIncomeId());
try{
for(IncomeAggregate incomeAggregate:incomeAggregates) {
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
String lockKey = "lock:storageTemp:" + incomeAggregate.getIncomeId();
RLock lock = redissonClient.getLock(lockKey);
try {
if (!lock.tryLock(LOCK_TIMEOUT, TimeUnit.SECONDS)) {
throw new RuntimeException("当前订单已有用户在处理");
}
EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(incomeAggregate.getIncomeId());
VoucherAggregate agg = voucherRepository.findByOutOrderNo(incomeAggregate.getIncomeId());
// 查询凭证 是否有申请中的
if(agg!=null && agg.getVoucherState().equals(VoucherStateApplyEnum.申请中.getValue())){
throw new RuntimeException("当前回单入账在途中");
}
if(entryBillAggregate!=null && entryBillAggregate.getVoucherState().getValue().equals(VoucherStateEnum.已入账.getValue())
&& agg.getVoucherState().equals(VoucherStateApplyEnum.已入账.getValue())
){
throw new RuntimeException("已经入账的回单不允许再次入账");
}
if(incomeAggregate.getReceiptBillVo().getIncomeTypeName()!=null &&
incomeAggregate.getReceiptBillVo().getIncomeTypeName().equals("已线下入账")){
throw ExceptionUtil.getException(null,"已线下入账不允许暂存入账:"+incomeAggregate.getIncomeId());
}
// 2 入账规则生成凭证
VoucherAggregate voucherAggregate = voucherUtil.generateVoucher(incomeAggregate, "否", VoucherStateApplyEnum.申请中.getValue(),"暂存入账");
voucherAggregate.setBatchId(batchId);
Long voucherId = voucherRepository.save(voucherAggregate);
List<VchDetailEntity> detailList = voucherAggregate.getVchDetailList();
detailList.forEach(t->{
t.setCustomerSupplierName(incomeAggregate.getPaymentAccount().getAccountName());
t.setBillNo(incomeAggregate.getReceiptBillVo().getBillNo());
});
//3 入账保存 上面有
//EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(incomeAggregate.getIncomeId());
List<EntryBillFlowEntity> flowEntityList = new ArrayList<>();
//这里暂存 只能有一个入账过程 底层处理
EntryBillFlowEntity entity = new EntryBillFlowEntity(null, null, new Date(),
EntryTypeEnum.getByValue(EntryTypeEnum.到款转暂存.getValue()),
null,
incomeAggregate.getIncomeId(),
null, voucherId
);
entity.setBatchId(batchId);
flowEntityList.add(entity);
entryBillAggregate = new EntryBillAggregate(
entryBillAggregate.getEntryBillId(),incomeAggregate.getIncomeId(),null,null,
VoucherStateEnum.getByValue(VoucherStateEnum.暂存.getValue())
,flowEntityList
);
Long entryBillFlowId = entryBillRepository.save(entryBillAggregate);
map.put(entryBillFlowId,incomeAggregate);
detailListAll.addAll(detailList);
incomeAggregate.getReceiptBillVo().setIncomeSpeed(IncomeSpeedEnum.入账.getValue());
incomeRepository.save(incomeAggregate);
}catch (InterruptedException e){
Thread.currentThread().interrupt();
throw new RuntimeException("获取锁失败", e);
}finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}, threadPoolExecutor);
futures.add(future);
}
// 2 入账规则生成凭证
VoucherAggregate voucherAggregate = voucherUtil.generateVoucher(incomeAggregate, "否", VoucherStateApplyEnum.申请中.getValue(),"暂存入账");
voucherAggregate.setBatchId(batchId);
Long voucherId = voucherRepository.save(voucherAggregate);
List<VchDetailEntity> detailList = voucherAggregate.getVchDetailList();
detailList.forEach(t->{
t.setCustomerSupplierName(incomeAggregate.getPaymentAccount().getAccountName());
t.setBillNo(incomeAggregate.getReceiptBillVo().getBillNo());
});
//3 入账保存 上面有
//EntryBillAggregate entryBillAggregate = entryBillRepository.findByIncomeId(incomeAggregate.getIncomeId());
List<EntryBillFlowEntity> flowEntityList = new ArrayList<>();
//这里暂存 只能有一个入账过程 底层处理
EntryBillFlowEntity entity = new EntryBillFlowEntity(null, null, new Date(),
EntryTypeEnum.getByValue(EntryTypeEnum.到款转暂存.getValue()),
null,
incomeAggregate.getIncomeId(),
null, voucherId
);
entity.setBatchId(batchId);
flowEntityList.add(entity);
entryBillAggregate = new EntryBillAggregate(
entryBillAggregate.getEntryBillId(),incomeAggregate.getIncomeId(),null,null,
VoucherStateEnum.getByValue(VoucherStateEnum.暂存.getValue())
,flowEntityList
);
Long entryBillFlowId = entryBillRepository.save(entryBillAggregate);
map.put(entryBillFlowId,incomeAggregate);
detailListAll.addAll(detailList);
incomeAggregate.getReceiptBillVo().setIncomeSpeed(IncomeSpeedEnum.入账.getValue());
incomeRepository.save(incomeAggregate);
} catch (Exception e) {
// 如果任一任务失败,抛出异常触发事务回滚
throw new RuntimeException("暂存入账失败",e);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
//行号
for (VchDetailEntity entity:detailListAll) {
entity.setRowIndex(++i);