package com.palmnest.webapp.controller.notice;

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

import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.superdesk.libs.common.json.JsonUtils;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.input.ApprovalSetDto;
import com.palmnest.model.User;
import com.palmnest.service.notice.AppNoticeManager;
import com.palmnest.service.notice.NoticeApprovalsetManager;
import com.palmnest.webapp.controller.BaseFormController;


@Slf4j
@Controller
@RequestMapping("notice/approval*")
public class NoticeApprovalController extends BaseFormController  {
	@Autowired
	private NoticeApprovalsetManager noticeApprovalsetManager;
	@Autowired
	private AppNoticeManager      appNoticeManager;
	
	
	/**
	 * 获取APP公告审核角色
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getApprovalRole")
	@ResponseBody
	public AppResultDto getApprovalRole(Integer orgId) {
		log.info("NoticeApprovalController-getApprovalRole-orgId:{}",orgId);
		if(null == orgId){
			return AppResultUtil.createErrorDto("500", "请先选择项目");
		}
		List<Map<String,Object>> list=appNoticeManager.getApprovalRole(orgId, CodeConstants.AUDIT_NOTICE_PERMISSION);
		log.info("NoticeApprovalController-getApprovalRole-list:{}",JsonUtils.toJson(list));
		return AppResultUtil.createSuccesDto(list);
	}
	
	/**
	 * 获取项目是否开启了多级审核功能；0-未开启；1-开启；
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSetByOrgId")
	@ResponseBody
	public AppResultDto getSetByOrgId(Integer orgId) {
		log.info("NoticeApprovalController-getSetByOrgId-orgId:{}",orgId);
		if(null == orgId){
			return AppResultUtil.createErrorDto("500", "请先选择项目");
		}
		Map<String,Object> map=noticeApprovalsetManager.getSetByOrgId(orgId);
		log.info("NoticeApprovalController-getSetByOrgId-map:{}",JsonUtils.toJson(map));
//		int isOpen=0;
//		if(null != map && map.containsKey("isOpen") && null != map.get("isOpen") && StringUtils.isNotEmpty(map.get("isOpen").toString()) && 1 == Integer.parseInt(map.get("isOpen").toString())){
//			isOpen=1;
//		}
//		log.info("NoticeApprovalController-getSetByOrgId-isOpen:{}",isOpen);
		return AppResultUtil.createSuccesDto(map);
	}
	
	
	/**
	 * 保存项目设置多级审核信息
	 * @param request
	 * @param dto
	 * @return
	 */
	@RequestMapping(value = "/saveApprovalSet", method = RequestMethod.POST, produces = "application/json")
	@ResponseBody
	public AppResultDto saveApprovalSet(@RequestBody ApprovalSetDto dto,HttpServletRequest request) {
		log.info("NoticeApprovalController-saveApprovalSet-dto:{}",JsonUtils.toJson(dto));
		User   user      = (User) request.getSession().getAttribute("UserCache");
		if(null == dto || null == dto.getOrgId()){
			return AppResultUtil.createErrorDto("500", "请先选择项目");
		}
		return noticeApprovalsetManager.saveApprovalSet(dto, Integer.parseInt(user.getId().toString()));
	}
	
	
	
	/**
	 * 获取项目开启多级审核信息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getApprovalSetDto")
	@ResponseBody
	public AppResultDto getApprovalSetDto(Integer orgId) {
		log.info("NoticeApprovalController-getApprovalSetDto-orgId:{}",orgId);
		if(null == orgId){
			return AppResultUtil.createErrorDto("500", "请先选择项目");
		}
		return noticeApprovalsetManager.getApprovalSetDto(orgId);
	}
	
	/**
	 * 获取项目开启多级审核信息
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getApprovalType")
	@ResponseBody
	public AppResultDto getApprovalType(Integer orgId) {
		log.info("NoticeApprovalController-getApprovalType-orgId:{}",orgId);
		if(null == orgId){
			return AppResultUtil.createErrorDto("500", "请先选择项目");
		}
		return AppResultUtil.createSuccesDto(noticeApprovalsetManager.getApprovalTypeList(orgId));
	}
	
	

}
