package com.platform.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.BaseController;
import com.platform.dao.GatherOracleDao;
import com.platform.entities.GatherOracleInfo;
import com.platform.form.oracleForm;
import com.platform.service.ILogRead;
import com.platform.service.IOracleExtractService;
import com.platform.service.thread.ThreadExtractStandardOracle;
import com.platform.service.thread.ThreadGainOracleConnect;
import com.platform.utils.Configs;
import com.platform.utils.Constant;

/** oracle相关业务
 * @author chen
 *
 */
@Controller
public class OracleController extends BaseController {
	
	/**
	 * 日志
	 */
	public final static Logger log = Logger.getLogger(OracleController.class);
	
	/**
	 * oracle汇总 业务类
	 */
	@Resource(name = "OracleExtract")
	private IOracleExtractService OracleExtract;
	
	/**
	 * oracle汇总日志记录类
	 */
	@Resource(name = "logReadService")
	private ILogRead logReadService;
	
	/**
	 * oracle汇总库，抽取库持久层
	 */
	@Resource(name = "gatherOracleDao")
	private GatherOracleDao gatherOracleDao;

	/** oracle 标准表抽取
	 * @param res
	 * @param req
	 * @param form
	 * @throws Exception
	 */
	@RequestMapping(value = "/oracle/standardextract/{name}/extract", method = RequestMethod.POST)
	public void oracleStandardExtract(HttpServletRequest res, HttpServletResponse req,
			@RequestBody oracleForm form) throws Exception {
		log.info("/oracle/standardextract/{name}/extract");
		log.info(form.getInneed().get(0).getName());
		boolean isConnect = false;
		//5秒内是否能获得oracle连接，否则认为超时。
		if (null != form.getTarget()) {
			// 汇总库的信息
			GatherOracleInfo oracleInfo = null;
			List<GatherOracleInfo> oracleConnects = gatherOracleDao.selectAllOracle();
			for (GatherOracleInfo info : oracleConnects) {
				//抽取标准表的汇总库
				if (Constant.ORACLE_EXTRACT_TYPE_ONE.equals(info.getType())) {
					oracleInfo = info;
					break;
				}
			}
			if (null == oracleInfo) {
				isConnect = false;
			}
			else {
				form.setTarget(oracleInfo);
				ThreadGainOracleConnect thOrcl = new ThreadGainOracleConnect(form, OracleExtract);
				thOrcl.start();
				for (int i = 0; i < 10; i++) {
					Thread.sleep(400);
					isConnect = thOrcl.isConnect();
					if (isConnect) {
						break;
					}
					else {
						if (thOrcl.isExcept()) {
							break;
						}
						Thread.sleep(100);
					}
				}
			}
			
		}
		if (isConnect)
			req.setStatus(200);
		else
			req.setStatus(500);
		// 开始抽取数据到汇总库
		if (isConnect && null != form.getInneed() && form.getInneed().size() > 0) {
			ThreadExtractStandardOracle thExtra = new ThreadExtractStandardOracle(form, OracleExtract);
			thExtra.start();
		}
	}
	
	/** 读取标准表 抽取的日志，包括sqlserver，oracle
	 * @param name
	 * @param res
	 * @param req
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/standardextract/log", method = RequestMethod.POST)
	@ResponseBody
	public Object getStandardExtractLog(@RequestParam("rcName") String name,
			HttpServletRequest res, HttpServletResponse req) throws Exception {
		Configs.CONSOLE_LOGGER.info("/StandardExtract/log");
		String result = logReadService.readStandardLog(name);
		// StringBuilder sb = new StringBuilder();
		// sb.append("查看相应日志").append("\n").append("看到了");
		Map<String, String> log = new HashMap<String, String>();
		log.put(name, result);
		return log;
	}
}
