package com.palmnest.webapp.controller.dorm;

import com.palmnest.application.AppConstants;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.OrgDto;
import com.palmnest.application.dto.WebPageResultDto;
import com.palmnest.application.dto.input.DormRoomerListDto;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.dorm.service.DormCheckOutService;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/admin/dorm/*")
public class DormCheckOutController {


	@Autowired
	private AppUserService      appUserService;
	@Autowired
	private DormCheckOutService dormCheckOutService;

	/**
	 * 获取退宿审核管理列表
	 *
	 * @param dormDto  查询参数
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getDormCheckOutList")
	@ResponseBody
	public WebPageResultDto getDormCheckOutList(DormRoomerListDto dormDto, int page, int limit, HttpServletRequest request, HttpServletResponse response) throws Exception {
		User         user   = (User) request.getSession().getAttribute("UserCache");
		List<OrgDto> dtos   = appUserService.getUserOrgs(user.getId());
		String       orgIds = "";
		try {
			//判断传过来的项目存不存在？如果存在就直接用传过来的参数，如果没有就用登录用户所管辖的项目
			if (StringUtils.isEmpty(dormDto.getOrgId()) || "0".equals(dormDto.getOrgId())) {
				if (dtos != null && dtos.size() > 0) {
					for (int i = 0; i < dtos.size(); i++) {
						OrgDto dto = dtos.get(i);
						orgIds = orgIds + dto.getId() + ",";
					}
				}
			}
			//多个项目id，以逗号分隔，用于sql中查询对应的项目下的数据 org in (1,2,3)
			if (StringUtils.isNotEmpty(orgIds)) {
				//去除最后面一个逗号
				orgIds = orgIds.substring(0, orgIds.length() - 1);
				dormDto.setOrgId(orgIds);
			}
			//查询退宿申请列表
			Page dormPage = dormCheckOutService.getDormCheckOutList(dormDto, page, limit);
			//PC端列表界面返回WebPageResultDto格式的数据
			WebPageResultDto dto = new WebPageResultDto(AppConstants.SUCCESS_CODE, "成功", dormPage.getItems());
			dto.setCount(dormPage.getTotalCount());
			return dto;
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return new WebPageResultDto("100", "查询列表失败:" + e.getMessage(), null);
		}
	}

	/**
	 * 获取申请退宿单据的详情
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "getDormRoomerInfo")
	@ResponseBody
	public AppResultDto getDormRoomerInfo(Long id) {

		Map<String, Object> map = dormCheckOutService.getDormCheckById(id);

		if (map == null) {
			return AppResultUtil.createErrorDto("100", "获取不到该单据");
		}

		return AppResultUtil.createSuccesDto(map);
	}

	/**
	 * 审批退宿申请单
	 *
	 * @param id               申请单id
	 * @param status（1审核通过2拒绝）
	 * @param reason           审核意见
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "auditDormCheckOut")
	@ResponseBody
	public AppResultDto auditDormCheckOut(Long id, String status, String reason, MultipartHttpServletRequest request, HttpServletResponse response) {

		User user = (User) request.getSession().getAttribute("UserCache");

		String userId = "";
		if (user != null) {
			userId = user.getId().toString();
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		map.put("status", status);
		map.put("reason", reason);
		map.put("userId", userId);

		AppResultDto dto = dormCheckOutService.auditDormCheckOut(map, request);
		return dto;
	}
}
