package com.changhong.epc.count.service.format.impl; import com.changhong.epc.bean.count.format.CpnInfo; import com.changhong.epc.bean.count.format.CpnInfoCommons; import com.changhong.epc.bean.count.format.FormInfo; import com.changhong.epc.bean.count.format.ItemInfo; import com.changhong.epc.bean.tenant.bill.IOCRBill; import com.changhong.epc.count.service.count.model.CountResultLast; import com.iemsoft.framework.cloud.core.tools.MsgTool; import com.iemsoft.framework.cloud.core.tools.ObjectUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.math.NumberUtils; import org.springframework.stereotype.Service; import java.math.RoundingMode; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.*; import java.util.function.Function; /** * 格式化票据信息 */ @Service("formatBillInfoCount") @Slf4j public class FormatBillInfoCount extends FormatFormInfoCount{ /** * 添加票据信息 * @param formInfo * @param countResultLast */ @Override public void formatResult(FormInfo formInfo, CountResultLast countResultLast) { super.formatResult(formInfo, countResultLast); if(ObjectUtil.notEmpty(countResultLast.getIParamIter().getBillList())) { Map> billList = countResultLast.getIParamIter().getBillList(); CpnInfo journey = getJourneyInfo(formInfo); // 有行程 if (journey != null) { for (ItemInfo itemInfo : journey.getChild()) { List iocrBills; if ((iocrBills = billList.get(itemInfo.getCId())) == null) { continue; } forJourney(formInfo, itemInfo, iocrBills); } } } // 主表单 setCpnBillMsg(formInfo, countResultLast.getIParamIter().getAllBillList()); } /** * 循环行程数据 * @param formInfo * @param itemInfo * @param iocrBills */ protected void forJourney(FormInfo formInfo, ItemInfo itemInfo, List iocrBills){ setCpnBillMsg(itemInfo, iocrBills); } /** * 添加控件值 * @param itemInfo * @param iocrBills */ protected void setCpnBillMsg(CpnInfoCommons itemInfo, List iocrBills) { Map billKeyCode = getBillKeyCode(iocrBills); Map billKeyName = getBillKeyName(iocrBills); log.debug("开始赋值表单{}, code:{}, name:{}", itemInfo, billKeyCode, billKeyName); itemInfo.cpnInfos().stream() .filter(item->billKeyCode.get(item.getFieldKey()) != null) .forEach(item-> itemInfo.addCpnMsg(item.getFieldKey() , CpnInfo.CpnMsgCode.header , new TreeSet<>( Arrays.asList( new CpnInfo.CpnMsgInfo(MsgTool.get(BILL_TITLE_NAME) , billKeyName.get(item.getFieldKey()) , billKeyCode.get(item.getFieldKey()) , true) ) ) ) ); } /** * 获得票据信息 * 别名:编码 * @param iocrBills * @return */ public Map getBillKeyCode(List iocrBills){ Map billInfo = new HashMap<>(); iocrBills.stream().forEach(iocrBill ->appendBillInfo(billInfo, iocrBill, IOCRBill::getBillKeyCode)); return billInfo; } /** * 获得票据信息 * 别名:名称 * @param iocrBills * @return */ public Map getBillKeyName(List iocrBills){ Map billInfo = new HashMap<>(); iocrBills.stream().forEach(iocrBill ->appendBillInfo(billInfo, iocrBill, IOCRBill::getBillKeyName)); return billInfo; } protected void appendBillInfo(Map billInfo, IOCRBill iocrBill, Function> map){ Map iocrBillInfo = map.apply(iocrBill); iocrBillInfo.entrySet().stream().forEach(entry -> { String val = billInfo.get(entry.getKey()); if(ObjectUtil.empty(val)){ billInfo.put(entry.getKey(), entry.getValue()); return; } if(NumberUtils.isNumber(val) && NumberUtils.isNumber(entry.getValue())){ billInfo.put(entry.getKey() , toNumber(NumberUtils.createDouble(val) + NumberUtils.createDouble(entry.getValue()))); }else { billInfo.put(entry.getKey(), entry.getValue()); } }); } // public static void main(String... args){ // System.out.println(toNumber(6040.76)); // System.out.println(toNumber(100)); // System.out.println(toNumber(0.100)); // } 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); } }