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.crdn.service.CrdnService;
import cokr.xit.fims.cvlc.CrdnCvlcpt;
import cokr.xit.fims.cvlc.service.CrdnCvlcptService;

/**민원 개별 등록 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/cvlc/cvlc04 로 접근할 수 있다.
 * @author leebj
 */
public class Cvlc04Controller extends ApplicationController {

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

	public class METHOD_URL {
		public static final String
		civilComplaintIndividualRegistrationMain = "/010/main.do",
		getDmndDataList = "/010/list.do",
		createDmndDataInfo = "/010/create.do",
		removeDmndDataInfo = "/010/remove.do"
		;
	}

	@Resource(name="crdnService")
	private CrdnService crdnService;

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

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

	/** 민원 개별 등록 메인화면을 연다.
	 * @return fims/cvlc/cvlc04010-main
	 */
	public ModelAndView civilComplaintIndividualRegistrationMain() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("fims/cvlc/cvlc04010-main");
		mav.addObject("pageName", "cvlc04010-main");

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

	/** 새올 요청자료 목록을 조회한다.
	 * {@link CrdnCvlcService#getDmndDataList(CrdnQuery)} 참고
	 * @param query 새올 요청자료 목록 조회 조건
	 * @return jsonView
	 */
	public ModelAndView getDmndDataList(CrdnQuery query) {

		query.setSggCd(globalStngMapper.selectSggCdForCurrentUser());

		setFetchSize(query);
		return setCollectionInfo(new ModelAndView("jsonView"), crdnCvlcptService.getDmndDataList(query),"");

	}

	/** 새올 요청자료를 생성한다.
	 * {@link CrdnCvlcService#createDmndDataInfo(crdnCvlcpt)} 참고
	 * @param crdnCvlcpt 새올요청자료 정보
	 * @return jsonView
	 * <pre><code> {
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView createDmndDataInfo(CrdnCvlcpt crdnCvlcpt) {

		crdnCvlcpt.setSggCd(globalStngMapper.selectSggCdForCurrentUser());

		boolean saved = false;

		saved = crdnCvlcptService.createDmndDataInfo(crdnCvlcpt);

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

	}

	/**새올 요청자료를 제거한다.
	 * @param IDs 개별요청 ID
	 * @return jsonView
	 * <pre><code> {
	 *     "affected": 저장된 정보수
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	public ModelAndView removeDmndDataInfo(String[] IDs) {

		boolean saved = crdnCvlcptService.removeDmndDataInfo(IDs);

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