package com.cku.controller;

import java.io.IOException;
import java.util.List;

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

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 com.cab.model.User;
import com.cab.service.UserService;
import com.cku.core.RESTResponse;
import com.cku.model.CkuMemberBalance;
import com.cku.model.CkuUserPayList;
import com.cku.service.CkuDvUserServiceImpl;
import com.cku.service.CkuMemberBalanceServiceImpl;
import com.cku.util.PageBean;
import com.cku.util.ServletUtils;

@Controller
@RequestMapping("cku")
public class CkuMemberLoginController {
	
	@Autowired
	public CkuDvUserServiceImpl ckuDvUserServiceImpl;
	
	@Autowired
	public CkuMemberBalanceServiceImpl ckuMemberBalanceService;
	/**
	 * 
	 * @Description：以CKU会员登录验证
	 * @author: zhuoHeng
	 * @version: 2016年4月11日 上午9:31:31
	 */
	@RequestMapping("/ckuLogin")
	public void ckuMemberLogin(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "flag", required = false)String flag)throws IOException {
		RESTResponse result = null;
		try {
			String userName = ServletUtils.getParameter(request, "userName");
			String passWord = ServletUtils.getParameter(request, "passWord");
			String dynamic = ServletUtils.getParameter(request, "dynamic");
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html");
			String ip = request.getRemoteAddr();
			User user = ckuDvUserServiceImpl.ckuMemberLogin(userName,passWord,ip,flag,dynamic);
			result = new RESTResponse("item", user);
		}catch (Exception e){
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}
	
	/**
	 * 
	 * @Description：查询CKU会员账户余额
	 * @author: zhuoHeng
	 * @version: 2016年4月14日 上午10:07:16
	 */
	@RequestMapping("/ckuBalance")
	public void getCkuMemberBalance(HttpServletResponse response,HttpServletRequest request) throws IOException{
		RESTResponse result = null;
		try
		{
			Long userId = UserService.verifyUserId(request);
			CkuMemberBalance obj = ckuMemberBalanceService.getCkuMemberBalance(new Long(userId).intValue());
			result = new RESTResponse("item", obj);
		}
		catch (Exception e)
		{
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}

	
	/**
	 * 
	 * @Description：查询CKU会员我的账户余额收支详情
	 * @author: zhuoHeng
	 * @version: 2016年4月14日 下午3:46:53
	 */
	@RequestMapping("/getCkuBalanceInfo")
	public void getCkuBalanceInfo(HttpServletResponse response,HttpServletRequest request) throws IOException{
		RESTResponse result = null;
		try
		{
			Long userId = UserService.verifyUserId(request);
			PageBean pageBean = ServletUtils.getParameterBean(request);
			String flag = ServletUtils.getParameter(request, "flag","all");
			List<CkuUserPayList> obj = ckuMemberBalanceService.getCkuBalanceInfo(new Long(userId).intValue(),pageBean,flag);
			result = new RESTResponse("item", obj);
		}
		catch (Exception e)
		{
			result = new RESTResponse(e);
		}
		
		ServletUtils.writeResponse(response, result);
	}

}
