package cokr.xit.fims.cvlc.web;

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

import javax.annotation.Resource;

import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.crdn.dao.GlobalStngMapper;
import cokr.xit.fims.cvlc.CrdnCvlcpt;
import cokr.xit.fims.cvlc.service.CrdnCvlcptService;

/**계도문 문구 관리 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/cvlc/cvlc06 로 접근할 수 있다.
 * @author leebj
 */
public class Cvlc06Controller extends ApplicationController {

	public static final String CLASS_URL = "/cvlc/cvlc06";

	public class METHOD_URL {
		public static final String
		warningWordsManageMain = "/010/main.do",
		getWarningWordsList = "/010/list.do",
		saveCvlcptAnswerWordsInfo = "/010/save.do"
		;
	}

	@Resource(name="crdnCvlcptService")
	private CrdnCvlcptService crdnCvlcptService;

	@Resource(name="globalStngMapper")
	private GlobalStngMapper globalStngMapper;

	/** 계도문 문구 관리 메인화면을 연다.
	 * @return fims/cvlc/cvlc06010-main
	 */
	public ModelAndView warningWordsManageMain() {

		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/cvlc/cvlc06010-main");
		mav.addObject("pageName", "cvlc06010-main");

		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM054");
		mav.addObject("FIM054List", commonCodes.get("FIM054"));

		return mav;
	}

	/** 계도문 문구 설정 목록을 조회한다.
	 * @return fims/cvlc/cvlc06010-info
	 */
	public ModelAndView getWarningWordsList(CrdnQuery crdnQuery) {
		String currentSggCd = globalStngMapper.selectSggCdForCurrentUser();
		crdnQuery.setSggCd(currentSggCd);
		return setCollectionInfo(new ModelAndView("jsonView"), crdnCvlcptService.getWarningWordsList(crdnQuery),"");
	}

	/** 계도문 문구 설정을 저장한다.
	 * @param crdnCvlcpt 문구 정보
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView saveCvlcptAnswerWordsInfo(CrdnCvlcpt crdnCvlcpt) {
		boolean saved = false;

		String currentSggCd = globalStngMapper.selectSggCdForCurrentUser();
		crdnCvlcpt.setSggCd(currentSggCd);

		saved = crdnCvlcptService.saveWarningWordsInfo(crdnCvlcpt);

		return new ModelAndView("jsonView")
				.addObject("saved", saved);
	}

}
