package com.iamberry.wechat.handles.drp;

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.PlaceWithDrawalsLogs;
import com.iamberry.wechat.face.drp.PlaceWithDrawalsLogsService;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.ValidationResult;
import com.iamberry.wechat.tools.ValidationUtils;


/**
 * @description 线下代理提现控制器
 * @author 欧阳明
 *
 */

@Controller
@RequestMapping("/wechat/WithDrawalsLogs")
public class PlaceWithDrawalsLogsHandler {

	@Autowired
	private PlaceWithDrawalsLogsService placeWithDrawalsLogsService;
	
	@Autowired
	private WechatUtils wechatUtils;
	
	
	/**
	 * 进入申请页面
	 * @param openid 前端传过来的openid 可以默认为null;
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/getWithdrawPage")
	public ResultMsg  getWithdrawApply(HttpServletRequest request) throws Exception{
		Map map=new HashMap();
		ResultMsg resultMsg=getSuccessResult();
		String openid=getOpenid(request);
		if(openid==null || openid.equals("")){
			resultMsg.setMessage(ResultInfo.paramFormatError);
			resultMsg.setResultCode(ResultInfo.ERRORCODE);
			resultMsg.setStatus(false);
			return resultMsg;
		}
		
		//ShopSystemRule sr=systemService.selectOneShopRuleById(100);
		//map.put("withdrawalsCount",)

		resultMsg.setData(placeWithDrawalsLogsService.apply(openid));
		return resultMsg;
	}
	
	
	/**
	 * 提交提现申请表处理
	 * @param PlaceWithDrawalsLogs 提现的插入对象
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/submitWithdraw",method=RequestMethod.POST)
	public ResultMsg  submitWithdraw(PlaceWithDrawalsLogs placeWithDrawalsLogs,HttpServletRequest request) throws Exception{
		ResultMsg remsg=new ResultMsg();
		ValidationResult result =ValidationUtils.validateEntity(placeWithDrawalsLogs);
		if(result.isHasErrors()){
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setMessage(result.getErrorMsgList().get(0));
			remsg.setData(result.getErrorMsgList());
			return remsg;
		}
		
		//String openid=placeWithDrawalsLogs.getOpenid();
		String openid=getOpenid(request);
		
		placeWithDrawalsLogs.setOpenid(openid);
		remsg=placeWithDrawalsLogsService.submitApply(placeWithDrawalsLogs);
		return remsg;
	}
	
	/**
	 * 历史提现记录
	 * @param String openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/historyWithdraw")
	public ResultMsg  HistoryWithdraw(String openid,HttpServletRequest request) throws Exception{
		Map map=placeWithDrawalsLogsService.HistoryRecords(openid!=null && !openid.equals("")?openid:wechatUtils.getUserBySession(request).getUserOpenid());
		ResultMsg resultMsg=getSuccessResult();
		resultMsg.setData(map);
		return resultMsg;
	}
	
	/**
	 * 历史提现记录下一个分页
	 * @param PlaceWithDrawalsLogs pwd 需要回传一个openid
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/historyWithdrawNext",method=RequestMethod.POST)
	public ResultMsg  HistoryWithdrawNext(EasyPage epage,HttpServletRequest request) throws Exception{
		ResultMsg remsg=getSuccessResult();
		String openid=getOpenid(request);//epage.getOpenid();
		if(openid==null || openid.equals("")){
			remsg.setMessage(ResultInfo.paramFormatError);
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setStatus(false);
			return remsg;
		}
		PlaceWithDrawalsLogs pwd=new PlaceWithDrawalsLogs();
		PageBean page=new PageBean();
		page.setPageNumber(epage.getPageNumber());
		page.setPageSize(epage.getPageSize());
		pwd.setPage(page);
		pwd.setOpenid(openid);
		if(openid==null || openid.equals("")){
			remsg.setMessage(ResultInfo.paramFormatError);
			remsg.setResultCode(ResultInfo.ERRORCODE);
			remsg.setStatus(false);
			return remsg;
		}
		Map map=placeWithDrawalsLogsService.HistoryRecordsNext(pwd);
		remsg.setData(map);
		return remsg;
	}
	
	
	//返回处理结果
	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";
		}
	}
}
