package com.zhiwei.manage.service;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhiwei.manage.bean.CrawTemplate;
import com.zhiwei.manage.dao.CrawTemplateDao;
@Component
public class CrawTemplateServiceImpl implements CrawTemplateService {

	@Autowired
	private CrawTemplateDao crawDao;
	
	@Override
	public List<CrawTemplate> findAll() {
		List<CrawTemplate> list=crawDao.findAll();
		for(CrawTemplate ct:list) {
			ct.setPt(getPtByCt(ct));
		}
		return list;
	}

	@Override
	public String findPt(String spyId) {
		CrawTemplate ct=crawDao.getPt(spyId);
		if(ct!=null) {
			return getPtByCt(ct);
		}else {
			return null;
		}
		
	}
	
	
	public String getPtByCt(CrawTemplate ct) {
		String pt="";
		List<Map<String,Object>> list=ct.getStaticFieldList();
		for(Map<String,Object> map:list) {
			Set<String> keys=map.keySet();
			for(Iterator<String> it=keys.iterator();it.hasNext();) {
				String key=it.next();
				if(String.valueOf(map.get(key)).equals("pt")) {
					pt=String.valueOf(map.get("value"));
				}
			}
		}
		return pt;
	}

	@Override
	public String getSpIdByConfigName(String tempName) {
		return crawDao.getSpIdByConfigName(tempName);
	}

}
