package com.seeyon.apps.src_automatic.ctrl;

import java.io.IOException;

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

import org.springframework.web.servlet.ModelAndView;

import com.seeyon.apps.src_automatic.dao.IAutomaticDao;
import com.seeyon.apps.src_automatic.service.IAutomaticService;
import com.seeyon.apps.src_automatic.task.AutomaticTask;
//import com.seeyon.apps.src_automatic.task.PushAuCardTask;
import com.seeyon.ctp.common.AppContext;
//import com.seeyon.apps.src_automatic.util.ConfigKit;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.quartz.QuartzHolder;
import com.seeyon.ctp.util.annotation.NeedlessCheckLogin;

/**
 * Description
 * <pre>定时任务控制器</pre>
 * Copyright(c) Beijing Seeyon Software Co.,LTD
 */
public class StartTaskController extends BaseController {
	
//	private PushZcService pushZcService;
	private IAutomaticService automaticService;
	
	
	
	public IAutomaticService getAutomaticService() {
		if (automaticService == null) {
			automaticService = (IAutomaticService) AppContext.getBean("automaticService");
		}
		return automaticService;	}

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

	@NeedlessCheckLogin
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws IOException {
//		用于执行定时任务的开启和关闭
		String opera = request.getParameter("task");
		if("start".equals(opera)) {
			String time;
			try {
//				time = ConfigKit.getTaskTime();
				time = automaticService.getTaskTime();
			} catch (Exception e) {
				super.rendJavaScript(response, "alert('获取配置出错');");
				return null;
			}
			if(null == time) {
				super.rendJavaScript(response, "alert('请正確配置nczc.properties中的taskTime参数，如果定时执行，启用D-开头，如D-03,15表示每天三点十五执行，如果间隔时间执行，请用I-开头，例如I-03,00表示每三个小时执行一次！');");
				return null;
			} else if(time.startsWith("D-")) {
				// D-开头，每天执行任务
				String[] arr = time.split("-");
				try {
					Integer[] itime = getTime(arr);
					AutomaticTask.registerSyncTask(false, itime[0], itime[1]);
					super.rendJavaScript(response, "alert('任务注册成功，每天" + itime[0] + "点," + itime[1] +  "分执行');");
					return null;
				} catch (Exception e) {
					super.rendJavaScript(response, "alert('" + e.getMessage() + "');");
					return null;
				}
			} else if(time.startsWith("I-")) {
				// i-开头，是间隔时间执行
				String[] arr = time.split("-");
				try {
					Integer[] itime = getTime(arr);
					AutomaticTask.registerSyncTask(true, itime[0], itime[1]);
					super.rendJavaScript(response, "alert('任务注册成功，间隔" + itime[0] + "小时," + itime[1] +  "分钟执行');");
					return null;
				} catch (Exception e) {
					super.rendJavaScript(response, "alert('" + e.getMessage() + "');");
					return null;
				}
			} else {
				super.rendJavaScript(response, "alert('请正確配置nczc.properties中的taskTime参数，如果定时执行，启用D-开头，如D-03,15表示每天三点十五执行，如果间隔时间执行，请用I-开头，例如I-03,00表示每三个小时执行一次！');");
				return null;
			}
		} else if("stop".equals(opera)){
			QuartzHolder.deleteQuartzJob(AutomaticTask.jobName);
			super.rendJavaScript(response, "alert('定时推送短信定时任务已经终止！');");
			return null;
		} else if("run".equals(opera)) {
			String runall = request.getParameter("all");
//			String res = pushZcService.calcBudget("true".equals(runall));
//			super.rendJavaScript(response, "alert('推送资产任务执行完毕: " + res + "');");
			return null;
		} else {
			super.rendJavaScript(response, "alert('参数有误，请联系开发人员！');");
			return null;
		}
	}
	
	private Integer[] getTime(String[] arr) throws Exception {
		String time = arr[1];
		arr = time.split(",");
		if(arr.length !=  2) {
			throw new Exception("小时和分钟请用逗号隔开");
		}
		int our = Integer.valueOf(arr[0]);
		int minute = Integer.valueOf(arr[1]);
		if(our < 0 || our >= 24) {
			throw new Exception("小时数必须在0~24之间");
		}
		if(minute < 0 || minute >= 60)  {
			throw new Exception("分钟数必须在0~60之间");
		}
		return new Integer[] {our, minute};
	}

//	public void setPushZcService(PushZcService pushZcService) {
//		this.pushZcService = pushZcService;
//	}

}
