package kr.co.jinwoosi.clfd.framework.cd.web;

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

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

import kr.co.jinwoosi.clfd.framework.base.web.BaseController;
import kr.co.jinwoosi.clfd.framework.cd.model.Cd;
import kr.co.jinwoosi.clfd.framework.cd.model.CdCls;
import kr.co.jinwoosi.clfd.framework.cd.service.CdClsService;
import kr.co.jinwoosi.clfd.framework.cd.service.CdService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CdClsController extends BaseController {
	
	@Resource (name="cdClsService")
	private CdClsService cdClsService;
	
	@Resource (name="cdService")
	private CdService cdService;
	
	//화면전환
	@RequestMapping("/system/cd/mng.mng")
	public String site(Model model, Cd cd) {
		// model.addAttribute("cdClsList", cdClsService.selectList(cdCls));
		model.addAttribute("cdList", cdService.selectList(cd));
		return "system/cd/mng";
	}
	
	//리스트 재전송
	@RequestMapping(value="/system/cdCls/list.mng", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, List<CdCls>> list(HttpServletRequest request, CdCls cdCls) {
		Map<String, List<CdCls>> result = new HashMap<String, List<CdCls>>();
		result.put("result", cdClsService.selectList(cdCls));
		return  result;
	}
	
	//상세 내역 전송
	@RequestMapping(value="/system/cdCls/select.mng", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> select(HttpServletRequest request, CdCls cdCls) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", cdClsService.select(cdCls));
		return result;
	}
	
	
	//등록
	@RequestMapping(value="/system/cdCls/insert.mng", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insert(HttpServletRequest request, CdCls cdCls) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			if(cdClsService.existCount (cdCls) > 0) {//코드분류아이디 또는 코드분류이름이 중복된 경우
				result.put("result", "duplicate");
				result.put("message", "이미 등록된 코드 분류입니다. 다시 등록하여 주십시오.");
			} else {
				cdCls.setRegId(getUserId());
				cdClsService.insert(cdCls);
				result.put("result", "success");
			}
		} catch (Exception e){
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
	
	//수정
	@RequestMapping(value="/system/cdCls/update.mng", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(HttpServletRequest request, CdCls cdCls) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			cdCls.setUpdId(getUserId());
			cdClsService.update(cdCls);
			result.put("result", "success");
		} catch (Exception e){
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
	
	//삭제
	@RequestMapping(value="/system/cdCls/delete.mng", method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request, CdCls cdCls) {
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			Cd cd = new Cd();
			cd.setCdClsId(cdCls.getCdClsId());
			int cdCnt = cdService.count(cd);
			if(cdCnt > 0){
				result.put("result", "cntFail");
			}else{
				cdCls.setDelId(getUserId());
				//cdClsService.delete(cdCls);
				result.put("result", "success");
			}
		} catch (Exception e){
			e.printStackTrace();
			result.put("result", "fail");
			result.put("message", e.getMessage());
		}
		return result;
	}
}
