package com.palmnest.webapp.controller.dorm.api;

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.input.DormRoomerListDto;
import com.palmnest.model.User;
import com.palmnest.service.AppUserService;
import com.palmnest.service.UserManager;
import com.palmnest.service.dorm.service.DormCheckOutService;
import com.palmnest.service.redis.BaseRedisService;
import lombok.extern.slf4j.Slf4j;
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("/api/dorm/*")
public class ApiDormCheckOutController {


	@Autowired
	private AppUserService      appUserService;
	@Autowired
	private DormCheckOutService dormCheckOutService;
	@Autowired
	private BaseRedisService    baseRedisService;
	@Autowired
	private UserManager         userManager;
	//审核列表

	/**
	 * 获取退宿申请信息列表
	 *
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "getCheckOutList")
	@ResponseBody
	public AppResultDto getCheckOutList(HttpServletRequest request, HttpServletResponse response, int pageNo, int pageSize) {

		String userName = baseRedisService.get(request.getHeader("token"));

		if (userName == null) {
			return AppResultUtil.createErrorDto(AppConstants.TOKEN_EXPIRED, "token已过期");
		}

		User user = userManager.getUserByUsername2(userName);

		List<OrgDto> dtos = appUserService.getUserOrgs(user.getId());

		String orgId = "";

		if (dtos != null && dtos.size() > 0) {
			OrgDto dto = dtos.get(0);
			orgId = dto.getId().toString();
		}

		// 状态( 1待审核 2待放行 3已放行 4已拒绝 5全部 6放行已拒绝)
		String status = request.getParameter("status");

		try {
			DormRoomerListDto dto = new DormRoomerListDto();
			dto.setOrgId(orgId);
			dto.setStatus(status);
			Page page = dormCheckOutService.getDormCheckOutList(dto, pageNo, pageSize);

			return AppResultUtil.createSuccesDto(page);

		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "查询列表失败:" + e.getMessage());
		}

	}
	//审核详情

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

		String userName = baseRedisService.get(request.getHeader("token"));

		if (userName == null) {
			return AppResultUtil.createErrorDto(AppConstants.TOKEN_EXPIRED, "token已过期");
		}

		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, String recycledGoods, MultipartHttpServletRequest request) {

		String userName = baseRedisService.get(request.getHeader("token"));

		if (userName == null) {
			return AppResultUtil.createErrorDto(AppConstants.TOKEN_EXPIRED, "token已过期");
		}

		User user = userManager.getUserByUsername2(userName);

		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);
		map.put("recycledGoods", recycledGoods);

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

		return dto;
	}

}
