package com.zhiwei.manage.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiwei.manage.bean.PageEty;
import com.zhiwei.manage.bean.Template;
import com.zhiwei.manage.dao.TemplateDao;
import com.zhiwei.manage.handle.DelayedThread;
import com.zhiwei.manage.handle.FieldThread;
import com.zhiwei.manage.handle.MainThread;
import com.zhiwei.manage.handle.PingThread;

@Service
public class TemplateServiceImpl implements TemplateService {

	private static final Log log = LogFactory.getLog(TemplateServiceImpl.class);
	@Autowired
	private TemplateDao tempDao;

	@Override
	public boolean insert(Template template) {
		try {
			tempDao.insert(template);
			return true;
		} catch (Exception e) {
			log.info("存储出错,错误信息{}", e);
			return false;
		}
	}

	@Override
	public boolean update(Template template) {
		try {
			return tempDao.update(template);
		} catch (Exception e) {
			log.info("模板更新出错，错误信息{}", e);
			return false;
		}
	}

	@Override
	public PageEty findAll(int pageNo, int pageSize, String pt) {
		int count = tempDao.allCount(pt);
		List<Template> data = tempDao.findAll(pageNo, pageSize, pt);
		for (Template tp : data) {
			if (MainThread.allTmp.get(tp.getTemplateId()) == null) {
				MainThread.allTmp.put(tp.getTemplateId(), tp);
			}
			if (MainThread.mainMap.get(tp.getTemplateId()) != null) {
				tp.setOpen(true);
			}
		}
		PageEty page = new PageEty();
		page.setData(data);
		page.setDataCount(count);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);
		if (count % pageSize == 0) {
			page.setPageCount(count / pageSize);
		} else
			page.setPageCount(count / pageSize + 1);
		return page;
	}

	@Override
	public Template findById(String templateId, String pt) {
		return tempDao.findById(templateId, pt);
	}

	@Override
	public PageEty findByTmpName(String templateName, int pageNo, int pageSize, String pt) {
		try {
			int count = tempDao.findByUserCount(templateName, pt);
			List<Template> data = tempDao.findByTmpName(templateName, pageNo, pageSize, pt);
			for (Template tp : data) {
				if (MainThread.allTmp.get(tp.getTemplateId()) == null) {
					MainThread.allTmp.put(tp.getTemplateId(), tp);
				}
				if (MainThread.mainMap.get(tp.getTemplateId()) != null) {
					tp.setOpen(true);
				}
			}
			PageEty page = new PageEty();
			if (data.size() == 0) {
				page.setData(new ArrayList<>());
			} else {
				page.setData(data);
			}
			page.setPageNo(pageNo);
			page.setPageSize(pageSize);
			if (count % pageSize == 0) {
				page.setPageCount(count / pageSize);
			} else
				page.setPageCount(count / pageSize + 1);
			return page;
		} catch (Exception e) {
			log.info("模板查询出错，错误信息{}", e);
			return null;
		}
	}

	@Override
	public boolean delete(String templateId, String pt) {
		try {
			return tempDao.delete(templateId, pt);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<Template> findAll() {
		List<Template> list = tempDao.findAll();
		for (Template tp : list) {
			if (MainThread.allTmp.get(tp.getTemplateId()) == null) {
				MainThread.allTmp.put(tp.getTemplateId(), tp);
			}
			if (MainThread.mainMap.get(tp.getTemplateId()) != null) {
				tp.setOpen(true);
			} 
		}
		return list;
	}

}
