package org.opencps.statistic.rest.engine.service;

import java.util.List;
import java.util.Map;

import org.opencps.statistic.rest.dto.DossierStatisticData;
import org.opencps.statistic.rest.dto.GetDossierData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatisticEngineFetch {

	private final static Logger LOG = LoggerFactory.getLogger(StatisticEngineFetch.class);

	public void fecthStatisticData(long groupId, Map<String, DossierStatisticData> statisticData,
			List<GetDossierData> lsDossierData, int month) {

		//LOG.info("STARTTING TIME " + LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));

		for (GetDossierData dossierData : lsDossierData) {

			StatisticEngineFetchEntry engineFetchEntry = new StatisticEngineFetchEntry();

			// all site, all domain
			
			String type1 = "all@all@" + groupId;

			DossierStatisticData dataType1 = new DossierStatisticData();

			if (statisticData.containsKey(type1)) {
				dataType1 = statisticData.get(type1);
			}

			engineFetchEntry.updateDossierStatisticData(dataType1, dossierData, month);

			statisticData.put(type1, dataType1);
			

			// all site each domain
			String type2 = "all@" + dossierData.getDomainCode() + "@" + groupId;
			
			DossierStatisticData dataType2 = new DossierStatisticData();


			dataType2.setDomainCode(dossierData.getDomainCode());
			dataType2.setDomainName(dossierData.getDomainName());

			if (statisticData.containsKey(type2)) {
				dataType2 = statisticData.get(type2);
			}

			engineFetchEntry.updateDossierStatisticData(dataType2, dossierData, month);

			statisticData.put(type2, dataType2);

			// each site all domain
			
			String type3 = dossierData.getGovAgencyCode() + "@all@" + groupId;


			DossierStatisticData dataType3 = new DossierStatisticData();
			dataType3.setGovAgencyCode(dossierData.getGovAgencyCode());
			dataType3.setGovAgencyName(dossierData.getGovAgencyName());


			if (statisticData.containsKey(type3)) {
				dataType3 = statisticData.get(type3);
			}

			engineFetchEntry.updateDossierStatisticData(dataType3, dossierData, month);

			statisticData.put(type3, dataType3);

			// each site each domain
			
			String type4 = dossierData.getGovAgencyCode() + "@" + dossierData.getDomainCode() + "@" + groupId;
			
			DossierStatisticData dataType4 = new DossierStatisticData();
			dataType4.setGovAgencyCode(dossierData.getGovAgencyCode());
			dataType4.setGovAgencyName(dossierData.getGovAgencyName());

			dataType4.setDomainCode(dossierData.getDomainCode());
			dataType4.setDomainName(dossierData.getDomainName());
			
			if (statisticData.containsKey(type4)) {
				dataType4 = statisticData.get(type4);
			}

			engineFetchEntry.updateDossierStatisticData(dataType4, dossierData, month);

			statisticData.put(type4, dataType4);
		}

	}

}

@@ -1073,32 +1073,80 @@
 ierData, month);
+%0A%09%09%09dataType1 = processOnTimePercent(dataType1);
 %0A%0A%09%09%09statisticDa
@@ -1621,32 +1621,80 @@
 ierData, month);
+%0A%09%09%09dataType2 = processOnTimePercent(dataType2);
 %0A%0A%09%09%09statisticDa
@@ -2176,32 +2176,80 @@
 ierData, month);
+%0A%09%09%09dataType3 = processOnTimePercent(dataType3);
 %0A%0A%09%09%09statisticDa
@@ -2896,53 +2896,625 @@
 h);%0A
-%0A%09%09%09statisticData.put(type4, dataType4);%0A%09%09%7D%0A
+%09%09%09dataType4 = processOnTimePercent(dataType4);%0A%0A%09%09%09statisticData.put(type4, dataType4);%0A%09%09%7D%0A%09%7D%0A%0A%09private DossierStatisticData processOnTimePercent(DossierStatisticData dataType) %7B%0A%09%09int ontimePercent = 0;%0A%09%09int releaseCount = dataType.getReleaseCount();%0A%09%09//_log.info(%22releaseCount: %22+releaseCount);%0A%09%09%0A%09%09if (releaseCount %3E 0) %7B%0A%09%09%09int betimesCount = dataType.getBetimesCount();%0A%09%09%09int ontimeCount = dataType.getOntimeCount();%0A%09%09%09ontimePercent = (betimesCount + ontimeCount) * 100 / releaseCount;%0A%09%09%09//_log.info(%22ontimePercent: %22+ontimePercent);%0A%09%09%7D%0A%09%09dataType.setOntimePercentage(ontimePercent);%0A%0A%09%09return dataType;
 %0A%09%7D%0A

update statistic