package com.iamberry.wechat.service.member;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iamberry.wechat.core.entity.Page;
import com.iamberry.wechat.core.entity.drp.PlaceInfo;
import com.iamberry.wechat.core.entity.member.Member;
import com.iamberry.wechat.face.member.MemberService;
import com.iamberry.wechat.face.qrcode.QrcodeService;
import com.iamberry.wechat.service.mapper.PlaceInfoMapper;
import com.iamberry.wechat.service.member.dao.MemberDao;
import com.iamberry.wechat.service.system.dao.SystemDao;

/**
 * 会员信息 service 实现
 * 2016年4月21日
 * @author Administrator
 *
 */
@Service
public class MemberServiceImpl implements MemberService {
	@Autowired
	private MemberDao memberDao;
	@Autowired
	private PlaceInfoMapper placeInfoMapper;
	@Autowired
	public QrcodeService qrcodeService; 
	@Autowired
	public SystemDao systemDao;
	/**
	 * 根据userid得到会员信息
	 * @param userId
	 * @return
	 */
	@Override
	public Member getMemberByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return memberDao.getMemberByUserId(userId);
	}
	
	/**
	 * 根据 openid 查询一个member
	 * 2016年4月28日
	 * @author 穆再兴
	 * @return
	 */
	public Member getMemberByUserOpenId(String openid){
		return memberDao.getMemberByUserOpenId(openid);
	}
	@Override
	public Integer getCount() {
		return memberDao.getCount();
	}
	@Override
	public List<Member> getMemberByPage(Page page) {
		// TODO Auto-generated method stub		

		return memberDao.getMemberByPage(page);
	}
	@Override
	public Integer updateUserIntegral(Member member) {
		// TODO Auto-generated method stub
		return memberDao.updateUserIntegral(member);
	}
	/**
	 * 根据 userid 修改会员信息
	 * 2016年4月21日
	 * @author 穆再兴
	 * @param userId
	 * @return
	 */
	@Override
	public Integer updateMemberByUserId(Member member){
		return memberDao.updateMemberByUserId(member);
	}
	@Override
	public Integer updateUserUseIntegral(Member member) {
		// TODO Auto-generated method stub
		return memberDao.updateUserUseIntegral(member);
	}
	@Override
	public List<Member> selectUserIntegralByWechatProxyId(Page page) {
		// TODO Auto-generated method stub
		return memberDao.selectUserIntegralByWechatProxyId(page);
	}
	@Override
	public Member selectUserIdentityAndQrcodeByOpenId(String openid) {
		// TODO Auto-generated method stub
		return memberDao.selectUserIdentityAndQrcodeByOpenId(openid);
	}

	@Override
	public Member selectUserResAndIdSByOpenId(String openid) {
		// TODO Auto-generated method stub
		return memberDao.selectUserResAndIdSByOpenId(openid);
	}
	
	/**
	 * 根据 openid 修改用户可用金额
	 * 2016年5月4日
	 * @param member
	 * @return
	 */
	public Integer updateUserIncomeByOpenId(Member member){
		return memberDao.updateUserIncomeByOpenId(member);
	}

	@Override
	public boolean updateUserFlagByOpenId(String openId) {
		// TODO Auto-generated method stub
		return memberDao.updateUserFlagByOpenId(openId) >= 1 ? true : false;
	}
	
	/**
	 * 根据userid获取当前用户的“下线人数”和“已购买人数”
	 * 2016年5月11日
	 * @param openId
	 * @return
	 */
	public List<Integer> selectDownCountByUserId(Integer userId){
		return memberDao.selectDownCountByUserId(userId);
	}

	@Override
	public boolean buildChildUser(String shareOpenid, Member member,int userType) {
		// TODO Auto-generated method stub
		if(StringUtils.isNotEmpty(shareOpenid) && member != null){
			//绑定关系
			if(!shareOpenid.equals(member.getUserOpenid())){//用户点击自己分享的链接则不绑定
				boolean flag = false;
				if(userType == 1){
					//绑定为微代理下线
					flag = qrcodeService.bulidUser(member.getUserOpenid(), shareOpenid, 1);
				}else if(userType == 2){
					PlaceInfo placeInfo = placeInfoMapper.getOnePlaceInfo2(shareOpenid);
					//内容格式：商户层可以绑定关系的角色id:41,42	41 42 分别为可以绑定关系的角色id
					String str = systemDao.selectOneShopRuleById(121).getRuleDesc();
//System.out.println("--systemDao.selectOneShopRuleById(121).getRuleDesc()-------------------------" + str);
					String rolesStr = str.split(":")[1];
					String[] roleArr = rolesStr.split(",");
					if(placeInfo != null && 
							Arrays.asList(roleArr).contains(Integer.toString(placeInfo.getRoleId()))){
						//改商户存在并且为可以绑定关系的就是 则绑定为其下线
						flag = qrcodeService.bulidUser(member.getUserOpenid(), placeInfo.getId(), 2);
					}
				}
				return flag;
			}
		}
		return false;
	}

	@Override
	public Map<String, Object> getDownInfoAndBuyedCount(Page page) {
		List<Member> list = memberDao.selectUserIntegralByWechatProxyId(page);
		
		List<Integer> countList = memberDao.selectDownCountByUserId(page.getTotalRecord());	//page.getTotalRecord 用户userid
		int downNum = 0;		//下线人数
		int buyedNum = 0;		//已购买人数
		if(countList.size() == 2){
			downNum = countList.get(0);
			buyedNum = countList.get(1);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("downNum", downNum);
		map.put("buyedNum", buyedNum);
		if(list.size()>0){
			map.put("beginNo", list.get(0).getUserId());
			map.put("endNo", list.get(list.size()-1).getUserId());
		}
		map.put("list", list);
		return map;
	}
}
