package com.gx.obe.evaluation.step.utils;

import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.gx.obe.common.file.enumeration.FileConstants;
import com.gx.obe.components.core.vo.BidPriceTable;
import com.gx.obe.util.utils.XmlUtils;

public class ParsingBidPriceUtils {
	
	private static String BID_PRICE_TABLE_DETAIL_INDEX_XML = "BidPriceTableDetailIndex.xml";// 企业资质描述文件
	

	/**
	 * @Description: 解析投标报价汇总表
	 * @author chenxw
	 * @param supplier
	 * @return
	 */
	public static BidPriceTable parsintBidPriceTable(String tenderId, String supplierId) {
		File xmlFile = new File(FileConstants.getBidFilePath(tenderId, supplierId).concat(BID_PRICE_TABLE_DETAIL_INDEX_XML));
		if (!xmlFile.isFile() || !xmlFile.exists()) {
			return null;
		}
		Document document = XmlUtils.getXmlDocument(xmlFile.getAbsolutePath());
		if (null == document) {
			return null;
		}
		List<?> bidPriceTableList = document.getRootElement().elements("bidPriceTable");
		if (null == bidPriceTableList || bidPriceTableList.size() != 1) {
			return null;
		}
		return getBidPriceTable((Element) bidPriceTableList.get(0));
	}
	
	/**
	 * @Description: 获取投标报价表
	 * @author chenxw
	 * @param element
	 * @return
	 */
	private static BidPriceTable getBidPriceTable(Element element) {
		BidPriceTable bidPriceTable = new BidPriceTable();
		bidPriceTable.setSubjectTableID(element.elementTextTrim("subjectTableID"));
		bidPriceTable.setTotalFileName(element.elementTextTrim("totalFileName"));
		bidPriceTable.setDetailFileName(element.elementTextTrim("detailFileName"));
		Element subBidPriceTableListElement = element.element("subBidPriceTableList");
		if (null != subBidPriceTableListElement) {
			List<?> bidPriceTableList = subBidPriceTableListElement.elements("bidPriceTable");
			if (null != bidPriceTableList && bidPriceTableList.size() > 0) {
				for (Object object : bidPriceTableList) {
					bidPriceTable.addBidPriceTable(getBidPriceTable((Element) object));
				}
			}
		}
		return bidPriceTable;
	}
}
