package com.seeyon.apps.src_automatic.test;


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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_automatic.service.IAutomaticService;
import com.seeyon.apps.src_automatic.util.MoblieMsgUtil;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.ctp.util.json.JSONUtil;

public class SendTest  extends BaseController{

	private static final Log log = LogFactory.getLog(SendTest.class);
	
	private IAutomaticService automaticService;
	
	public IAutomaticService getAutomaticService() {
		if (automaticService == null) {
			automaticService = (IAutomaticService) AppContext.getBean("automaticService");
		}
		return automaticService;
	}

	public void setAutomaticService(IAutomaticService automaticService) {
		this.automaticService = automaticService;
	}



	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
//		StringBuffer sb = new StringBuffer();
//		String mode = "0";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = new Date();
			String dateStr = sdf.format(date);
//			获取需要发送短信的主表id
//			System.out.println(dateStr);
			List<Map<String,Object>> datas = automaticService.getSendNote(dateStr);
			for (Map<String,Object> maps : datas) {
//				根据主表id获取需要发送的短信数据
				List<Map<String,Object>> lists= automaticService.getSendNotes(maps.get("id").toString());
				int succeed = 0;
				int defeated = 0;
//				int i = 1 ; 
				for (Map<String,Object> map : lists) {
//					System.out.println("给"+map.get("recipient")+"发短信，电话号码是："+map.get("phone")+"，需要发送的类容：“"+map.get("content")+"”");
					if(map.get("phone")!=null) {
//						System.out.println(map.get("phone"));
						String[] phones = map.get("phone").toString().split(",");
//						去除手机号重复
						List<String> list = new ArrayList<String>();
						for (int i = 0; i < phones.length; i++) {
						   if (!list.contains(phones[i])) {
						       list.add(phones[i]);
						   }
						}
						phones = list.toArray(new String[list.size()]);
						int defeatedNum = 0;
						for (String phone : phones) {
							if(phone.length()>0) {
//								调用发送短信
								String res = MoblieMsgUtil.sendMsg(phone, UUIDLong.longUUID() + "", map.get("content").toString());
//								System.out.println(res);
								JSONObject json = JSONUtil.parseJSONString(res, JSONObject.class);
//								处理返回值
								int codeint = json.getIntValue("result");
								if(codeint == 0) {
//									发送成功
									succeed++;
									if(defeatedNum<0) {
										int in=automaticService.updateNotes(codeint+"", null, map.get("id").toString());
									}
//									sb.append("短信发送成功:" + map.get("phone").toString() + ",");
								} else {
//									发送失败
									defeated++;defeatedNum++;
									int in = automaticService.updateNotes(codeint+"", json.getString("desc"), map.get("id").toString());
//									sb.append(json.getString("desc") + ",");
								}
							}
						}
					}
				}
				String content;
				if(defeated==0){
					content = "总共发送短信"+succeed+"条，成功"+succeed+"条。";
				}else {
					content = "总共发送短信"+(succeed+defeated)+"条，成功"+succeed+"条，失败"+defeated+"条，失败原因请查看日消息通知列表";
				}
//				System.out.println(content);
				automaticService.sendMsg(content, Long.valueOf(maps.get("field0002").toString()));
			}
			
		}catch (Exception e) {
			log.error("循环发送短信发生异常：", e);
		}
		return null;
	}
	
	
}
