package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.dto.CommonDto;
import com.palmnest.application.dto.input.EmigrationInputDto;
import com.palmnest.model.EnterpriseInfo;
import com.palmnest.model.VirArchModel;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.estateserve.EmigrationManager;
import com.palmnest.service.estateserve.EnterpriseManager;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.VirArchModelManager;
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.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 企业迁出微信端控制器
 *
 * @author longmeizhen
 */
@Slf4j
@Controller
@RequestMapping(value = "/wechat/emig/*")
public class WechatEmigrationController {


	@Autowired
	private VirArchModelManager virArchModelManager;
	@Autowired
	private ScWxuserManager     scWxuserManager;
	@Autowired
	private EmigrationManager   emigrationManager;
	@Autowired
	private EnterpriseManager   enterpriseManager;

	/**
	 * 获取企业入驻配置模板
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getVirModel")
	@ResponseBody
	public AppResultDto getVirModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int type = 0;
		if (StringUtils.isNotEmpty(request.getParameter("type"))) {
			type = Integer.parseInt(request.getParameter("type"));
		}
		String             orgId        = request.getParameter("orgId");
		List<VirArchModel> virModelList = virArchModelManager.getVirArchModelByType(type, orgId);
		return AppResultUtil.createSuccesDto(virModelList);
	}

	/**
	 * 登录微信用户根据userId查找对应的user,然后根据user查该用户负责的企业
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getVirInfoList")
	@ResponseBody
	public AppResultDto getVirInfoList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String openId = request.getParameter("openId");
		WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
		if (wxUser != null) {
			int             userId  = wxUser.getUserId();
			List<CommonDto> listDto = emigrationManager.getVirListByReman(userId);
			return AppResultUtil.createSuccesDto(listDto);
		}
		return AppResultUtil.createSuccesDto();
	}

	/**
	 * 根据企业ID获取企业位置，负责人，楼栋信息
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getEmigInfoByVirId")
	@ResponseBody
	public AppResultDto getEmigInfoByVirId(HttpServletRequest request, HttpServletResponse response) {
		String virId = request.getParameter("virId");
		if (StringUtils.isNotEmpty(virId)) {
			try {
				Map<String, Object> map = emigrationManager.getEmigInfoByVirId(virId);
				return AppResultUtil.createSuccesDto(map);
			} catch (Exception e) {
				e.printStackTrace();
				log.error(e.getMessage());
				return AppResultUtil.createErrorDto("100", "获取企业相关信息失败：" + e.getMessage());
			}
		}
		return AppResultUtil.createErrorDto("100", "传输的数据异常");
	}

	/**
	 * 保存企业迁出
	 *
	 * @return
	 */
	@RequestMapping(value = "saveEmigration")
	@ResponseBody
	public AppResultDto saveEmigration(@ModelAttribute("emigInputDto") EmigrationInputDto emigInputDto, HttpServletRequest request, HttpServletResponse response) throws Exception {

		//校验该企业是否已经发起过迁出,如果有则拒绝再次请求  1-有待迁出单待处理 2-该企业已经迁出成功
		boolean b = emigrationManager.checkHasUnhandleOrder(emigInputDto.getVirId());
		if (b) {
			return AppResultUtil.createErrorDto("100", "该企业已发起过迁出申请");
		}
		//根据传过来的企业ID，找到对应的企业，然后将企业中的值放到对应的企业迁出值中
		String openId = request.getParameter("openId");
		WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
		String userId = String.valueOf(wxUser.getId());//微信用户的id
		try {
			emigInputDto.setEmigrationReason(0);
			emigInputDto.setEmigrationType(0);
			EnterpriseInfo enterInfo = emigrationManager.saveEnterInfo(emigInputDto, Integer.parseInt(userId), "1");//newWay 1-微信端 2-app
			if (enterInfo != null) {
				AppResultDto dto = AppResultUtil.createSuccesDto(enterInfo.getId());
				if ("200".equals(dto.getCode())) {
					enterpriseManager.sendMessageByCusService(enterInfo);
				}
				return dto;
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return AppResultUtil.createErrorDto("100", "保存企业迁出失败：" + e.getMessage());
		}
		return AppResultUtil.createSuccesDto("没有权限", null);
	}
}
