package com.iamberry.wechat.handles.statistics;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.order.ProbationOrderDto;
import com.iamberry.wechat.core.entity.statictics.GoldMemberStatisticsDto;
import com.iamberry.wechat.core.entity.statictics.SalesmanDto;
import com.iamberry.wechat.core.entity.statictics.SalesmanInfo;
import com.iamberry.wechat.core.entity.statictics.StatisticManagerDto;
import com.iamberry.wechat.core.entity.statictics.StatisticOrderByMonthDto;
import com.iamberry.wechat.core.entity.statictics.StatisticsAdministratorDTO;
import com.iamberry.wechat.core.entity.statictics.StatisticsOrderDetailsDTO;
import com.iamberry.wechat.core.entity.statictics.StoresStatisticsDto;
import com.iamberry.wechat.core.entity.statictics.VipOfflineDto;
import com.iamberry.wechat.face.statistics.StatisticsRelationService;
import com.iamberry.wechat.tools.DateTimeUtil;
import com.iamberry.wechat.tools.ResponseJson;
import com.iamberry.wechat.tools.ResultInfo;

/**
 * 业务员统计 controller
 * 2016年12月27日
 * @author all~
 *
 */
@Controller
@RequestMapping("/wechat/statistics")
public class StatisticsRelationHandler {
	
	@Autowired
	private StatisticsRelationService statisticsRelationService;

	/**
	 * 订单统计
	 * @return
	 * @author 献
	 * @Time   2016年12月21日
	 */
	@RequestMapping(value = "/order_statistics", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getOrderStatistics(
			@RequestParam(value = "pageNO", required = false, defaultValue = "1") Integer pageNO, 
			HttpServletRequest request, HttpServletResponse response) {
		// 获取当前用户是否为业务员
		Member member = WechatUtils.getUserBySession(request);
		SalesmanInfo salesmanInfo = statisticsRelationService.getRelationByOpenID(member.getUserOpenid());
		
		ResponseJson json = new ResponseJson();
		if (salesmanInfo == null) {
			// 用户不是业务员
			json.setResultCode(200);
			json.setResultMsg("MEMBER_NOT_SALESMANRELATION");
			json.setReturnCode(403);
			return json;
		}
		
		// 统计订单
		json = statisticsRelationService.getStatisticsOrderByOpenID(salesmanInfo.getsId(), pageNO, 10);
		return json;
	}
	
	/**
	 * 获取订单详情
	 * @return
	 * @author 献
	 * @Time   2016年12月21日
	 */
	@RequestMapping(value = "/order_details", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getOrderDetails(
			@RequestParam("dealer") Integer dealer, 
			@RequestParam("orderId") String orderId,
			HttpServletRequest request) {
		Member member = WechatUtils.getUserBySession(request);
		SalesmanInfo salesmanInfo = statisticsRelationService.getRelationByOpenID(member.getUserOpenid());
		
		ResponseJson json = new ResponseJson();
		if (salesmanInfo == null) {
			// 用户不是业务员
			json.setResultCode(200);
			json.setResultMsg("MEMBER_NOT_SALESMANRELATION");
			json.setReturnCode(403);
			return json;
		}
//		StatisticsOrderDetailsDTO dto = statisticsRelationService.getOrderDetails(2034, "1220180129T11902G");
		StatisticsOrderDetailsDTO dto = statisticsRelationService.getOrderDetails(dealer, orderId);
		json.setResultCode(200);
		json.setResultMsg("SUCCESS");
		if (dto == null) {
			json.setReturnCode(201);
		} else {
			json.setReturnCode(200);
		}
		json.addResponseKeyValue("data", dto);
		return json;
	}
	
	/**
	 * 分页查询试用订单列表
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/selectProbationStatictics")
	public ResultMsg selectProbationStatictics(HttpServletRequest request, 
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
			@RequestParam(value = "status", defaultValue = "1",required=false) Integer status) throws Exception {
		ResultMsg msg = new ResultMsg();
		Map<String,Object> map = new HashMap<String,Object>();
		SalesmanDto salesmanDto = new SalesmanDto();
		
//		Member member = new Member();
		Member member = WechatUtils.getUserBySession(request);
//		member.setUserOpenid("o-icas-WiwbEXfwO1wLGDKQ1iWJU");
		salesmanDto.setsOpenId(member.getUserOpenid());
		salesmanDto.setProbationStatus(status);
		salesmanDto.getPage().setPageNumber(pageNO);
		salesmanDto.getPage().setPageSize(pageSize);
		salesmanDto.getPage().initRecordBegin();
		//按月获取试用订单列表
		List<ProbationOrderDto> orderList = statisticsRelationService.selectProbationStatictics(salesmanDto);
		//按月获取试用订单列表总页数和总条目数
		Map<String,Integer> numMap = statisticsRelationService.selectProbationStaticticsCount(salesmanDto);
		map.put("orderList", orderList);
		map.put("pageCount", numMap.get("pageSize"));
		map.put("probationNum", numMap.get("probationNum"));
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}
	
	/**
	 * 根据probationOrderId查询订单详情
	 * @param request
	 * @param probationOrderId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/selectProbationInfo")
	public ResultMsg selectProbationInfo(HttpServletRequest request,
			@RequestParam("probationOrderId") String probationOrderId) throws Exception {
		ResultMsg msg = new ResultMsg();
		ProbationOrderDto probationOrderDto = new ProbationOrderDto();
		if (probationOrderId == null || "".equals(probationOrderId)) {
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("订单编号为空");
			return msg;
		}
		probationOrderDto.setProbationOrderid(probationOrderId);
		//获取试用订单详情
		probationOrderDto = statisticsRelationService.selectProbationInfo(probationOrderDto);
		msg.setData(probationOrderDto);
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		return msg;
	}
	/**
	 * 分页查询门店信息(业务员)
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectStoresStatistics", method = RequestMethod.POST)
	//@RequestMapping("/selectStoresStatistics")
	public ResultMsg selectStoresStatistics(HttpServletRequest request, 
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
			@RequestParam(value = "name", defaultValue = "",required=false) String name) throws Exception {
		ResultMsg msg = new ResultMsg();
		Map<String,Object> map = new HashMap<String,Object>();
		StoresStatisticsDto storesStatisticsDto = new StoresStatisticsDto();
		Member member = WechatUtils.getUserBySession(request);
		SalesmanInfo salesmanInfo = statisticsRelationService.getRelationByOpenID(member.getUserOpenid());
//		SalesmanInfo salesmanInfo = statisticsRelationService.getRelationByOpenID("o-icas-WiwbEXfwO1wLGDKQ1iWJU");
		if(name != null && !name.equals("")){
			storesStatisticsDto.setName(name);
			map.put("name", name);
		}
		storesStatisticsDto.setSid(salesmanInfo.getsId());
		storesStatisticsDto.getPage().setPageNumber(pageNO);
		storesStatisticsDto.getPage().setPageSize(pageSize);
		storesStatisticsDto.getPage().initRecordBegin();
		List<StoresStatisticsDto> storesList = statisticsRelationService.selectStoresStatistics(storesStatisticsDto);
		Map<String,Integer> numMap = statisticsRelationService.selectStoresStatisticsCount(storesStatisticsDto);
		map.put("storesList", storesList);
		map.put("pageSize", numMap.get("pageSize"));
		map.put("probationNum", numMap.get("probationNum"));
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}
	
	/**
	 * 分页查询门店vip会员列表
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectVipOffline", method = RequestMethod.POST)
	//@RequestMapping("/selectVipOffline")
	public ResultMsg selectVipOffline(HttpServletRequest request, 
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
			@RequestParam(value = "placeid", defaultValue = "0",required=false) Integer placeid,
			@RequestParam(value = "name", defaultValue = "",required=false) String name) throws Exception {
		ResultMsg msg = new ResultMsg();
		Map<String,Object> map = new HashMap<String,Object>();
		VipOfflineDto vipOfflineDto = new VipOfflineDto();
		if(name != null && !name.equals("")){
			vipOfflineDto.setName(name);
			map.put("name", name);
		}
		vipOfflineDto.setPlaceId(placeid);
		vipOfflineDto.getPage().setPageNumber(pageNO);
		vipOfflineDto.getPage().setPageSize(pageSize);
		vipOfflineDto.getPage().initRecordBegin();
		List<VipOfflineDto> vipOfflineList = statisticsRelationService.selectVipOffline(vipOfflineDto);
		Map<String,Integer> numMap = statisticsRelationService.selectVipOfflineCount(vipOfflineDto);
		map.put("vipOfflineList", vipOfflineList);
		map.put("pageSize", numMap.get("pageSize"));
		map.put("probationNum", numMap.get("probationNum"));
		map.put("placeid", numMap.get("placeid"));
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}
	
	/**
	 * 分页查询门店信息(管理员)
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectAdministratorStatistics", method = RequestMethod.POST)
	//@RequestMapping("/selectAdministratorStatistics")
	public ResultMsg selectAdministratorStatistics(HttpServletRequest request, 
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
			@RequestParam(value = "name", defaultValue = "",required=false) String name) throws Exception {
		ResultMsg msg = new ResultMsg();
		Map<String,Object> map = new HashMap<String,Object>();
		StoresStatisticsDto storesStatisticsDto = new StoresStatisticsDto();
		// 获取当前用户openId,查询是否
		Member member = WechatUtils.getUserBySession(request);
		SalesmanDto salesmanDto = statisticsRelationService.selectAdminInfo(member.getUserOpenid());
		//判断是否为管理员，如果不是，则退出
		if (salesmanDto == null || salesmanDto.getsRoleId().intValue() != 2) {
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("该用户不是管理员");
			return msg;
		}
		if(name != null && !name.equals("")){
			storesStatisticsDto.setName(name);
			map.put("name", name);
		}
		storesStatisticsDto.getPage().setPageNumber(pageNO);
		storesStatisticsDto.getPage().setPageSize(pageSize);
		storesStatisticsDto.getPage().initRecordBegin();
		List<StoresStatisticsDto> storesList = statisticsRelationService.selectAdministratorStatistics(storesStatisticsDto);
		Map<String,Integer> numMap = statisticsRelationService.selectAdministratorStatisticsCount(storesStatisticsDto);
		map.put("storesList", storesList);
		map.put("pageSize", numMap.get("pageSize"));
		map.put("probationNum", numMap.get("probationNum"));
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}
	
	/**
	 * 销售管理页面接口 查询今日、本月的试用订单和销售订单总数		(业务员)
	 * 2016年12月21日
	 * @author mzx
	 * @param page
	 * @return
	 */
	@RequestMapping("/sales_manager")
	@ResponseBody
	public ResultMsg getStatisticManager(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		
		PaperBean page = new PaperBean();
		page.setText(WechatUtils.getUserBySession(req).getUserOpenid());
		page.setBeginDate(DateTimeUtil.getFirstDayOfYear(new Date()));
		page.setEndDate(DateTimeUtil.getLastDayOfYear(new Date()));
		StatisticManagerDto sDto = statisticsRelationService.selectSalesCount(page);
		
		if(sDto != null){
			rm.setStatus(true);
			rm.setResultCode(ResultInfo.SUCCESSCODE);
			rm.setData(sDto);
		}else{
			rm.setStatus(false);
			rm.setResultCode(ResultInfo.ERRORCODE);
			rm.setMessage("获取统计信息失败!请刷新重试!");
		}
		return rm;
	}
	
	/**
	 * 销售管理接口试用订单、销售订单列表	(业务员)
	 * 2016年12月22日
	 * @author mzx
	 * @return
	 */
	@RequestMapping("/sales_order_list")
	@ResponseBody
	public ResultMsg getOrderList(HttpServletRequest req,
			@RequestParam(value= "recordBegin",defaultValue= "0" ,required=false) Integer recordBegin,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize){
		ResultMsg rm = new ResultMsg();
		//订单类型	probationOrder ：试用订单(默认)	salesOrder ：销售订单
		String orderType = req.getParameter("orderType");
		if(recordBegin > 0){
			recordBegin = recordBegin - 1;
		}
		if(StringUtils.isEmpty(orderType)){
			orderType = "probationOrder";
		}
		PaperBean page = new PaperBean();
		page.setText(WechatUtils.getUserBySession(req).getUserOpenid());
		page.setStr(orderType);
		page.setMaxNum(pageSize);
		page.setMinNum(recordBegin);
		
		StatisticManagerDto sDto = statisticsRelationService.selectOrderList(page);
		if(sDto != null){
			rm.setStatus(true);
			rm.setResultCode(ResultInfo.SUCCESSCODE);
			rm.setData(sDto);
		}else{
			rm.setStatus(false);
			rm.setResultCode(ResultInfo.ERRORCODE);
			rm.setMessage("获取统计信息失败!请刷新重试!");
		}
		return rm;
	}
	/**
	 * 销量统计接口		(业务员)
	 * 按月份统计试用、销售订单的台数
	 * 2016年12月22日
	 * @author mzx
	 * @param req
	 * @return
	 */
	@RequestMapping("/sales_count_num")
	@ResponseBody
	public ResultMsg getStatisticMonth(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		String openid = WechatUtils.getUserBySession(req).getUserOpenid();
		PaperBean page = new PaperBean();
		page.setText(openid);
		String thisYear = req.getParameter("thisYear");
		int thisYearNum = 0;
		if(StringUtils.isEmpty(thisYear)){
			thisYearNum = DateTimeUtil.year();
		}else{
			thisYearNum = Integer.parseInt(thisYear);
		}
		page.setOtherInt(thisYearNum);
		List<StatisticOrderByMonthDto> list = statisticsRelationService.selectOrderCountNumByMonth(page);
		Map<String, Object> map = new HashMap<String, Object>();
		if(list != null && list.size() > 0){
			rm.setStatus(true);
			rm.setResultCode(ResultInfo.SUCCESSCODE);
			map.put("list", list);
			map.put("thisYearNum", thisYearNum);
			rm.setData(map);
		}else{
			rm.setStatus(false);
			rm.setResultCode(ResultInfo.ERRORCODE);
			rm.setMessage("统计销售总数失败!请刷新重试!");
		}
		return rm;
	}
	
	/**
	 * 查询管理员销售统计首页
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/selectSalesStatisticInfo")
	public ResultMsg selectSalesStatisticInfo(HttpServletRequest request) throws Exception {
		ResultMsg msg = new ResultMsg();
		StatisticsAdministratorDTO salesInfo = new StatisticsAdministratorDTO();
		
		// 获取当前用户openId,查询是否
		Member member = WechatUtils.getUserBySession(request);
		//member.setUserOpenid("o-icas5pQFZWnQ2LAZ5v3eQtkYNU");
		SalesmanDto salesmanDto = statisticsRelationService.selectAdminInfo(member.getUserOpenid());
		//判断是否为管理员，如果不是，则退出
		if (salesmanDto == null || salesmanDto.getsRoleId().intValue() != 2) {
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("该用户不是管理员");
			return msg;
		}
		
		salesInfo.setsNickName(salesmanDto.getsNickName());
		salesInfo.setsRoleName(salesmanDto.getsRoleName());
		
		//查询今日试用订单，今日销售订单，本月试用订单，本月销售订单
		List<StatisticsAdministratorDTO> salesList = statisticsRelationService.selectSalesInfo();
		//查询销售订单总额，试用订单已付，试用订单待付
		List<StatisticsAdministratorDTO> orderList = statisticsRelationService.selectOrderMoney();
		//查询已激活合伙人，已激活门店，已激活VIP会员，金牌会员
		StatisticsAdministratorDTO roleInfo = statisticsRelationService.selectRoleInfo();
		//查询商城用户数，购买过商品的用户数
		StatisticsAdministratorDTO statistics = statisticsRelationService.selectUserNum();
		//查询金牌会员下线个数
		int subordinateNum = statisticsRelationService.selectUserSubordinateInfo();
		
		if (salesList != null && salesList.size() > 0) {
			for (StatisticsAdministratorDTO statisticsInfo : salesList) {
				if (statisticsInfo.getTodaySales() != null) {
					salesInfo.setTodaySales(statisticsInfo.getTodaySales());
				}
				if (statisticsInfo.getTodayProbation() != null) {
					salesInfo.setTodayProbation(statisticsInfo.getTodayProbation());
				}
				if (statisticsInfo.getMonthProbation() != null) {
					salesInfo.setMonthProbation(statisticsInfo.getMonthProbation());
				}
				if (statisticsInfo.getMonthSales() != null) {
					salesInfo.setMonthSales(statisticsInfo.getMonthSales());
				}
				if (statisticsInfo.getProbationNum() != null) {
					salesInfo.setProbationNum(statisticsInfo.getProbationNum());
				}
				if (statisticsInfo.getSalesNum() != null) {
					salesInfo.setSalesNum(statisticsInfo.getSalesNum());
				}
			}
		}
		
		if (orderList != null && orderList.size() > 0) {
			for (StatisticsAdministratorDTO orderInfo : orderList) {
				if (orderInfo.getSalesMoney() != null) {
					salesInfo.setSalesMoney(orderInfo.getSalesMoney());
				}
				if (orderInfo.getProbationUnpaidMoney() != null) {
					salesInfo.setProbationUnpaidMoney(orderInfo.getProbationUnpaidMoney());
				}
				if (orderInfo.getProbationPaidMoney() != null) {
					salesInfo.setProbationPaidMoney(orderInfo.getProbationPaidMoney());
				}
			}
		}
		
		if (roleInfo != null) {
			salesInfo.setPartnerNum(roleInfo.getPartnerNum());
			salesInfo.setStoreNum(roleInfo.getStoreNum());
			salesInfo.setVipNum(roleInfo.getVipNum());
			salesInfo.setMemberNum(roleInfo.getMemberNum());
		}
		
		if (statistics != null) {
			salesInfo.setUserNum(statistics.getUserNum());
			salesInfo.setUserSalesNum(statistics.getUserSalesNum());
		}
		
		salesInfo.setMemberSubordinateNum(subordinateNum);
		//将为空数据初始化为0
		salesInfo.initStatisticsAdministrator();
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(salesInfo);
		return msg;
	}
	
	/**
	 * 按月份查询销售订单和试用订单的机器数
	 * @param request
	 * @param month
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/selectOrderByMonth")
	public ResultMsg selectOrderByMonth(HttpServletRequest request,
			@RequestParam(value= "year",defaultValue= "" ,required=false) String year) throws Exception {
		ResultMsg msg = new ResultMsg();
		Map<String,Object> map = new HashMap<String,Object>();
		StatisticOrderByMonthDto statisticOrderByMonthDto = new StatisticOrderByMonthDto();
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		
		// 获取当前用户openId,查询是否
		Member member = WechatUtils.getUserBySession(request);
		//member.setUserOpenid("o-icas5pQFZWnQ2LAZ5v3eQtkYNU");
		SalesmanDto salesmanDto = statisticsRelationService.selectAdminInfo(member.getUserOpenid());
		//判断是否为管理员，如果不是，则退出
		if (salesmanDto == null || salesmanDto.getsRoleId().intValue() != 2) {
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("该用户不是管理员");
			return msg;
		}
		
		if (!StringUtils.isEmpty(year)) {
			statisticOrderByMonthDto.setMonth(year);
		} else {
			year = format.format(new Date());
			statisticOrderByMonthDto.setMonth(format.format(new Date()));
		}
		
		//按月查询销售订单和试用订单售出水机总数
		List<StatisticOrderByMonthDto> orderList = statisticsRelationService.selectOrderByMonth(statisticOrderByMonthDto);
		
		map.put("year", year);
		map.put("orderList", orderList);
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}
	
	/**
	 *	所有金牌会员名称，对应的下线总销量，发展的用户		(管理员)
	 *	2016年12月26日
	 *	@author mzx
	 * @return
	 */
	@RequestMapping("/gold_member_list")
	@ResponseBody
	public ResultMsg goldMemberStatistics( HttpServletRequest req,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
			GoldMemberStatisticsDto goldDto){
		ResultMsg msg = new ResultMsg();
		msg.setStatus(false);
		msg.setResultCode("500");
		boolean flag = isSalesManManager(req);
		if( !flag ){ return msg;/* 没有管理员权限则退出 */}
		goldDto.getPage().setPageSize(pageSize);
		goldDto.getPage().setPageNumber(pageNO);
		goldDto.getPage().initRecordBegin();
		List<GoldMemberStatisticsDto> goldList = statisticsRelationService.selectAllGoldMemberStatistics(goldDto);
		Integer i = statisticsRelationService.selectAllGoldMemberStatisticsCountNum(goldDto.getPlaceName());
		if(goldList != null && goldList.size() > 0 && i != null && i > 0 ){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goldList", goldList);
			map.put("goldCount", i);
			map.put("pageSize", pageSize);
			msg.setStatus(true);
			msg.setResultCode("200");
			msg.setData(map);
		}
		return msg;
	}
	
	/**
	 * 	单个金牌会员下线会员信息及购买数量 		(管理员)
	 *	2016年12月26日
	 *	@author mzx
	 * @return
	 */
	@RequestMapping("/gold_under_list")
	@ResponseBody
	public ResultMsg getGoldUnderUserBoughtInfo(HttpServletRequest req,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO,
			GoldMemberStatisticsDto goldDto){
		ResultMsg rm = new ResultMsg();
		rm.setStatus(false);
		rm.setResultCode("500");
		boolean flag = isSalesManManager(req);
		if( !flag || goldDto.getPlaceId() == null){ return rm;/*1.没有管理员权限则退出；2.金牌会员id为空则退出*/}
		goldDto.getPage().setPageSize(pageSize);
		goldDto.getPage().setPageNumber(pageNO);
		goldDto.getPage().initRecordBegin();
		
		List<GoldMemberStatisticsDto> goldUserList = statisticsRelationService.selectGoldUnderUserAndBoughtCount(goldDto);
		Integer i = statisticsRelationService.selectGoldUnderUserAndBoughtCountNum(goldDto);
		if(goldUserList != null && goldUserList.size() > 0 && i != null && i.intValue() > 0){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("goldUserList", goldUserList);
			map.put("userCount", i);
			map.put("pageSize", pageSize);
			rm.setStatus(true);
			rm.setResultCode("200");
			rm.setData(map);
		}
		return rm;
	}
	
	/**
	 * 	判断当前商户是否有业务员角色，有并且状态是正常的(sStatus=1)则返回该业务员信息，否则返回false
	 * 	2016年12月27日
	 * @author mzx
	 * @return
	 */
	@RequestMapping("/is_sales_man")
	@ResponseBody
	public ResultMsg getSalesManRole(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		rm.setStatus(false);
		rm.setResultCode("500");
		String openID = WechatUtils.getUserBySession(req).getUserOpenid();
		SalesmanInfo salesInfo = statisticsRelationService.getRelationByOpenID(openID);
		if(salesInfo != null && salesInfo.getsStatus() != null && salesInfo.getsStatus().intValue() == 1){
			rm.setStatus(true);
			rm.setResultCode("200");
			rm.setData(salesInfo);
		}
		return rm;
	}
	
	/**
	 * 判断当前用户是否有业务员中管理员的权限
	 * 2016年12月27日
	 * @author mzx
	 * @param req
	 * @return
	 */
	private boolean isSalesManManager(HttpServletRequest req){
		boolean flag = false;
		String openID = WechatUtils.getUserBySession(req).getUserOpenid();
		SalesmanInfo salesInfo = statisticsRelationService.getRelationByOpenID(openID);
		if(salesInfo != null && salesInfo.getsStatus() != null && salesInfo.getsStatus().intValue() == 1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 按月查询试用订单数
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/selectProbationSumByMonth")
	public ResultMsg selectProbationSumByMonth(HttpServletRequest request,
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ResultMsg msg = new ResultMsg();
		StatisticOrderByMonthDto order = new StatisticOrderByMonthDto();
		order.getPage().setPageNumber(pageNO);
		order.getPage().setPageSize(pageSize);
		order.getPage().initRecordBegin();
		// 获取当前用户openId,查询是否
		Member member = WechatUtils.getUserBySession(request);
		//member.setUserOpenid("o-icas-WiwbEXfwO1wLGDKQ1iWJU");
		SalesmanDto salesmanDto = statisticsRelationService.selectAdminInfo(member.getUserOpenid());
		//判断是否为管理员，如果不是，则退出
		if (salesmanDto == null || salesmanDto.getsRoleId().intValue() != 2) {
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("该用户不是管理员");
			return msg;
		}
		//查询本月试用订单数
		List <StatisticOrderByMonthDto> orderList = statisticsRelationService.selectProbationSumByMonth(order);
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(orderList);
		return msg;
	}
	
	/**
	 * 按月查询销售订单数
	 * @param request
	 * @param pageSize
	 * @param pageNO
	 * @param status
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/selectSalesNumbers", method = RequestMethod.POST)
	//@RequestMapping("/selectAdministratorStatistics")
	public ResultMsg selectSalesNumbers(HttpServletRequest request, 
			@RequestParam(value= "pageSize",defaultValue= "6" ,required=false) Integer pageSize, 
			@RequestParam(value = "pageNO", defaultValue = "1",required=false) Integer pageNO) throws Exception {
		ResultMsg msg = new ResultMsg();
		Map<String,Object> map = new HashMap<String,Object>();
		StatisticOrderByMonthDto statisticOrderByMonthDto = new StatisticOrderByMonthDto();
		// 获取当前用户openId,查询是否
		Member member = WechatUtils.getUserBySession(request);
		SalesmanDto salesmanDto = statisticsRelationService.selectAdminInfo(member.getUserOpenid());
		//判断是否为管理员，如果不是，则退出
		if (salesmanDto == null || salesmanDto.getsRoleId().intValue() != 2) {
			msg.setResultCode(ResultInfo.ERRORCODE);
			msg.setStatus(false);
			msg.setMessage("该用户不是管理员");
			return msg;
		}
		statisticOrderByMonthDto.getPage().setPageNumber(pageNO);
		statisticOrderByMonthDto.getPage().setPageSize(pageSize);
		statisticOrderByMonthDto.getPage().initRecordBegin();
		List<StatisticOrderByMonthDto> storesList = statisticsRelationService.selectSalesNumbers(statisticOrderByMonthDto);
		Map<String,Integer> numMap = statisticsRelationService.selectSalesNumbersCount(statisticOrderByMonthDto);
		map.put("storesList", storesList);
		map.put("pageSize", numMap.get("pageSize"));
		map.put("probationNum", numMap.get("probationNum"));
		msg.setResultCode(ResultInfo.SUCCESSCODE);
		msg.setStatus(true);
		msg.setData(map);
		return msg;
	}
}
