package com.iamberry.wechat.handles.member;

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

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.member.CashLog;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.member.CashLogService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.tools.ResultInfo;

/**
 * 用户现金记录
 * 2016年5月3日
 * @author 穆再兴
 *
 */
@Controller
@RequestMapping("/wechat/cashLog")
public class CashLogHandler {
	@Autowired
	private WechatUtils wechatUtils;
	
	@Autowired
	private MemberService memberService;			//会员service
	
	@Autowired
	private CashLogService cashLogService;
	
	/**
	 * 前台分页获取当前用户所有现金记录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public ResultMsg getAllCashLog(HttpServletRequest req,
			@RequestParam(value="pageSize",defaultValue= "6", required=false)String pageSize,
			@RequestParam(value="pageNO",defaultValue= "1", required=false)String pageNO){
		ResultMsg rm = new ResultMsg();
		Member member = new Member();
//		member.setUserOpenid("o-icas-pRAoYgP594rRabeog4SSU");
//		member.setUserIncome(158);
		try {
			member = wechatUtils.getUserBySession(req);
			if(member == null){
				rm.setMessage(ResultInfo.loginOutError);
				return rm;
			}
			member = memberService.getMemberByUserOpenId(member.getUserOpenid());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		CashLog cashLog = new CashLog();
		cashLog.setCashLogsOpenid(member.getUserOpenid());
		cashLog.getPage().setPageSize(Integer.parseInt(pageSize));//分页
		cashLog.getPage().setPageNumber(Integer.parseInt(pageNO));
		
		List<CashLog> cashList = cashLogService.getAllCashLog(cashLog);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userIncome", member.getUserIncome() == null ? 0 : member.getUserIncome().intValue());	//当前可用金额
		map.put("cashlist", cashList);
		
		rm.setStatus(true);
		rm.setMessage(ResultInfo.SUCCESSINFO);
		rm.setData(map);
		return rm;
	}
}
