package com.iamberry.wechat.service.agent;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.apache.commons.lang.StringUtils;
import com.iamberry.wechat.core.entity.agent.AgentMemberDto;
import com.iamberry.wechat.core.entity.agent.OrderStatusDto;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.agent.AgentMemberService;
import com.iamberry.wechat.service.agent.dao.AgentMemberDAO;
import com.iamberry.wechat.service.agent.dao.AgentOrderDAO;
import com.iamberry.wechat.tools.StrUtils;

/**
 * 微代理会员中心	service实现类
 * 2016年4月20日
 * @author 穆再兴
 *
 */
@Service
public class AgentMemberServiceImpl implements AgentMemberService{
	@Autowired
	private AgentMemberDAO agentMemberDAO;
	@Autowired
	private AgentOrderDAO agentOrderDAO;
	
	/**
	 * 根据openid获取该用户 各种状态的订单数量
	 * 2016年4月20日
	 * @author 穆再兴
	 * @param openid
	 * @return
	 */
	
	public List<Map<String, Integer>> getMyOrderStatusCount(String openid) {
		return agentMemberDAO.getMyOrderStatusCount(openid);
	}
	/**
	 * 获取当前用户微代理信息
	 * @author 穆再兴
	 * 2016年4月20日
	 * @return
	 */
	
	public AgentMemberDto getAgentMemberInfo(String openid) {
		return agentMemberDAO.getAgentMemberInfo(openid);
	}
	
	/**
	 * 查询当前用户<微代理>的待返还金额 <包括下线将返的100 及下线的下线将返还的40>
	 * 2016年5月19日
	 * @author mzx
	 * @return
	 */
	public Integer getWillIncomeByOpenId(String openid){
		return agentMemberDAO.getWillIncomeByOpenId(openid);
	}
	@Override
	public AgentMemberDto getMemberInfo(Member member) {
		AgentMemberDto agentMember = new AgentMemberDto();
		/***********填充返回数据***********/
		agentMember.setUserHead(StrUtils.checkStr(member.getUserHead()));
		agentMember.setUserNickname(StrUtils.checkStr(member.getUserNickname()));
		agentMember.setUserIncome(member.getUserIncome() == null ? 0 : member.getUserIncome());
		agentMember.setUserOpenid(StrUtils.checkStr(member.getUserOpenid()));
		//各种状态订单的数量
		List<Map<String, Integer>> list = agentMemberDAO.getMyOrderStatusCount(member.getUserOpenid());
		OrderStatusDto statusDto = getOrderStatusDtoByMap(list);
		agentMember.setOrderStatusDto(statusDto);
		//当前用户的身份、微代理待返金额 
		if(member.getUserIdentity() !=null && member.getUserIdentity().intValue() == 2){//当前用户为微代理
			agentMember.setUserIdentity(2);
			Integer willIncome = agentMemberDAO.getWillIncomeByOpenId(member.getUserOpenid());
			agentMember.setWillIncome(willIncome == null ? 0 : willIncome.intValue());
		}else{//当前用户为普通会员
			agentMember.setUserIdentity(1);
		}
		return agentMember;
	}
	
	/**
	 * 将查询出来的订单状态 转换为 OrderStatusDto对象
	 * 2016年4月20日
	 * @author 穆再兴
	 * @param list
	 * @return
	 */
	private OrderStatusDto getOrderStatusDtoByMap(List<Map<String,Integer>> list){
		OrderStatusDto dto = new OrderStatusDto();
		int allCount = 0;
		for (Map<String, Integer> map : list) {
			Object obj = map.get("countAmount");
			Integer salesStatus = map.get("salesStatus");
			if(new Integer(1).equals(salesStatus)){
				if(obj != null && StringUtils.isNotEmpty(obj.toString())){
					dto.setPayCount(Integer.parseInt(obj.toString()));
				}
			}else if(new Integer(2).equals(salesStatus)){
				if(obj != null && StringUtils.isNotEmpty(obj.toString())){
					dto.setDeliverCount(Integer.parseInt(obj.toString()));
				}
			}else if(new Integer(3).equals(salesStatus)){
				if(obj != null && StringUtils.isNotEmpty(obj.toString())){
					dto.setReturnMoneyCount(Integer.parseInt(obj.toString()));
				}
			}else if(new Integer(5).equals(salesStatus)){
				if(obj != null && StringUtils.isNotEmpty(obj.toString())){
					dto.setDeliveredCount(Integer.parseInt(obj.toString()));
				}
			}else if(new Integer(7).equals(salesStatus)){
				if(obj != null && StringUtils.isNotEmpty(obj.toString())){
					dto.setReturnGoodsCount(Integer.parseInt(obj.toString()));
				}
			}else if(new Integer(12).equals(salesStatus)){
				if(obj != null && StringUtils.isNotEmpty(obj.toString())){
					dto.setFinishCount(Integer.parseInt(obj.toString()));
				}
			}
			if(obj != null){
				allCount += Integer.parseInt(obj.toString());
			}
		}
		dto.setAllCount(allCount);
		return dto;
	}
}
