From fa17cd9ac7fd1c954b46ed731672f3dd98c6bd2f Mon Sep 17 00:00:00 2001 From: zhengyf Date: Thu, 31 Jul 2025 00:44:29 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=87=AD=E8=AF=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=AA=E6=98=AF=E5=8F=AA=E7=94=9F=E6=88=90?= =?UTF-8?q?=E4=B8=80=E6=9D=A1=202=E3=80=81=E4=BC=98=E5=8C=96=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=E3=80=82=203=E3=80=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=A0=A1=E9=AA=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/service/impl/ICoreServiceImpl.java | 1185 ++++++++++------- .../dao/impl/AePushVoucherLogDaoImpl.java | 1 + .../entity/AePushVoucherLogDetailsEntity.xml | 4 +- .../impl/AePushVoucherLogServiceImpl.java | 3 + 4 files changed, 747 insertions(+), 446 deletions(-) diff --git a/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/generate/core/service/impl/ICoreServiceImpl.java b/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/generate/core/service/impl/ICoreServiceImpl.java index 0861ae98..f04eb80b 100644 --- a/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/generate/core/service/impl/ICoreServiceImpl.java +++ b/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/generate/core/service/impl/ICoreServiceImpl.java @@ -18,7 +18,11 @@ import com.hzya.frame.voucher.ae.comf.bd.dao.IAeConfBdOrgBookVODao; import com.hzya.frame.voucher.ae.comf.bd.dao.IMdmDBQueryVODAO; import com.hzya.frame.voucher.ae.comf.bd.dao.IMdmDbFiledVODAO; import com.hzya.frame.voucher.ae.comf.bd.entity.AeConfBdAccsubjEntity; -import com.hzya.frame.voucher.ae.comf.bd.entity.vo.*; +import com.hzya.frame.voucher.ae.comf.bd.entity.vo.BdCurrtypeVO; +import com.hzya.frame.voucher.ae.comf.bd.entity.vo.BdVoucherTypeVO; +import com.hzya.frame.voucher.ae.comf.bd.entity.vo.MdmDBQueryVO; +import com.hzya.frame.voucher.ae.comf.bd.entity.vo.MdmDbFiledVO; +import com.hzya.frame.voucher.ae.comf.bd.entity.vo.OrgBookVO; import com.hzya.frame.voucher.ae.comf.factor.dao.IAeConfInfluenceFactorCorrelationDao; import com.hzya.frame.voucher.ae.comf.factor.dao.IAeConfInfluenceFactorDao; import com.hzya.frame.voucher.ae.comf.factor.entity.AeConfInfluenceFactorCorrelationEntity; @@ -42,9 +46,12 @@ import com.hzya.frame.voucher.ae.generate.core.vo.AccsubjVO; import com.hzya.frame.voucher.ae.generate.core.vo.CreateVoucherVO; import com.hzya.frame.voucher.ae.generate.core.vo.VoucherData; import com.hzya.frame.voucher.ae.push.dao.IAePushVoucherLogDao; +import com.hzya.frame.voucher.ae.push.dao.IAePushVoucherLogDetailsDao; +import com.hzya.frame.voucher.ae.push.entity.AePushVoucherLogDetailsEntity; import com.hzya.frame.voucher.ae.push.entity.AePushVoucherLogEntity; import com.hzya.frame.voucher.ae.push.service.IAePushVoucherLogService; import org.apache.commons.beanutils.BeanUtils; +import org.apache.commons.collections.map.HashedMap; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -232,6 +239,10 @@ public class ICoreServiceImpl implements ICoreService { Assert.state(false, "根据mdmId:{},查询凭证模板为空,请检查!", createVoucherVO.getMdmId()); } if ("Y".equals(templateList.get(0).getTypeMerge())) {//不区分类别 + + //获取模板类型。存在只生成一次的问题. + + AeConfVoucherTemplateEntity aeConfVoucherTemplate = templateList.get(0); List voucher = new ArrayList<>(); List details = new ArrayList<>(); @@ -267,406 +278,537 @@ public class ICoreServiceImpl implements ICoreService { //生成分录 System.out.println(groupedResult); + Map typeDef1Map = new HashedMap(); for (Map.Entry, List>> dataEntry : groupedResult.entrySet()) { try { List dateKey = dataEntry.getKey(); List> dataValue = dataEntry.getValue(); - System.out.println(dateKey); - System.out.println(dataValue); - for (AeConfVoucherTemplateEntity template : templateList) { - System.out.println(template); - VoucherData.Voucher.Detail detail = new VoucherData.Voucher.Detail(); - //汇率:excrate2、locRate、groupRate、globalRate - String exchangeRate = template.getExchangeRate(); - detail.setExcrate2(exchangeRate); - detail.setLocRate(exchangeRate); - detail.setGroupRate(exchangeRate); - detail.setGlobalRate(exchangeRate); + //模板分类 + Map> collect_VoucherType = + templateList.stream().collect(Collectors.groupingBy(index -> index.getTemplateTypeId())); + System.out.println(collect_VoucherType); - //单价:price + for (Map.Entry> voucherTypeEntry : collect_VoucherType.entrySet()) { + String voucherTypeEntry_key = voucherTypeEntry.getKey();//类型id + List voucherTypeEntry_value = voucherTypeEntry.getValue();//分录 - //摘要:explanation - boolean b = templateService.checkAbstract(template); - if (!b) { - Assert.state(false, "根据Id:{},校验摘要失败!", template.getId()); - } - try { - transSbstractRes(createVoucherVO.getMdmId(), template, dataValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.state(false, "根据Id:{},转换摘要失败!失败原因:{}", template.getId(), e.getMessage()); - } - detail.setExplanation(template.getAbstractStr()); - - //科目:pk_accsubj、accountCode、accountName - //分类定义 查 影响因素,再根据影响因素 查 影响因素关联的字段。 - //分类定义 查 对照表 - //影响因素关联的字段,对应对照表的 会计计科目,如果没有对照表 去默认科目 都没有则跳过该条分录 - Long ClassificationId = template.getSubjectClassificationId(); - String ClassificationCode = template.getSubjectClassificationCode(); - String ClassificationName = template.getSubjectClassificationName(); - AeConfSubjectClassificationEntity aeConfSubjectClassificationEntity = new AeConfSubjectClassificationEntity(); - aeConfSubjectClassificationEntity.setId(String.valueOf(ClassificationId)); - List classificationList = subjectClassificationDao.query(aeConfSubjectClassificationEntity); - if (classificationList.size() == 0 || classificationList.size() > 1) { - Assert.state(false, "根据影响因素id:{}。未查询到分类定义或存在多个!{},{}", ClassificationId, ClassificationCode, ClassificationName); - } - AeConfSubjectClassificationEntity subjectClassification = classificationList.get(0); - String factorIds = subjectClassification.getFactorids();//影响因素定义 - String[] factorIdSplit = factorIds.split(","); - Map factorCorrelationMap = new LinkedHashMap<>();//影响因素关联map:影响因素id,单据选项 name中有公式,整个字段或者字段截取 - for (String factorId : factorIdSplit) { - AeConfInfluenceFactorCorrelationEntity factorCorrelationEntity = new AeConfInfluenceFactorCorrelationEntity(); - factorCorrelationEntity.setFactorId(Long.valueOf(factorId)); - List factorCorrelationEntityList = factorCorrelationDao.query(factorCorrelationEntity); - if (factorCorrelationEntityList.size() == 0 || factorCorrelationEntityList.size() > 1) { - Assert.state(false, "根据影响因素id:{}。未查询到或查询到多条影响因素关联。", factorId); - } - String mdmId = factorCorrelationEntityList.get(0).getMdmId(); - String mappingFieldName = factorCorrelationEntityList.get(0).getMappingFieldName(); - //如果存在substr公式,则切割,否则返回单据值 - String strValue = formulaTransFactorCorrelation(mdmId, mappingFieldName, dataValue); - - factorCorrelationMap.put(factorId, strValue); - } - System.out.println(factorCorrelationMap);//影响因素关联的解析 - -// for (Map.Entry factorCorrelationEntry : factorCorrelationMap.entrySet()) { -// String key = factorCorrelationEntry.getKey(); -// //查询影响因素对应的映射档案 -// AeConfInfluenceFactorEntity influenceFactorEntity = new AeConfInfluenceFactorEntity(); -// influenceFactorEntity.setId(key); -// List factorEntityList = factorDao.query(influenceFactorEntity); -// if (factorEntityList.size() == 0 || factorEntityList.size() > 1) { -//// Assert.state(false, "根据影响因素id:{}。未查询到或查询到多条影响因素。", factorId); -// } -// AeConfInfluenceFactorEntity influenceFactor = factorEntityList.get(0); -// String mappingFileId = influenceFactor.getMappingFileId(); -// MdmModuleEntity mdmModuleEntity = new MdmModuleEntity(); -// mdmModuleEntity.setId(mappingFileId); -// List mdmModuleEntityList = mdmModuleDao.query(mdmModuleEntity); -// if (mdmModuleEntityList.size() == 0 || mdmModuleEntityList.size() > 1) { -// Assert.state(false, "根据映射档案id:{}。未查询到或查询到多映射档案。", mappingFileId); -// } -// MdmModuleEntity mdmModule = mdmModuleEntityList.get(0); -// -// -// } -// - String contrastId = subjectClassification.getContrastId();//该分类定义下的对照表 - System.out.println(contrastId); - AeConfSubjectContrastBEntity aeConfSubjectContrastBEntity = new AeConfSubjectContrastBEntity(); - aeConfSubjectContrastBEntity.setContrastId(Long.valueOf(contrastId)); - List subjectContrastBEntityList = subjectContrastBDao.query(aeConfSubjectContrastBEntity); - System.out.println(subjectContrastBEntityList); - AeConfBdAccsubjEntity confBdAccsubj = matchSubj(factorCorrelationMap, subjectContrastBEntityList); - if (confBdAccsubj == null) {//没有匹配到对照表,查询分类定义是否有默认科目 - //分类的定义的默认科目 - String desdocvalueDefaultId = subjectClassification.getDesdocvalueDefaultId(); - String desdocvalueDefaultName = subjectClassification.getDesdocvalueDefaultName(); - if (desdocvalueDefaultId != null && !"".equals(desdocvalueDefaultId) && desdocvalueDefaultName != null && !"".equals(desdocvalueDefaultName)) { - - //查询科目 - AeConfBdAccsubjEntity accsubj = new AeConfBdAccsubjEntity(); - accsubj.setPkAccsubj(desdocvalueDefaultId); - List accsubjList = aeConfBdAccsubjDao.query(accsubj); - if (accsubjList.size() > 1 || accsubjList.size() == 0) { - Assert.state(false, "根据默认科目主键:{},默认科目名称:{},未查询到或存在多条科目,请检查", desdocvalueDefaultId, desdocvalueDefaultName); - } - detail.setPk_accsubj(accsubjList.get(0).getSubjcode());//u8c需要编码 - detail.setAccountId(desdocvalueDefaultId); - detail.setAccountCode(accsubjList.get(0).getSubjcode()); - detail.setAccountName(accsubjList.get(0).getSubjname()); - } else { + if (typeDef1Map.get(voucherTypeEntry_key) != null) { + if (typeDef1Map.get(voucherTypeEntry_key)) { continue; } - } else { - detail.setPk_accsubj(confBdAccsubj.getSubjcode());//u8c需要编码 - detail.setAccountId(confBdAccsubj.getPkAccsubj()); - detail.setAccountCode(confBdAccsubj.getSubjcode()); - detail.setAccountName(confBdAccsubj.getSubjname()); } - //币种编码:pk_currtype、currtypeCode、currtypeName - String currencyFieldName = template.getCurrencyFieldName(); - BdCurrtypeVO bdCurrtypeVO = formulaTransCurrency(currencyFieldName); - if (bdCurrtypeVO == null) { - Assert.state(false, "根据币种字段:{},转换失败"); - } - detail.setPk_currtype(bdCurrtypeVO.getCode()); - detail.setCurrtypeId(bdCurrtypeVO.getPkCurrtype()); - detail.setCurrtypeCode(bdCurrtypeVO.getCode()); - detail.setCurrtypeName(bdCurrtypeVO.getName()); + for (AeConfVoucherTemplateEntity template : voucherTypeEntry_value) { + System.out.println(voucherTypeEntry_key); + + //获取模板类型。存在只生成一次的问题. + AeConfVoucherTemplateTypeEntity templateTypeEntity = new AeConfVoucherTemplateTypeEntity(); + templateTypeEntity.setId(voucherTypeEntry_key); + List templateTypeEntityList = templateTypeDao.query(templateTypeEntity); + if (templateTypeEntityList.size() == 0) { + Assert.state(false, "根据模板类别id:{},查询模板类型失败。请检查", voucherTypeEntry_key); + } + AeConfVoucherTemplateTypeEntity templateType = templateTypeEntityList.get(0); + String templateTypeDef1 = templateType.getDef1(); + if (templateTypeDef1 != null && "Y".equals(templateTypeDef1)) { + typeDef1Map.put(templateType.getId(), true); + } - //借方数量:debitquantity - BigDecimal jNumField = sumAmountAndNum(template.getjNumFieldName(), dataValue); - detail.setDebitquantity(jNumField.toString()); - //原币借方金额:debitamount - BigDecimal jYbSumField = sumAmountAndNum(template.getjYbSumFieldName(), dataValue); - detail.setDebitquantity(jYbSumField.toString()); - //本币借方金额:localdebitamount//本币为null,但是汇率右值,原币*汇率=本币 - if (template.getjBbSumFieldName() == null || "".equals(template.getjBbSumFieldName()) && exchangeRate != null) { - BigDecimal jbb = jYbSumField.divide(new BigDecimal(exchangeRate)); - detail.setDebitquantity(jbb.toString()); - } else { - BigDecimal jBbSumField = sumAmountAndNum(template.getjBbSumFieldName(), dataValue); - detail.setDebitquantity(jBbSumField.toString()); - } + VoucherData.Voucher.Detail detail = new VoucherData.Voucher.Detail(); - //贷方数量:creditquantity - BigDecimal dNumField = sumAmountAndNum(template.getdNumFieldName(), dataValue); - detail.setCreditquantity(dNumField.toString()); - //原币贷方金额:creditamount - BigDecimal dYbSumField = sumAmountAndNum(template.getdYbSumFieldName(), dataValue); - detail.setDebitquantity(dYbSumField.toString()); - //本币贷方金额:localcreditamount - if (template.getdBbSumFieldName() == null || "".equals(template.getdBbSumFieldName()) && exchangeRate != null) { - BigDecimal dbb = dYbSumField.divide(new BigDecimal(exchangeRate)); - detail.setLocalcreditamount(dbb.toString()); - } else { - BigDecimal dBbSumField = sumAmountAndNum(template.getdBbSumFieldName(), dataValue); - detail.setLocalcreditamount(dBbSumField.toString()); - } + //汇率:excrate2、locRate、groupRate、globalRate + String exchangeRate = template.getExchangeRate(); + detail.setExcrate2(exchangeRate); + detail.setLocRate(exchangeRate); + detail.setGroupRate(exchangeRate); + detail.setGlobalRate(exchangeRate); - //业务日期:busidate - detail.setBusidate(date); + //单价:price - //辅助核算 - //查询该科目下的辅助核算 - String templateId = template.getId(); - String pkGlorgbook = template.getPkGlorgbook(); - AeConfBdAccsubjEntity confBdAccsubj1 = new AeConfBdAccsubjEntity(); - confBdAccsubj1.setPkAccsubj(detail.getAccountId()); - confBdAccsubj1.setPkGlorgbook(pkGlorgbook); - List aeConfBdAccsubjEntities = aeConfBdAccsubjDao.queryAssist(confBdAccsubj1);//科目必要的辅助核算 - System.out.println(aeConfBdAccsubjEntities); - Map assCodeNameMap = transferSubjAss(aeConfBdAccsubjEntities); + //摘要:explanation + boolean b = templateService.checkAbstract(template); + if (!b) { + Assert.state(false, "根据Id:{},校验摘要失败!", template.getId()); + } + try { + transSbstractRes(createVoucherVO.getMdmId(), template, dataValue); + } catch (Exception e) { + e.printStackTrace(); + Assert.state(false, "根据Id:{},转换摘要失败!失败原因:{}", template.getId(), e.getMessage()); + } + detail.setExplanation(template.getAbstractStr()); + + //科目:pk_accsubj、accountCode、accountName + //分类定义 查 影响因素,再根据影响因素 查 影响因素关联的字段。 + //分类定义 查 对照表 + //影响因素关联的字段,对应对照表的 会计计科目,如果没有对照表 去默认科目 都没有则跳过该条分录 + Long ClassificationId = template.getSubjectClassificationId(); + String ClassificationCode = template.getSubjectClassificationCode(); + String ClassificationName = template.getSubjectClassificationName(); + AeConfSubjectClassificationEntity aeConfSubjectClassificationEntity = new AeConfSubjectClassificationEntity(); + aeConfSubjectClassificationEntity.setId(String.valueOf(ClassificationId)); + List classificationList = subjectClassificationDao.query(aeConfSubjectClassificationEntity); + if (classificationList.size() == 0 || classificationList.size() > 1) { + Assert.state(false, "根据影响因素id:{}。未查询到分类定义或存在多个!{},{}", ClassificationId, ClassificationCode, ClassificationName); + } + AeConfSubjectClassificationEntity subjectClassification = classificationList.get(0); + String factorIds = subjectClassification.getFactorids();//影响因素定义 + String[] factorIdSplit = factorIds.split(","); + Map factorCorrelationMap = new LinkedHashMap<>();//影响因素关联map:影响因素id,单据选项 name中有公式,整个字段或者字段截取 + for (String factorId : factorIdSplit) { + AeConfInfluenceFactorCorrelationEntity factorCorrelationEntity = new AeConfInfluenceFactorCorrelationEntity(); + factorCorrelationEntity.setFactorId(Long.valueOf(factorId)); + List factorCorrelationEntityList = factorCorrelationDao.query(factorCorrelationEntity); + if (factorCorrelationEntityList.size() == 0 || factorCorrelationEntityList.size() > 1) { + Assert.state(false, "根据影响因素id:{}。未查询到或查询到多条影响因素关联。", factorId); + } + String mdmId = factorCorrelationEntityList.get(0).getMdmId(); + String mappingFieldName = factorCorrelationEntityList.get(0).getMappingFieldName(); + //如果存在substr公式,则切割,否则返回单据值 + String strValue = formulaTransFactorCorrelation(mdmId, mappingFieldName, dataValue); + if(strValue==null){ + Assert.state(false,"影响因素关联的字段解析为空,请检查业务数据"); + } + + factorCorrelationMap.put(factorId, strValue); + } + String contrastId = subjectClassification.getContrastId();//该分类定义下的对照表 + AeConfSubjectContrastBEntity aeConfSubjectContrastBEntity = new AeConfSubjectContrastBEntity(); + aeConfSubjectContrastBEntity.setContrastId(Long.valueOf(contrastId)); + List subjectContrastBEntityList = subjectContrastBDao.query(aeConfSubjectContrastBEntity); + AeConfBdAccsubjEntity confBdAccsubj = matchSubj(factorCorrelationMap, subjectContrastBEntityList); + if (confBdAccsubj == null) {//没有匹配到对照表,查询分类定义是否有默认科目 + //分类的定义的默认科目 + String desdocvalueDefaultId = subjectClassification.getDesdocvalueDefaultId(); + String desdocvalueDefaultName = subjectClassification.getDesdocvalueDefaultName(); + if (desdocvalueDefaultId != null && !"".equals(desdocvalueDefaultId) && desdocvalueDefaultName != null && !"".equals(desdocvalueDefaultName)) { + + //查询科目 + AeConfBdAccsubjEntity accsubj = new AeConfBdAccsubjEntity(); + accsubj.setPkAccsubj(desdocvalueDefaultId); + List accsubjList = aeConfBdAccsubjDao.query(accsubj); + if (accsubjList.size() > 1 || accsubjList.size() == 0) { + Assert.state(false, "根据默认科目主键:{},默认科目名称:{},未查询到或存在多条科目,请检查", desdocvalueDefaultId, desdocvalueDefaultName); + } + detail.setPk_accsubj(accsubjList.get(0).getSubjcode());//u8c需要编码 + detail.setAccountId(desdocvalueDefaultId); + detail.setAccountCode(accsubjList.get(0).getSubjcode()); + detail.setAccountName(accsubjList.get(0).getSubjname()); + } else { + continue; + } + } else { + detail.setPk_accsubj(confBdAccsubj.getSubjcode());//u8c需要编码 + detail.setAccountId(confBdAccsubj.getPkAccsubj()); + detail.setAccountCode(confBdAccsubj.getSubjcode()); + detail.setAccountName(confBdAccsubj.getSubjname()); + } + + //币种编码:pk_currtype、currtypeCode、currtypeName + String currencyFieldName = template.getCurrencyFieldName(); + BdCurrtypeVO bdCurrtypeVO = formulaTransCurrency(currencyFieldName); + if (bdCurrtypeVO == null) { + Assert.state(false, "根据币种字段:{},转换失败"); + } + detail.setPk_currtype(bdCurrtypeVO.getCode()); + detail.setCurrtypeId(bdCurrtypeVO.getPkCurrtype()); + detail.setCurrtypeCode(bdCurrtypeVO.getCode()); + detail.setCurrtypeName(bdCurrtypeVO.getName()); + + //借方数量:debitquantity + BigDecimal jNumField = sumAmountAndNum(template.getjNumFieldName(), dataValue); + detail.setDebitquantity(jNumField.toString()); + //原币借方金额:debitamount + BigDecimal jYbSumField = sumAmountAndNum(template.getjYbSumFieldName(), dataValue); + detail.setDebitamount(jYbSumField.toString()); + //本币借方金额:localdebitamount//本币为null,但是汇率右值,原币*汇率=本币 + if (template.getjBbSumFieldName() == null || "".equals(template.getjBbSumFieldName()) && exchangeRate != null) { + BigDecimal jbb = jYbSumField.divide(new BigDecimal(exchangeRate)); + detail.setLocaldebitamount(jbb.toString()); + } else { + BigDecimal jBbSumField = sumAmountAndNum(template.getjBbSumFieldName(), dataValue); + detail.setLocaldebitamount(jBbSumField.toString()); + } + + //贷方数量:creditquantity + BigDecimal dNumField = sumAmountAndNum(template.getdNumFieldName(), dataValue); + detail.setCreditquantity(dNumField.toString()); + //原币贷方金额:creditamount + BigDecimal dYbSumField = sumAmountAndNum(template.getdYbSumFieldName(), dataValue); + detail.setCreditamount(dYbSumField.toString()); + //本币贷方金额:localcreditamount + if (template.getdBbSumFieldName() == null || "".equals(template.getdBbSumFieldName()) && exchangeRate != null) { + BigDecimal dbb = dYbSumField.divide(new BigDecimal(exchangeRate)); + detail.setLocalcreditamount(dbb.toString()); + } else { + BigDecimal dBbSumField = sumAmountAndNum(template.getdBbSumFieldName(), dataValue); + detail.setLocalcreditamount(dBbSumField.toString()); + } + //如果借贷都为0,则不生成该条分录 + String detail_jYb = detail.getDebitamount(); + String detail_jBb = detail.getLocaldebitamount(); + String detail_dYb = detail.getCreditamount(); + String detail_dBb = detail.getLocalcreditamount(); + if ("0".equals(detail_jYb) && "0".equals(detail_jBb) && "0".equals(detail_dYb) && "0".equals(detail_dBb)) { + continue; + } + + //业务日期:busidate + detail.setBusidate(date); + + //辅助核算 + //查询该科目下的辅助核算 + String templateId = template.getId(); + String pkGlorgbook = template.getPkGlorgbook(); + AeConfBdAccsubjEntity confBdAccsubj1 = new AeConfBdAccsubjEntity(); + confBdAccsubj1.setPkAccsubj(detail.getAccountId()); + confBdAccsubj1.setPkGlorgbook(pkGlorgbook); + List aeConfBdAccsubjEntities = aeConfBdAccsubjDao.queryAssist(confBdAccsubj1);//科目必要的辅助核算 + System.out.println(aeConfBdAccsubjEntities); + Map assCodeNameMap = transferSubjAss(aeConfBdAccsubjEntities); // Map> assCodePkBdinfoMap = querySubjAssCodePkBdinfo(aeConfBdAccsubjEntities); - if (assCodeNameMap != null) {//不等于空,需要辅助核算 + if (assCodeNameMap != null) {//不等于空,需要辅助核算 - //查询科目下的辅助核算对应关系 - Map AssByBdCodeMap = queryAssByBdCode(aeConfBdAccsubjEntities); - System.out.println(AssByBdCodeMap); + //查询科目下的辅助核算对应关系 + Map AssByBdCodeMap = queryAssByBdCode(aeConfBdAccsubjEntities); + System.out.println(AssByBdCodeMap); - //查询该分录下配置的辅助核算,模板上的辅助核算 - AeConfVoucherTemplateAssistEntity assistEntity = new AeConfVoucherTemplateAssistEntity(); - assistEntity.setVoucherTemplateId(Long.valueOf(templateId)); - List templateAssist = templateAssDao.query(assistEntity); - System.out.println(templateAssist);//分录配置的辅助核算 - Map AssByMdmId = queryAssByMdmId(templateAssist); - System.out.println(AssByMdmId); + //查询该分录下配置的辅助核算,模板上的辅助核算 + AeConfVoucherTemplateAssistEntity assistEntity = new AeConfVoucherTemplateAssistEntity(); + assistEntity.setVoucherTemplateId(Long.valueOf(templateId)); + List templateAssist = templateAssDao.query(assistEntity); + System.out.println(templateAssist);//分录配置的辅助核算 + Map AssByMdmId = queryAssByMdmId(templateAssist); + System.out.println(AssByMdmId); - List assList = new ArrayList<>(); - //根据科目的辅助核算匹配模板的配置 - for (Map.Entry assByBdCodeEntry : AssByBdCodeMap.entrySet()) { + List assList = new ArrayList<>(); + //根据科目的辅助核算匹配模板的配置 + for (Map.Entry assByBdCodeEntry : AssByBdCodeMap.entrySet()) { - String key = assByBdCodeEntry.getKey(); - String value = assByBdCodeEntry.getValue(); - AeConfVoucherTemplateAssistEntity templateAssistEntity = AssByMdmId.get(key);//去单据辅助核算 + String key = assByBdCodeEntry.getKey(); + String value = assByBdCodeEntry.getValue(); + AeConfVoucherTemplateAssistEntity templateAssistEntity = AssByMdmId.get(key);//去单据辅助核算 - //辅助核算取值 - Map assValue = getAssValue(templateAssistEntity, dataValue, orgBookVO); - System.out.println(assValue); - if(assValue.size()!=0){ - VoucherData.Voucher.Detail.Ass assEntity = new VoucherData.Voucher.Detail.Ass(); - assEntity.setChecktypecode(value); - assEntity.setCheckvaluecode(assValue.get("code")); - assList.add(assEntity); + //辅助核算取值 + Map assValue = getAssValue(templateAssistEntity, dataValue, orgBookVO); + System.out.println(assValue); + if (assValue.size() != 0) { + VoucherData.Voucher.Detail.Ass assEntity = new VoucherData.Voucher.Detail.Ass(); + assEntity.setChecktypecode(value); + assEntity.setCheckvalueid(assValue.get("pk")); + assEntity.setCheckvaluecode(assValue.get("code")); + assEntity.setCheckvaluename(assValue.get("name")); + //类型 + for (AeConfBdAccsubjEntity aeConfBdAccsubjEntity : aeConfBdAccsubjEntities) { + if (value.equals(aeConfBdAccsubjEntity.getBdcode())) { + assEntity.setChecktypename(aeConfBdAccsubjEntity.getBdname()); + break; + } + } + assList.add(assEntity); + } + + + } + if (assList.size() != 0) { + detail.setAss(assList); } } - if(assList.size()!=0){ - detail.setAss(assList); - } + details.add(detail); } - details.add(detail); - } } catch (Exception e) { e.printStackTrace(); + //分录生成失败 Assert.state(false, "分录生成失败,失败原因:{}", e.getMessage()); } } - + if(details.size()==0){ + return; + } vouche.setDetails(details); voucher.add(vouche); + //记录日志 - AePushVoucherLogEntity aePushVoucherLogEntity = new AePushVoucherLogEntity(); - aePushVoucherLogEntity.setMdmId(createVoucherVO.getMdmId()); - aePushVoucherLogEntity.setTemplateTypeId(null); - aePushVoucherLogEntity.setBillCode(createVoucherVO.getBillCode()); - aePushVoucherLogEntity.setBillDate(date); - aePushVoucherLogEntity.setBillStatus("Y"); - aePushVoucherLogDao.saveOrUpdateBymdmIdAndBillCodeAndTypeId(aePushVoucherLogEntity); - - + saveLog("Y",createVoucherVO.getBillCode(),null,createVoucherVO.getMdmId(),voucher); } else {//按类型生成不同的凭证 //根据凭证类型分组 账簿主键+凭证类别+模板id Map> collectTempType = templateList.stream().collect(Collectors.groupingBy(index -> index.getPkGlorgbook() + "," + index.getVoucherTypeId() + "," + index.getTemplateTypeId())); - List voucher = new ArrayList<>(); for (Map.Entry> entry : collectTempType.entrySet()) { + List voucher = new ArrayList<>(); List details = new ArrayList<>(); String key = entry.getKey(); List value = entry.getValue(); + AeConfVoucherTemplateEntity aeConfVoucherTemplateEntity1 = value.get(0); String[] split = key.split(","); - VoucherData.Voucher vouche = new VoucherData.Voucher(); - voucher.add(vouche); //凭证组装头 + VoucherData.Voucher vouche = new VoucherData.Voucher(); //pk_corp;//公司 - OrgBookVO orgBookVO = queryPkCorpByPkGlOrgBook(split[0]); + OrgBookVO orgBookVO = queryPkCorpByPkGlOrgBook(aeConfVoucherTemplateEntity1.getPkGlorgbook()); vouche.setPk_corp(orgBookVO.getPkentityorg()); vouche.setCorp_code(orgBookVO.getGlorgcode()); vouche.setCorp_name(orgBookVO.getGlorgname()); - //pk_glorgbook;//账簿 - vouche.setPk_glorgbook(split[0]); - + vouche.setPk_glorgbook(orgBookVO.getPkglorgbook()); + vouche.setAccbookCode(orgBookVO.getGlorgbookcode()); + vouche.setAccbookName(orgBookVO.getGlorgbookname()); //pk_prepared;//制单人编码 vouche.setPk_prepared("tbadmin"); - + vouche.setPrepared("tbadmin"); //pk_vouchertype;//凭证类别简称 -// BdVoucherTypeVO voucherTypeVO = queryGlOrgBookShortNameByVoucherTypeId(split[1]); -// System.out.println(voucherTypeVO); -// vouche.setPk_vouchertype(voucherTypeVO.getPkVouchertype()); -// vouche.setVouchertype_code(voucherTypeVO.getCode()); -// vouche.setVouchertype_name(voucherTypeVO.getName()); -// vouche.setVouchertype_shortname(voucherTypeVO.getShortname()); - - + BdVoucherTypeVO voucherTypeVO = queryGlOrgBookShortNameByVoucherTypeId(aeConfVoucherTemplateEntity1.getVoucherTypeId()); + System.out.println(voucherTypeVO); + vouche.setPk_vouchertype(voucherTypeVO.getPkVouchertype()); + vouche.setVouchertype_code(voucherTypeVO.getCode()); + vouche.setVouchertype_name(voucherTypeVO.getName()); + vouche.setVouchertype_shortname(voucherTypeVO.getShortname()); //prepareddate;//制单日期(格式示例:2021-04-27)) 根据sys_date找到单据日期字段 vouche.setPrepareddate(date); + vouche.setYear(date.substring(0, 4)); + vouche.setPeriod(date.substring(5, 7)); + //生成分录 + System.out.println(groupedResult); + for (Map.Entry, List>> dataEntry : groupedResult.entrySet()) { + try { + List dateKey = dataEntry.getKey(); + List> dataValue = dataEntry.getValue(); + for (AeConfVoucherTemplateEntity template : templateList) { + VoucherData.Voucher.Detail detail = new VoucherData.Voucher.Detail(); + //汇率:excrate2、locRate、groupRate、globalRate + String exchangeRate = template.getExchangeRate(); + detail.setExcrate2(exchangeRate); + detail.setLocRate(exchangeRate); + detail.setGroupRate(exchangeRate); + detail.setGlobalRate(exchangeRate); - //凭证模板6行,数据三行,那就是18行分录 - for (Map.Entry, List>> dateListEntry : groupedResult.entrySet()) { - System.out.println(collectFileds);//单据汇总规则 - List dateKey = dateListEntry.getKey();//单据汇总规则 - List> dateValue = dateListEntry.getValue(); - if (dateValue.size() == 0) { - continue; - } + //单价:price - System.out.println(dateKey); - System.out.println(dateValue); + //摘要:explanation + boolean b = templateService.checkAbstract(template); + if (!b) { + Assert.state(false, "根据Id:{},校验摘要失败!", template.getId()); + } + try { + transSbstractRes(createVoucherVO.getMdmId(), template, dataValue); + } catch (Exception e) { + e.printStackTrace(); + Assert.state(false, "根据Id:{},转换摘要失败!失败原因:{}", template.getId(), e.getMessage()); + } + detail.setExplanation(template.getAbstractStr()); - //生成分录,根据汇总规则金额相加 - for (AeConfVoucherTemplateEntity template : value) { - System.out.println(template); - VoucherData.Voucher.Detail detail = new VoucherData.Voucher.Detail(); - details.add(detail); + //科目:pk_accsubj、accountCode、accountName + //分类定义 查 影响因素,再根据影响因素 查 影响因素关联的字段。 + //分类定义 查 对照表 + //影响因素关联的字段,对应对照表的 会计计科目,如果没有对照表 去默认科目 都没有则跳过该条分录 + Long ClassificationId = template.getSubjectClassificationId(); + String ClassificationCode = template.getSubjectClassificationCode(); + String ClassificationName = template.getSubjectClassificationName(); + AeConfSubjectClassificationEntity aeConfSubjectClassificationEntity = new AeConfSubjectClassificationEntity(); + aeConfSubjectClassificationEntity.setId(String.valueOf(ClassificationId)); + List classificationList = subjectClassificationDao.query(aeConfSubjectClassificationEntity); + if (classificationList.size() == 0 || classificationList.size() > 1) { + Assert.state(false, "根据影响因素id:{}。未查询到分类定义或存在多个!{},{}", ClassificationId, ClassificationCode, ClassificationName); + } + AeConfSubjectClassificationEntity subjectClassification = classificationList.get(0); + String factorIds = subjectClassification.getFactorids();//影响因素定义 + String[] factorIdSplit = factorIds.split(","); + Map factorCorrelationMap = new LinkedHashMap<>();//影响因素关联map:影响因素id,单据选项 name中有公式,整个字段或者字段截取 + for (String factorId : factorIdSplit) { + AeConfInfluenceFactorCorrelationEntity factorCorrelationEntity = new AeConfInfluenceFactorCorrelationEntity(); + factorCorrelationEntity.setFactorId(Long.valueOf(factorId)); + List factorCorrelationEntityList = factorCorrelationDao.query(factorCorrelationEntity); + if (factorCorrelationEntityList.size() == 0 || factorCorrelationEntityList.size() > 1) { + Assert.state(false, "根据影响因素id:{}。未查询到或查询到多条影响因素关联。", factorId); + } + String mdmId = factorCorrelationEntityList.get(0).getMdmId(); + String mappingFieldName = factorCorrelationEntityList.get(0).getMappingFieldName(); + //如果存在substr公式,则切割,否则返回单据值 + String strValue = formulaTransFactorCorrelation(mdmId, mappingFieldName, dataValue); - //explanation 摘要--需要转换 - boolean b = templateService.checkAbstract(template); - if (!b) { - Assert.state(false, "根据Id:{},校验摘要失败!", template.getId()); - } - try { - transSbstractRes(createVoucherVO.getMdmId(), template, dateValue); - } catch (Exception e) { - e.printStackTrace(); - Assert.state(false, "根据Id:{},转换摘要失败!失败原因:{}", template.getId(), e.getMessage()); - } - detail.setExplanation(template.getAbstractStr()); + factorCorrelationMap.put(factorId, strValue); + } + String contrastId = subjectClassification.getContrastId();//该分类定义下的对照表 + AeConfSubjectContrastBEntity aeConfSubjectContrastBEntity = new AeConfSubjectContrastBEntity(); + aeConfSubjectContrastBEntity.setContrastId(Long.valueOf(contrastId)); + List subjectContrastBEntityList = subjectContrastBDao.query(aeConfSubjectContrastBEntity); + AeConfBdAccsubjEntity confBdAccsubj = matchSubj(factorCorrelationMap, subjectContrastBEntityList); + if (confBdAccsubj == null) {//没有匹配到对照表,查询分类定义是否有默认科目 + //分类的定义的默认科目 + String desdocvalueDefaultId = subjectClassification.getDesdocvalueDefaultId(); + String desdocvalueDefaultName = subjectClassification.getDesdocvalueDefaultName(); + if (desdocvalueDefaultId != null && !"".equals(desdocvalueDefaultId) && desdocvalueDefaultName != null && !"".equals(desdocvalueDefaultName)) { - //pk_accsubj 科目 - //TODO...未根据影响因素,影响因素关联判断 - AeConfSubjectClassificationEntity aeConfSubjectClassificationEntity = new AeConfSubjectClassificationEntity(); - aeConfSubjectClassificationEntity.setId(String.valueOf(template.getSubjectClassificationId())); - List aeConfSubjectClassificationList = subjectClassificationDao.query(aeConfSubjectClassificationEntity); - if (aeConfSubjectClassificationList == null || aeConfSubjectClassificationList.size() == 0) { - Assert.state(false, "根据科目分类id:{},查询科目分类为空或不唯一,请检查!", template.getSubjectClassificationId()); - } - AeConfSubjectClassificationEntity aeConfSubjectClassification = aeConfSubjectClassificationList.get(0); - //取默认 - if (aeConfSubjectClassification.getDesdocvalueDefaultId() != null && !"".equals(aeConfSubjectClassification.getDesdocvalueDefaultId())) { - AeConfBdAccsubjEntity aeConfBdAccsubjEntity = new AeConfBdAccsubjEntity(); - aeConfBdAccsubjEntity.setPkAccsubj(aeConfSubjectClassification.getDesdocvalueDefaultId()); - List aeConfBdAccsubjList = aeConfBdAccsubjDao.query(aeConfBdAccsubjEntity); - //如果没有默认,则不生成该条分录 - if (aeConfBdAccsubjList == null || aeConfBdAccsubjList.size() == 0) { + //查询科目 + AeConfBdAccsubjEntity accsubj = new AeConfBdAccsubjEntity(); + accsubj.setPkAccsubj(desdocvalueDefaultId); + List accsubjList = aeConfBdAccsubjDao.query(accsubj); + if (accsubjList.size() > 1 || accsubjList.size() == 0) { + Assert.state(false, "根据默认科目主键:{},默认科目名称:{},未查询到或存在多条科目,请检查", desdocvalueDefaultId, desdocvalueDefaultName); + } + detail.setPk_accsubj(accsubjList.get(0).getSubjcode());//u8c需要编码 + detail.setAccountId(desdocvalueDefaultId); + detail.setAccountCode(accsubjList.get(0).getSubjcode()); + detail.setAccountName(accsubjList.get(0).getSubjname()); + } else { + continue; + } + } else { + detail.setPk_accsubj(confBdAccsubj.getSubjcode());//u8c需要编码 + detail.setAccountId(confBdAccsubj.getPkAccsubj()); + detail.setAccountCode(confBdAccsubj.getSubjcode()); + detail.setAccountName(confBdAccsubj.getSubjname()); + } + //币种编码:pk_currtype、currtypeCode、currtypeName + String currencyFieldName = template.getCurrencyFieldName(); + BdCurrtypeVO bdCurrtypeVO = formulaTransCurrency(currencyFieldName); + if (bdCurrtypeVO == null) { + Assert.state(false, "根据币种字段:{},转换失败"); + } + detail.setPk_currtype(bdCurrtypeVO.getCode()); + detail.setCurrtypeId(bdCurrtypeVO.getPkCurrtype()); + detail.setCurrtypeCode(bdCurrtypeVO.getCode()); + detail.setCurrtypeName(bdCurrtypeVO.getName()); + //借方数量:debitquantity + BigDecimal jNumField = sumAmountAndNum(template.getjNumFieldName(), dataValue); + detail.setDebitquantity(jNumField.toString()); + //原币借方金额:debitamount + BigDecimal jYbSumField = sumAmountAndNum(template.getjYbSumFieldName(), dataValue); + detail.setDebitamount(jYbSumField.toString()); + //本币借方金额:localdebitamount//本币为null,但是汇率右值,原币*汇率=本币 + if (template.getjBbSumFieldName() == null || "".equals(template.getjBbSumFieldName()) && exchangeRate != null) { + BigDecimal jbb = jYbSumField.divide(new BigDecimal(exchangeRate)); + detail.setLocaldebitamount(jbb.toString()); + } else { + BigDecimal jBbSumField = sumAmountAndNum(template.getjBbSumFieldName(), dataValue); + detail.setLocaldebitamount(jBbSumField.toString()); + } + + //贷方数量:creditquantity + BigDecimal dNumField = sumAmountAndNum(template.getdNumFieldName(), dataValue); + detail.setCreditquantity(dNumField.toString()); + //原币贷方金额:creditamount + BigDecimal dYbSumField = sumAmountAndNum(template.getdYbSumFieldName(), dataValue); + detail.setCreditamount(dYbSumField.toString()); + //本币贷方金额:localcreditamount + if (template.getdBbSumFieldName() == null || "".equals(template.getdBbSumFieldName()) && exchangeRate != null) { + BigDecimal dbb = dYbSumField.divide(new BigDecimal(exchangeRate)); + detail.setLocalcreditamount(dbb.toString()); + } else { + BigDecimal dBbSumField = sumAmountAndNum(template.getdBbSumFieldName(), dataValue); + detail.setLocalcreditamount(dBbSumField.toString()); + } + //如果借贷都为0,则不生成该条分录 + String detail_jYb = detail.getDebitamount(); + String detail_jBb = detail.getLocaldebitamount(); + String detail_dYb = detail.getCreditamount(); + String detail_dBb = detail.getLocalcreditamount(); + if("0".equals(detail_jYb)&&"0".equals(detail_jBb)&&"0".equals(detail_dYb)&&"0".equals(detail_dBb)){ continue; } - detail.setPk_accsubj(aeConfBdAccsubjList.get(0).getSubjcode()); - } + //业务日期:busidate + detail.setBusidate(date); + //辅助核算 + //查询该科目下的辅助核算 + String templateId = template.getId(); + String pkGlorgbook = template.getPkGlorgbook(); + AeConfBdAccsubjEntity confBdAccsubj1 = new AeConfBdAccsubjEntity(); + confBdAccsubj1.setPkAccsubj(detail.getAccountId()); + confBdAccsubj1.setPkGlorgbook(pkGlorgbook); + List aeConfBdAccsubjEntities = aeConfBdAccsubjDao.queryAssist(confBdAccsubj1);//科目必要的辅助核算 + System.out.println(aeConfBdAccsubjEntities); + Map assCodeNameMap = transferSubjAss(aeConfBdAccsubjEntities); +// Map> assCodePkBdinfoMap = querySubjAssCodePkBdinfo(aeConfBdAccsubjEntities); + if (assCodeNameMap != null) {//不等于空,需要辅助核算 - //pk_currtype 币种编码 - if (template.getCurrencyField() == null || "".equals(template.getCurrencyField())) { - detail.setPk_currtype("CNY"); - } + //查询科目下的辅助核算对应关系 + Map AssByBdCodeMap = queryAssByBdCode(aeConfBdAccsubjEntities); + System.out.println(AssByBdCodeMap); - //excrate2 汇率 - if (template.getExchangeRate() == null || "".equals(template.getExchangeRate())) { - detail.setExcrate2("1"); - } else { - detail.setExcrate2(template.getExchangeRate()); - } + //查询该分录下配置的辅助核算,模板上的辅助核算 + AeConfVoucherTemplateAssistEntity assistEntity = new AeConfVoucherTemplateAssistEntity(); + assistEntity.setVoucherTemplateId(Long.valueOf(templateId)); + List templateAssist = templateAssDao.query(assistEntity); + System.out.println(templateAssist);//分录配置的辅助核算 + Map AssByMdmId = queryAssByMdmId(templateAssist); + System.out.println(AssByMdmId); - //原币*汇率=本币 - //debitquantity 借数量 - BigDecimal jNum = new BigDecimal(0); - String jNumFieldName = null; - if (template.getjNumField() != null && "".equals(template.getjNumField())) { - jNumFieldName = validFieldPath(createVoucherVO.getMdmId(), template.getjNumField()); - if (jNumFieldName != null) { - for (Map dateMap : dateValue) { - if (jNumFieldName != null) { - Object o = dateMap.get(jNumFieldName); + List assList = new ArrayList<>(); + //根据科目的辅助核算匹配模板的配置 + for (Map.Entry assByBdCodeEntry : AssByBdCodeMap.entrySet()) { + + String keyy = assByBdCodeEntry.getKey(); + String valuee = assByBdCodeEntry.getValue(); + AeConfVoucherTemplateAssistEntity templateAssistEntity = AssByMdmId.get(keyy);//去单据辅助核算 + + //辅助核算取值 + Map assValue = getAssValue(templateAssistEntity, dataValue, orgBookVO); + System.out.println(assValue); + if(assValue.size()!=0){ + VoucherData.Voucher.Detail.Ass assEntity = new VoucherData.Voucher.Detail.Ass(); + assEntity.setChecktypecode(valuee); + assEntity.setCheckvalueid(assValue.get("pk")); + assEntity.setCheckvaluecode(assValue.get("code")); + assEntity.setCheckvaluename(assValue.get("name")); + //类型 + for (AeConfBdAccsubjEntity aeConfBdAccsubjEntity : aeConfBdAccsubjEntities) { + if(valuee.equals(aeConfBdAccsubjEntity.getBdcode())){ + assEntity.setChecktypename(aeConfBdAccsubjEntity.getBdname()); + break; + } + } + assList.add(assEntity); } } + if (assList.size() != 0) { + detail.setAss(assList); + } } + details.add(detail); } - //localdebitamount 借本 - //debitamount 借原 - - - //creditquantity 贷数量 - //localcreditamount 贷本 - //creditamount 贷原 - - - //ass 辅助核算 - //辅助核算-checktypecode 辅助核算类型编码 - //辅助核算-checkvaluecode 辅助核算的值编码 - - - //cashflow 现金流映射 - //现金流映射-money 原币金额 - //现金流映射-pk_cashflow 现金流量表项编码 - //现金流映射-pk_currtype 现金流量币种编码 - + } catch (Exception e) { + e.printStackTrace(); + //分录生成失败 + Assert.state(false, "分录生成失败,失败原因:{}", e.getMessage()); } - } - vouche.setDetails(details); + if(details.size()==0){ + return; + } - System.out.println(JSONUtil.toJsonStr(vouche)); + vouche.setDetails(details); + voucher.add(vouche); + //记录日志 + saveLog("Y",createVoucherVO.getBillCode(),null,createVoucherVO.getMdmId(),voucher); } } } catch (Exception e) { + //抽取业务单据报错 e.printStackTrace(); //记录日志 AePushVoucherLogEntity aePushVoucherLogEntity = new AePushVoucherLogEntity(); aePushVoucherLogEntity.setMdmId(createVoucherVO.getMdmId()); aePushVoucherLogEntity.setBillCode(createVoucherVO.getBillCode()); - aePushVoucherLogEntity.setBillDate(date); aePushVoucherLogEntity.setBillStatus("N"); + aePushVoucherLogEntity.setBillDate(date); aePushVoucherLogEntity.setPushRes(e.getMessage()); - aePushVoucherLogDao.save(aePushVoucherLogEntity); + aePushVoucherLogDao.saveOrUpdateBymdmIdAndBillCodeAndTypeId(aePushVoucherLogEntity); Assert.state(false, "生成凭证过程报错,报错信息:{}", e.getMessage()); } } @@ -1484,150 +1626,192 @@ public class ICoreServiceImpl implements ICoreService { * @param orgBookVO 公司账簿信息 * @return */ - private Map validFieldPathAss(AeConfVoucherTemplateAssistEntity file, Map dataMap, OrgBookVO orgBookVO) { - Map map=new LinkedHashMap<>(); - //查询档案数据 - List> fileDataMap = queryDataMapByMdmId(file.getMdmId(), orgBookVO); - //查需该档案的pk,code,name字段 - MdmDbFiledVO mdmDbFiledVO = queryDataPkCodeNameFiledByMdmId(file.getMdmId()); + private static final String RULE_FORM_NAME = "typeOptionRuleForm"; + private static final String RULE_CODE_SERVICE = "service"; + private static final String RULE_CODE_LABEL = "label"; - String[] pathParts = file.getFieldName().split("\\."); - String fileMdmId = file.getMdmId(); - switch (pathParts.length) { - case 1: { - Assert.state(false, ":{}。辅助核算字段关联报错,请检查", pathParts); - } - break; - case 2: { - MdmModuleDbEntity currentDb = queryDb(pathParts[0]); - MdmModuleDbFiledsEntity field = queryFileds(currentDb.getId(), pathParts[1]); - String dataValue = dataMap.get(field.getEnName()).toString(); - //匹配档案数据 - Map stringObjectMap = dataComparison(fileDataMap, mdmDbFiledVO, dataValue); - if (stringObjectMap == null) { - Assert.state(false, "辅助核算内容匹配失败!"); - } - map.put("pk", stringObjectMap.get("pk")); - map.put("code", stringObjectMap.get("code")); - map.put("name", stringObjectMap.get("name")); - } - break; - case 3: { - //取业务数据pathParts[1]字段的值。 - //查询pathParts[1]关联的表,查询关联表的所有字段 - //使用pathParts[2]匹配字段 - MdmModuleDbEntity currentDb = queryDb(pathParts[0]); - MdmModuleDbFiledsEntity field1 = queryFileds(currentDb.getId(), pathParts[1]); - String dataValue = dataMap.get(field1.getEnName()).toString();//循环匹配的值 - - //查询pathParts[1]关联的表,查询关联表的所有字段 - String field1Id = field1.getId(); - MdmModuleDbFiledsRuleEntity filedsRule1 = new MdmModuleDbFiledsRuleEntity(); - filedsRule1.setFiledId(field1Id); - List field1Rules = mdmModuleDbFiledsRuleDao.query(filedsRule1); - MdmModuleDbFiledsRuleEntity rule1Value_bdName=null;//关联的表 - MdmModuleDbFiledsRuleEntity rule1Value_contFiled=null;//关联的字段 - for (MdmModuleDbFiledsRuleEntity field1Rule : field1Rules) { - if ("typeOptionRuleForm".equals(field1Rule.getFormName()) && "service".equals(field1Rule.getRuleCode())) { - rule1Value_bdName = field1Rule; - } - if ("typeOptionRuleForm".equals(field1Rule.getFormName()) && "label".equals(field1Rule.getRuleCode())) { - rule1Value_contFiled = field1Rule; - } - } - //字段跟踪,表+字段+数据+公司 - Map map1 = valueDispose(rule1Value_bdName,rule1Value_contFiled,dataValue,orgBookVO); - //根据bdName查询该档案的pk,code,name字段 - MdmDbFiledVO mdmDbFiledVO1 = queryPkCodeNameByBdName(rule1Value_bdName.getRuleValue()); - String tablepkname1 = mdmDbFiledVO1.getTablepkname(); - String codefieldname1 = mdmDbFiledVO1.getCodefieldname(); - String namefieldname1 = mdmDbFiledVO1.getNamefieldname(); - map.put("pk",map1.get(tablepkname1)==null?null:map1.get(tablepkname1).toString()); - map.put("code",map1.get(codefieldname1)==null?null:map1.get(codefieldname1).toString()); - map.put("name",map1.get(namefieldname1)==null?null:map1.get(namefieldname1).toString()); - } - break; - case 4:{ - //取业务数据pathParts[1]字段的值。 - //查询pathParts[1]关联的表,查询关联表的所有字段 - //使用pathParts[2]匹配字段 - //查询pathParts[2]关联的表,查需关联表的所有字段 - //使用pathParts[3]匹配字段 - MdmModuleDbEntity currentDb = queryDb(pathParts[0]); - MdmModuleDbFiledsEntity field1 = queryFileds(currentDb.getId(), pathParts[1]); - String dataValue = dataMap.get(field1.getEnName()).toString();//循环匹配的值 - - //查询pathParts[1]关联的表,查询关联表的所有字段 - String field1Id = field1.getId(); - MdmModuleDbFiledsRuleEntity filedsRule1 = new MdmModuleDbFiledsRuleEntity(); - filedsRule1.setFiledId(field1Id); - List field1Rules = mdmModuleDbFiledsRuleDao.query(filedsRule1); - MdmModuleDbFiledsRuleEntity rule1Value_bdName=null;//关联的表 - MdmModuleDbFiledsRuleEntity rule1Value_contFiled=null;//关联的字段 - for (MdmModuleDbFiledsRuleEntity field1Rule : field1Rules) { - if ("typeOptionRuleForm".equals(field1Rule.getFormName()) && "service".equals(field1Rule.getRuleCode())) { - rule1Value_bdName = field1Rule; - } - if ("typeOptionRuleForm".equals(field1Rule.getFormName()) && "label".equals(field1Rule.getRuleCode())) { - rule1Value_contFiled = field1Rule; - } - } - //字段跟踪,表+字段+数据+公司 - Map map1 = valueDispose(rule1Value_bdName,rule1Value_contFiled,dataValue,orgBookVO); - //根据bdName查询该档案的pk,code,name字段 - MdmDbFiledVO mdmDbFiledVO1 = queryPkCodeNameByBdName(rule1Value_bdName.getRuleValue()); - String tablepkname1 = mdmDbFiledVO1.getTablepkname(); - String codefieldname1 = mdmDbFiledVO1.getCodefieldname(); - String namefieldname1 = mdmDbFiledVO1.getNamefieldname(); - - - //查询pathParts[2]关联的表,查需关联表的所有字段 - String bdName2 = rule1Value_bdName.getRuleValue(); - //查询mdm_module_bd2 - MdmModuleDbEntity mdmModuleDb2Entity = new MdmModuleDbEntity(); - mdmModuleDb2Entity.setDbName(bdName2); - List dbEntity2List = mdmModuleDbDao.query(mdmModuleDb2Entity); - if(dbEntity2List.size()==0||dbEntity2List.size()>1){ - Assert.state(false,"根据bdName2:{},查询mdm_module_bd2表失败",bdName2); - } - MdmModuleDbEntity mdmModuleDb2 = dbEntity2List.get(0); - //查询字段 - MdmModuleDbFiledsEntity field2 = queryFileds(mdmModuleDb2.getId(), pathParts[2]); - String field2Id = field2.getId(); - MdmModuleDbFiledsRuleEntity filedsRule2 = new MdmModuleDbFiledsRuleEntity(); - filedsRule2.setFiledId(field2Id); - List field2Rules = mdmModuleDbFiledsRuleDao.query(filedsRule2); - MdmModuleDbFiledsRuleEntity rule2Value_bdName=null;//关联的表 - MdmModuleDbFiledsRuleEntity rule2Value_contFiled=null;//关联的字段 - for (MdmModuleDbFiledsRuleEntity field2Rule : field2Rules) { - if ("typeOptionRuleForm".equals(field2Rule.getFormName()) && "service".equals(field2Rule.getRuleCode())) { - rule2Value_bdName = field2Rule; - } - if ("typeOptionRuleForm".equals(field2Rule.getFormName()) && "label".equals(field2Rule.getRuleCode())) { - rule2Value_contFiled = field2Rule; - } - } - Object o2 = map1.get(field2.getEnName()); - if(o2==null){ - Assert.state(false,"根据字段:{},查询上层数据失败",map1); - } - String value2 = map1.get(field2.getEnName()).toString(); - Map map2 = valueDispose(rule2Value_bdName,rule2Value_contFiled,value2,orgBookVO); - MdmDbFiledVO mdmDbFiledVO2 = queryPkCodeNameByBdName(rule2Value_bdName.getRuleValue()); - String tablepkname2 = mdmDbFiledVO2.getTablepkname(); - String codefieldname2 = mdmDbFiledVO2.getCodefieldname(); - String namefieldname2 = mdmDbFiledVO2.getNamefieldname(); - map.put("pk",map2.get(tablepkname2)==null?null:map2.get(tablepkname2).toString()); - map.put("code",map2.get(codefieldname2)==null?null:map2.get(codefieldname2).toString()); - map.put("name",map2.get(namefieldname2)==null?null:map2.get(namefieldname2).toString()); - } - break; + private Map validFieldPathAss(AeConfVoucherTemplateAssistEntity file, + Map dataMap, + OrgBookVO orgBookVO) { + Map result = new LinkedHashMap<>(); + // 解析字段路径 + String fieldName = file.getFieldName(); + if (fieldName == null || fieldName.trim().isEmpty()) { + throw new IllegalArgumentException("字段路径不能为空"); } - return map; + String[] pathParts = fieldName.split("\\."); + int level = pathParts.length; + + try { + switch (level) { + case 1: + handleLevel1(pathParts, result); + break; + case 2: + handleLevel2(pathParts, dataMap, orgBookVO, file.getMdmId(), result); + break; + case 3: + handleLevel3(pathParts, dataMap, orgBookVO, file.getMdmId(), result); + break; + case 4: + handleLevel4(pathParts, dataMap, orgBookVO, file.getMdmId(), result); + break; + case 5: + handleLevel5(pathParts, dataMap, orgBookVO, file.getMdmId(), result); + break; + default: + throw new IllegalArgumentException("不支持的字段路径层级:" + level + ",路径:" + fieldName); + } + } catch (Exception e) { + // 可以记录日志 + throw new RuntimeException("辅助核算字段解析失败,路径:" + fieldName, e); + } + + return result; } + private void handleLevel1(String[] pathParts, Map result) { + throw new IllegalArgumentException("字段路径层级为 1,格式错误:" + pathParts[0] + "。辅助核算字段必须至少为 'db.field' 格式。"); + } + private void handleLevel2(String[] pathParts, Map dataMap, OrgBookVO orgBookVO, + String fileMdmId, Map result) { + MdmModuleDbEntity currentDb = queryDb(pathParts[0]); + MdmModuleDbFiledsEntity field = queryFileds(currentDb.getId(), pathParts[1]); + + Object rawValue = dataMap.get(field.getEnName()); + if (rawValue == null) { + throw new IllegalArgumentException("业务数据中缺少字段:" + field.getEnName()); + } + String dataValue = rawValue.toString(); + + List> fileDataMap = queryDataMapByMdmId(fileMdmId, orgBookVO); + MdmDbFiledVO mdmDbFiledVO = queryDataPkCodeNameFiledByMdmId(fileMdmId); + + Map match = dataComparison(fileDataMap, mdmDbFiledVO, dataValue); + if (match == null) { + throw new IllegalArgumentException("辅助核算匹配失败,值:" + dataValue); + } + + result.put("pk", match.get("pk")); + result.put("code", match.get("code")); + result.put("name", match.get("name")); + } + private void handleLevel3(String[] pathParts, Map dataMap, OrgBookVO orgBookVO, + String fileMdmId, Map result) { + // 第1跳 + MdmModuleDbEntity db1 = queryDb(pathParts[0]); + MdmModuleDbFiledsEntity field1 = queryFileds(db1.getId(), pathParts[1]); + Object rawValue1 = dataMap.get(field1.getEnName()); + if (rawValue1 == null) {throw new IllegalArgumentException("字段不存在:" + field1.getEnName());} + String value1 = rawValue1.toString(); + + MdmModuleDbFiledsRuleEntity rule1_bdName = getRuleByCode(field1.getId(), RULE_CODE_SERVICE); + MdmModuleDbFiledsRuleEntity rule1_contFiled = getRuleByCode(field1.getId(), RULE_CODE_LABEL); + + Map map1 = valueDispose(rule1_bdName, rule1_contFiled, value1, orgBookVO); + + // 获取最终档案的 pk/code/name 字段名 + MdmDbFiledVO vo = queryPkCodeNameByBdName(rule1_bdName.getRuleValue()); + result.put("pk", getAsString(map1, vo.getTablepkname())); + result.put("code", getAsString(map1, vo.getCodefieldname())); + result.put("name", getAsString(map1, vo.getNamefieldname())); + } + private void handleLevel4(String[] pathParts, Map dataMap, OrgBookVO orgBookVO, + String fileMdmId, Map result) { + // 第1跳 + MdmModuleDbEntity db1 = queryDb(pathParts[0]); + MdmModuleDbFiledsEntity field1 = queryFileds(db1.getId(), pathParts[1]); + Object rawValue1 = dataMap.get(field1.getEnName()); + if (rawValue1 == null) {throw new IllegalArgumentException("字段不存在:" + field1.getEnName());} + String value1 = rawValue1.toString(); + + MdmModuleDbFiledsRuleEntity rule1_bdName = getRuleByCode(field1.getId(), RULE_CODE_SERVICE); + MdmModuleDbFiledsRuleEntity rule1_contFiled = getRuleByCode(field1.getId(), RULE_CODE_LABEL); + Map map1 = valueDispose(rule1_bdName, rule1_contFiled, value1, orgBookVO); + + // 第2跳 + MdmModuleDbEntity db2 = getDbByBdName(rule1_bdName.getRuleValue()); + MdmModuleDbFiledsEntity field2 = queryFileds(db2.getId(), pathParts[2]); + Object rawValue2 = map1.get(field2.getEnName()); + if (rawValue2 == null) {throw new IllegalArgumentException("字段不存在:" + field2.getEnName());} + String value2 = rawValue2.toString(); + + MdmModuleDbFiledsRuleEntity rule2_bdName = getRuleByCode(field2.getId(), RULE_CODE_SERVICE); + MdmModuleDbFiledsRuleEntity rule2_contFiled = getRuleByCode(field2.getId(), RULE_CODE_LABEL); + Map map2 = valueDispose(rule2_bdName, rule2_contFiled, value2, orgBookVO); + + // 获取最终档案字段 + MdmDbFiledVO vo = queryPkCodeNameByBdName(rule2_bdName.getRuleValue()); + result.put("pk", getAsString(map2, vo.getTablepkname())); + result.put("code", getAsString(map2, vo.getCodefieldname())); + result.put("name", getAsString(map2, vo.getNamefieldname())); + } + private void handleLevel5(String[] pathParts, Map dataMap, OrgBookVO orgBookVO, + String fileMdmId, Map result) { + // 第1跳 + MdmModuleDbEntity db1 = queryDb(pathParts[0]); + MdmModuleDbFiledsEntity field1 = queryFileds(db1.getId(), pathParts[1]); + Object rawValue1 = dataMap.get(field1.getEnName()); + if (rawValue1 == null) {throw new IllegalArgumentException("字段不存在:" + field1.getEnName());} + String value1 = rawValue1.toString(); + + MdmModuleDbFiledsRuleEntity rule1_bdName = getRuleByCode(field1.getId(), RULE_CODE_SERVICE); + MdmModuleDbFiledsRuleEntity rule1_contFiled = getRuleByCode(field1.getId(), RULE_CODE_LABEL); + Map map1 = valueDispose(rule1_bdName, rule1_contFiled, value1, orgBookVO); + + // 第2跳 + MdmModuleDbEntity db2 = getDbByBdName(rule1_bdName.getRuleValue()); + MdmModuleDbFiledsEntity field2 = queryFileds(db2.getId(), pathParts[2]); + Object rawValue2 = map1.get(field2.getEnName()); + if (rawValue2 == null) {throw new IllegalArgumentException("字段不存在:" + field2.getEnName());} + String value2 = rawValue2.toString(); + + MdmModuleDbFiledsRuleEntity rule2_bdName = getRuleByCode(field2.getId(), RULE_CODE_SERVICE); + MdmModuleDbFiledsRuleEntity rule2_contFiled = getRuleByCode(field2.getId(), RULE_CODE_LABEL); + Map map2 = valueDispose(rule2_bdName, rule2_contFiled, value2, orgBookVO); + + // 第3跳 + MdmModuleDbEntity db3 = getDbByBdName(rule2_bdName.getRuleValue()); + MdmModuleDbFiledsEntity field3 = queryFileds(db3.getId(), pathParts[3]); + Object rawValue3 = map2.get(field3.getEnName()); + if (rawValue3 == null) {throw new IllegalArgumentException("字段不存在:" + field3.getEnName());} + String value3 = rawValue3.toString(); + + MdmModuleDbFiledsRuleEntity rule3_bdName = getRuleByCode(field3.getId(), RULE_CODE_SERVICE); + MdmModuleDbFiledsRuleEntity rule3_contFiled = getRuleByCode(field3.getId(), RULE_CODE_LABEL); + Map map3 = valueDispose(rule3_bdName, rule3_contFiled, value3, orgBookVO); + + // 最终目标档案 + MdmDbFiledVO vo = queryPkCodeNameByBdName(rule3_bdName.getRuleValue()); + result.put("pk", getAsString(map3, vo.getTablepkname())); + result.put("code", getAsString(map3, vo.getCodefieldname())); + result.put("name", getAsString(map3, vo.getNamefieldname())); + } + private MdmModuleDbFiledsRuleEntity getRuleByCode(String fieldId, String ruleCode) { + MdmModuleDbFiledsRuleEntity ruleQuery = new MdmModuleDbFiledsRuleEntity(); + ruleQuery.setFiledId(fieldId); + return mdmModuleDbFiledsRuleDao.query(ruleQuery).stream() + .filter(r -> RULE_FORM_NAME.equals(r.getFormName()) && ruleCode.equals(r.getRuleCode())) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("未找到字段规则,fieldId: " + fieldId + ", ruleCode: " + ruleCode)); + } + + private MdmModuleDbEntity getDbByBdName(String bdName) { + MdmModuleDbEntity query = new MdmModuleDbEntity(); + query.setDbName(bdName); + List list = mdmModuleDbDao.query(query); + if (list.size() != 1) { + throw new IllegalArgumentException("根据 bdName 查询到 " + list.size() + " 条记录,期望 1 条,bdName: " + bdName); + } + return list.get(0); + } + + private String getAsString(Map map, String key) { + Object value = map.get(key); + return value != null ? value.toString() : null; + } @@ -1763,4 +1947,115 @@ public class ICoreServiceImpl implements ICoreService { } -} + @Autowired + private IAePushVoucherLogDetailsDao logDetailsDao; + /** + * 日志保存,凭证一条,分录明细多条 + * @param status 状态 + * @param billCode 单据号 + * @param templateTypeId 模板id + * @param mdmId 档案id + * @param voucherList + */ + public void saveLog(String status,String billCode,String templateTypeId,String mdmId,List voucherList){ + for (VoucherData.Voucher voucher : voucherList) { + System.out.println(voucher); + System.out.println(voucher.getDetails()); + AePushVoucherLogEntity voucherLogEntity = new AePushVoucherLogEntity(); + voucherLogEntity.setMdmId(mdmId);//业务id + voucherLogEntity.setBillCode(billCode);//单据号 + voucherLogEntity.setTemplateTypeId(null);//凭证类别id + voucherLogEntity.setBillStatus(status);//推送状态 + voucherLogEntity.setBillDate(voucher.getPrepareddate());//单据日期 + voucherLogEntity.setPushInfo(JSONUtil.toJsonStr(voucher));//推送日志 + voucherLogEntity.setGlOrgbookCode(voucher.getAccbookCode());//账簿 + voucherLogEntity.setGlOrgbookName(voucher.getAccbookName());//账簿 + voucherLogEntity.setVoucherTypeName(voucher.getVouchertype_name());//凭证类别 + + //凭证号 + + voucherLogEntity.setAbstractStr(voucher.getDetails().get(0).getExplanation());//摘要 + + //借方金额计算 + //贷方方金额计算 + countMoney(voucher,voucherLogEntity); + + AePushVoucherLogEntity aePushVoucherLogEntity = aePushVoucherLogDao.saveOrUpdateBymdmIdAndBillCodeAndTypeId(voucherLogEntity); + String HId = aePushVoucherLogEntity.getId(); + + //每次重新生成,分录日志删除重新生成 + AePushVoucherLogDetailsEntity deleteDetails = new AePushVoucherLogDetailsEntity(); + deleteDetails.setVoucherId(Long.valueOf(HId)); + logDetailsDao.logicRemove(deleteDetails); + + //分录保存 + for (VoucherData.Voucher.Detail detail : voucher.getDetails()) { + AePushVoucherLogDetailsEntity logDetail = new AePushVoucherLogDetailsEntity(); + logDetail.setVoucherId(Long.valueOf(HId)); + logDetail.setAbstractStr(detail.getExplanation());//摘要 + logDetail.setPkAccsubj(detail.getAccountId());//科目 + logDetail.setSubjcode(detail.getAccountCode());//科目 + logDetail.setSubjname(detail.getAccountName());//科目 + logDetail.setSubjdispname(detail.getAccountCode()+"-"+detail.getAccountName());//科目 + logDetail.setCurrencyCode(detail.getCurrtypeCode());//币种 + logDetail.setCurrencyName(detail.getCurrtypeName());//币种 + logDetail.setJYbSum(detail.getDebitamount());//借方 + logDetail.setJBbSum(detail.getLocaldebitamount());//借方 + logDetail.setDYbSum(detail.getCreditamount());//贷方 + logDetail.setDBbSum(detail.getLocalcreditamount());//贷方 + + //辅助核算str + List assStrList=new ArrayList<>(); + List assList = detail.getAss(); + if (assList != null && assList.size() != 0) { + for (VoucherData.Voucher.Detail.Ass ass : assList) { + assStrList.add("["+ass.getChecktypename()+":"+ass.getCheckvaluename()+"]"); + } + String join = String.join(",", assStrList); + logDetail.setAssist(join); + } + logDetailsDao.save(logDetail); + } + + } + + } + + public void countMoney(VoucherData.Voucher voucher,AePushVoucherLogEntity voucherLogEntity) { + try { + BigDecimal jYbTotal = new BigDecimal(0); + BigDecimal jBbTotal = new BigDecimal(0); + BigDecimal dYbTotal = new BigDecimal(0); + BigDecimal dBbTotal = new BigDecimal(0); + List details = voucher.getDetails(); + for (VoucherData.Voucher.Detail detail : details) { + //安全地将字符串转为 BigDecimal + jYbTotal = jYbTotal.add(toBigDecimalOrZero(detail.getDebitamount())); + jBbTotal = jBbTotal.add(toBigDecimalOrZero(detail.getLocaldebitamount())); + dYbTotal = dYbTotal.add(toBigDecimalOrZero(detail.getCreditamount())); + dBbTotal = dBbTotal.add(toBigDecimalOrZero(detail.getLocalcreditamount())); + } + //借方金额计算 + voucherLogEntity.setjYbSum(jYbTotal.stripTrailingZeros().toPlainString()); + voucherLogEntity.setjBbSum(jBbTotal.stripTrailingZeros().toPlainString()); + + //贷方金额计算 + + voucherLogEntity.setdYbSum(dYbTotal.stripTrailingZeros().toPlainString()); + voucherLogEntity.setdBbSum(dBbTotal.stripTrailingZeros().toPlainString()); + }catch (Exception e){ + e.printStackTrace(); + } + } + private BigDecimal toBigDecimalOrZero(String value) { + if (value == null || value.trim().isEmpty()) { + return BigDecimal.ZERO; + } + try { + return new BigDecimal(value.trim()); + } catch (NumberFormatException e) { + // 可选:打印警告日志,如:logger.warn("Invalid number format: " + value, e); + return BigDecimal.ZERO; + } + } +} \ No newline at end of file diff --git a/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/push/dao/impl/AePushVoucherLogDaoImpl.java b/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/push/dao/impl/AePushVoucherLogDaoImpl.java index 20edb41d..52cf68a6 100644 --- a/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/push/dao/impl/AePushVoucherLogDaoImpl.java +++ b/base-buildpackage/src/main/java/com/hzya/frame/voucher/ae/push/dao/impl/AePushVoucherLogDaoImpl.java @@ -36,6 +36,7 @@ public class AePushVoucherLogDaoImpl extends MybatisGenericDaoand id = #{id} + and voucher_id = #{voucherId}