package com.palmnest.webapp.controller.notice;

import com.alibaba.fastjson.JSONObject;
import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.service.notice.AppNoticeManager;
import com.palmnest.service.notice.EmailSendManager;
import com.palmnest.service.notice.PhoneMessageManager;
import com.palmnest.service.notice.WxtemplateMessageManager;
import com.palmnest.webapp.controller.BaseFormController;

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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/notice/publicNotice*")
public class PublicNoticeController extends BaseFormController {


	@Autowired
	private PhoneMessageManager      phoneMessageManager;
	@Autowired
	private WxtemplateMessageManager wxtemplateMessageManager;
	@Autowired
	private EmailSendManager         emailSendManager;
	@Autowired
	private AppNoticeManager         appNoticeManager;

	/**
	 * 获取消息列表
	 *
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getList")
	public void getList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject json        = new JSONObject();
		String     c_beginTime = request.getParameter("c_beginTime");
		String     c_endTime   = request.getParameter("c_endTime");
		String     a_beginTime = request.getParameter("a_beginTime");
		String     a_endTime   = request.getParameter("a_endTime");
		String     state       = request.getParameter("state");
		String     pageNo      = request.getParameter("page");
		if (StringUtils.isEmpty(pageNo)) {
			pageNo = "1";
		}
		String pageSize = request.getParameter("limit");
		if (StringUtils.isEmpty(pageSize)) {
			pageSize = "10";
		}
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(c_beginTime)) {
			map.put("c_beginTime", c_beginTime);
		}
		if (!StringUtils.isEmpty(c_endTime)) {
			map.put("c_endTime", c_endTime);
		}
		if (!StringUtils.isEmpty(a_beginTime)) {
			map.put("a_beginTime", a_beginTime);
		}
		if (!StringUtils.isEmpty(a_endTime)) {
			map.put("a_endTime", a_endTime);
		}
		if (!StringUtils.isEmpty(state)) {
			map.put("state", state);
		}
		try {
			String type = request.getParameter("type");
			//List<PublicMsgModel> publicMsgs = new ArrayList<PublicMsgModel>();

			Page page = null;
			if (type.equals("app")) {
				page = appNoticeManager.getDataList(map, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
				//List<AppNoticeModel> ls = page.getItems();

			}
			if (type.equals("wx")) {
				page = wxtemplateMessageManager.getDataList(map, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			}
			if (type.equals("msg")) {
				page = phoneMessageManager.getDataList(map, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			}
			if (type.equals("email")) {
				page = emailSendManager.getDataList(map, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			}

			json.put("code", "0");
			json.put("data", page.getItems());
			json.put("msg", "");
			json.put("count", page.getTotalCount());
			StringUtil.charSet(json, request, response);
		} catch (Exception e) {
			json.put("code", "1");
			json.put("data", "");
			json.put("msg", e.getMessage());
			StringUtil.charSet(json, request, response);
			log.error(e.getMessage());
			throw new Exception(e);
		}


	}

}
