package com.palmnest.webapp.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.CodeConstants;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.NumberUtils;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.model.estateserve.SmsAuthCode;
import com.palmnest.model.estateserve.WxUser;
import com.palmnest.service.estateserve.ScWxuserManager;
import com.palmnest.service.estateserve.SmsAuthCodeManager;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 验证码控制前
 *
 * @author yubing
 * @since 2017-1-16
 */
@Controller
@RequestMapping(value = "/wechat/sms*")
public class SmsAuthCodeController {

	@Autowired
	private SmsAuthCodeManager smsAuthCodeManager;
	@Autowired
	private ScWxuserManager    scWxuserManager;

	/**
	 * 获取验证码接口
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getAuthCode", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	String authCode(HttpServletRequest request, HttpServletResponse response) {

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

		String result = "Error";

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

		if (StringUtils.isNotBlank(userMobile)) {
			String code    = NumberUtils.getValidateCode();
			String content = "您的验证码为" + code + "，15分钟内有效，如非本人操作，请忽略。";
			//发送短信
//			result=smsAuthCodeManager.sendSms(userMobile, content);
			result = smsAuthCodeManager.smsSendByOrgId(orgId, content, userMobile, code, CodeConstants.SMS_CODE_TYPE);
		}
		return result;
	}

	/**
	 * 获取验证码接口
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getSunMeiAuthCode", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	void getSunMeiAuthCode(HttpServletRequest request, HttpServletResponse response) {

		JSONObject json = new JSONObject();

		String userMobile  = request.getParameter("userMobile");
		String sendContent = request.getParameter("content");
		String result      = "Error";
		if (StringUtils.isNotBlank(userMobile)) {
			String code    = NumberUtils.getValidateCode();
			Map    codeMap = new HashMap();
			codeMap.put("code", code);
			json.put("data", codeMap);

			String content = "您的验证码为" + code + "，15分钟内有效，如非本人操作，请忽略。";
			if (StringUtils.isNotEmpty(sendContent)) {
				content = sendContent;
			}
			//发送短信
//			result=smsAuthCodeManager.sendSms(userMobile, content);
			result = smsAuthCodeManager.smsSend(userMobile, content);
			if ("Sucess".equals(result.trim())) {
				SmsAuthCode model = new SmsAuthCode();
				model.setCode(code);
				model.setUserMobile(userMobile);
				model.setType("1");
				model.setCreateTime(new Date());
				model.setIsEnabled(ElementConstant.YESORNO_1);
				smsAuthCodeManager.saveModel(model);

				json.put("code", 200);
				json.put("msg", "发送验证码成功");

			} else {
				json.put("code", 500);
				json.put("msg", "发送验证码失败");
			}
		}
		StringUtil.charSet(json, request, response);
		;
	}


	/**
	 * 获取验证码接口 新
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getAuthCodeNew", method = {RequestMethod.GET, RequestMethod.POST})
	public @ResponseBody
	AppResultDto authCodeNew(HttpServletRequest request, HttpServletResponse response) {
		String userMobile = request.getParameter("userMobile");
		String orgId      = request.getParameter("orgId");
		String openId     = request.getParameter("openId");
		String result     = "Error";
		if (StringUtils.isEmpty(orgId) || "null".equals(orgId)) {
			WxUser user = scWxuserManager.getScWxuserByOpenId(openId);
			if (user != null && user.getOrganizationId() != null) {
				orgId = user.getOrganizationId().toString();
			}
			if (StringUtils.isEmpty(orgId) || "null".equals(orgId)) {
				return AppResultUtil.createErrorDto("100", "项目不允许为空");
			}
		}

		if (StringUtils.isNotBlank(userMobile)) {
			String code    = NumberUtils.getValidateCode();
			String content = "您的验证码为" + code + "，15分钟内有效，如非本人操作，请忽略。";
			//发送短信
//			result=smsAuthCodeManager.sendSms(userMobile, content);
			result = smsAuthCodeManager.smsSendByOrgId(orgId, content, userMobile, code, CodeConstants.SMS_CODE_TYPE);
		}
		return AppResultUtil.createSuccesDto();
	}

}
