package com.palmnest.webapp.controller.wechat;

import com.palmnest.application.core.util.DateUtils;
import com.palmnest.service.estateserve.CountServerManager;
import com.palmnest.service.property.PropertyManager;
import org.apache.commons.lang.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.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 统计服务前端控制器
 *
 * @author yubing
 * @since 2017-08-07 16:00:00
 */
@Controller
@RequestMapping(value = "/wechat/countServer*")
public class CountServerController {

	@Autowired
	private CountServerManager countServerManager;

	@Autowired
	private PropertyManager propertyManager;

	/**
	 * 充值业绩统计页面
	 * 各个项目
	 */
	@RequestMapping(value = "/rechargeRank")
	public ModelAndView indexPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model  model          = new ExtendedModelMap();
		String flag           = "defalut";
		String month          = request.getParameter("month");
		String organizationId = request.getParameter("organizationId");
		String keyWords       = request.getParameter("keyWords");
		if (StringUtils.isEmpty(month)) {
			month = DateUtils.getDate("yyyy-MM");
		}

		if (StringUtils.isNotBlank(organizationId)) {
			flag = "search";
			model.addAttribute("organizationId", organizationId);
			model.addAttribute("organizationName", propertyManager.getTbOrganizationById(Integer.parseInt(organizationId)).getName());
		} else {
			model.addAttribute("organizationName", "全部");
		}

		if (StringUtils.isNotBlank(keyWords)) {
			flag = "search";
			model.addAttribute("keyWords", keyWords);
		}

		List<String> monthList = new ArrayList<String>();
		for (int i = -2; i <= 0; i++) {
			monthList.add(DateUtils.getPreMonthDates(new Date(), i));
		}
		model.addAttribute("monthList", monthList);
		model.addAttribute("dataList", countServerManager.querySumRechargeGain(organizationId, month, keyWords));
		model.addAttribute("organizationList", propertyManager.queryTbOrganizationList());
		model.addAttribute("month", month);
		model.addAttribute("flag", flag);
		return new ModelAndView("wechat/countRechargeRank", model.asMap());
	}

	/**
	 * 充值业绩统计页面
	 * 各个项目
	 */
	@RequestMapping(value = "/fansRank")
	public ModelAndView fansRank(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Model  model          = new ExtendedModelMap();
		String flag           = "defalut";
		String organizationId = request.getParameter("organizationId");
		String keyWords       = request.getParameter("keyWords");
		if (StringUtils.isNotBlank(organizationId)) {
			flag = "search";
			model.addAttribute("organizationId", organizationId);
			model.addAttribute("organizationName", propertyManager.getTbOrganizationById(Integer.parseInt(organizationId)).getName());
		} else {
			model.addAttribute("organizationName", "全部");
		}

		if (StringUtils.isNotBlank(keyWords)) {
			flag = "search";
			model.addAttribute("keyWords", keyWords);
		}
		model.addAttribute("dataList", countServerManager.queryCountOrganizationFans(organizationId, keyWords));
		model.addAttribute("organizationList", propertyManager.queryTbOrganizationList());
		model.addAttribute("flag", flag);
		return new ModelAndView("wechat/countFansRank", model.asMap());
	}

}
