package cokr.xit.fims.crdn.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.crdn.Team;
import cokr.xit.fims.crdn.service.CrdnStngService;
import cokr.xit.fims.task.Task;

/**단속팀 관리 서비스 웹 컨트롤러.<br />
 * {웹 컨텍스트}/crdn/crdn07 로 접근할 수 있다.
 * @author leebj
 */
@Controller
@RequestMapping(name="단속팀 관리", value=Crdn07Controller.CLASS_URL)
public class Crdn07Controller extends ApplicationController {

	public static final String CLASS_URL = "/crdn/crdn07";

	public class METHOD_URL {
		public static final String
		  crackdownTeamManagementMain = "/010/main.do"
		, getTeamList = "/010/list.do"
		, getRemovedTeamList = "/010/removedTeamList.do"
		, removeTeamInfo = "/010/remove.do"
		, saveTeamInfo = "/010/save.do"
		, getTeamInfo = "/010/info.do"
		;
	}

	@Resource(name="crdnStngService")
	private CrdnStngService crdnStngService;
	@Resource(name="userMapper")
	protected UserMapper userMapper;
	@Resource(name = "stngBean")
	private StngBean stngBean;

	/** 단속팀 관리 메인화면을 연다.
	 * @return fims/crdn/crdn07010-main
	 */
	@RequestMapping(name="단속팀 관리 메인", value=METHOD_URL.crackdownTeamManagementMain)
	public ModelAndView crackdownTeamManagementMain() {
		Map<String, List<CommonCode>> commonCodes = getCodesOf("FIM054","FIM071","FIM002");

		ModelAndView mav = new ModelAndView("fims/crdn/crdn07010-main")
			.addObject("pageName", "crdn07010")
			.addObject("FIM054List", commonCodes.get("FIM054"))
			.addObject("TaskListForSgg", stngBean.filterTaskSectionCodeForSgg(commonCodes.get("FIM054")))
			.addObject("FIM071List", commonCodes.get("FIM071"))
			.addObject("FIM002List", commonCodes.get("FIM002"));
		addCodes(commonCodes, mav, "FIM054");
		addCodes(commonCodes, mav, "FIM071");
		return addCodes(commonCodes, mav, "FIM002");
	}

	/**단속팀 목록을 조회한다.<br />
	 * {@link CrdnStngService#getTeamList(query)} 참고
	 * @param crdn 단속
	 * @return jsonView
	 */
	@Task({"PVS","BPV"})
	@RequestMapping(name="단속팀 목록 조회", value=METHOD_URL.getTeamList)
	public ModelAndView getTeamList(CrdnQuery query) {
		query.setSggCd(currentUser().getOrgID());
		return setPagingInfo(new ModelAndView("jsonView"), crdnStngService.getTeamList(query),"");
	}

	/**비사용 단속팀 목록을 조회한다.<br />
	 * {@link CrdnStngService#getRemovedTeamList(query)} 참고
	 * @param crdn 단속
	 * @return jsonView
	 */
	@Task({"PVS","BPV"})
	@RequestMapping(name="비사용 단속팀 목록 조회", value=METHOD_URL.getRemovedTeamList)
	public ModelAndView getRemovedTeamList(CrdnQuery query) {
		query.setSggCd(currentUser().getOrgID());

		return setPagingInfo(new ModelAndView("jsonView"), crdnStngService.getRemovedTeamList(query),"");
	}

	/** 단속팀 정보를 삭제한다.
	 * {@link CrdnStngService#removeTeamInfo(exmptnVhclId)} 참고
	 * @param teamId 단속팀 ID
	 * @return jsonView
	 * <pre><code> {
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	@Task({"PVS","BPV"})
	@RequestMapping(name="단속팀 정보 삭제", value=METHOD_URL.removeTeamInfo)
	public ModelAndView removeTeamInfo(String teamId) {
		String rtnMsg = crdnStngService.removeTeamInfo(teamId);
		return new ModelAndView("jsonView")
			.addObject("saved", rtnMsg.contains("[S]"));
	}

	/** 단속팀 정보를 저장한다.
	 * {@link CrdnStngService#saveTeamInfo(team)} 참고
	 * @param team 단속팀 정보
	 * @return jsonView
	 * <pre><code> {
	 *     "saved": 저장되었으면 true, 그렇지 않으면 false
	 * }</code></pre>
	 */
	@Task({"PVS","BPV"})
	@RequestMapping(name="단속팀 정보 저장", value=METHOD_URL.saveTeamInfo)
	public ModelAndView saveTeamInfo(Team team) {
		team.setSggCd(currentUser().getOrgID());
		String rtnMsg = crdnStngService.saveTeamInfo(team);

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

	/**단속팀 정보를 조회한다.<br />
	 * {@link CrdnStngService#getTeamInfo(String)} 참고
	 * @param crdn 단속
	 * @return jsonView
	 */
	@Task({"PVS","BPV"})
	@RequestMapping(name="단속팀 정보 조회", value=METHOD_URL.getTeamInfo)
	public ModelAndView getTeamInfo(String teamId) {
		return new ModelAndView("jsonView")
			.addObject("teamInfo", crdnStngService.getTeamInfo(teamId));
	}
}