package com.iamberry.wechat.handles.admin;

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

import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.PaperBean;
import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.member.Withdraw;
import com.iamberry.wechat.face.member.WithdrawService;
/**
 * @author 何秀刚
 * Class Description: 后台提现记录
 * Create Date:2016年4月21日
 * Update Date:2016年4月21日
 */
@RequestMapping("/admin/withdraw")
@Controller
public class AdminWithdrawHandler {
	
	@Autowired
	private WithdrawService withdrawService;

	/**
	 * 后台-记录页面
	 * @param pageNO
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/index")
	public ModelAndView index(
			@RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
			@RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
			HttpServletRequest request
			) {
		ModelAndView mv = new ModelAndView("admin/withdrawlogs/withdrawLogsList");
		Page page = new Page();
		page.setPageNo((pageNO - 1) * pageSize);
		page.setPageSize(pageSize);
		// JSP 数据
		mv.addObject("list", withdrawService.selectLogsByOpenid(page));
		
		// 分页数据
		mv.addObject("pageSize", PaperBean.justPaperCount(withdrawService.selectLogsCountByOpenid(page), pageSize));
		mv.addObject("pageNO", pageNO);
		mv.addObject("url", request.getContextPath() + "/admin/withdraw/index?pageSize=" + pageSize + "&pageNO=");
		return mv;
	}

	@RequestMapping("/updateWithdrawStatus")
	@ResponseBody
	public ResultMsg updateWithdrawStatus(
			@RequestParam(value = "withdrawId", required = true) Integer withdrawId,
			@RequestParam(value = "whthdrawStatus", required = true) Integer whthdrawStatus // 1通过;3:驳回
			) {
		ResultMsg msg = new ResultMsg();
		msg.setStatus(false);
		if (whthdrawStatus != 1 && whthdrawStatus != 3) return msg;
		
		Withdraw withdraw = withdrawService.selectWithdrawLogsById(withdrawId); // 查询
		
		if (withdraw == null)return msg;
		
		if (withdraw.getWithdrawalsStatus() != 2)return msg;
		
		try {
			boolean flag = withdrawService.handleWithdrawStatus(whthdrawStatus, withdraw);
			msg.setStatus(flag);
		} catch (Exception e) {
			msg.setStatus(false);
			msg.setMessage(e.getMessage());
		}
		
		return msg;
	}
	
	/**
	 * 后台-记录页面
	 * @param pageNO
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/indexCash")
	public ModelAndView indexCash(
			@RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
			HttpServletRequest request
			) {
		ModelAndView mv = new ModelAndView("admin/withdrawlogs/withdrawCashLogsList");
		Page page = new Page();
		page.setPageNo((pageNO - 1) * pageSize);
		page.setPageSize(pageSize);
		// JSP 数据
		mv.addObject("list", withdrawService.selectLogsByOpenid(page));
		
		// 分页数据
		mv.addObject("pageSize", PaperBean.justPaperCount(withdrawService.selectLogsCountByOpenid(page), pageSize));
		mv.addObject("pageNO", pageNO);
		mv.addObject("url","/admin/withdraw/indexCash?pageSize=" + pageSize + "&pageNO=");
		return mv;
	}

	
	/**
	 * 提现搜索
	 * @param pageNO
	 * @param pageSize
	 * @param request
	 * @param beginDate
	 * @param endDate
	 * @param userInput
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/seachInfoHistory", method=RequestMethod.GET)
	public ModelAndView seachInfoHistory(
			@RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
			@RequestParam(value = "pageSize", defaultValue = "10", required = false) int pageSize,
			HttpServletRequest request,
			String beginDate,
			String endDate,
			String status,
			String userInput)throws Exception{
				//获取请求页面时的URL
				Page page = new Page();
				//写入参数
				Map params=new HashMap<String, Object>();
				params.put("userInput",userInput);
				params.put("beginDate",beginDate);
				params.put("endDate",endDate);
				params.put("status",status);
				
				page.setPageSize(0);
				page.setPageNo(0);
				page.setParams(params);
				List<Withdraw> withdrawList = withdrawService.selectWithDrawAllSumMoney(page);
				
				int count = withdrawList.get(0).getWithdrawalsLogsIntegralNum();		//总数量	
				
				//分页查询
				page.setPageNo((pageNO - 1) * pageSize);//
				page.setPageSize(pageSize);				
				int pageCount = count % pageSize == 0 ? count / pageSize : count/ pageSize + 1;
				List<Withdraw> list = withdrawService.selectWithDraw(page);
				
				ModelAndView mv = new ModelAndView();
				//写入页面需要参数
				mv.addObject("userInput", userInput);
				mv.addObject("beginDate", beginDate);
				mv.addObject("endDate", endDate);
				
				mv.addObject("list", list);
				mv.addObject("pageNO", pageNO);
				mv.addObject("pageSize", pageCount);
				StringBuffer url=new StringBuffer("/admin/withdraw/seachInfo?");
				url.append("pageSize="+pageSize);
				if(userInput!=null){
					url.append("&userInput="+userInput);
				}
				if(userInput!=null){
					url.append("&beginDate="+beginDate);
				}
				if(userInput!=null){
					url.append("&endDate="+endDate);
				}
				
				for(Withdraw withdraw :withdrawList){
					Integer ii = withdraw.getWithdrawalsStatus();
					if(ii == 1){ //等于1 成功的
						mv.addObject("sumMoney",withdraw.getSumMoney());
					}
					if(ii == 3){ //等于1 成功的
						mv.addObject("sumMoney3",withdraw.getSumMoney());
					}
				}
				
				url.append("&pageNO=");
				mv.addObject("url",url.toString());
//				mv.addObject("url","/admin/withdraw/seachInfo?pageSize=" + pageSize + "&pageNO=");
//System.out.println("url============================"+url);
				mv.setViewName("/admin/withdrawlogs/withdrawHistory");
				return mv;
		
	}
	
	
	/**
	 * 提现搜索
	 * @param pageNO
	 * @param pageSize
	 * @param request
	 * @param beginDate
	 * @param endDate
	 * @param userInput
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/seachInfo", method=RequestMethod.GET)
	public ModelAndView seachInfo(
			@RequestParam(value = "pageNO", defaultValue = "1", required = false) int pageNO,
			@RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
			HttpServletRequest request,
			String status,
			String beginDate,
			String endDate,
			String userInput)throws Exception{
				//获取请求页面时的URL
				Page page = new Page();
				//写入参数
				Map params=new HashMap<String, Object>();
				params.put("beginDate",beginDate);
				params.put("endDate",endDate);
				params.put("userInput",userInput);
				params.put("status",status);
				page.setPageSize(0);
				page.setPageNo(0);
				page.setParams(params);
				List<Withdraw> withdrawList = withdrawService.selectWithDraw(page);
				int count = withdrawList.size();			//总数量		
				//分页查询
				page.setPageNo((pageNO - 1) * pageSize);//
				page.setPageSize(pageSize);				
				int pageCount = count % pageSize == 0 ? count / pageSize : count/ pageSize + 1;
				List<Withdraw> list = withdrawService.selectWithDraw(page);
				
			
				
				ModelAndView mv = new ModelAndView();
				//写入页面需要参数
				mv.addObject("userInput", userInput);
				mv.addObject("beginDate", beginDate);
				mv.addObject("endDate", endDate);
				
				mv.addObject("list", list);
				mv.addObject("pageNO", pageNO);
				mv.addObject("pageSize", pageCount);
				StringBuffer url=new StringBuffer("/admin/withdraw/seachInfo?");
				url.append("pageSize="+pageSize);
				if(userInput!=null){
					url.append("&userInput="+userInput);
				}
				if(userInput!=null){
					url.append("&beginDate="+beginDate);
				}
				if(userInput!=null){
					url.append("&endDate="+endDate);
				}
				url.append("&pageNO=");
				mv.addObject("url",url.toString());
//				mv.addObject("url","/admin/withdraw/seachInfo?pageSize=" + pageSize + "&pageNO=");
//System.out.println("url============================"+url);
				mv.setViewName("/admin/withdrawlogs/withdrawCashLogsList");
				return mv;
		
	}
	
	@RequestMapping("/updateWithdrawCashStatus")
	@ResponseBody
	public ResultMsg updateWithdrawCashStatus(
			@RequestParam(value = "withdrawId", required = true) Integer withdrawId,
			@RequestParam(value = "whthdrawStatus", required = true) Integer whthdrawStatus // 1通过;3:驳回
			,@RequestParam(value = "whthdrawRemark", required = true) String whthdrawRemark 
			) {
		ResultMsg msg = new ResultMsg();
		msg.setStatus(false);
		if (whthdrawStatus != 1 && whthdrawStatus != 3) return msg;
		
		Withdraw withdraw = withdrawService.selectWithdrawLogsById(withdrawId); // 查询
		
		if (withdraw == null)return msg;
		
		if (withdraw.getWithdrawalsStatus() != 2)return msg;
		
		try {
			withdraw.setWithdrawalsRemark(whthdrawRemark);
			boolean flag = withdrawService.handleWithdrawCashStatus(whthdrawStatus, withdraw);
			msg.setStatus(flag);
		} catch (Exception e) {
			msg.setStatus(false);
			msg.setMessage(e.getMessage());
		}
		
		return msg;
	}
	
	
	@RequestMapping("/updateWithdrawCashRemark")
	@ResponseBody
	public ModelAndView updateWithdrawCashRemark(
			@RequestParam(value = "withdrawId", required = true) Integer withdrawId,
			@RequestParam(value = "whthdrawStatus", required = true) Integer whthdrawStatus // 1通过;3:驳回
			) {
		ModelAndView mv = new ModelAndView("/admin/withdrawlogs/withdrawCashLogsRemark");
		//写入页面需要参数
		mv.addObject("withdrawId", withdrawId);
		mv.addObject("whthdrawStatus", whthdrawStatus);
		return mv;
	}
}
