package com.seeyon.apps.src_automatic.service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;

import com.seeyon.apps.src_automatic.dao.IAutomaticDao;
import com.seeyon.apps.src_automatic.dao.ISendNoteMsgDao;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.ctpenumnew.manager.EnumManager;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.services.ServiceException;
import com.seeyon.oainterface.impl.exportdata.MessageExporter;

public class AutomaticServiceImpl implements IAutomaticService{

	private IAutomaticDao automaticDao;
	private EnumManager enumManagerNew;
	private ISendNoteMsgDao sendNoteMsgDao;
	private OrgManager orgManager;
	
	public OrgManager getOrgManager() {
		if (this.orgManager == null) {
	        this.orgManager = ((OrgManager)AppContext.getBean("orgManager"));
	      }
	  return orgManager;
	}

	public void setOrgManager(OrgManager orgManager) {
		this.orgManager = orgManager;
	}
	
	
	public EnumManager getEnumManager() {
		if (this.enumManagerNew == null) {
	        this.enumManagerNew = ((EnumManager)AppContext.getBean("enumManagerNew"));
	      }
	  return enumManagerNew;
	}

	public void setEnumManagerNew(EnumManager enumManagerNew) {
		this.enumManagerNew = enumManagerNew;
	}
	
	public ISendNoteMsgDao getSendNoteMsgDao() {
		if (sendNoteMsgDao == null) {
			sendNoteMsgDao = (ISendNoteMsgDao) AppContext.getBean("sendNoteMsgDao");
		}
		return sendNoteMsgDao;
	}

	public void setSendNoteMsgDao(ISendNoteMsgDao sendNoteMsgDao) {
		this.sendNoteMsgDao = sendNoteMsgDao;
	}
	
	public IAutomaticDao getAutomaticDao() {
		if (automaticDao == null) {
			automaticDao = (IAutomaticDao) AppContext.getBean("automaticDao");
		}
		return automaticDao;
	}

	public void setAutomaticDao(IAutomaticDao automaticDao) {
		this.automaticDao = automaticDao;
	}

	@Override
	public String getTaskTime() {
		List<Map<String, Object>> lists;
		String str = "";
		try {
			lists = automaticDao.getAutomaticTime();
		
		Map<String , Object> map;
		if(lists.size()>0) {
			map = lists.get(0);
		}else {
			return null;
		}		
		StringBuffer stb = new StringBuffer();
		if("01".equals(enumManagerNew.getEnumItem(Long.parseLong(map.get("time").toString())).getValue())) {
			stb.append("D-").append(map.get("timingHour")).append(",").append(map.get("timingMinute"));
		}else if("02".equals(enumManagerNew.getEnumItem(Long.parseLong(map.get("time").toString())).getValue())){
			stb.append("I-").append(map.get("intervalHour")).append(",").append(map.get("intervalMinute"));
		}
		
		str = stb.toString();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}

	@Override
	public List<Map<String,Object>> getSendNote(String date) {
		List<Map<String,Object>> lists = new ArrayList<Map<String,Object>>();
		try {
			List<Map<String,Object>> ids = sendNoteMsgDao.findNoteByDate(date);
			lists = ids;
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lists;
	}
	
	
	
	@Override
	public List<Map<String,Object>> getSendNotes(String id) {
		List<Map<String,Object>> lists = new ArrayList<Map<String,Object>>();
		try {
			List<Map<String,Object>> maps = sendNoteMsgDao.findNotesByFormId(id);
			lists = maps;
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return lists;
	}

	@Override
	public int updateNotes(String result, String desc, String id) {
		int i = 0 ; 
		try {
			if(desc==null) {
				i =sendNoteMsgDao.updateNotesbySucceed(result, id);
			}else {
				i =sendNoteMsgDao.updateNotesbyDefeated(result, desc, id);
			}
		} catch (BusinessException | SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	

	/**
	  * //发送消息给指定人
	  * @param content
	  */
	 public void sendMsg(String content, Long msgMemberId){
	  
	  MessageExporter messageExporter = new MessageExporter();
	  try {
//	   String msgLoginName = "shenxian";
		  V3xOrgMember orgMember = orgManager.getMemberById(msgMemberId);
		  String msgLoginName = orgMember.getLoginName();
	   if(StringUtils.isNotEmpty(msgLoginName)){
	    String[] m = {msgLoginName};
//	    List<String> list = new ArrayList<String>();
//	    list.add(msgLoginName);
	    messageExporter.sendMessage(-7273032013234748168l, m,content , null,null,null);
	   }
	  } catch (ServiceException e) {
	   // TODO Auto-generated catch block
	   e.printStackTrace();
	  } catch (BusinessException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	 }

}
