package com.iamberry.wechat.handles.agent;

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

import com.iamberry.wechat.core.entity.ResultMsg;
import com.iamberry.wechat.core.entity.WechatUtils;
import com.iamberry.wechat.core.entity.agent.AgentMemberDto;
import com.iamberry.wechat.core.entity.agent.AgentOrderDto;
import com.iamberry.wechat.core.entity.agent.OrderStatusDto;
import com.iamberry.wechat.core.entity.cart.ShopSalesOrderDto;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.core.entity.mq.MQMessage;
import com.iamberry.wechat.face.agent.AgentMemberService;
import com.iamberry.wechat.face.agent.AgentOrderService;
import com.iamberry.wechat.face.cart.CartService;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.mq.MQSerivce;
import com.iamberry.wechat.face.qrcode.QrcodeService;
import com.iamberry.wechat.handles.mq.MQServiceProxy;
import com.iamberry.wechat.tools.ResultInfo;
import com.iamberry.wechat.tools.StrUtils;

/**
 * 微代理 handler 类
 * @author 穆再兴
 *	2016年4月19日
 */
@Controller
@RequestMapping("/wechat/agent")
public class AgentHandler {
	@Autowired
	private AgentOrderService agentOrderService;	//微代理订单service
	@Autowired
	private MemberService memberService;			//会员service
	@Autowired
	private AgentMemberService agentMemberService;	//微代理会员service
	@Autowired
	private WechatUtils wechatUtils;
	@Autowired
	private QrcodeService qrcodeService;
	@Autowired
	private CartService cartService;
	@Autowired
	private MQServiceProxy mQservice;
	/**
	 * 获取当前用户<微代理> 的订单记录
	 * 2016年4月19日
	 * @author 穆再兴
	 * @return json格式
	 */
	@RequestMapping("/agentOrder")
	@ResponseBody
	public ResultMsg getAgentOrderJson(HttpServletRequest req,
			@RequestParam(value="rebackStatus",defaultValue= "4", required=false)Integer rebackStatus,
			@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();
		try {
			member = wechatUtils.getUserBySession(req);
			if(member == null){
				rm.setMessage(ResultInfo.loginOutError);
				return rm;
			}
			member = memberService.getMemberByUserOpenId(member.getUserOpenid());
			if(member == null){ member = new Member();}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//查当前用户<微代理>userid 所有订单
		AgentOrderDto orderDto = new AgentOrderDto();
		orderDto.setOrderOpenid(member.getUserOpenid());
		orderDto.getPage().setPageSize(Integer.parseInt(pageSize));
		orderDto.getPage().setPageNumber(Integer.parseInt(pageNO));
		orderDto.setRebackStatus(rebackStatus);
		
		// 我加的
		orderDto.getPage().initRecordBegin();
		
		Map<String, Object> map = agentOrderService.getOrderListAndCountByOpenid(orderDto);
		map.put("userIncome", member.getUserIncome() == null ? 0 : member.getUserIncome().intValue());
		rm.setResultCode(ResultInfo.SUCCESSCODE);
		rm.setMessage(ResultInfo.SUCCESSINFO);
		rm.setStatus(true);
		rm.setData(map);
		return rm;
	}
	
	/**
	 * 会员中心页面
	 * 2016年4月21日
	 * @author 穆再兴
	 * @return
	 */
	@RequestMapping("/member")
	@ResponseBody
	public ResultMsg getMemberInfo(HttpServletRequest req){
		ResultMsg rm = new ResultMsg();
		Member member = new Member();
		try {
			member = wechatUtils.getUserBySession(req);
			if(member == null){
				rm.setMessage(ResultInfo.loginOutError);
				rm.setStatus(false);
				return rm;
			}
			member = memberService.getMemberByUserOpenId(member.getUserOpenid());
			if(member == null){ member = new Member();}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		AgentMemberDto agentMember = agentMemberService.getMemberInfo(member);

		ShopSalesOrderDto shopSalesOrderDto = new ShopSalesOrderDto();
		shopSalesOrderDto.setSalesOpenid(member.getUserOpenid());
		List<ShopSalesOrderDto> orderList = cartService.getOrderList(shopSalesOrderDto);
		if (null != orderList && orderList.size() > 0) {
			for (ShopSalesOrderDto orderDto : orderList) {
				MQMessage message = new MQMessage();
				if ("2".equals(orderDto.getSalesStatus())) {
					message.setServiceToMessage(orderDto.getSalesOrderid());
					message.setServiceOtherMessage("efast.trade.detail.get");
					message.setServiceIsSend(2);			// 没有推送的信息
					message.setServiceStatus(1); 			// 推送成功
					message.setServiceType(1); 				// 添加订单信息
					message.setServiceHandlerObjectName("efastOrderServiceImpl");
					message.setServiceHandlerMethodName("selectEfastSendOrderInfo");
					mQservice.insertMQMessage(message);
				} else if ("8".equals(orderDto.getSalesStatus())) {
					message.setServiceToMessage(orderDto.getSalesOrderid());
					message.setServiceOtherMessage("efast.order.return.info.fetch");
					message.setServiceIsSend(2);			// 没有推送的信息
					message.setServiceStatus(1); 			// 推送成功
					message.setServiceType(1); 				// 添加订单信息
					message.setServiceHandlerObjectName("efastOrderServiceImpl");
					message.setServiceHandlerMethodName("selectEfastReturnOrderInfo");
					mQservice.insertMQMessage(message);
				}
			}
		}
		
		rm.setMessage(ResultInfo.SUCCESSINFO);
		rm.setStatus(true);
		rm.setData(agentMember);
		return rm;
	}
	
	/**
	 * 前台根据openid获取用户信息
	 * create date 2016年6月2日
	 * @param openid
	 * @return
	 */
	@RequestMapping("/memberInfo")
	@ResponseBody
	public ResultMsg getMemberInfo(HttpServletRequest req,
			@RequestParam(value="openid",defaultValue= "", required=false)String openid){
		ResultMsg rm = new ResultMsg();
		Member member = null;
		if(StringUtils.isEmpty(openid)){
			try {
				member = wechatUtils.getUserBySession(req);
				if(member == null){
					rm.setMessage(ResultInfo.loginOutError);
					rm.setStatus(false);
					return rm;
				}
				member = memberService.getMemberByUserOpenId(member.getUserOpenid());
				if(member == null){ member = new Member();}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			member = memberService.getMemberByUserOpenId(openid);
		}
		
		rm.setStatus(true);
		rm.setResultCode(ResultInfo.SUCCESSCODE);
		rm.setMessage(ResultInfo.SUCCESSINFO);
		rm.setData(member);
		return rm;
	}
	
}
