package cokr.xit.fims.cvlc.web;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cokr.xit.base.code.CommonCode;
import cokr.xit.base.user.dao.UserMapper;
import cokr.xit.base.web.ApplicationController;
import cokr.xit.fims.cmmn.service.bean.StngBean;
import cokr.xit.fims.crdn.CrdnQuery;
import cokr.xit.fims.cvlc.CrdnCvlcpt;
import cokr.xit.fims.cvlc.service.CrdnCvlcptService;
import cokr.xit.fims.task.Task;

/**계고문 문구 관리 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/cvlc/cvlc06 로 접근할 수 있다.
 * @author leebj
 */
@Controller
@RequestMapping(name="계고문 문구 관리", value=Cvlc06Controller.CLASS_URL)
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 = "stngBean")
	private StngBean stngBean;
	@Resource(name="userMapper")
	private UserMapper userMapper;

	/** 계고문 문구 관리 메인화면을 연다.
	 * @return fims/cvlc/cvlc06010-main
	 */
	@RequestMapping(name="계고문 문구 관리 메인", value=METHOD_URL.warningWordsManageMain)
	public ModelAndView warningWordsManageMain() {
		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM054");

		return new ModelAndView("fims/cvlc/cvlc06010-main")
			.addObject("pageName", "cvlc06010")
			.addObject("FIM054List", commonCodes.get("FIM054"))
			.addObject("TaskListForSgg", stngBean.filterTaskSectionCodeForSgg(commonCodes.get("FIM054")));
	}

	/** 계고문 문구 설정 목록을 조회한다.
	 * @return fims/cvlc/cvlc06010-info
	 */
	@Task
	@RequestMapping(name="계고문 문구 설정 목록", value=METHOD_URL.getWarningWordsList)
	public ModelAndView getWarningWordsList(CrdnQuery crdnQuery) {
		crdnQuery.setSggCd(currentUser().getOrgID());
		return setPagingInfo(new ModelAndView("jsonView"), crdnCvlcptService.getWarningWordsList(crdnQuery),"");
	}

	/** 계고문 문구 설정을 저장한다.
	 * @param crdnCvlcpt 문구 정보
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	@Task
	@RequestMapping(name="계고문 문구 설정 저장", value=METHOD_URL.saveCvlcptAnswerWordsInfo)
	public ModelAndView saveCvlcptAnswerWordsInfo(CrdnCvlcpt crdnCvlcpt) {
		crdnCvlcpt.setSggCd(currentUser().getOrgID());
		String rtnMsg = crdnCvlcptService.saveWarningWordsInfo(crdnCvlcpt);

		return new ModelAndView("jsonView")
			.addObject("saved", rtnMsg.contains("[S]"));
	}
}