package cokr.xit.fims.base.web;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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.service.CodeQuery;
import cokr.xit.base.code.service.bean.CodeBean;
import cokr.xit.fims.base.service.bean.OgdpBean;
import cokr.xit.fims.cmmn.CmmnQuery;
import cokr.xit.fims.mngt.service.bean.TaskBean;
import cokr.xit.foundation.UserInfo;
import cokr.xit.foundation.data.DataObject;

@Controller
public class CodeController extends cokr.xit.base.code.web.CodeController {
	@Resource(name = "codeBean")
	private CodeBean codeBean;
	@Resource(name = "taskBean")
	private TaskBean taskBean;
	@Resource(name = "ogdpBean")
	private OgdpBean ogdpBean;

	@RequestMapping(name="모든 업무구분코드 조회",value="/getAllTaskSeCd.do")
	public ModelAndView getAllTaskSeCd() {
		CodeQuery codeQuery = new CodeQuery()
			.setGroupIDs("FIM054");
		List<DataObject> taskList = codeBean.getCodeList(codeQuery);
		Map<String,String> allTask = taskList.stream()
			.collect(Collectors.toMap((item) -> item.string("CODE"), (item) -> item.string("CODE_VAL")));

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

	@RequestMapping(name="시군구 업무 설정 유무 조회", value="/sggHasTask.do")
	public ModelAndView sggHasTask() {
		ModelAndView mav = new ModelAndView("jsonView");

		UserInfo userInfo = currentUser();
		if(userInfo.getInstitute().equals("ADMIN00")) {
			mav.addObject("sggHasTask", "Y");
		} else {
			ogdpBean.initUserInfo(userInfo);

			String sggCd = (String)userInfo.getInfo().get("sggCd");

			CmmnQuery req = new CmmnQuery()
				.setUseYn("Y")
				.setSggCd(sggCd);
			List<DataObject> result = taskBean.getTasks(req);
			mav.addObject("sggHasTask", !isEmpty(result) ? "Y" : "N");
		}

		return mav;
	}
}