package com.changhong.epc.parsing.service.bill.cpnval.imp; 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.parsing.BillLevel; import com.changhong.epc.bean.tenant.bill.OCRBillBodyEntity; import com.changhong.epc.bean.tenant.bill.OCRBillHeaderEntity; import com.changhong.epc.constter.parsing.bill.ConstBill; import com.changhong.epc.constter.system.businesscode.BusinessMeaningCode; import com.changhong.epc.parsing.service.bill.cpnval.IBillToCpnValData; import com.iemsoft.framework.cloud.core.exception.IEMRuntimeException; 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.StringUtils; import java.io.*; import java.util.*; import java.util.Map.Entry; @Slf4j public class BillToCpnValData implements IBillToCpnValData>, ConstBill { private List data; /** * 自定义表单原始数据 */ private FormBaseEntity formBaseEntity; /** * 组 * */ private CpnVal journeySubFrom; /** * 子表单 中间结构 对应自定义表单 */ private Fieldsets fieldsets = new Fieldsets(); @Override public void setDatSource(List data , FormBaseEntity formAll) { this.data = data; if(this.data == null){ this.data = new ArrayList<>(); } this.formBaseEntity = formAll; } private List journeyCpn; private List> allJourneyCpn; private List getJourneyCpnCopy(){ return deepCopy(journeyCpn); } public static List deepCopy(List src) { if(src instanceof Serializable){ return ObjectUtil.byteToObject(ObjectUtil.objectToByte((Serializable) src), List.class); } else{ return JSONTool.toObj(JSONTool.toJson(src), List.class); } } @Override public void analysisSource() { List> all = new ArrayList<>(); // 处理原始自定义表单结构 formData(); for (BillLevel billLevel : data) { List journeyCpn = getJourneyCpnCopy(); List billLevels = billLevel.getBillLevels(); for (OCRBillHeaderEntity ocrBillHeaderEntity : billLevels) { setJourneyCpnVal(ocrBillHeaderEntity.getBillKeyCode(), journeyCpn); for (OCRBillBodyEntity billBody : ocrBillHeaderEntity.getBillBodyEntityList()) { setJourneyCpnVal(billBody.getBillKeyCode(), journeyCpn); // setJourneyCpnVal(billBody.getBodyMeaningValCodeMap(), journeyCpn); } } // 添加 开始日期、离开日期 setDate(billLevel, journeyCpn); all.add(journeyCpn); } log.debug("分配{}个行程", all.size()); allJourneyCpn = all; } /** * 添加开始时间、离开时间 * @param billLevel * @param journeyCpn */ protected void setDate(BillLevel billLevel, List journeyCpn) { log.debug("开始格式化时间:{}", JSONTool.toJson(journeyCpn)); if(ObjectUtil.empty(journeyCpn)){ return; } journeyCpn.stream() .filter(cpn->Objects.equals(cpn.getAlias(), FROM_DATE)) .forEach(cpn->{ final String date = cpn.getDate(billLevel.getBeginDate(), billLevel.getBeginTime()); cpn.setValCode(date); OCRBillHeaderEntity.forEach(bill->{ if(ObjectUtil.empty(bill.getBillKeyCode(FROM_DATE))){ return; } bill.setBillKeyName(FROM_DATE, date); bill.setBillKeyCode(FROM_DATE, date); },billLevel.getBillLevels()); log.debug("修改开始日期格式"); }); journeyCpn.stream() .filter(cpn->Objects.equals(cpn.getAlias(), LEAVE_DATE)) .forEach(cpn->{ if(ObjectUtil.empty(billLevel.getEndDate())){ return; } final String date = cpn.getDate(billLevel.getEndDate(), billLevel.getEndTime()); cpn.setValCode(date); OCRBillHeaderEntity.forEach(bill->{ if(ObjectUtil.empty(bill.getBillKeyCode(LEAVE_DATE))){ return; } bill.setBillKeyName(LEAVE_DATE, date); bill.setBillKeyCode(LEAVE_DATE, date); },billLevel.getBillLevels()); log.debug("修改离开日期格式"); }); } public void formData(){ // System.out.println(); String fieldsetJson = formBaseEntity.getFieldset(); Map> fieldset = JSONTool.toObj(fieldsetJson, Map.class); if(ObjectUtil.empty(fieldset)){ fieldset = Collections.EMPTY_MAP; } List subformCpns = new ArrayList<>(); for (Map eachCpn : fieldset.values()) { CpnVal cpnVal = mapToDaom(eachCpn); // 找到一个子表单 if(Objects.equals(cpnVal.getTagType(), SUBFORM) && Objects.equals(cpnVal.getParentsubFormNum(), ROOT_ID)){ fieldsets.getSubform().put(cpnVal , new ArrayList<>()); // 找到一个根控件 }else if(Objects.equals(cpnVal.getParentsubFormNum(), ROOT_ID)){ fieldsets.getMainCpn().add(cpnVal); } // 找到一个子表单里的控件 subformCpns.add(cpnVal); } // 将子表单控件添加到 自己的子表单 for (CpnVal cpnVal : subformCpns) { CpnVal cpnValKey = getCpnValKey( fieldsets.getSubform(), cpnVal); List cpns = fieldsets.getSubform().get(cpnValKey); if(cpns!= null) cpns.add(cpnVal); } // 选择使用那个子表单 subFormsSelect(); // System.out.println(""); } public static void main(String... args) { // System.out.println(JSONTool.toObj("", Map.class)); } /** * 将控件Map 转换成实体 CpnVal * @param eachCpn * @return */ private CpnVal mapToDaom(Map eachCpn){ CpnVal cpnVal = new CpnVal(); String tagId = Objects.toString(eachCpn.get(TAG_ID), "") ; String tagName = Objects.toString(eachCpn.get(TAG_NAME), "") ; String tagType = Objects.toString(eachCpn.get(TAG_TYPE), "") ; String columnOrderNum = Objects.toString(eachCpn.get(COLUMN_ORDERNUM), "") ; String parentsubFormNum = Objects.toString(eachCpn.get(PARENT_SUBFOR_MNUM), "") ; String mdCode = ""; String alias = ""; String dateType = ""; Map tagAttribute; try { tagAttribute = (Map) eachCpn.get(TAG_ATTRIBUTE); } catch (Exception e) { tagAttribute = null; } if(tagAttribute != null){ dateType = Objects.toString(tagAttribute.get(DATE_TYPE), ""); alias = Objects.toString(tagAttribute.get(ALIAS), ""); String maste = Objects.toString(tagAttribute.get("masterData")); if(ObjectUtil.notEmpty(maste)) { Map obj = JSONTool.toObj(maste, Map.class); if (obj != null) { mdCode = Objects.toString(obj.get("defindCode"), ""); } } } cpnVal.setAlias(alias); cpnVal.setTagId(tagId); cpnVal.setTagName(tagName); cpnVal.setTagType(tagType); cpnVal.setColumnOrderNum(columnOrderNum); cpnVal.setParentsubFormNum(parentsubFormNum); cpnVal.setMdCode(mdCode); cpnVal.setDateType(dateType); return cpnVal; } /** * 寻找字表单Key * @param subform * @param eachCpnVal * @return */ private CpnVal getCpnValKey(Map> subform, CpnVal eachCpnVal){ Set keySet = fieldsets.getSubform().keySet(); for (CpnVal cpnVal : keySet) { if(Objects.equals(cpnVal.getColumnOrderNum(), eachCpnVal.getParentsubFormNum())){ return cpnVal; } } return new CpnVal(); } /** * 找到要行程子表单(配置规则里选择) * @param */ //@Override public List subFormsSelect() { for (CpnVal group : fieldsets.getSubform().keySet()) { if(StringUtils.equalsIgnoreCase(group.getAlias(), BusinessMeaningCode.JOURNEY_SUB)){ journeySubFrom = group; journeyCpn = fieldsets.getSubform().get(group); break; } } return null; } /** * 行程控件 与 行程数据比较器 */ private void setJourneyCpnVal(Map datas, List journeyCpn){ if(ObjectUtil.empty(datas) || ObjectUtil.empty(journeyCpn)){ return; } for (Entry entry : datas.entrySet()) { // System.out.println("控件::-------------------"+ JSONTool.toJson(entry)); for(CpnVal cpnVal: journeyCpn){ // System.out.println("行程;-------"+ JSONTool.toJson(journeyCpn)); if(!cpnVal.isFlag() // && Objects.equals(entry.getKey(), cpnVal.getMdCode()) && journeyCpnValEquals(entry, cpnVal, ALIAS) ){ cpnVal.setValCode(entry.getValue()); cpnVal.setFlag(true); } } } } /** * 在此处添加 开始时间拼接 */ // public String getCpnVal(CpnVal cpnVal, Map datas){ // if(cpnVal.getTagType()) // } /** * 表单控件 赋值 比较 * @param entry * @param cpnVal * @return */ public boolean journeyCpnValEquals(Entry entry,CpnVal cpnVal,String mode){ boolean res = false; switch (mode) { case "mdCode": res = Objects.equals(entry.getKey(), cpnVal.getMdCode()); break; case ALIAS: res = Objects.equals(entry.getKey(), cpnVal.getAlias()); if(res) // System.err.println("~~~~~~~~~~~~~~~"+ cpnVal.getAlias()+ "控件跟票据数据匹配成功了~~~~~~~~~~~~~~~"); break; default: break; } return res; } @Override public List> getAllJourneyCpn() { return allJourneyCpn; } @Override public void setEachBillJourneyId(List JourneyIds){ if(JourneyIds.size() != data.size()) throw new IEMRuntimeException("行程与行程id 无法对应!不能添加票据对应的行程。"); for(int i=0; i < JourneyIds.size(); i++ ){ addJourneyId(data.get(i) , JourneyIds.get(i)); } } private void addJourneyId(BillLevel billLevel, Integer JourneyId) { for (OCRBillHeaderEntity ocrBill : billLevel.getBillLevels()) { ocrBill.setBhJouneryId(JourneyId); } } @Override public CpnVal getJourneySubForm() { return this.journeySubFrom; } }