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

import java.text.MessageFormat;
import java.util.Date;
import java.util.List;

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

import com.gx.obe.WebserviceConstants;
import com.gx.obe.business.entity.BidPriceRound;
import com.gx.obe.business.http.HttpUrl;
import com.gx.obe.business.http.HttpUtils;
import com.gx.obe.common.widget.listener.ProgressMessageListener;
import com.gx.obe.common.widget.thread.MyThread;
import com.gx.obe.components.core.enumeration.BidPriceEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.XmlUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.price.BidPrice;
import com.gx.obe.web.entity.price.BidPriceResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.entity.tender.TenderProjectRuleEntity;
import com.gx.obe.web.service.BidPriceResultService;
import com.gx.obe.web.service.BidPriceService;
import com.gx.obe.web.service.ReBidPriceRoundService;
import com.gx.utils.ActionLogUtils;

/**
 * @Description: 申请再次报价
 * @author wangxaing
 */
public class ApplyReBidPriceThread extends MyThread {

	private ProgressMessageListener callBack;
	private BidPriceService bidPriceService;
	private BidPriceResultService bidPriceResultService ;
	private TenderProjectEntity tenderProjectEntity;
	private List<Supplier> supplierlist;
	private BidPriceRound bidPriceRound;
	private TenderProjectRuleEntity tenderProjectRuleEntity;
	private List<Supplier> evaluationStepSupplierList;

	public ApplyReBidPriceThread(TenderProjectEntity tenderProjectEntity, List<Supplier> supplierlist, List<Supplier> evaluationStepSupplierList,BidPriceRound bidPriceRound,TenderProjectRuleEntity tenderProjectRuleEntity, ProgressMessageListener callBack) {
		this.tenderProjectEntity = tenderProjectEntity;
		this.callBack = callBack;
		this.supplierlist = supplierlist;
		this.evaluationStepSupplierList = evaluationStepSupplierList;
		this.bidPriceRound = bidPriceRound;
		this.tenderProjectRuleEntity = tenderProjectRuleEntity;
		bidPriceResultService = new BidPriceResultService();
		bidPriceService = new BidPriceService();
	}

	public void run() {
		if(null != callBack) {
			callBack.message("正在申请再次报价...", 0);
		}
		boolean downloadFlag = appReBidPrice();
		if(null != thread) {
			if (downloadFlag) {
				ReBidPriceRoundService reBidPriceRoundService = new ReBidPriceRoundService();
				reBidPriceRoundService.insertNewBidpriceRound(bidPriceRound, tenderProjectEntity.getId(), supplierlist,evaluationStepSupplierList);
				if(null != callBack) {
					callBack.finish(true);
				}
			} else {
				if(null != callBack) {
					callBack.message(MessageFormat.format(Messages.Tips_startFailed, bidPriceRound.getRoundName()), -1);
					callBack.finish(false);
				}
			}
		}
	}

	private boolean appReBidPrice() {
		
		HttpUrl httpUrl = HttpUtils.getProjectHttpUrl(tenderProjectEntity, PropertiesUtils.getWebserviceProperty(WebserviceConstants.APPLYREBIDPRICE, "secondBidPriceHS.do?applySecondBidPrice"));

		String offerTitle = bidPriceRound.getRoundName();//再次报价标题
		String offerContent = bidPriceRound.getRoundMemo();//说明
		Date offerEndDateTime = bidPriceRound.getRoundEndTime();//截止时间
//		int offerTimes = bidPriceRound.getRoundNo();//第几次再次报价
		int offerTimes = bidPriceRound.getRoundNo() - 1;//第几次再次报价(-1是因为投标平台或者招标管家那面轮次是从0开始存在显示问题)
		StringBuilder orgCodes = new StringBuilder();
		for(int i = 0; i < supplierlist.size(); i++){
			orgCodes.append(supplierlist.get(i).getOrgCode());
			if(i < supplierlist.size()-1){
				orgCodes.append(",");	
			}
		}
		
		String bidPriceItemXML  = getBidPriceXmlToString();
		if(null == tenderProjectEntity.getParentId()){
			String packCode = tenderProjectEntity.getTenderNo();
			httpUrl.setAttribute("packCode", packCode);
		}
		httpUrl.setAttribute("stageIds", tenderProjectEntity.getStageId());
		httpUrl.setAttribute("offerTitle", offerTitle);
		httpUrl.setAttribute("offerContent", offerContent);
		httpUrl.setAttribute("offerEndDateTime", DateUtils.getTimestampFormat(offerEndDateTime));
		httpUrl.setAttribute("offerTimes", offerTimes+"");
		httpUrl.setAttribute("orgCodes", orgCodes.toString());
		httpUrl.setAttribute("relationId", null);
		httpUrl.setAttribute("bidPriceItemXML", bidPriceItemXML);
		httpUrl.setName("申请" + bidPriceRound.getRoundName());
		boolean flag = HttpUtils.doPost(httpUrl).getStatus();
		ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "申请" + bidPriceRound.getRoundName(), httpUrl, flag);
		return flag;
	}

	private String getBidPriceXmlToString() {
		Document document = DocumentHelper.createDocument();
		Element rootElement = document.addElement("bidPriceList");
		queryBidPrice(rootElement);
		return document.asXML();
	}
	
	/**
	 * @Description: 查询报价列表
	 * @author guoyr
	 * @param rootElement 
	 */
	private void queryBidPrice(Element rootElement){
		List<BidPrice> bidPriceList = bidPriceService.getTenderBidPriceTableList(tenderProjectEntity.getId());
		if(null != bidPriceList && bidPriceList.size() > 0){
			setBidPriceItemByList(rootElement, bidPriceList);
		}
	}
//	
		/**
		 * @Description: 组装bidPrice.xml节点信息
		 * @author guoyr
		 * @param evaluationStepElement
		 * @param bidPriceList 
		 */
		private void setBidPriceItemByList(Element evaluationStepElement, List<BidPrice> bidPriceList){
			List<BidPriceResult> bidPriceResultList = bidPriceResultService.getBidPriceResultList(tenderProjectEntity.getId());
			Element bidPriceElement = evaluationStepElement.addElement("bidPrice");
			for(BidPrice bidPrice : bidPriceList){
				Element priceItemElement = null;
				if (BidPriceEnum.IS_PRICE_ITEM.equals(bidPrice.getBidPriceType())) {
					priceItemElement = bidPriceElement.addElement("priceItem");
					priceItemElement.addElement("unit").setText(XmlUtils.getXmlText(bidPrice.getPriceUnitCN(tenderProjectRuleEntity.getPriceUnitUseDefault())));
					priceItemElement.addElement("unitType").setText(XmlUtils.getXmlText(CommonEnum.getPriceUnitType(tenderProjectRuleEntity.getPriceUnitUseDefault())));
					priceItemElement.addElement("currencyCode").setText(XmlUtils.getXmlText(bidPriceResultList.size() > 0 ? bidPriceResultList.get(0).getCurrencyCode() : BidPriceEnum.CUR_CODE));
					priceItemElement.addElement("currencyName").setText(XmlUtils.getXmlText(""));
				}else {
					priceItemElement = bidPriceElement.addElement("item");
					priceItemElement.addElement("unit").setText("");
					priceItemElement.addElement("unitType").setText("");
					priceItemElement.addElement("currencyCode").setText("");
					priceItemElement.addElement("currencyName").setText("");
				}
				priceItemElement.addElement("id").setText(XmlUtils.getXmlText(bidPrice.getId()));
				priceItemElement.addElement("name").setText(XmlUtils.getXmlText(bidPrice.getBidPriceName()));
			}
		}
}
