package com.eactive.eai.rms.bat.manage.rule.schedule;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.eactive.eai.rms.bat.manage.rule.schedule.vo.SchedulerMonitorVO;
import com.eactive.eai.rms.common.acl.user.BizDao;
import com.eactive.eai.rms.common.login.SessionManager;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.GridResponse;
import com.eactive.eai.rms.common.vo.JsonPageVo;

@Controller
public class BatSchedulerMonitorController {
	
	@Autowired private BatSchedulerMonitorService service;
	
	@Autowired @Qualifier("bizDao") private BizDao bizDao;
	
	@RequestMapping(value= "/bat/admin/schedule/schedulerMonitor.view")
	public String viewList(HttpServletRequest request, @RequestParam(value = "cmd", required = false ) String cmd ) throws Exception {
		if (cmd == null || "LIST".equals(cmd.toUpperCase())){
			cmd = "";
		}else {
			cmd = CamelCaseUtil.converCamelCase(cmd);
		}
		String path = request.getServletPath();
		return path.split("[.]")[0]+cmd;
	}
	
	@RequestMapping(value= "/bat/admin/schedule/schedulerMonitor.json", params = "cmd=LIST")
	public ResponseEntity<GridResponse<SchedulerMonitorVO>> selectList(JsonPageVo pageVo, @RequestParam Map<String, Object> paramMap) throws Exception {
		
		paramMap.put("startNum", pageVo.getStartNum());
		paramMap.put("endNum", pageVo.getEndNum());
		
		List<SchedulerMonitorVO> list = service.selectList(paramMap);
		Integer count = service.selectCount(paramMap);
		
		pageVo.setTotalCount(count);
		
		GridResponse<SchedulerMonitorVO> gridResponse = new GridResponse<>();
		gridResponse.setTotal(pageVo.getTotal());
		gridResponse.setPage(pageVo.getPage());
		gridResponse.setRecords(pageVo.getTotalCount());
		gridResponse.setRows(list);
		
		return ResponseEntity.ok()
				.contentType(MediaType.APPLICATION_JSON)
				.body(gridResponse);
	}
	
	@RequestMapping(value= "/bat/admin/schedule/schedulerMonitor.json",params = "cmd=LIST_INIT_COMBO")
	public ResponseEntity<Map<String, Object>> initCombo(HttpServletRequest request, @RequestParam HashMap<String, Object> paramMap) throws Exception {

		paramMap.put("userId", SessionManager.getUserId(request));
		paramMap.put("prptyGroupId", "BzSystem");
		List<HashMap<String, Object>> listBiz = bizDao.selectUserBizCodes(paramMap);

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bizList", listBiz);
		
		return ResponseEntity.ok(resultMap);
	}

}
