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.TaxFeesCheckResult;
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.evaluation.EvaluationStep;

/**
 * @Description: 税金费率检查
 * @author guoyr
 */
public class TaxFeesChecker extends BaseChecker implements IChecker {
	
//	private ConstructProjectService constructProjectService = ServiceFactory.getInstance("constructProjectService", ConstructProjectService.class);
	private List<TaxFeesCheckResult> checkResultTreeList = new ArrayList<TaxFeesCheckResult>();
	private List<TaxFeesCheckResult> checkResultErrorList = new ArrayList<TaxFeesCheckResult>();
	public static final BigDecimal ERROR_RATE = BigDecimal.valueOf(0.5);
	// 默认消项费率
	public static final BigDecimal OUTPUTTAX_RATE = BigDecimal.valueOf(9);
	public static final BigDecimal ERROR_PRICE = BigDecimal.valueOf(20);
	
	public void startCheck(ConstructProject constructProject) {
		BigDecimal ERROR_PRICE = evaluationStep.getEvaluationParamByKey(EvaluationStep.CALCULATION_ERROR_VALUE, BigDecimal.class);
		if (null == ERROR_PRICE || ERROR_PRICE.compareTo(BigDecimal.ZERO) == 0) {
			ERROR_PRICE = BigDecimal.valueOf(20);
		}
		checkResultTreeList.clear();
		checkResultErrorList.clear();
		checkResult = null;
		// 获得工程项目
		if (null != constructProject) {
			if (DictValueEnum.CleanTender.AQWMSGF_RATE_CHECK.equals(dictionaryValue.getDictValueCode())) {}
			// 获得单项工程
			List<ConstructProject> singleConstructionList = constructProject.getChildProjectList();
			if (null != singleConstructionList) {
				for (ConstructProject singleConstruction : singleConstructionList) {
					
					TaxFeesCheckResult singleResult = new TaxFeesCheckResult();
					singleResult.setName(singleConstruction.getProjectName());
					checkResultTreeList.add(singleResult);
					// 获得单位工程
					List<ConstructProject> unitConstructionList = singleConstruction.getChildProjectList();
					if (null != unitConstructionList) {
						for (ConstructProject unitConstruction : unitConstructionList) {
							
//							long startTime=System.currentTimeMillis(); //获取结束时间		
//							System.out.println("开始获取"+supplier.getSupplierName() + "  "+unitConstruction.getProjectName() + checkName);
//							List<RatesTable> billTableTempList = getCacheListener().getSupplierUnitConstructRatesTableList(tenderProject.getId(), supplier.getId(), unitConstruction, rateType);
//							System.out.println(supplier.getSupplierName() + "  "+unitConstruction.getProjectName()+ checkName +"获取" + billTableTempList.size()+"条，  用时：\t"+ DateUtils.timeSpan((System.currentTimeMillis()-startTime)));
						
//							unitConstruction.setPreTaxCost(BigDecimal.valueOf(1820943.09));
//							unitConstruction.setInputTax(BigDecimal.valueOf(146733.85));
//							unitConstruction.setOutputTax(BigDecimal.valueOf(184163.02));
//							unitConstruction.setValueAddedTax(BigDecimal.valueOf(37429.17));
//							unitConstruction.setSurchargeTax(BigDecimal.valueOf(4203.3));
//							unitConstruction.setSurchargeTaxRate(BigDecimal.valueOf(11));
//							unitConstruction.setTaxes(BigDecimal.valueOf(41632.47));
							
							TaxFeesCheckResult unitResult = new TaxFeesCheckResult();
							unitResult.setName(unitConstruction.getProjectName());
							// 检查规费
							unitResult.setPreTaxCost(unitConstruction.getPreTaxCost());
							unitResult.setInputTax(unitConstruction.getInputTax());
							unitResult.setOutputTax(unitConstruction.getOutputTax());
							BigDecimal outputTaxRate = BigDecimalUtils.mul(BigDecimalUtils.div(unitConstruction.getOutputTax(), BigDecimalUtils.sub(unitConstruction.getPreTaxCost(), unitConstruction.getInputTax())), BigDecimal.valueOf(100));
//							outputTaxRate = BigDecimalUtils.round(outputTaxRate, 2);
							unitResult.setOutputTaxRate(outputTaxRate);
							boolean reasonable = true;
							//销项税额费率应为固定值9%
							if (null != outputTaxRate && outputTaxRate.compareTo(BigDecimal.ZERO) > 0) {
								if (BigDecimalUtils.difference(outputTaxRate, TaxFeesChecker.OUTPUTTAX_RATE).compareTo(TaxFeesChecker.ERROR_RATE) > 0) {
									reasonable = false;
									unitResult.setOpinion("销项税额费率未按9%计取");
								}
							}
							
							// 增值税应纳税额 = 销项税额-进项税额   
							unitResult.setValueAddedTax(unitConstruction.getValueAddedTax());
							BigDecimal surchargeTax = BigDecimalUtils.sub(unitConstruction.getOutputTax(), unitConstruction.getInputTax());
							if (null == surchargeTax || surchargeTax.compareTo(BigDecimal.ZERO) < 0) {
								surchargeTax = BigDecimal.ZERO;
							}
							
							unitResult.setSurchargeTaxRate(unitConstruction.getSurchargeTaxRate());//附加税费费率
							// 附加税费 = 增值税应纳税额*附加税费费率  ， 其中：增值税应纳税额 = 销项税额-进项税额   
							unitResult.setSurchargeTax(unitConstruction.getSurchargeTax());
							BigDecimal calculateSurchargeTax = unitConstruction.getSurchargeTax();
							if (null != unitConstruction.getSurchargeTaxRate()) {
								calculateSurchargeTax = BigDecimalUtils.div(BigDecimalUtils.mul(surchargeTax, unitConstruction.getSurchargeTaxRate()), BigDecimal.valueOf(100));
//								calculateSurchargeTax = BigDecimalUtils.round(calculateSurchargeTax, 2);
								unitResult.setCalculateSurchargeTax(calculateSurchargeTax);
							}
							
							unitResult.setTaxes(unitConstruction.getTaxes());
							// 税金 = 增值税应纳税额+附加税费
							BigDecimal calculateTaxes = BigDecimalUtils.add(surchargeTax, calculateSurchargeTax);
//							calculateTaxes = BigDecimalUtils.round(calculateTaxes, 2);
							if (BigDecimalUtils.difference(calculateTaxes, unitConstruction.getTaxes()).compareTo(TaxFeesChecker.ERROR_PRICE) > 0) {
								reasonable = false;
								if (null != unitResult.getOpinion()) {
									unitResult.setOpinion(unitResult.getOpinion() + ";附加税费未按规定费率计取");
								} else {
									
									unitResult.setOpinion("附加税费未按规定费率计取");
								}
							}
							unitResult.setCalculateTaxes(calculateTaxes);
							unitResult.setReasonable(reasonable);
							
							singleResult.addChild(unitResult);
							if (!reasonable) {
								checkResultErrorList.add(unitResult);
							}
						}
					}
				}
				
			}
			
		}
	}
	
	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() + "个单位工程下的税金费率未按照规定费率计取");
			checkResult.setCheckResult(error.toString());
		} else {
			checkResult.setErrorNum(0);
			checkResult.setAccessment(com.gx.obe.components.core.enumeration.EvaluationStepEnum.QUALIFIED);
			checkResult.setCheckResult("无错误。");
		}
		
		return checkResult;
	}
	
}
