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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gx.obe.components.core.enumeration.EngineerConstants;
import com.gx.obe.evaluation.step.inter.BillCheckerCache;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.web.entity.boq.BillTable;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.boq.ExtraTable;
import com.gx.obe.web.entity.boq.MeasureTable;
import com.gx.obe.web.entity.boq.RatesTable;
import com.gx.obe.web.service.boq.BillTableService;
import com.gx.obe.web.service.boq.ConstructProjectService;
import com.gx.obe.web.service.boq.ExtraTableService;
import com.gx.obe.web.service.boq.MeasureTableService;
import com.gx.obe.web.service.boq.RatesTableService;

public class BillCheckerCacheUtils {
	
	private static ConstructProjectService constructProjectService = new ConstructProjectService();
	private static BillTableService billTableService = new BillTableService();
	private static MeasureTableService measureTableService = new MeasureTableService();
	private static RatesTableService ratesTableService = new RatesTableService();
	private static ExtraTableService extraTableService = new ExtraTableService();
	
//	private SupplierService supplierService = ServiceFactory.getInstance("supplierService", SupplierService.class);
//	private CheckResultService checkResultService = ServiceFactory.getInstance("checkResultService", CheckResultService.class);
	
	public static BillCheckerCache createBillCheckerCache() {
		BillCheckerCache cacheListener = new BillCheckerCache() {
			ConstructProject constructProject = null;
			Map<String, ConstructProject> supplierConstructProjectMap = new HashMap<String, ConstructProject>();
			Map<String, List<BillTable>> billTableMap = new HashMap<String, List<BillTable>>();
			Map<String, List<MeasureTable>> measureTableMap = new HashMap<String, List<MeasureTable>>();
			Map<String, List<RatesTable>> ratesTableeMap = new HashMap<String, List<RatesTable>>();
			
			Map<String, List<ExtraTable>> extraTableMap = new HashMap<String, List<ExtraTable>>();
			
			public void clearCache() {
				supplierConstructProjectMap.clear();
				billTableMap.clear();
				measureTableMap.clear();
				ratesTableeMap.clear();
			}
			
			public synchronized ConstructProject getConstructProject(String tenderId) {
				if (null == constructProject) {
					constructProject = constructProjectService.getAllConstructProject(tenderId, null);
				}
				return constructProject;
			}
			
			public synchronized ConstructProject getSupplierConstructProject(String tenderId, String supplierId) {
				
				ConstructProject constructProject = supplierConstructProjectMap.get(supplierId);
				if (null == constructProject) {
					constructProject = constructProjectService.getAllConstructProject(tenderId, supplierId);
					supplierConstructProjectMap.put(supplierId, constructProject);
				}
				return constructProject;
			}
			
			public synchronized List<BillTable> getProjectUnitConstructBillTableList(String tenderId, ConstructProject unitConstructProject) {
				List<BillTable> billTableList = null;
				billTableList = billTableMap.get(tenderId.concat(unitConstructProject.getProjectCode()));
				if (null == billTableList) {
					// 一次性只取当前单位工程下的清单
					System.out.println("一次性获取" + unitConstructProject.getProjectName() + "所有单位工程下的分部分项清单");
					long startTime = System.currentTimeMillis(); //获取结束时间	
					billTableList = billTableService.getTenderProjectBillTableQdList(tenderId, unitConstructProject.getProjectCode());
					if (null == billTableList) {
						billTableList = new ArrayList<BillTable>();
					}
					System.out.println("一次性获取" + unitConstructProject.getProjectName() + "所有单位工程下的分部分项清单   " + billTableList.size() + "  用时：\t" + DateUtils.timeSpan((System.currentTimeMillis() - startTime)));
					billTableMap.put(tenderId.concat(unitConstructProject.getProjectCode()), billTableList);
				}
				return billTableList;
			}
			
			public synchronized List<BillTable> getSupplierUnitConstructBillTableList(String tenderId, String supplierId, ConstructProject nuitConstructProject) {
				List<BillTable> billTableList = null;
				billTableList = billTableMap.get(tenderId.concat(supplierId).concat(nuitConstructProject.getId()));
				if (null == billTableList) {
					List<BillTable> allBillTableList = billTableMap.get(tenderId.concat(supplierId));
					if (null == allBillTableList) {
						// 一次性将供应商所有单位工程下的清单都拿到，进行组装
						System.out.println("一次性获取供应商" + supplierId + "所有单位工程下的分部分项清单");
						long startTime = System.currentTimeMillis(); //获取结束时间	
						allBillTableList = billTableService.getAllBillTableQDList(tenderId, supplierId);
						if (null == allBillTableList) {
							allBillTableList = new ArrayList<BillTable>();
						}
						billTableMap.put(tenderId.concat(supplierId), allBillTableList);
						System.out.println("一次性获取供应商" + supplierId + "所有单位工程下的分部分项清单   " + allBillTableList.size() + "  用时：\t" + DateUtils.timeSpan((System.currentTimeMillis() - startTime)));
						for (BillTable billTable : allBillTableList) {
							List<BillTable> tempList = billTableMap.get(tenderId.concat(supplierId).concat(billTable.getProjectId()));
							if (tempList == null) {
								tempList = new ArrayList<BillTable>();
								billTableMap.put(tenderId.concat(supplierId).concat(billTable.getProjectId()), tempList);
								tempList.add(billTable);
							} else {
								tempList.add(billTable);
							}
						}
					}
					billTableList = billTableMap.get(tenderId.concat(supplierId).concat(nuitConstructProject.getId()));
					
				}
				if (null == billTableList) {
					billTableList = new ArrayList<BillTable>();
				}
				return billTableList;
			}
			
			public synchronized List<MeasureTable> getProjectUnitConstructMeasureTableList(String tenderId, ConstructProject unitConstructProject) {
				List<MeasureTable> billTableList = null;
				billTableList = measureTableMap.get(tenderId.concat(unitConstructProject.getProjectCode()));
				if (null == billTableList) {
					// 一次性只取当前单位工程下的清单
					
					System.out.println("一次性获取" + unitConstructProject.getProjectName() + "所有单位工程下的分措施清单");
					long startTime = System.currentTimeMillis(); //获取结束时间	
					billTableList = measureTableService.getTenderProjectMeasureTableQdList(tenderId, unitConstructProject.getProjectCode(), EngineerConstants.CSXM_DJ);
					if (null == billTableList) {
						billTableList = new ArrayList<MeasureTable>();
					}
					System.out.println("一次性获取" + unitConstructProject.getProjectName() + "所有单位工程下的措施清单   " + billTableList.size() + "  用时：\t" + DateUtils.timeSpan((System.currentTimeMillis() - startTime)));
					
					measureTableMap.put(tenderId.concat(unitConstructProject.getProjectCode()), billTableList);
					
				}
				return billTableList;
			}
			
			public synchronized List<MeasureTable> getSupplierUnitConstructMeasureTableList(String tenderId, String supplierId, ConstructProject unitConstructProject) {
				List<MeasureTable> billTableList = null;
				billTableList = measureTableMap.get(tenderId.concat(supplierId).concat(unitConstructProject.getId()));
				if (null == billTableList) {
					
					List<MeasureTable> allBillTableList = measureTableMap.get(tenderId.concat(supplierId.concat(EngineerConstants.CSXM_DJ)));
					if (null == allBillTableList) {
						// 一次性将供应商所有单位工程下的清单都拿到，进行组装
						System.out.println("一次性获取供应商" + supplierId + "所有单位工程下的措施清单");
						long startTime = System.currentTimeMillis(); //获取结束时间		
						allBillTableList = measureTableService.getAllMeasureTableQDList(tenderId, supplierId, EngineerConstants.CSXM_DJ);
						if (null == allBillTableList) {
							allBillTableList = new ArrayList<MeasureTable>();
						}
						measureTableMap.put(tenderId.concat(supplierId.concat(EngineerConstants.CSXM_DJ)), allBillTableList);
						System.out.println("一次性获取供应商" + supplierId + "所有单位工程下的措施清单   " + allBillTableList.size() + "  用时：\t" + DateUtils.timeSpan((System.currentTimeMillis() - startTime)));
						for (MeasureTable billTable : allBillTableList) {
							List<MeasureTable> tempList = measureTableMap.get(tenderId.concat(supplierId).concat(billTable.getProjectId()));
							if (tempList == null) {
								tempList = new ArrayList<MeasureTable>();
								measureTableMap.put(tenderId.concat(supplierId).concat(billTable.getProjectId()), tempList);
								tempList.add(billTable);
							} else {
								tempList.add(billTable);
							}
						}
					}
					billTableList = measureTableMap.get(tenderId.concat(supplierId).concat(unitConstructProject.getId()));
					
				}
				if (null == billTableList) {
					billTableList = new ArrayList<MeasureTable>();
				}
				return billTableList;
			}
			
			public synchronized List<RatesTable> getSupplierUnitConstructRatesTableList(String tenderId, String supplierId, ConstructProject unitConstructProject, String rateType) {
				List<RatesTable> billTableList = null;
				billTableList = ratesTableeMap.get(tenderId.concat(supplierId).concat(unitConstructProject.getId()).concat(rateType));
				if (null == billTableList) {
					List<RatesTable> allBillTableList = ratesTableeMap.get(tenderId.concat(supplierId).concat(rateType));
					if (null == allBillTableList) {
						// 一次性将供应商所有单位工程下的清单都拿到，进行组装
						System.out.println("一次性获取供应商" + supplierId + "所有单位工程下的费率" + rateType);
						long startTime = System.currentTimeMillis(); //获取结束时间		
						allBillTableList = ratesTableService.getRatesTableList(tenderId, supplierId, null, rateType);
						if (null == allBillTableList) {
							allBillTableList = new ArrayList<RatesTable>();
						}
						ratesTableeMap.put(tenderId.concat(supplierId).concat(rateType), allBillTableList);
						System.out.println("一次性获取供应商" + supplierId + "所有单位工程下的费率   " + rateType + "      " + allBillTableList.size() + "  用时：\t" + DateUtils.timeSpan((System.currentTimeMillis() - startTime)));
						for (RatesTable billTable : allBillTableList) {
							List<RatesTable> tempList = ratesTableeMap.get(tenderId.concat(supplierId).concat(billTable.getProjectId()).concat(rateType));
							if (tempList == null) {
								tempList = new ArrayList<RatesTable>();
								ratesTableeMap.put(tenderId.concat(supplierId).concat(billTable.getProjectId()).concat(rateType), tempList);
								tempList.add(billTable);
							} else {
								tempList.add(billTable);
							}
						}
					}
					billTableList = ratesTableeMap.get(tenderId.concat(supplierId).concat(unitConstructProject.getId()).concat(rateType));
					
				}
				if (null == billTableList) {
					billTableList = new ArrayList<RatesTable>();
				}
				return billTableList;
			}
			
			public synchronized List<ExtraTable> getProjectUnitConstructExtraTableList(String tenderId, ConstructProject unitConstructProject) {
				List<ExtraTable> extraTableList = null;
				extraTableList = extraTableMap.get(tenderId.concat(unitConstructProject.getProjectCode()));
				if (null == extraTableList) {
					// 一次性只取当前单位工程下的其他项目清单
					extraTableList = extraTableService.getTenderProjectExtraTableQdList(tenderId, unitConstructProject.getProjectCode());
					if (null == extraTableList) {
						extraTableList = new ArrayList<ExtraTable>();
					}
					extraTableMap.put(tenderId.concat(unitConstructProject.getProjectCode()), extraTableList);
				}
				return extraTableList;
			}
			
			public synchronized List<ExtraTable> getSupplierUnitConstructExtraTableList(String tenderId, String supplierId, ConstructProject unitConstructProject) {
				List<ExtraTable> extraTableList = null;
				extraTableList = extraTableMap.get(tenderId.concat(supplierId).concat(unitConstructProject.getId()));
				if (null == extraTableList) {
					List<ExtraTable> allBillTableList = extraTableMap.get(tenderId.concat(supplierId));
					if (null == allBillTableList) {
						// 一次性将供应商所有单位工程下的其他项目清单
						allBillTableList = extraTableService.getAllExtraTableQDList(tenderId, supplierId);
						if (null == allBillTableList) {
							allBillTableList = new ArrayList<ExtraTable>();
						}
						extraTableMap.put(tenderId.concat(supplierId), allBillTableList);
						for (ExtraTable billTable : allBillTableList) {
							List<ExtraTable> tempList = extraTableMap.get(tenderId.concat(supplierId).concat(billTable.getProjectId()));
							if (tempList == null) {
								tempList = new ArrayList<ExtraTable>();
								extraTableMap.put(tenderId.concat(supplierId).concat(billTable.getProjectId()), tempList);
								tempList.add(billTable);
							} else {
								tempList.add(billTable);
							}
						}
					}
					extraTableList = extraTableMap.get(tenderId.concat(supplierId).concat(unitConstructProject.getId()));
					
				}
				if (null == extraTableList) {
					extraTableList = new ArrayList<ExtraTable>();
				}
				return extraTableList;
			}
			
		};
		return cacheListener;
	}
}
