package com.eactive.eai.rms.bap.tansaction.transactionWait;

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

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

import com.eactive.eai.rms.bap.tansaction.transactionStatus.BapTransactionStatusService;
import com.eactive.eai.rms.common.base.BapBaseAnnotationController;
import com.eactive.eai.rms.common.combo.IbatisComboService;
import com.eactive.eai.rms.common.util.CamelCaseUtil;
import com.eactive.eai.rms.common.vo.JsonPageVo;
import com.google.gson.Gson;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class BapTransactionWaitController extends BapBaseAnnotationController {

	@Autowired
	@Qualifier("bapTransactionStatusService")
	private BapTransactionStatusService statusService;

	@Autowired
	@Qualifier("bapTransactionWaitService")
	private BapTransactionWaitService service;

	@Autowired
	private IbatisComboService comboService;

	@RequestMapping(value = "/bap/transaction/transactionWaitMan.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;
	}

	/**
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/bap/transaction/transactionWaitMan.json", params = "cmd=LIST")
	public ModelAndView selectList(HttpServletRequest request, JsonPageVo pageVo,
			@RequestParam HashMap<String, Object> paramMap) throws Exception {

		Map<String, Object> map = service.selectList(pageVo.getStartNum(), pageVo.getEndNum(), paramMap);

		pageVo.setTotalCount((Integer) map.get("totalCount"));

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("total", pageVo.getTotal()); // 전체 페이지수
		resultMap.put("page", pageVo.getPage()); // 현재 페이지수
		resultMap.put("records", pageVo.getTotalCount());// 전체 레코드건수
		resultMap.put("rows", map.get("list"));

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}

	@RequestMapping(value = "/bap/transaction/transactionWaitMan.json", params = "cmd=DETAIL")
	public ModelAndView selectDetail(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> param) throws Exception {

		HashMap<String, Object> map = service.selectDetail(param);

		ModelAndView modelAndView = new ModelAndView("jsonView", map);

		return modelAndView;
	}

	@RequestMapping(value = "/bap/transaction/transactionWaitMan.json", params = "cmd=LIST_INIT_COMBO")
	public ModelAndView initCombo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		List<Map<String, Object>> bjobBzwkDstcd = comboService.selectListComboForTableWithCode("TSEAIBJ02",
				"BJOBBZWKDSTCD", "BJOBBZWKNAME");
		List<Map<String, Object>> osidInstiDstcd = statusService.selectOsidInstiDstcd("");
		// List<Map<String, Object>> svcPrcssNo =
		// comboService.selectListComboForTable("TSEAICM20", "code",
		// "codename","codegroup = 'SVC_PRCSS_NO' ");
		// comboService.selectListCombo("SVC_PRCSS_NO");

		Map<String, Object> resultMap = new HashMap<String, Object>();

		resultMap.put("bjobBzwkDstcd", bjobBzwkDstcd);
		resultMap.put("osidInstiDstcd", osidInstiDstcd);
		// resultMap.put("svcPrcssNo" , svcPrcssNo);

		ModelAndView modelAndView = new ModelAndView("jsonView", resultMap);
		return modelAndView;
	}

	@RequestMapping(value = "/bap/transaction/transactionWaitMan.json", params = "cmd=UPDATE")
	private String update(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map) throws Exception {

		service.update(map);

		return null;
	}

	@RequestMapping(value = "/bap/transaction/transactionWaitMan.json", params = "cmd=DELETE")
	private String delete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam HashMap<String, Object> map) throws Exception {

		service.delete(map);

		return null;
	}

	@RequestMapping(value = "/bap/transaction/transactionWaitMan.json", params = "cmd=DELETE_BS03")
	public ModelAndView deleteBs03(HttpServletRequest request,
			HttpServletResponse response, String gridData)
			throws Exception {

		Gson gson = new Gson();
		@SuppressWarnings("unchecked")
		HashMap<String, Object>[] list = gson.fromJson(gridData,
				HashMap[].class);

		service.deleteBs03(list);

		return null;
	}
}