package com.iamberry.wechat.handles.statistics;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.iamberry.wechat.core.entity.drp.PlaceRoleInfo;
import com.iamberry.wechat.core.entity.statictics.ActivateInfo;
import com.iamberry.wechat.core.entity.statictics.AttentionInfo;
import com.iamberry.wechat.core.entity.statictics.EarningsInfo;
import com.iamberry.wechat.core.entity.statictics.MarketInfo;
import com.iamberry.wechat.core.entity.statictics.StatisticsInfo;
import com.iamberry.wechat.core.entity.statictics.UerActivateInfo;
import com.iamberry.wechat.face.statistics.AdminStatisticsService;

@Controller
@RequestMapping("/admin/statistics")
public class AdminStatisticsHandler {

	@Autowired
	private AdminStatisticsService adminStatisticsService;

	/**
	 * 查询门店关注人数等报表数据
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectStoreAttentionList")
	public ModelAndView selectStoreAttentionList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/statistics/attentionList");
		String text = request.getParameter("text");
		StringBuilder url = new StringBuilder("/admin/statistics/selectStoreAttentionList?pageSize=" + pageSize);
		AttentionInfo attentionInfo = new AttentionInfo();
		PlaceRoleInfo placeRoleInfo = new PlaceRoleInfo();
		placeRoleInfo.setRoleName("门店");
		List<PlaceRoleInfo> roleList = adminStatisticsService.selectRoleName(placeRoleInfo);
		List<Integer> roles = new ArrayList<Integer>();
		if (null != roleList && roleList.size() > 0) {
			for (PlaceRoleInfo placeRole : roleList) {
				roles.add(placeRole.getRoleId());
			}
			attentionInfo.setRoleList(roles);
		}
		if (text != null && !"".equals(text)) {
			attentionInfo.setStoreName(text);
			url.append("&text=" + text);
			mv.addObject("text", text);
		}
		url.append("&pageNO=");
		attentionInfo.getPage().setPageSize(pageSize);
		attentionInfo.getPage().setPageNumber(pageNO);
		attentionInfo.getPage().initRecordBegin();
		List<AttentionInfo> attentionList = adminStatisticsService.selectStoreAttentionList(attentionInfo);
		//查询所有商户层关系信息
		List<MarketInfo> placeList = adminStatisticsService.selectPlaceList();
		if (null != attentionList && attentionList.size() > 0 && null != placeList && placeList.size() > 0) {
			for (AttentionInfo attentionInfos : attentionList) {
				for (MarketInfo marketInfo : placeList) {
					if (null != attentionInfos.getPlaceId() && !"".equals(attentionInfos.getPlaceId()) 
							&& null != marketInfo.getPlaceId() && !"".equals(marketInfo.getPlaceId()) 
							&& attentionInfos.getPlaceId().equals(marketInfo.getPlaceId())) {
						attentionInfos.setProvinceName(marketInfo.getProvinceName());
						attentionInfos.setCityName(marketInfo.getCityName());
						attentionInfos.setStoreName(marketInfo.getStoreName());
					}
				}
			}
		}
		mv.addObject("attentionList", attentionList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", adminStatisticsService.selectStoreAttentionListCount(attentionInfo));
		mv.addObject("url", url.toString());
		return mv;
	}
	
	/**
	 * 分页查询省代收益报表数据
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectProvinceEarningsList")
	public ModelAndView selectProvinceEarningsList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/statistics/earningsList");
		StringBuilder url = new StringBuilder("/admin/statistics/selectProvinceEarningsList?pageSize=" + pageSize);
		String text = request.getParameter("text");
		EarningsInfo earningsInfo = new EarningsInfo();
		
		if (text != null && !"".equals(text)) {
			earningsInfo.setProvinceName(text);
			url.append("&text=" + text);
			mv.addObject("text", text);
		}
		
		url.append("&pageNO=");
		earningsInfo.getPage().setPageSize(pageSize);
		earningsInfo.getPage().setPageNumber(pageNO);
		earningsInfo.getPage().initRecordBegin();
		List<EarningsInfo> earningsList = adminStatisticsService.selectProvinceEarningsList(earningsInfo);
		mv.addObject("earningsList", earningsList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", adminStatisticsService.selectProvinceEarningsListCount(earningsInfo));
		mv.addObject("url", url.toString());
		return mv;
	}
	
	/**
	 * 商户激活信息列表
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectActivateList")
	public ModelAndView selectActivateList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/statistics/activateList");
		StringBuilder url = new StringBuilder("/admin/statistics/selectActivateList?pageSize=" + pageSize + "&pageNO=");
		ActivateInfo activateInfo = new ActivateInfo();
		activateInfo.getPage().setPageNumber(pageNO);
		activateInfo.getPage().setPageSize(pageSize);
		activateInfo.getPage().initRecordBegin();
		List<ActivateInfo> activateList = adminStatisticsService.selectActivateList(activateInfo);
		mv.addObject("activateList", activateList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", adminStatisticsService.selectActivateListCount(activateInfo));
		mv.addObject("url", url.toString());
		return mv;
	}
	
	/**
	 * 查询商户详情
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectUserActivateList")
	public ModelAndView selectUserActivateList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/statistics/userActivateList");
		StringBuilder url = new StringBuilder("/admin/statistics/selectUserActivateList?pageSize=" + pageSize);
		UerActivateInfo uerActivateInfo = new UerActivateInfo();
		String roleId = request.getParameter("roleId");
		String isActivate = request.getParameter("isActivate");
		String text = request.getParameter("text");
		
		if (roleId != null && !"".equals(roleId)) {
			uerActivateInfo.setPlaceRoleId(roleId);
			url.append("&roleId=" + roleId);
			mv.addObject("roleId", roleId);
		}
		if (isActivate != null && !"".equals(isActivate)) {
			uerActivateInfo.setIsActivate(Integer.parseInt(isActivate));
			url.append("&isActivate=" + isActivate);
			mv.addObject("isActivate", isActivate);
		}
		if (text != null && !"".equals(text)) {
			uerActivateInfo.setPlaceName(text);
			url.append("&text=" + text);
			mv.addObject("text", text);
		}

		url.append("&pageNO=");
		uerActivateInfo.getPage().setPageNumber(pageNO);
		uerActivateInfo.getPage().setPageSize(pageSize);
		uerActivateInfo.getPage().initRecordBegin();
		List<UerActivateInfo> uersActivateList = adminStatisticsService.selectUserActivateList(uerActivateInfo);
		if (null != uersActivateList && uersActivateList.size() > 0) {
			for (UerActivateInfo userActivateInfo : uersActivateList) {
				if (userActivateInfo.getUserNickname() != null && !userActivateInfo.getUserNickname().isEmpty()) {
					userActivateInfo.setUserNickname(URLDecoder.decode(userActivateInfo.getUserNickname(), "UTF-8"));
				}
			}
		}
		mv.addObject("userActivateList", uersActivateList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", adminStatisticsService.selectUserActivateListCount(uerActivateInfo));
		mv.addObject("url", url.toString());
		return mv;
	}
	
	/**
	 * 查询商户收益记录列表
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectMarketList")
	public ModelAndView selectMarketList(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize,
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ModelAndView mv = new ModelAndView("admin/statistics/marketList");
		StringBuilder url = new StringBuilder("/admin/statistics/selectMarketList?pageSize=" + pageSize);
		String name = request.getParameter("name");
		String roleType = request.getParameter("roleType");
		String beginDate = request.getParameter("beginDate");
		MarketInfo marketInfo = new MarketInfo();
		marketInfo.getPage().setPageNumber(pageNO);
		marketInfo.getPage().setPageSize(pageSize);
		marketInfo.getPage().initRecordBegin();
		
		if (name != null && !"".equals(name)) {
			marketInfo.setPlaceName(name);
			url.append("&name=" + name);
			mv.addObject("name", name);
		}
		if (roleType != null && !"".equals(roleType)) {
			marketInfo.setPlaceType(roleType);
			url.append("&roleType=" + roleType);
			mv.addObject("roleType", roleType);
		}
		if (beginDate != null && !"".equals(beginDate)) {
			marketInfo.setRebackMonth(beginDate);
			url.append("&beginDate=" + beginDate);
			mv.addObject("beginDate", beginDate);
		}
		
		url.append("&pageNO=");
		//按月查询余额，销售额等
		List<MarketInfo> marketList = adminStatisticsService.selectMarketList(marketInfo);
		//查询所有商户层关系信息
		List<MarketInfo> placeList = adminStatisticsService.selectPlaceList();
		if (null != marketList && marketList.size() > 0 && null != placeList && placeList.size() > 0) {
			for (MarketInfo moneyInfo : marketList) {
				for (MarketInfo placeInfo : placeList) {
					if (null != moneyInfo.getPlaceId() && !"".equals(moneyInfo.getPlaceId()) 
							&& null != placeInfo.getPlaceId() && !"".equals(placeInfo.getPlaceId()) 
							&& moneyInfo.getPlaceId().toString().equals(placeInfo.getPlaceId().toString())) {
						moneyInfo.setProvinceName(placeInfo.getProvinceName());
						moneyInfo.setCityName(placeInfo.getCityName());
						moneyInfo.setStoreName(placeInfo.getStoreName());
					}
				}
			}
		}
		mv.addObject("name", name);
		mv.addObject("roleType", roleType);
		mv.addObject("beginDate", beginDate);
		mv.addObject("marketList", marketList);
		mv.addObject("pageNO", pageNO);
		mv.addObject("pageSize", adminStatisticsService.selectMarketListCount(marketInfo));
		mv.addObject("url", url.toString());
		return mv;
	}
	
	/**
	 * 查询统计信息(已关注总人数，总销售台数，总金额等)
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/selectStatisticsInfo")
	public ModelAndView selectStatisticsInfo(HttpServletRequest request) throws Exception {
		ModelAndView mv = new ModelAndView("admin/statistics/statisticsInfo");
		StatisticsInfo statisticsInfo = new StatisticsInfo();
		
		//查询总关注人数
		int attentionSum = adminStatisticsService.selectAttentionSum();
		statisticsInfo.setAttentionSum(attentionSum);
		
		//查询总销售台数和总金额
		StatisticsInfo salesAmount = adminStatisticsService.selectSalesAmount();
		if (salesAmount != null) {
			statisticsInfo.setSalesMoney(salesAmount.getSalesMoney());
			statisticsInfo.setSalesSum(salesAmount.getSalesSum());
		}
		
		//查询今日新增关注人数，今日取消关注人数
		StatisticsInfo  currentAttentionNum = adminStatisticsService.selectCurrentAttentionNum();
		if (currentAttentionNum != null) {
			statisticsInfo.setNewAttentionNum(currentAttentionNum.getNewAttentionNum());
			statisticsInfo.setCancelAttentionNum(currentAttentionNum.getCancelAttentionNum());
		}
		
		//查询当前已激活省代数量，市代数量、门店数量
		List<ActivateInfo> activateList = adminStatisticsService.selectPlaceActivate();
		int storeNum = 0;
		if (activateList != null && activateList.size() > 0) {
			for (ActivateInfo activateInfo : activateList) {
				switch (activateInfo.getRoleName()) {
				case "省代":
					statisticsInfo.setProvinceNum(activateInfo.getActivateNum());break;
				case "代理":
					statisticsInfo.setCityNum(activateInfo.getActivateNum());break;
				case "门店":
					storeNum += activateInfo.getActivateNum().intValue();break;
				case "母婴店":
					storeNum += activateInfo.getActivateNum().intValue();break;
				}
			}
		}
		statisticsInfo.setStoreNum(storeNum);//门店和母婴店需整合在一起统计
		
		//查询当前门店二维码启用数量
		int qrCodeNum = adminStatisticsService.selectQrCodeNum();
		statisticsInfo.setQrCodeNum(qrCodeNum);
		
		//查询当前通过门店二维码销售台数,总金额
		StatisticsInfo storeSalesInfo  = adminStatisticsService.selectStoreSalesInfo();
		if (storeSalesInfo != null) {
			statisticsInfo.setStoreSalesMoney(storeSalesInfo.getStoreSalesMoney());
			statisticsInfo.setStoreSalesNum(storeSalesInfo.getStoreSalesNum());
		}
		
		mv.addObject("statisticsInfo", statisticsInfo);
		return mv;
	}
}