Long t1 = System.currentTimeMillis();
logger.info(“開始Code批量生成任務” + new Date());
List listApplyQRCode = applyQRCodeService.listApplyQRCodeByStatus(EBatchStatus.TO_PRODUCED);
Long t2 = System.currentTimeMillis();
logger.info(“查詢待制作資料耗時” + (t2 - t1) + “毫秒” + listApplyQRCode.size() + “條資料”);
if (listApplyQRCode != null && listApplyQRCode.size() > 0) {
for (ApplyQRCode ApplyQRCode : listApplyQRCode) {
codeJobService.addApplyQRCode(ApplyQRCode);
}
}
Long end = System.currentTimeMillis();
logger.info(“結束Code批量生成任務” + new Date() + “執行時間長:” + (end - t1) + “毫秒”);
// 開始接口資料寫入生成任務
Long t3 = System.currentTimeMillis();
logger.info("開始接口資料寫入生成任務" + new Date());
interfaceTraceJobService.addCodeAndTrace();
Long end1 = System.currentTimeMillis();
logger.info("結束接口資料寫入生成任務" + new Date() + "執行時間長:" + (end1 - t3) + "毫秒");
// 後賦碼生成處理過程
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ServiceException.class)
public void addApplyQRCode(ApplyQRCode applyQRCode) throws ServiceException {
try {
// 後賦碼狀态改為待制作
applyQRCodeService.modifyApplyQRCodeStatus(applyQRCode.getApplyid(), EBatchStatus.IN_PRODUCTION);
CompanyWithBLOBs company = companyService.findCompanyByID(applyQRCode.getCompid());
Holder privateKey = new Holder();
BouncyCastleWapper.decodeKey(company.getPrivkey(), privateKey, true);
// 制作後賦碼
addApplyQRCodeWithPng(applyQRCode.getApplyid(), applyQRCode.getCompid(), applyQRCode.getCount(), privateKey);
// 後賦碼狀态改為制作完成
applyQRCodeService.modifyApplyQRCodeStatus(applyQRCode.getApplyid(), EBatchStatus.NORMAL);
} catch (Throwable e) {
applyQRCodeService.modifyApplyQRCodeStatus(applyQRCode.getApplyid(), EBatchStatus.APPROVAL_PENDING);
logger.error(“生成二維碼異常,需要重新審批批次資訊 後賦碼ID:” + applyQRCode.getApplyid(), e);
throw new ServiceException(“生成二維碼異常,需要重新審批批次資訊 後賦碼ID:” + applyQRCode.getApplyid(), e);
}
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = ServiceException.class)
public void addApplyQRCodeWithPng(Integer applyId, Integer compId, Integer count, Holder privateKey) {
try {
String pdir = Constant.APPLY_PREFIX.concat(String.valueOf(applyId));
ApplyQRCode applyQRCode = applyQRCodeService.findApplyQRCodeById(applyId);
while (count > 0) {
Code code = new Code();
code.setApplyid(applyId);
code.setCompid(compId);
code.setType(applyQRCode.getType());
codeMapper.insert(code);
String hashcode = new String(Hex.encode(BouncyCastleWapper.sign(new ByteArrayInputStream(String.valueOf(code.getCodeid()).getBytes()),
privateKey.value, DIGEST.MD5)));
String preHash = hashcode.substring(0, 15);
String content = String.format(ConfigureFileHelper.getProperty(“qcts.action.url”),
String.valueOf(code.getCodeid()), preHash);
qrcodeGenerator.buildPNGToDisk(content, pdir, code.getCodeid().toString());
String proHash = hashcode.substring(15, hashcode.length());
modifyCodeHash(code.getCodeid(), preHash, proHash);
count–;
}
} catch (Throwable e) {
logger.error(“背景業務邏輯異常”, e);
throw new ServiceException(“背景業務邏輯異常”, e);
}
}
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
public void modifyCodeHash(Integer codeId, String preHash, String proHash) {
Code code = new Code();
code.setCodeid(codeId);
code.setPrehash(preHash);
code.setProhash(proHash);
codeMapper.updateByPrimaryKeySelective(code);
}
public void buildPNGToDisk(String content, String dir, String pngName) throws Exception {
try {
File dist = new File(qrcodeRealPath.concat(File.separator).concat(dir));
if (!dist.isDirectory()) {
dist.mkdirs();
}
FileOutputStream fos = new FileOutputStream(qrcodeRealPath.concat(File.separator).concat(dir)
.concat(File.separator).concat(pngName).concat(“.png”));
if (hasbg.equals(“0”) || hasbg == null) {
// 帶背景的二維碼
qrcode4ZXingWithBg(content, width, height, fos);
} else {
// 不帶背景的二維碼
qrcode4ZXing(content, width, height, fos);
}
} catch (FileNotFoundException e) {
logger.error("二維碼檔案路徑錯誤", e);
throw new Exception("二維碼檔案路徑錯誤");
} catch (Exception e) {
logger.error("二維碼生成異常", e);
throw new Exception("二維碼生成異常");
}
}
private void qrcode4ZXingWithBg(String contents, int width, int height, OutputStream stream) throws Exception {
Map