package com.iamberry.wechat.handles.reback;

import java.util.HashMap;
import java.util.Map;

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

import com.iamberry.wechat.core.entity.PageBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.drp.EasyPage;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.face.drp.PlaceInfoService;
import com.iamberry.wechat.face.reback.RebackStaticService;
import com.iamberry.wechat.tools.ResultInfo;


/**
 * @description 返利的处理，返利的统计
 * @author 欧阳明
 * @date 2016-05-25
 */

@Controller
@RequestMapping("/wechat/rebackStatic")
public class RebackHandler {
	
	@Autowired
	private RebackStaticService rebackStaticService;
	@Autowired
	private WechatUtils wechatUtils;
	@Autowired
	private PlaceInfoService placeInfoService;

	/**
	 * 查看未入账订单信息
	 * @param openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/unIncomeOrderCast")
	public ResultMsg getUnIncomeOrderCastNext(HttpServletRequest request,EasyPage pages ) throws Exception{
		ResultMsg resultMsg=getSuccessResult();
		//String openid=request.getParameter("openid");
		PageBean page=new PageBean();
		page.setPageNumber(Integer.valueOf(request.getParameter("pageNumber")));
		page.setPageSize(Integer.valueOf(request.getParameter("pageSize")));

		String openid=getOpenid(request);

		if(openid==null || openid.equals("")){
			resultMsg.setMessage(ResultInfo.paramFormatError);
			resultMsg.setStatus(false);
			return resultMsg;
		}
		page.initRecordBegin();
		Map map=rebackStaticService.getUnIncomeOrderList(openid,page);
		resultMsg.setData(map);
		return resultMsg;
	}
	
	/**
	 * 查看未入账订单信息（非门店，母婴店）
	 * @param openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/unIncomeOrderCast2")
	public ResultMsg getUnIncomeOrderCast2(HttpServletRequest request,EasyPage pages ) throws Exception{
		ResultMsg resultMsg=getSuccessResult();
		//String openid=request.getParameter("openid");
		PageBean page=new PageBean();
		page.setPageNumber(Integer.valueOf(request.getParameter("pageNumber")));
		page.setPageSize(Integer.valueOf(request.getParameter("pageSize")));

		String openidInit=request.getParameter("openid");
		String openid=getOpenid(request);
		
		if(openid==null || openid.equals("")){
			resultMsg.setMessage(ResultInfo.paramFormatError);
			resultMsg.setStatus(false);
			return resultMsg;
		}
		page.initRecordBegin();
		Map map=null;
//System.out.println("============openid2======="+openid);
		if(openidInit!=null && !openidInit.equals("null") && !openidInit.equals("") ){
			map=rebackStaticService.getUnIncomeOrderList2Top(openid,openidInit,page);  
		}else{
			map=rebackStaticService.getUnIncomeOrderList2(openid,page);
		}
		resultMsg.setData(map);
		return resultMsg;
	}
	
	
	/**
	 * 获取统计信息 7Day,6Mon,Year
	 * @param openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/statisInfo",method=RequestMethod.POST)
	public ResultMsg getStaticsInfo(HttpServletRequest request){
	  ResultMsg resultMsg=getSuccessResult();
	  //String openid=request.getParameter("openid");
	  String openid=getOpenid(request);
	  if(openid==null || openid.equals("")){
			resultMsg.setMessage(ResultInfo.paramFormatError);
			resultMsg.setStatus(false);
			return resultMsg;
		}
	  resultMsg.setData(rebackStaticService.getIncomeStatistics(openid, request.getParameter("type")));
		//List<ObjRecord>
	  return resultMsg;
	}
	
	
	/**
	 * 获取分销商的下线，可能是下线是分销商，也可能是微代理
	 * @author 欧阳明
	 * @param request
	 * @return ResultMsg
	 */
	@ResponseBody
	@RequestMapping(value="/underDrpListInfo",method=RequestMethod.POST)
	public ResultMsg underDrpListInfo(HttpServletRequest request){
		String openid=getOpenid(request);
		
		String adaptOpenid=request.getParameter("openid");
		if(adaptOpenid!=null && !adaptOpenid.equals("")){
			openid=adaptOpenid;
		}
		PlaceInfo placeInfo=placeInfoService.getPlaceInfoByOpenId(openid);
		if(placeInfo.getHasQrcode()==2){
			return getUnderlingStaticsInfo2(request); //下级微代理
		}else{
			return getUnderlingStaticsInfo(request);  //下级分销商
		}
	}
	
	/**
	 * 获取分销商的下线，下线是分销商，传回几个tab
	 * @author 欧阳明
	 * @param request
	 * @return ResultMsg
	 */
	@ResponseBody
	@RequestMapping(value="/underDrpListInfoInit",method=RequestMethod.POST)
	public ResultMsg underDrpListInfoInit(HttpServletRequest request){
		ResultMsg resultMsg=getSuccessResult();
		 String openid=getOpenid(request);
		  if(openid==null || openid.equals("")){
				resultMsg.setMessage(ResultInfo.paramFormatError);
				resultMsg.setStatus(false);
				return resultMsg;
			}	  
		  
		  String adaptOpenid=request.getParameter("openid");
		  Map map=null;
		  if(adaptOpenid!=null && !adaptOpenid.equals("")){
			   map=rebackStaticService.getNextDrpStatisc(adaptOpenid);
		  }else{
			   map=rebackStaticService.getNextDrpStatisc(openid);
		  }
		  resultMsg.setData(map);
		  return resultMsg;
	}
	
	
	/**
	 * 获取分销的下一级分销的统计情况
	 * @param openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/underDrpStatisc",method=RequestMethod.POST)
	public ResultMsg getUnderlingStaticsInfo(HttpServletRequest request){
		
		ResultMsg resultMsg=getSuccessResult();
		  //String type=request.getParameter("type");
		  int pageSize=Integer.valueOf(request.getParameter("pageSize"));
		  int pageNumber=Integer.valueOf(request.getParameter("pageNumber"));
		  PageBean page=new PageBean();
		  page.setPageSize(pageSize);
		  page.setPageNumber(pageNumber);
		  
		  String openid=request.getParameter("openid");
		  if(openid==null || openid.equals("")){
			  openid=getOpenid(request);
		  }
		 
		  if(openid==null || openid.equals("")){
				resultMsg.setMessage(ResultInfo.paramFormatError);
				resultMsg.setStatus(false);
				return resultMsg;
			}
		  
		  Map map=new HashMap();
		 
		  if(null!=request.getParameter("roleId")){
			  int roleId=Integer.valueOf(request.getParameter("roleId"));
			  map=rebackStaticService.getNextDrpStatisc(openid, page,roleId);
		  }else{
			  map=rebackStaticService.getNextDrpStatisc(openid, page);
		  }
		  
		  resultMsg.setData(map);
		  return resultMsg;
	
	}

	/**
	 * 获取分销的下一级微代理的统计情况
	 * @param openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/underUnderlingStatisc",method=RequestMethod.POST)
	public ResultMsg getUnderlingStaticsInfo2(HttpServletRequest request){
		
		  ResultMsg resultMsg=getSuccessResult();
		  String openid=request.getParameter("openid");
		  int pageSize=Integer.valueOf(request.getParameter("pageSize"));
		  String endNostr=request.getParameter("endNo");
		  int endNo=0;
		  if(endNostr!=null && !endNostr.equals("") ){
			  endNo =Integer.valueOf(endNostr);  //pageNumber
		  }
		  String beginNostr=request.getParameter("beginNo");
		  int beginNo=0;
		  if(beginNostr!=null  && !endNostr.equals("") ){
			  endNo =Integer.valueOf(beginNostr);  //pageNumber
		  }
		  PageBean page=new PageBean();
		  page.setPageSize(pageSize);
		 //page.setPageNumber(pageNumber);
		  
		  if(openid==null || openid.equals("")){
		  openid=getOpenid(request);
		  if(openid==null || openid.equals("")){
				resultMsg.setMessage(ResultInfo.paramFormatError);
				resultMsg.setStatus(false);
				return resultMsg;
			}
		  }
		  Map map=new HashMap();
		  if(endNo>0){
			  map=rebackStaticService.getUnderlingStatisc(openid, page,endNo,"endNo");  
		  }
		  if(beginNo>0){
			  map=rebackStaticService.getUnderlingStatisc(openid, page,beginNo,"beginNo");  
		  }
		  
		  if(endNo==0 && beginNo==0){
			  map=rebackStaticService.getUnderlingStatisc(openid, page,null,"null");
		  }
		  
		  resultMsg.setData(map);
		  return resultMsg;
	
	}
	
	
	//返回处理结果
		 public ResultMsg getSuccessResult(){
			ResultMsg remsg=new ResultMsg();
			remsg.setResultCode(ResultInfo.SUCCESSCODE);
			remsg.setMessage(ResultInfo.SUCCESSINFO);
			remsg.setStatus(true);
			return remsg;
		 }
		 
		//获取openid
		public String getOpenid(HttpServletRequest request){
			try {
				return wechatUtils.getUserBySession(request).getUserOpenid();
			} catch (Exception e) {
				e.printStackTrace();
				//return "oaH_GwHQSHd2-KxQpihr_b9Y9lOc";
				return "o3dG7wSXkG3RgD75pWttGwwmoUsk";
			}
		}
	
	
	
	
}
