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

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

import com.gx.obe.business.vo.MaterialPriceCheckResult;
import com.gx.obe.evaluation.step.inter.IChecker;
import com.gx.obe.util.utils.BigDecimalUtils;
import com.gx.obe.web.entity.CheckResult;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.boq.MaterialSummary;
import com.gx.obe.web.service.boq.MaterialSummaryService;

/**
 * @Description: 材料报价检查
 * @author guoyr
 */
public class MaterialPriceItemChecker extends BaseChecker implements IChecker {
	
	private MaterialSummaryService materialSummaryService = new MaterialSummaryService();
	private List<MaterialPriceCheckResult> checkResultTreeList = new ArrayList<MaterialPriceCheckResult>();
	private List<MaterialPriceCheckResult> checkResultErrorList = new ArrayList<MaterialPriceCheckResult>();
	
	public void startCheck(ConstructProject constructProject) {
		checkResultTreeList.clear();
		checkResultErrorList.clear();
		if (null != constructProject) {
			
			// 获得单项工程
			List<ConstructProject> singleConstructionList = constructProject.getChildProjectList();
			if (null != singleConstructionList) {
				for (ConstructProject singleConstruction : singleConstructionList) {
					// 获得单位工程
					List<ConstructProject> unitConstructionList = singleConstruction.getChildProjectList();
					if (null != unitConstructionList) {
						for (ConstructProject unitConstruction : unitConstructionList) {
							
							MaterialPriceCheckResult unitResult = new MaterialPriceCheckResult(getEvaluationStep());
							unitResult.setName(singleConstruction.getProjectName() + "-" + unitConstruction.getProjectName());
							checkResultTreeList.add(unitResult);
							
							// 检查材料报价
							checkMaterialSummary(unitResult, unitConstruction);
						}
					}
				}
			}
		}
	}
	
	/**
	 * 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 void checkMaterialSummary(MaterialPriceCheckResult parentCheckResult, ConstructProject constructProject) {
		
		List<MaterialSummary> topBillTableList = materialSummaryService.getMaterialSummaryAndChildList(constructProject.getId());
		for (MaterialSummary topMaterial : topBillTableList) {
			List<MaterialSummary> billTableList = topMaterial.getChildMaterialList();
			if (null != billTableList && billTableList.size() > 0) {
				
				for (MaterialSummary qd : billTableList) {
					
					List<MaterialSummary> deList = qd.getChildMaterialList();
					if (null != deList && deList.size() > 0) {
						BigDecimal computerprice = BigDecimal.ZERO;
						for (MaterialSummary subMaterial : deList) {
							BigDecimal childPrice = BigDecimalUtils.mul(subMaterial.getPrice(), subMaterial.getRate());
//							childPrice = BigDecimalUtil.div(childPrice, BigDecimal.valueOf(100));
							computerprice = BigDecimalUtils.add(computerprice, childPrice);
						}
						
						MaterialPriceCheckResult qdResult = new MaterialPriceCheckResult(getEvaluationStep());
						qdResult.setCode(qd.getMaterialCode());
						qdResult.setName(qd.getMaterialName());
						qdResult.setParent(parentCheckResult);
						qdResult.setQuantity(qd.getQuantity());
						qdResult.setPrice(qd.getPrice());
						qdResult.setComparePrice(computerprice);
						qdResult.setUnit(qd.getUnit());
						
						if (qdResult.getFlag() != 0) {
							checkResultErrorList.add(qdResult);
						}
						parentCheckResult.addChild(qdResult);
					}
					
				}
			}
		}
	}
	
	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) {
			int maxCount = 0;
			int minCount = 0;
			for (MaterialPriceCheckResult result : checkResultErrorList) {
				if (result.getFlag() > 0) {
					maxCount++;
				} else if (result.getFlag() < 0) {
					minCount++;
				}
			}
			StringBuilder error = new StringBuilder();
			if (maxCount > 0) {
				
				error.append(maxCount);
				error.append("项材料的单价过高");
			} else if (minCount > 0) {
				if (error.length() > 0) {
					error.append("；");
				}
				error.append(maxCount);
				error.append("项材料的单价过低");
			}
			error.append("。");
			checkResult.setCheckResult(error.toString());
			checkResult.setErrorNum(checkResultErrorList.size());
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.UNQUALIFIED);
		} else {
			checkResult.setErrorNum(0);
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			checkResult.setCheckResult("无错误。");
		}
		
		return checkResult;
	}
	
}
