Java tutorial
/* * Copyright 2015-2102 RonCoo(http://www.appleframework.com) Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.appleframework.pay.app.reconciliation.parser; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.stereotype.Component; import com.appleframework.pay.app.reconciliation.vo.AlipayAccountLogVO; import com.appleframework.pay.common.core.enums.PayWayEnum; import com.appleframework.pay.reconciliation.entity.RpAccountCheckBatch; import com.appleframework.pay.reconciliation.enums.BatchStatusEnum; import com.appleframework.pay.reconciliation.vo.ReconciliationEntityVo; /** * ??? . * * http://www.appleframework.com * * @author Cruise.Xu */ @Component("ALIPAYParser") public class ALIPAYParser implements ParserInterface { private static final Log LOG = LogFactory.getLog(ALIPAYParser.class); private static final String DATE_FORMAT_STYLE = "yyyy-MM-dd HH:mm:ss"; /** * ???? * * @param file * ?? * @param billDate * ? * @param batch * * @return */ @SuppressWarnings({ "rawtypes", "unchecked" }) public List<ReconciliationEntityVo> parser(File file, Date billDate, RpAccountCheckBatch batch) throws IOException { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_STYLE); // xml?file SAXReader reader = new SAXReader(); Document document; try { document = reader.read(file); // dom4jXpathAccountQueryAccountLogVO List projects = document.selectNodes( "alipay/response/account_page_query_result/account_log_list/AccountQueryAccountLogVO"); Iterator it = projects.iterator(); // ? List<AlipayAccountLogVO> tradeList = new ArrayList<AlipayAccountLogVO>(); // ? List<AlipayAccountLogVO> otherAll = new ArrayList<AlipayAccountLogVO>(); while (it.hasNext()) { AlipayAccountLogVO vo = new AlipayAccountLogVO(); Element elm = (Element) it.next(); List<Element> childElements = elm.elements(); for (Element child : childElements) { String name = child.getName(); // switch (name) { case "balance": vo.setBalance(new BigDecimal(child.getText())); break; case "rate": vo.setBankRate(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "buyer_account": vo.setBuyerAccount(child.getText()); break; case "goods_title": vo.setGoodsTitle(child.getText()); break; case "income": vo.setIncome(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "outcome": vo.setOutcome(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "merchant_out_order_no": vo.setMerchantOrderNo(child.getText()); break; case "total_fee": vo.setTotalFee(new BigDecimal(("").equals(child.getText()) ? "0" : child.getText())); break; case "trade_no":// ? vo.setTradeNo(child.getText()); break; case "trans_code_msg":// vo.setTransType(child.getText()); break; case "trans_date": String dateStr = child.getText(); Date date; try { date = sdf.parse(dateStr); } catch (ParseException e) { date = billDate; } vo.setTransDate(date); break; default: break; } } // ?? if ("".equals(vo.getTransType())) { tradeList.add(vo); } else { otherAll.add(vo); } } // ????????? // ????????? for (AlipayAccountLogVO trade : tradeList) { String tradeNo = trade.getTradeNo(); for (AlipayAccountLogVO other : otherAll) { String otherTradeNo = other.getTradeNo(); if (tradeNo.equals(otherTradeNo)) { trade.setBankFee(other.getOutcome()); } } } // AlipayAccountLogVOvoReconciliationEntityVo?list List<ReconciliationEntityVo> list = new ArrayList<ReconciliationEntityVo>(); // ??? int totalCount = 0; BigDecimal totalAmount = BigDecimal.ZERO; BigDecimal totalFee = BigDecimal.ZERO; for (AlipayAccountLogVO trade : tradeList) { // totalCount++; totalAmount = totalAmount.add(trade.getTotalFee()); totalFee = totalFee.add(trade.getBankFee()); // AlipayAccountLogVOReconciliationEntityVo ReconciliationEntityVo vo = new ReconciliationEntityVo(); vo.setAccountCheckBatchNo(batch.getBatchNo()); vo.setBankAmount(trade.getTotalFee()); vo.setBankFee(trade.getBankFee()); vo.setBankOrderNo(trade.getMerchantOrderNo()); vo.setBankRefundAmount(BigDecimal.ZERO); vo.setBankTradeStatus("SUCCESS"); vo.setBankTradeTime(trade.getTransDate()); vo.setBankTrxNo(trade.getTradeNo()); vo.setBankType(PayWayEnum.ALIPAY.name()); vo.setOrderTime(trade.getTransDate()); list.add(vo); } batch.setBankTradeCount(totalCount); batch.setBankTradeAmount(totalAmount); batch.setBankRefundAmount(BigDecimal.ZERO); batch.setBankFee(totalFee); return list; } catch (DocumentException e) { LOG.warn("?", e); batch.setStatus(BatchStatusEnum.FAIL.name()); batch.setCheckFailMsg("?, payway[" + PayWayEnum.ALIPAY.name() + "], billdata[" + sdf.format(billDate) + "]"); return null; } } }