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

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.application.dto.input.DormCheckOutDto;
import com.palmnest.application.dto.output.DormRoomerInfoDto;
import com.palmnest.model.dorm.DormRoomerInfo;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.dorm.service.DormCheckOutService;
import com.palmnest.service.dorm.service.DormRoomerService;
import com.palmnest.service.estateserve.ScWxuserManager;
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.List;
import java.util.Map;


@Slf4j
@Controller
@RequestMapping("/wechat/dorm/*")
public class WechatDormCheckOutController {


	@Autowired
	private ScWxuserManager     scWxuserManager;
	@Autowired
	private DormCheckOutService dormCheckOutService;
	@Autowired
	private DormRoomerService   dormRoomerService;

	private String image_server = PropertiesKit.readPropertiesValue("image_server", "wechat.properties");

	/**
	 * 首先判断员工信息是否存在，然后再发送验证码
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getDormAuthCode")
	@ResponseBody
	public AppResultDto getDormAuthCode(HttpServletRequest request, HttpServletResponse response) {

		String userMobile = request.getParameter("userMobile");
		String idCard     = request.getParameter("idCard");
		String openId     = request.getParameter("openId");
		String type       = request.getParameter("type");

		if (StringUtils.isEmpty(userMobile) || StringUtils.isEmpty(idCard)) {
			return AppResultUtil.createErrorDto("100", "身份证号或者手机号为空，无法发送验证码");
		}

		String orgId = request.getParameter("orgId");

		if (StringUtils.isEmpty(orgId)) {
			WxUser user = scWxuserManager.getScWxuserByOpenId(openId);
			if (user != null && user.getOrganizationId() != null) {
				orgId = user.getOrganizationId().toString();
			}
			if (StringUtils.isEmpty(orgId)) {
				return AppResultUtil.createErrorDto("100", "项目不允许为空");
			}
		}

		AppResultDto dto = dormCheckOutService.getDormAuthCode(type, userMobile, idCard, orgId, openId);

		return dto;
	}

	/**
	 * 登录员工信息获取入宿信息
	 */
	@RequestMapping(value = "login")
	@ResponseBody
	public AppResultDto login(HttpServletRequest request, HttpServletResponse response) {

		String userMobile = request.getParameter("userMobile");
		String idCard     = request.getParameter("idCard");
		String code       = request.getParameter("authCode");
		String openId     = request.getParameter("openId");
		String type       = request.getParameter("type");

		if (StringUtils.isEmpty(userMobile) || StringUtils.isEmpty(idCard)) {
			return AppResultUtil.createErrorDto("100", "身份证号或者手机号为空，无法提交");
		}

		String orgId = request.getParameter("orgId");

		if (StringUtils.isEmpty(orgId)) {
			WxUser user = scWxuserManager.getScWxuserByOpenId(openId);
			if (user != null && user.getOrganizationId() != null) {
				orgId = user.getOrganizationId().toString();
			}
			if (StringUtils.isEmpty(orgId)) {
				return AppResultUtil.createErrorDto("100", "项目不允许为空");
			}
		}

		AppResultDto dto = dormCheckOutService.login(type, userMobile, idCard, code, orgId, openId);
		return dto;
	}

	/**
	 * 根据openId获取员工入宿信息
	 *
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "getDormInfoByOpenId")
	@ResponseBody
	public AppResultDto getDormInfoByOpenId(String openId, String orgId) {

		try {

			if (StringUtils.isEmpty(orgId)) {

				WxUser user = scWxuserManager.getScWxuserByOpenId(openId);
				if (user != null && user.getOrganizationId() != null) {
					orgId = user.getOrganizationId().toString();
				}
				if (StringUtils.isEmpty(orgId)) {
					return AppResultUtil.createErrorDto("100", "项目不允许为空");
				}
			}

			DormRoomerInfo dormInfo = dormRoomerService.getDormInfoByOpenId(openId, orgId);

			if (dormInfo == null) {
				return AppResultUtil.createErrorDto("100", "抱歉！没有您的入住信息，请联系部门行政，确认是否已提交入住信息。");
			} else {
				String res = dormInfo.getApartment() + dormInfo.getBuild() + dormInfo.getFloor() + dormInfo.getRoom() + dormInfo.getBedNo();
				return AppResultUtil.createSuccesDto(res);
			}

		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "获取失败:" + e.getMessage());
		}
	}

	/**
	 * 根据openId获取舍友
	 *
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "getRoomerByOpenId")
	@ResponseBody
	public AppResultDto getRoomerByOpenId(String openId, String orgId) {

		try {

			if (StringUtils.isEmpty(orgId)) {

				WxUser user = scWxuserManager.getScWxuserByOpenId(openId);

				if (user != null && user.getOrganizationId() != null) {
					orgId = user.getOrganizationId().toString();
				}

				if (StringUtils.isEmpty(orgId)) {
					return AppResultUtil.createErrorDto("100", "项目不允许为空");
				}
			}

			List<DormRoomerInfoDto> dtoList = dormRoomerService.getRoomerByOpenId(openId, orgId);
			return AppResultUtil.createSuccesDto(dtoList);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return AppResultUtil.createErrorDto("100", "获取失败:" + e.getMessage());
		}
	}

	/**
	 * 申请退宿
	 *
	 * @param checkDto
	 * @return
	 */
	@RequestMapping(value = "applyCheckOut")
	@ResponseBody
	public AppResultDto applyCheckOut(DormCheckOutDto checkDto, MultipartHttpServletRequest request) {

		AppResultDto dto = dormCheckOutService.applyCheckOut(checkDto, request);

		return dto;
	}

	/**
	 * 获取申请信息列表
	 *
	 * @param openId
	 * @return
	 */
	@RequestMapping(value = "getCheckOutList")
	@ResponseBody
	public AppResultDto getCheckOutList(String openId, String orgId) {

		if (StringUtils.isEmpty(orgId)) {

			WxUser user = scWxuserManager.getScWxuserByOpenId(openId);

			if (user != null && user.getOrganizationId() != null) {
				orgId = user.getOrganizationId().toString();
			}

			if (StringUtils.isEmpty(orgId)) {
				return AppResultUtil.createErrorDto("100", "项目不允许为空");
			}
		}

		AppResultDto dto = dormCheckOutService.getCheckOutList(openId, orgId);
		return dto;
	}

	/**
	 * 获取申请退宿单据的详情
	 *
	 * @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 openId
	 * @return
	 */
	@RequestMapping(value = "exitLogin")
	@ResponseBody
	public AppResultDto exitLogin(String openId, String orgId) {

		if (StringUtils.isEmpty(orgId)) {

			WxUser user = scWxuserManager.getScWxuserByOpenId(openId);

			if (user != null && user.getOrganizationId() != null) {
				orgId = user.getOrganizationId().toString();
			}

			if (StringUtils.isEmpty(orgId)) {
				return AppResultUtil.createErrorDto("100", "项目不允许为空");
			}
		}

		String res = dormCheckOutService.exitLogin(openId, orgId);
		if ("SUCCESS".equals(res)) {
			return AppResultUtil.createSuccesDto();
		}
		return AppResultUtil.createErrorDto("100", res);
	}

	/**
	 * 获取二维码信息
	 *
	 * @param id 退宿订单id
	 * @return
	 */
	@RequestMapping(value = "getQRCode")
	@ResponseBody
	public AppResultDto getQRCode(Long id) {
		AppResultDto dto = dormCheckOutService.getQRCode(id);
		return dto;
	}


}
