package com.iamberry.wechat.handles.giftCard;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
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.ResponseBody;

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.giftCard.AgentInfo;
import com.iamberry.wechat.core.entity.giftCard.AgentPayLogs;
import com.iamberry.wechat.face.giftCard.AgentService;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.StrUtils;

/**
 * 代理商	handler
 * 2017年2月16日
 * @author muzx
 */
@Controller
@RequestMapping("/wechat/agent_pay")
public class AgentInfoHandler {
	@Autowired
	private AgentService agentService;
	/**
	 * 查询当前代理商的昵称和头像
	 * 2017年2月16日
	 * @author muzx
	 * @return
	 */
	@RequestMapping("/agent_info")
	@ResponseBody
	public ResultMsg getAgentInfo(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		rm.setStatus(false);
		rm.setMessage("获取用户信息失败!");
		String openId = WechatUtils.getUserBySession(req).getUserOpenid();
		AgentInfo agent = agentService.selectAgentByOpenId(openId);
		if(agent != null){
			rm.setStatus(true);
			rm.setMessage("获取用户信息成功!");
			rm.setData(agent);
		}
		return rm;
	}
	
	/**
	 * 根据单号获取支付记录
	 * 单号格式：AOID000_ + 支付记录id + "_" + 代理商物流记录id + ("_" + 推荐人物流记录id)<若无推荐人则无此项>
	 * 2017年2月20日
	 * mzx
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/check_agent_pay_success")
	public ResultMsg checkAgentPaySuccess(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		String orderId = req.getParameter("orderId");
		if(StringUtils.isEmpty(orderId)){
			rm.setStatus(false);
			rm.setResultCode("4000");
			rm.setMessage("订单单号不能为空!");
		}
		String[] arrs = StrUtils.strToArr(orderId, "_");
		if(arrs == null || arrs.length < 1){
			rm.setStatus(false);
			rm.setResultCode("4000");
			rm.setMessage("订单单号不能为空!");
		}
		AgentPayLogs payLog = agentService.selectPayLogById(Integer.parseInt(arrs[1]));
		if(payLog == null){
			rm.setStatus(false);
			rm.setResultCode("4001");
			rm.setMessage("该订单不存在!");
		}else{
			rm.setStatus(true);
			rm.setResultCode(ResultInfo.SUCCESSCODE);
			rm.setMessage(ResultInfo.SUCCESSCODE);
			rm.setData(payLog.getPayStatus());
		}
		return rm;
	}
	
}
