package com.palmnest.webapp.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.palmnest.application.Constants;
import com.palmnest.application.bean.ReturnObject;
import com.palmnest.application.core.util.LogExceptionStackUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.core.util.WxConfigUtil;
import com.palmnest.model.estateserve.WxPublicno;
import com.palmnest.service.estateserve.PublicNoManager;
import com.palmnest.service.estateserve.ReserveManager;
import com.palmnest.service.redis.BaseRedisService;

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.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Map;

/**
 * 预约记录前端处理控制器
 *
 * @author yubing
 * @since 2017-1-14
 */
@Controller
@RequestMapping(value = "/wechat/reserve*")
public class ReserveController {

	@Autowired
	private ReserveManager reserveManager;

	@Autowired
	private PublicNoManager publicNoManager;
	@Autowired
	private BaseRedisService baseRedisService;

	/**
	 * 预约水服务
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "reserveWather", method = {RequestMethod.POST, RequestMethod.GET})
	public @ResponseBody
	String reserveWather(HttpServletRequest request, HttpServletResponse response) {

		ReturnObject rt = new ReturnObject();

		String str       = "";
		String telePhone = request.getParameter("telePhone");
		String userName  = request.getParameter("userName");

		userName = StringUtil.getUTF_8(userName);

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

		if (StringUtils.isNotBlank(telePhone)) {
			String retCode = reserveManager.saveReserveRecordWather(telePhone, userName, type);
			if ("success".equals(retCode)) {
				rt.setResultCode(Constants.RESULTCODE_SUCC);
			} else {
				rt.setResultCode(Constants.RESULTCODE_NODATA);
			}
			rt.setResult(Constants.RESULT_SUCC);
			rt.setResultDesc("success");
		} else {
			rt.setResult(Constants.RESULT_FIAL);
			rt.setResultCode(Constants.RESULTCODE_PARAMINVALID);
			rt.setResultDesc("please check params.");
		}
		str = JSONObject.toJSONString(rt, SerializerFeature.WriteMapNullValue);
		if (StringUtils.isNotBlank(request.getParameter("callback"))) {
			return request.getParameter("callback") + "(" + str + ")";
		}
		return str;
	}

	/**
	 * 异步注入分享接口
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/fenxiang", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json;charset=UTF-8")
	public @ResponseBody
	String fenxiang(HttpServletRequest request, HttpServletResponse response) {
		ReturnObject rt  = new ReturnObject();
		String       str = "";
		try {
			String              developId  = request.getParameter("developId");
			String              url        = request.getParameter("url");
//			String              jsapi_tike = Constants.CacheMap.get("jsapi_" + developId);
			String              jsapi_tike = baseRedisService.get(Constants.JSAPI+developId);
			WxPublicno          wxPublicno = publicNoManager.getByDevelopId(developId);
			Map<String, String> wxMap      = WxConfigUtil.sign(jsapi_tike, url);
			wxMap.put("appid", wxPublicno.getAppId());
			rt.setResultCode(Constants.RESULTCODE_SUCC);
			rt.setReturnDTO(wxMap);
		} catch (Exception e) {
			LogExceptionStackUtil.LogExceptionStack(e);
			rt.setResult(Constants.RESULT_FIAL);
			rt.setResultCode(Constants.RESULTCODE_PARAMINVALID);
			rt.setResultDesc("程序异常！");
			rt.setReturnDTO(null);
		}
		str = JSONObject.toJSONString(rt, SerializerFeature.WriteMapNullValue);
		if (StringUtils.isNotBlank(request.getParameter("callback"))) {
			return request.getParameter("callback") + "(" + str + ")";
		}
		return str;

	}

}
