package com.palmnest.webapp.controller.api;

import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.model.estateserve.AppUser;
import com.palmnest.service.estateserve.AppUserManager;
import com.palmnest.service.notice.EmailSendManager;
import com.palmnest.service.notice.PhoneMessageManager;
import com.palmnest.service.notice.WxtemplateMessageManager;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * @Author hxt
 * @Description 公共消息发送接口(短信 / 邮件 / 公众号模板消息等)
 * @Date 2019/1/18 17:02
 **/
@Slf4j
@Controller
@RequestMapping("/appweb/message")
public class ApiPublicPushMessageController {


	@Autowired
	private PhoneMessageManager      phoneMessageManager;
	@Autowired
	private EmailSendManager         emailSendManager;
	@Autowired
	private WxtemplateMessageManager wxtemplateMessageManager;
	@Autowired
	private AppUserManager           appUserManager;

	@RequestMapping(value = "/sendMessage")
	@ResponseBody
	public AppResultDto sendMessage(HttpServletRequest request) {

		String msgType    = request.getParameter("msgType");// 消息类型
		String msgContent = request.getParameter("msgContent");// 消息内容
		String userIds    = request.getParameter("userIds");// 推送用户id,分号分割

		if (StringUtils.isAnyBlank(msgType, msgContent, userIds)) {
			return AppResultUtil.createErrorDto("100", "必填参数为空");
		}

		// 获取用户列表
		List<AppUser> users = appUserManager.getAppUserByIds(StringUtils.join(userIds.split(";"), ","));

		if (ElementConstant.MSG_SMS_TYPE.equals(msgType)) {// 短信消息

			phoneMessageManager.sendMessage(users, msgContent);
		} else if (ElementConstant.MSG_EMAIL_TYPE.equals(msgType)) {// 邮件消息

			emailSendManager.sendMessage(users, msgContent);
		} else if (ElementConstant.MSG_WXTEMP_TYPE.equals(msgType)) {// 微信模板消息

			String templateId      = request.getParameter("templateId");// 模板id
			String templateContent = request.getParameter("templateContent");// 模板内容,json格式
			if (StringUtils.isAnyBlank(templateContent, templateId)) {
				return AppResultUtil.createErrorDto("100", "必填参数为空");
			}

//            wxtemplateMessageManager.sendTemplate(users,templateId,templateContent);

		}


		return AppResultUtil.createSuccesDto();
	}
}
