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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.gx.obe.DictValueEnum;
import com.gx.obe.business.vo.RatesCheckResult;
import com.gx.obe.components.core.enumeration.EngineerConstants;
import com.gx.obe.evaluation.step.inter.IChecker;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.web.entity.CheckResult;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.boq.RatesTable;

/**
 * @Description: 费率检查
 * @author guoyr
 */
public class RatesCompeteItemChecker extends BaseChecker implements IChecker {
	
	private List<RatesCheckResult> checkResultTreeList = new ArrayList<RatesCheckResult>();
	private List<RatesCheckResult> checkResultErrorList = new ArrayList<RatesCheckResult>();
	private String checkName = "规费";
	private String rateType = EngineerConstants.RATE_GF;
	
	public void startCheck(ConstructProject constructProject) {
		checkResultTreeList.clear();
		checkResultErrorList.clear();
		checkResult = null;
		// 获得工程项目
		if (null != constructProject) {
			if (DictValueEnum.CleanTender.AQWMSGF_RATE_CHECK.equals(dictionaryValue.getDictValueCode())) {
				rateType = EngineerConstants.RATE_AQWMSGF;
				checkName = "安全文明施工费";
			}
			// 获得单项工程
			List<ConstructProject> singleConstructionList = constructProject.getChildProjectList();
			if (null != singleConstructionList) {
				for (ConstructProject singleConstruction : singleConstructionList) {
					
					RatesCheckResult singleResult = new RatesCheckResult();
					singleResult.setName(singleConstruction.getProjectName());
					singleResult.setPrice(singleConstruction.getFloorage());
					singleResult.setCheckType(EngineerConstants.DXGC);
					singleResult.setCalculateRemark(null != singleConstruction.getFloorage() ? NumberFormatUtils.format(singleConstruction.getFloorage()) + "m2" : "");
					
					checkResultTreeList.add(singleResult);
					// 获得单位工程
					List<ConstructProject> unitConstructionList = singleConstruction.getChildProjectList();
					if (null != unitConstructionList) {
						for (ConstructProject unitConstruction : unitConstructionList) {
							
							long startTime = System.currentTimeMillis(); //获取结束时间		
							LOG.debug("开始获取" + supplier.getSupplierName() + "  " + unitConstruction.getProjectName() + checkName);
							List<RatesTable> billTableTempList = getCacheListener().getSupplierUnitConstructRatesTableList(tenderProject.getId(), supplier.getId(), unitConstruction, rateType);
							LOG.debug(supplier.getSupplierName() + "  " + unitConstruction.getProjectName() + checkName + "获取" + billTableTempList.size() + "条，  用时：\t" + DateUtils.timeSpan((System.currentTimeMillis() - startTime)));
							
							RatesCheckResult unitResult = new RatesCheckResult(getEvaluationStep());
							unitResult.setName(unitConstruction.getProjectName());
							unitResult.setCheckType(EngineerConstants.DWGC);
							// 检查规费
							BigDecimal unitFeesValue = checkFeesAndTaxes(unitResult, unitConstruction, billTableTempList);
//							unitFeesValue = BigDecimalUtils.round(unitFeesValue, 2);
							if (DictValueEnum.CleanTender.AQWMSGF_RATE_CHECK.equals(dictionaryValue.getDictValueCode())) {
								unitResult.setPrice(unitConstruction.getSafeFee());
							} else {
								unitResult.setPrice(unitConstruction.getLawFee());
							}
							unitResult.setCalculate(unitFeesValue);
							singleResult.addChild(unitResult);
							if (unitResult.isReasonable()) {
								unitResult.setOpinion("按规定费率计取");
							} else {
//								unitResult.setOpinion(checkName + "未按规定费率计取");
								unitResult.setOpinion("请校验" + checkName + "费率计取情况");
								checkResultErrorList.add(unitResult);
							}
						}
					}
				}
				
			}
			
		}
	}
	
	/**
	 * Description: 检查规费
	 * Create Date: Jul 3, 20145:41:49 PM<br/>
	 * Author : guoyr <br/>
	 * Modify Date: <br/>
	 * Modify By : <br/>
	 * 
	 * @param itemIndex
	 * @param parentItem
	 * @param projectId
	 * @return
	 */
	private BigDecimal checkFeesAndTaxes(RatesCheckResult parentCheckResult, ConstructProject constructProject, List<RatesTable> ratesTableList) {
		
		BigDecimal unitFeesValue = BigDecimal.ZERO;
		if (null != ratesTableList) {
			for (RatesTable feesTaxes : ratesTableList) {
				RatesCheckResult result = new RatesCheckResult();
				result.setName(feesTaxes.getName());
				result.setRate(feesTaxes.getRate());
				if (null != feesTaxes.getBasePrice()) {
					result.setPrice(feesTaxes.getBasePrice());
					result.setCalculateRemark(feesTaxes.getRemark());
					// 当前项目
					BigDecimal feesValue = BigDecimalUtils.div(BigDecimalUtils.mul(feesTaxes.getBasePrice(), feesTaxes.getRate()), new BigDecimal(100));
					result.setCalculate(feesValue);
					unitFeesValue = BigDecimalUtils.add(unitFeesValue, feesValue);
					
				}
				parentCheckResult.addChild(result);
			}
		}
		return unitFeesValue;
		
	}
	
	public int getErrorCount() {
		return checkResultErrorList.size();
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getCheckResultDetailTreeList() {
		return (List<T>) checkResultTreeList;
	}
	
	@SuppressWarnings("unchecked")
	public <T> List<T> getCheckResultErrorList() {
		return (List<T>) checkResultErrorList;
	}
	
	public CheckResult getCheckResult() {
		if (null != checkResult) return checkResult;
		checkResult = initCheckResult(checkResult);
		if (checkResultErrorList.size() > 0) {
			checkResult.setErrorNum(checkResultErrorList.size());
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
			StringBuilder error = new StringBuilder();
			error.append(checkResultErrorList.size() + "个单位工程下的" + checkName + "未按照规定费率计取");
			checkResult.setCheckResult(error.toString());
		} else {
			checkResult.setErrorNum(0);
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			checkResult.setCheckResult("无错误。");
		}
		
		return checkResult;
	}
	
}
