package com.product.data.center.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.product.core.controller.support.AbstractBaseController;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.data.center.config.ErrorCode;
import com.product.data.center.service.JournalManagerService;
import com.product.module.sys.version.ApiVersion;
import com.product.util.BaseUtil;

/**
 * 	鐩戞帶
 * @author 86151
 *
 */
@RequestMapping("/api/monitor")
@RestController
public class MonitorController extends AbstractBaseController{

	@Autowired
	JournalManagerService journalManagerService;

	/**
	 * 	宸︿晶鍒嗙粍
	 * @param request
	 * @return
	 */
	@PostMapping("/list-monitor/{version}")
    @ApiVersion(1)
	public String listMonitor(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			return OK_List(journalManagerService.journalServerView());
		} catch (BaseException e) {
            e.printStackTrace();
            return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.MONITOR_LIST_LEFT_GROUP_FAIL);
		}
	}

	/**
	 * 	鍙充晶瑙嗗浘
	 * @param request
	 * @return
	 */
	@PostMapping("/find-monitor/{version}")
    @ApiVersion(1)
	public String findMonitorView(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			return OK_List(journalManagerService.groupCountErrorLog(fse));
		} catch (BaseException e) {
            e.printStackTrace();
            return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.MONITOR_LIST_RIGHT_VIEW_FAIL);
		}
	}

	/**
	 * 	鏃ュ織鍒楄〃
	 * @param request
	 * @return
	 */
	@PostMapping("/list-log/{version}")
    @ApiVersion(1)
	public String listLogInfo(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			if(BaseUtil.strIsNull(fse.getString("cpage"))|| BaseUtil.strIsNull(fse.getString("pagesize"))) {
				fse.setValue("cpage", 1);
				fse.setValue("pagesize", 20);
			}
			return OK_List(journalManagerService.listJournal(fse, false));
		} catch (BaseException e) {
            e.printStackTrace();
            return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.MONITOR_LIST_LOG_FAIL);
		}
	}

	/**
	 * 	閿欒鏃ュ織鍒楄〃
	 * @param request
	 * @return
	 */
	@PostMapping("/list-error-log/{version}")
    @ApiVersion(1)
	public String listErrorLogInfo(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			if(BaseUtil.strIsNull(fse.getString("cpage"))|| BaseUtil.strIsNull(fse.getString("pagesize"))) {
				fse.setValue("cpage", 1);
				fse.setValue("pagesize", 20);
			}
			return OK_List(journalManagerService.listJournal(fse, true));
		} catch (BaseException e) {
            e.printStackTrace();
            return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.MONITOR_LIST_ERROR_LOG_FAIL);
		}
	}

	/**
	 * 	鏃ュ織璇︽儏
	 * @param request
	 * @return
	 */
	@PostMapping("/find-log/{version}")
    @ApiVersion(1)
	public String findLogInfo(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			return OK_List(journalManagerService.findJournal(fse));
		} catch (BaseException e) {
            e.printStackTrace();
            return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.MONITOR_FIND_LOG_FAIL);
		}
	}

	/**
	 * 	閿欒鏃ュ織閲嶆柊澶勭悊
	 * @param request
	 * @return
	 */
	@PostMapping("/reprocess-log/{version}")
	@ApiVersion(1)
	public String reprocessLog(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);
			journalManagerService.secondOperation(fse.getUUID());
			return OK();
		} catch (BaseException e) {
			e.printStackTrace();
			return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.LOGMONITOR_OPERATE_FAIL+e.getMessage());
		}
	}

	/**
	 * 	閿欒鏃ュ織鏍囪瘑澶勭悊
	 * @param request
	 * @return
	 */
	@PostMapping("/identification-log/{version}")
    @ApiVersion(1)
	public String identificationLog(HttpServletRequest request) {
		try {
			FieldSetEntity fse = BaseUtil.getFieldSetEntity(request);

			journalManagerService.signOperation(fse.getUUID());
			return OK();
		} catch (BaseException e) {
            e.printStackTrace();
            return error(e);
		} catch (Exception e) {
			e.printStackTrace();
			return error(ErrorCode.MONITOR_SIGN_ERROR_LOG_FAIL);
		}
	}
}