ariesy 采集入库数据重复-加分布式锁

release
yefei 2 years ago
parent 87ccf9689e
commit c3e63f6e17
  1. 232
      dxhy-core/src/main/java/com/dxhy/core/job/service/impl/InvoiceInterfaceServiceImpl.java

@ -81,7 +81,7 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService {
@Resource @Resource
private RedissonClient redisson; private RedissonClient redisson;
private static final String REDIS_KEY = "redis_oa_01"; private static final String REDIS_KEY = "collect_invoice_";
private static final int MAX_SIZE = 1000; private static final int MAX_SIZE = 1000;
@ -657,6 +657,9 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService {
String company = tDxRecordInvoiceJobDao.findCompany(taxno); String company = tDxRecordInvoiceJobDao.findCompany(taxno);
List<InvoiceLog> logList = new ArrayList<>(); List<InvoiceLog> logList = new ArrayList<>();
for (InvoiceSelectInfo invoiceSelectInfo : invoicesList) { for (InvoiceSelectInfo invoiceSelectInfo : invoicesList) {
//加分布式锁
String lockKey = REDIS_KEY + invoiceSelectInfo.getUuid();
RLock lock = redisson.getLock(lockKey);
invoiceSelectInfo.setUuid(invoiceSelectInfo.getInvoiceCode() + invoiceSelectInfo.getInvoiceNo()); invoiceSelectInfo.setUuid(invoiceSelectInfo.getInvoiceCode() + invoiceSelectInfo.getInvoiceNo());
if ("1".equals(invoiceSelectInfo.getLegalizeState())) { if ("1".equals(invoiceSelectInfo.getLegalizeState())) {
//认证处理状态 0-未认证 1-已勾选未确认,2已确认 3 已发送认证 4 认证成功 5 认证失败 //认证处理状态 0-未认证 1-已勾选未确认,2已确认 3 已发送认证 4 认证成功 5 认证失败
@ -664,126 +667,127 @@ public class InvoiceInterfaceServiceImpl implements InvoiceInterfaceService {
} else { } else {
invoiceSelectInfo.setAuthStatus("0"); invoiceSelectInfo.setAuthStatus("0");
} }
try {
//加分布式锁 lock.lock();
String lockKey = invoiceSelectInfo.getUuid(); stringRedisTemplate.opsForValue().set(lockKey, String.valueOf(0));
RLock lock = redisson.getLock(lockKey); // 判断库里是否已经存在,存在则只更新发票状态,状态更新时间,认证状态以及相关字段更新
lock.lock(); DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
stringRedisTemplate.opsForValue().set(REDIS_KEY, String.valueOf(0)); TDxRecordInvoiceJobEntity entity = tDxRecordInvoiceJobDao.findInvoiceByUUid(invoiceSelectInfo.getUuid());
if (entity != null) {
// 判断库里是否已经存在,存在则只更新发票状态,状态更新时间,认证状态以及相关字段更新 boolean flag = false;
DynamicContextHolder.push(db + DbConstant.BUSINESS_READ); if (StringUtils.isBlank(entity.getCheckCode())
TDxRecordInvoiceJobEntity entity = tDxRecordInvoiceJobDao.findInvoiceByUUid(invoiceSelectInfo.getUuid()); && StringUtils.isNotBlank(invoiceSelectInfo.getCheckCode())) {
if (entity != null) { entity.setCheckCode(invoiceSelectInfo.getCheckCode());
boolean flag = false; entity.setCheckDate(new Date());
if (StringUtils.isBlank(entity.getCheckCode()) flag = true;
&& StringUtils.isNotBlank(invoiceSelectInfo.getCheckCode())) { }
entity.setCheckCode(invoiceSelectInfo.getCheckCode()); if (!entity.getInvoiceStatus().equals(invoiceSelectInfo.getInvoiceStatus())) {
entity.setCheckDate(new Date()); entity.setInvoiceStatus(invoiceSelectInfo.getInvoiceStatus());
flag = true; entity.setStatusUpdateDate(new Date());
} flag = true;
if (!entity.getInvoiceStatus().equals(invoiceSelectInfo.getInvoiceStatus())) { }
entity.setInvoiceStatus(invoiceSelectInfo.getInvoiceStatus()); if ("0".equals(entity.getRzhYesorno())
entity.setStatusUpdateDate(new Date()); && !entity.getRzhYesorno().equals(invoiceSelectInfo.getLegalizeState())) {
flag = true; if ("1".equals(invoiceSelectInfo.getRzlx())) {
} entity.setRzhYesorno(invoiceSelectInfo.getLegalizeState());
if ("0".equals(entity.getRzhYesorno()) entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate());
&& !entity.getRzhYesorno().equals(invoiceSelectInfo.getLegalizeState())) { entity.setRzlx(invoiceSelectInfo.getRzlx());
if ("1".equals(invoiceSelectInfo.getRzlx())) { entity.setRzhType(invoiceSelectInfo.getLegalizeType());
entity.setRzhYesorno(invoiceSelectInfo.getLegalizeState()); entity.setSfygx("1");
entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) {
entity.setRzlx(invoiceSelectInfo.getRzlx()); SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
entity.setRzhType(invoiceSelectInfo.getLegalizeType()); entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate()));
entity.setSfygx("1"); }
if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { if ("1".equals(invoiceSelectInfo.getLegalizeState())) {
SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); entity.setAuthStatus("4");
entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); } else {
} entity.setAuthStatus("0");
if ("1".equals(invoiceSelectInfo.getLegalizeState())) { }
entity.setAuthStatus("4"); entity.setBdkStatus("1");
} else { } else if ("4".equals(invoiceSelectInfo.getRzlx())) {
entity.setAuthStatus("0"); entity.setRzhYesorno("2");
} entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate());
entity.setBdkStatus("1"); entity.setRzlx(invoiceSelectInfo.getRzlx());
} else if ("4".equals(invoiceSelectInfo.getRzlx())) { entity.setRzhType(invoiceSelectInfo.getLegalizeType());
entity.setRzhYesorno("2"); entity.setSfygx("1");
entity.setRzhBelongDate(invoiceSelectInfo.getLegalizeBlongDate()); if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) {
entity.setRzlx(invoiceSelectInfo.getRzlx()); SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd");
entity.setRzhType(invoiceSelectInfo.getLegalizeType()); entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate()));
entity.setSfygx("1"); }
if (StringUtils.isNotBlank(invoiceSelectInfo.getLegalizeDate())) { if ("1".equals(invoiceSelectInfo.getLegalizeState())) {
SimpleDateFormat sim = new SimpleDateFormat("yyyyMMdd"); entity.setAuthStatus("4");
entity.setRzhDate(sim.parse(invoiceSelectInfo.getLegalizeDate())); } else {
entity.setAuthStatus("0");
}
entity.setBdkStatus("2");
} }
if ("1".equals(invoiceSelectInfo.getLegalizeState())) { flag = true;
entity.setAuthStatus("4");
} else { } else if (!"0".equals(entity.getRzhYesorno())
entity.setAuthStatus("0"); && !"1".equals(invoiceSelectInfo.getLegalizeState())) {
entity.setRzhYesorno("0");
entity.setRzhBelongDate(null);
entity.setRzlx("0");
entity.setRzhType(null);
entity.setRzhDate(null);
entity.setAuthStatus("0");
entity.setBdkStatus("0");
entity.setSfygx("0");
flag = true;
}
//TODO 这块业务不理解??
if ("1".equals(entity.getSourceSystem()) && "0".equals(entity.getCollectStatus())) {
entity.setCollectStatus("1");
entity.setCollectFrom("0");
entity.setCollectDate(new Date());
entity.setPoolStatus("1");
entity.setInPoolReason("数据重复");
flag = true;
}
if (flag) {
DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
if (StringUtils.isBlank(entity.getInvoiceCode())) {
entity.setInvoiceCode("");
} }
entity.setBdkStatus("2"); tDxRecordInvoiceJobDao.updateInvoice(entity, taxno);
} }
flag = true; // 艺龙推送数据
if (yLcompany.equals(company)) {
} else if (!"0".equals(entity.getRzhYesorno()) DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
&& !"1".equals(invoiceSelectInfo.getLegalizeState())) { InvoiceScanEntity selectSign = tDxRecordInvoiceJobDao
entity.setRzhYesorno("0"); .selectByScan(entity.getInvoiceCode() + entity.getInvoiceNo(), dxhyAdmin);
entity.setRzhBelongDate(null); if (selectSign != null && "1".equals(selectSign.getQsStatus())) {
entity.setRzlx("0"); String record = JSON.toJSONString(entity);
entity.setRzhType(null); sender.sendToStatus(Base64.encode(record));
entity.setRzhDate(null); }
entity.setAuthStatus("0");
entity.setBdkStatus("0");
entity.setSfygx("0");
flag = true;
}
//TODO 这块业务不理解??
if ("1".equals(entity.getSourceSystem()) && "0".equals(entity.getCollectStatus())) {
entity.setCollectStatus("1");
entity.setCollectFrom("0");
entity.setCollectDate(new Date());
entity.setPoolStatus("1");
entity.setInPoolReason("数据重复");
flag = true;
}
if (flag) {
DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
if (StringUtils.isBlank(entity.getInvoiceCode())) {
entity.setInvoiceCode("");
} }
tDxRecordInvoiceJobDao.updateInvoice(entity, taxno); } else {
} if (StringUtils.isBlank(invoiceSelectInfo.getLegalizeDate())) {
// 艺龙推送数据 invoiceSelectInfo.setLegalizeDate(null);
if (yLcompany.equals(company)) {
DynamicContextHolder.push(db + DbConstant.BUSINESS_READ);
InvoiceScanEntity selectSign = tDxRecordInvoiceJobDao
.selectByScan(entity.getInvoiceCode() + entity.getInvoiceNo(), dxhyAdmin);
if (selectSign != null && "1".equals(selectSign.getQsStatus())) {
String record = JSON.toJSONString(entity);
sender.sendToStatus(Base64.encode(record));
} }
TDxRecordInvoiceJobEntity record = exchangePo2Entity(invoiceSelectInfo);
record.setCompany(company);
record.setCollectStatus("1");
record.setCollectFrom("0");
record.setCollectDate(new Date());
record.setPoolStatus("0");
DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE);
tDxRecordInvoiceJobDao.insert(record);
} }
} else { InvoiceLog invoiceLog = new InvoiceLog();
if (StringUtils.isBlank(invoiceSelectInfo.getLegalizeDate())) { invoiceLog.setInvoiceCode(invoiceSelectInfo.getInvoiceCode());
invoiceSelectInfo.setLegalizeDate(null); invoiceLog.setInvoiceNo(invoiceSelectInfo.getInvoiceNo());
} invoiceLog.setType("1");
TDxRecordInvoiceJobEntity record = exchangePo2Entity(invoiceSelectInfo); invoiceLog.setInputStatus("0");
record.setCompany(company); invoiceLog.setCreateDate(new Date());
record.setCollectStatus("1"); invoiceLog.setInputName("系统自动");
record.setCollectFrom("0"); logList.add(invoiceLog);
record.setCollectDate(new Date()); }catch (Exception e) {
record.setPoolStatus("0"); e.printStackTrace();
DynamicContextHolder.push(db + DbConstant.BUSINESS_WRITE); } finally {
tDxRecordInvoiceJobDao.insert(record); lock.unlock();
} }
InvoiceLog invoiceLog = new InvoiceLog();
invoiceLog.setInvoiceCode(invoiceSelectInfo.getInvoiceCode());
invoiceLog.setInvoiceNo(invoiceSelectInfo.getInvoiceNo());
invoiceLog.setType("1");
invoiceLog.setInputStatus("0");
invoiceLog.setCreateDate(new Date());
invoiceLog.setInputName("系统自动");
logList.add(invoiceLog);
} }
httpLog.setStatus("1"); httpLog.setStatus("1");
httpLog.setTotal(total); httpLog.setTotal(total);

Loading…
Cancel
Save