package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.core.util.PropertiesKit;
import com.palmnest.model.InPuchaseUser;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.estateserve.NeigouManager;
import com.palmnest.service.estateserve.ScWxuserManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * 服务之窗-企业内购;此控制器针对在服务之窗服务处配置的企业内购入口;
 *
 * @author yubing
 * @since 2017-08-07 16:00:00
 */
@Slf4j
@Controller
@RequestMapping(value = "/wechat/assets*")
public class AssetsController {


	@Autowired
	private NeigouManager   neigouManager;
	@Autowired
	private ScWxuserManager scWxuserManager;
	@Autowired
	private AppUserManager  appUserManager;

	/**
	 * 进驻首页页面
	 */
	@RequestMapping(value = "/indexPage")
	public ModelAndView indexPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model  model      = new ExtendedModelMap();
		String openId     = request.getParameter("openId");
		String developId  = request.getParameter("developId");
		String userId     = request.getParameter("userId");
		String pageType   = request.getParameter("pageType");
		String userMobile = request.getParameter("userMobile");// 用户手机号
		String virId      = request.getParameter("virId");// 企业id
		String orgId      = request.getParameter("orgId");// 项目id

		log.info("openId:" + openId);
		log.info("developId:" + developId);
		log.info("userId:" + userId);
		log.info("pageType:" + pageType);
		log.info("userMobile:" + userMobile);
		log.info("virId:" + virId);
		log.info("orgId:" + orgId);

		if (StringUtils.isAnyBlank(openId, developId)) {
			return null;
		}

		// 根据openId和developId查询当前微信用户的企业购登录记录信息
		InPuchaseUser puchaseUser = neigouManager.getPuchaseUser(openId, developId, null);
		if (StringUtils.isAnyBlank(virId, orgId)) {
			List<Map<String, Object>> list = neigouManager.getVirListByUser(puchaseUser);

			if (null == list) {
				log.info("============= 企业列表为空 =============");
				return null;
			}

			if (list.size() > 1) {// 如果有多个企业,则去选择企业列表页
				return new ModelAndView("redirect:/wechat/index/toSelectCompanyPurchase?openId=" + openId + "&developId=" + developId);
			} else if (list.size() == 1) {// 如果只有一个企业,那么直接进首页
				orgId = list.get(0).get("organizationId") + "";
				virId = list.get(0).get("virId") + "";
				userId = list.get(0).get("userId") + "";
				//设置用户的wxUser-openId
				WxUser wxUser = scWxuserManager.getScWxuserByOpenId(openId);
				if (wxUser != null) {
					AppUser appUser = appUserManager.get(Integer.parseInt(userId));
					appUser.setWxUserId(wxUser.getId());
					appUser.setModifyTime(new Date());
					appUserManager.updateModel(appUser);
				}
			}
		}
		if (StringUtils.isBlank(pageType)) {
			pageType = "innerShoppingMall";
		}
		String redirectUrl = PropertiesKit.readPropertiesValue(pageType, "wechat.properties");
		//进雨那里有动态pageType需求 这里得不到redirectUrl 直接使用pageType value 并encode
		if (StringUtils.isEmpty(redirectUrl) && StringUtils.isNotBlank(pageType)) {
			redirectUrl = pageType;
		}

		if (redirectUrl.indexOf("?") != -1) {
//			redirectUrl+="&userMobile="+puchaseUser.getMobile()+"&virId="+virId+"&orgId="+orgId+"&userId="+userId;
			redirectUrl += "&userMobile=" + puchaseUser.getMobile() + "&core_enterprise=" + virId + "&core_organization=" + orgId + "&core_user=" + userId;
		} else {
//			redirectUrl+="?userMobile="+puchaseUser.getMobile()+"&virId="+virId+"&orgId="+orgId+"&userId="+userId;
			redirectUrl += "?userMobile=" + puchaseUser.getMobile() + "&core_enterprise=" + virId + "&core_organization=" + orgId + "&core_user=" + userId;
		}

		log.info("redirectUrl:" + redirectUrl);

		model.addAttribute("redirectFullUrl", redirectUrl);
		return new ModelAndView("wechat/toWechatAuth", model.asMap());
	}


}
