package com.ps.visualization.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ps.base.dao.BaseDao;
import com.ps.base.service.impl.BaseServiceImpl;
import com.ps.visualization.dao.CountryProgressDao;
import com.ps.visualization.service.CountryProgressService;
import com.ps.visualization.vo.CountryProgress;

@Repository(value = "countryProgressService")
public class CountryProgressServiceImpl extends
		BaseServiceImpl<CountryProgress, Integer> implements
		CountryProgressService {

	@Resource(name = "countryProgressDao")
	private CountryProgressDao countryProgressDao;

	@Override
	public BaseDao<CountryProgress, Integer> getBaseDao() {
		// TODO Auto-generated method stub
		return countryProgressDao;
	}

	@Override
	public List<Object> getScanProgress(String countryName,
			String[] protocolGroup) {
		// TODO Auto-generated method stub

		List<Object> returnList = new ArrayList<Object>();
		List<Object[]> spList = countryProgressDao.getScanProgress(countryName,
				protocolGroup);
		for (Object[] objArray : spList) {
			HashMap<String, Object> currentMap = new HashMap<String, Object>();
			currentMap.put("countryName", objArray[0]);
			double rate = 0;
			Object tempObj = objArray[1];
			if (tempObj == null || tempObj == "")
				rate = 0;
			else {
				rate = Double.parseDouble(objArray[1].toString()) * 100;
			}

			DecimalFormat df = new DecimalFormat("#.00");
			String answer = df.format(rate);

			currentMap.put("progress", Double.parseDouble(answer));
			returnList.add(currentMap);
		}

		return returnList;
	}

	@Override
	public List<Object> getScanTopKey(String countryName, String[] protocolGroup) {
		// TODO Auto-generated method stub
		List<Object> returnList = new ArrayList<Object>();
		List<Object[]> spList = countryProgressDao.getScanTopKey(countryName,
				protocolGroup);
		for (Object[] objArray : spList) {
			HashMap<String, Object> currentMap = new HashMap<String, Object>();
			currentMap.put("countryName", objArray[0]);
			currentMap.put("exist", objArray[1]);
			returnList.add(currentMap);
		}

		return returnList;
	}

}
