package com.changhong.epc.parsing.service.bill.impl; import com.changhong.epc.bean.form.CpnVal; import com.changhong.epc.bean.form.Fieldsets; import com.changhong.epc.bean.form.FormBaseEntity; import com.changhong.epc.bean.form.FormDataEntity; import com.changhong.epc.bean.parsing.BillInfo; import com.changhong.epc.bean.tenant.bill.IOCRBill; import com.changhong.epc.bean.tenant.bill.OCRBillEntity; import com.changhong.epc.bean.tenant.bill.OCRBillHeaderEntity; import com.changhong.epc.constter.system.SystemClients; import com.changhong.epc.parsing.service.util.IFromOperate; import com.iemsoft.framework.cloud.core.tools.JSONTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; /** * 其它票据,分配行程(向表单赋值) */ @Slf4j @Service @Scope("prototype") public class OtherTypeJourneyImpl extends AbstractJourney implements SystemClients { @Resource private IFromOperate fromOperate; /** * 把票据信息放到控件中 * @param oCRBillEntity * @param billInfo * @return */ @Override @Transactional public Fieldsets addBillInfo(OCRBillEntity oCRBillEntity, BillInfo billInfo) { /* 准备数据 */ // 获得表单信息 FormBaseEntity formBaseEntity = fromOperate.selectFromAll(billInfo); Fieldsets fieldsets = new Fieldsets(formBaseEntity); Map lineFromData = fromOperate.selectFromData(billInfo, fieldsets, oCRBillEntity); Map billVal = getBillVal(oCRBillEntity); Integer dataId = null, childDataId = null; CpnVal childCpn = null; /* 查找数据存放位置 */ for(Map.Entry lineBillVal : billVal.entrySet()) { CpnVal child = getContainsChild(lineBillVal, fieldsets); if(child != null){ childDataId = setVal(lineFromData, lineBillVal, child); childCpn = child; }else if(containsMainCpn(lineBillVal, fieldsets)){ dataId = setVal(lineFromData, lineBillVal, null); } } // 修改主表数据 FormDataEntity formDataEntity = new FormDataEntity(); formDataEntity.setTenantId(Objects.toString(oCRBillEntity.getTenantId())); formDataEntity.setFormId(billInfo.getFormId()); if(dataId != null) { formDataEntity.setDataRowNum(dataId); formDataEntity.setMap(getMapInfo(lineFromData, fieldsets.getMainCpn())); fromOperate.updateFormData(formDataEntity); } // 修改子表数据 if(ObjectUtil.notEmpty(childDataId)) { if(childCpn != null){ formDataEntity.setDataRowNum(childDataId); formDataEntity.setChildAlias(childCpn.getAlias()); formDataEntity.setMap(getChildInfo((List>) lineFromData.get(childCpn.getAlias()), fieldsets.getSubform().get(childCpn))); fromOperate.updateFormData(formDataEntity); } for (OCRBillHeaderEntity ocrBillHeaderEntity : oCRBillEntity.getBillDatas()) { ocrBillHeaderEntity.setBhJouneryId(childDataId); } } billInfoCheck(oCRBillEntity); // 添加票据数据 insertBillData(billInfo, oCRBillEntity); return fieldsets; } private Map getChildInfo(List> fromDataChild, List childCpn) { if(ObjectUtil.empty(fromDataChild)){ return null; } return getMapInfo(fromDataChild.get(0), childCpn); } protected Map getMapInfo(Map lineFromData, List cpnVals){ Map info = new HashMap<>(); if(lineFromData == null || cpnVals == null){ return info; } cpnVals.forEach(cpnVal -> info.put(cpnVal.getAlias(), lineFromData.get(cpnVal.getAlias()))); return info; } private Integer setVal(Map lineFromData, Map.Entry lineBillVal, CpnVal child) { if(child == null){ lineFromData.remove(lineBillVal.getKey()); lineFromData.put(lineBillVal.getKey(), lineBillVal.getValue()); return NumberUtils.createInteger(Objects.toString(lineFromData.get("DATAROWNUM"))); }else{ List> childDatas = ((List>)lineFromData.get(child.getAlias())); Map childData = ObjectUtil.empty(childDatas) ? null : childDatas.get(0); if(childData != null) { childData.remove(lineBillVal.getKey()); childData.put(lineBillVal.getKey(), lineBillVal.getValue()); return NumberUtils.createInteger(Objects.toString(childData.get("DATAROWNUM"))); } return null; } } /** * 获得包含的子表单 * @param lineBillVal * @param fieldsets * @return */ public static CpnVal getContainsChild(Map.Entry lineBillVal, Fieldsets fieldsets){ if(fieldsets.getSubform() == null || fieldsets.getSubform().isEmpty()){ return null; } for(Map.Entry> entry : fieldsets.getSubform().entrySet()){ if(ObjectUtil.empty(entry.getValue())){ continue; } for(CpnVal cpnVal : entry.getValue()){ if(Objects.equals(cpnVal.getAlias(), lineBillVal.getKey())){ return entry.getKey(); } } } return null; } /** * 是否包含在主控件中 * @param lineBillVal * @param fieldsets * @return */ public static boolean containsMainCpn(Map.Entry lineBillVal, Fieldsets fieldsets){ for(CpnVal cpnVal : fieldsets.getMainCpn()){ if(Objects.equals(cpnVal.getAlias(), lineBillVal.getKey())){ return true; } } return false; } /** * 获得票据值 * @param oCRBillEntity * @return */ protected Map getBillVal(OCRBillEntity oCRBillEntity){ if(oCRBillEntity == null){ return new HashMap<>(); } Map billVal = new HashMap<>(); // 添加 billVal 数据 oCRBillEntity.getBillDatas() .forEach(ocrBillHeaderEntity ->{ if(ObjectUtil.empty(ocrBillHeaderEntity.getBillBodyEntityList())) { addBillVal(ocrBillHeaderEntity, billVal); }else { ocrBillHeaderEntity.getBillBodyEntityList().forEach(ocrBillBodyEntity -> { addBillVal(ocrBillBodyEntity, billVal); }); } }); log.debug("票据合并信息:{}", JSONTool.toJson(billVal)); return billVal; } protected void addBillVal(IOCRBill iocrBill, Map billVal){ iocrBill.getBillKeyCode() .entrySet() .stream() .forEach(entry -> addBillVal(billVal, entry)); } protected void addBillVal(Map billVal, Map.Entry entry){ if(isNumber(entry.getValue())){ Object val = billVal.get(entry.getKey()); if(isNumber(val)){ double sum = NumberUtils.createDouble(Objects.toString(val)) + NumberUtils.createDouble(Objects.toString(entry.getValue())); entry.setValue(toNumber(sum)); } } billVal.put(entry.getKey(), Objects.toString(entry.getValue())); } protected static boolean isNumber(Object object){ return NumberUtils.isNumber(Objects.toString(object, "")); } public static String toNumber(double d) { NumberFormat nf = new DecimalFormat("0.00"); // 保留两位小数 // nf.setMaximumFractionDigits(2); // 如果不需要四舍五入,可以使用RoundingMode.DOWN nf.setRoundingMode(RoundingMode.UP); return nf.format(d); } }