package com.buy507.mall.portal.service.impl;

import java.math.BigDecimal;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buy507.mall.mapper.DmsTeamLevelMapper;
import com.buy507.mall.mapper.DmsTeamMemberNumberMapper;
import com.buy507.mall.mapper.DmsTeamTotalConsumeMapper;
import com.buy507.mall.mapper.DmsTeamTotalIncomeMapper;
import com.buy507.mall.model.DmsTeamLevel;
import com.buy507.mall.model.DmsTeamMemberNumber;
import com.buy507.mall.model.DmsTeamTotalConsume;
import com.buy507.mall.model.DmsTeamTotalIncome;
import com.buy507.mall.model.MemberLevel;
import com.buy507.mall.model.UmsMember;
import com.buy507.mall.portal.service.InitDistributionService;

@Service
public class InitDistributionServiceImpl implements InitDistributionService {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(InitDistributionServiceImpl.class);
	
	@Autowired
	private DmsTeamLevelMapper teamLevelMapper;
	
	@Autowired
	private DmsTeamTotalIncomeMapper teamTotalIncomeMapper;
	
	@Autowired
	private DmsTeamTotalConsumeMapper teamTotalConsumeMapper;
	
	@Autowired
	private DmsTeamMemberNumberMapper teamMemberNumberMapper;

	/**
	 * 计算上级团队成员数量
	 * @param member
	 */
	
	@Override
	public void updateUpTeamMemberNumberForAddInviter(UmsMember member) {
		/**
		 * 重新计算上级团队的团队成员数量
		 * 团队成员数量表
		 * 先查询出自己的下级团队成员数量表，再递归更新上级团队成员的数量表
		 */
		LOGGER.info("updateUpTeamMemberNumber start: {}", member.getUsername());
		LOGGER.info("updateUpTeamMemberNumber memberId: {}, memberLevel: {}", member.getId(), member.getMemberLevel());
		DmsTeamMemberNumber teamMemberNumber = teamMemberNumberMapper.selectOneByMemberId(member.getId());
		int total = 0;
		int consumer = 0;
		int partner = 0;
		int oneDiamond = 0;
		int twoDiamond = 0;
		int threeDiamond = 0;
		int fourDiamond = 0;
		if(teamMemberNumber != null) {
			total = teamMemberNumber.getTotal();
			consumer = teamMemberNumber.getConsumer();
			partner = teamMemberNumber.getPartner();
			oneDiamond = teamMemberNumber.getOneDiamond();
			twoDiamond = teamMemberNumber.getTwoDiamond();
			threeDiamond = teamMemberNumber.getThreeDiamond();
			fourDiamond = teamMemberNumber.getFourDiamond();
		}
		
		// 判断自己属于那个等级，在相应的人数上加1，如果自己是普通会员，则只加在total 上加1
		total += 1;
		if(MemberLevel.Consumer.equals(member.getMemberLevel())) {
			consumer += 1;
		} else if(MemberLevel.Partner.equals(member.getMemberLevel())) {
			partner += 1;
		} else if(MemberLevel.OneDiamond.equals(member.getMemberLevel())) {
			oneDiamond += 1;
		} else if(MemberLevel.TwoDiamond.equals(member.getMemberLevel())) {
			twoDiamond += 1;
		} else if(MemberLevel.ThreeDiamond.equals(member.getMemberLevel())) {
			threeDiamond += 1;
		} else if(MemberLevel.FourDiamond.equals(member.getMemberLevel())) {
			fourDiamond += 1;
		}
		
		//递归更新上级团队成员的数量表
		recursionUpdateUpTeamMemberNumber(member.getId(), total, consumer, partner, oneDiamond, twoDiamond, threeDiamond, fourDiamond);
		
		LOGGER.info("updateUpTeamMemberNumber end: {}", member.getUsername());
		
	}
	
	private void recursionUpdateUpTeamMemberNumber(Long memberId, int total, int consumer, int partner, 
			int oneDiamond, int twoDiamond, int threeDiamond, int fourDiamond) {
		DmsTeamLevel teamLevel = teamLevelMapper.selectOneByMemberId(memberId);
		if(teamLevel == null) {
			return;
		} else {
			DmsTeamMemberNumber memberNumber = teamMemberNumberMapper.selectOneByMemberId(teamLevel.getParentId());
			memberNumber.setTotal(memberNumber.getTotal() + total);
			memberNumber.setConsumer(memberNumber.getConsumer() + consumer);
			memberNumber.setPartner(memberNumber.getPartner() + partner);
			memberNumber.setOneDiamond(memberNumber.getOneDiamond() + oneDiamond);
			memberNumber.setTwoDiamond(memberNumber.getTwoDiamond() + twoDiamond);
			memberNumber.setThreeDiamond(memberNumber.getThreeDiamond() + threeDiamond);
			memberNumber.setFourDiamond(memberNumber.getFourDiamond() + fourDiamond);
			
			teamMemberNumberMapper.updateById(memberNumber);
			recursionUpdateUpTeamMemberNumber(teamLevel.getParentId(), total, consumer, partner, oneDiamond, twoDiamond, threeDiamond, fourDiamond);
		}
		
	}
	

	@Override
	public void initDistributionTable(Long memberId) {
		BigDecimal decimal = new BigDecimal(0);
		
		//团队成员数量表
		DmsTeamMemberNumber teamMemberNumber = new DmsTeamMemberNumber();
		teamMemberNumber.setMemberId(memberId);
		teamMemberNumber.setTotal(0);
		teamMemberNumber.setConsumer(0);
		teamMemberNumber.setPartner(0);
		teamMemberNumber.setOneDiamond(0);
		teamMemberNumber.setTwoDiamond(0);
		teamMemberNumber.setThreeDiamond(0);
		teamMemberNumber.setFourDiamond(0);
		teamMemberNumberMapper.insert(teamMemberNumber);
		
		//团队累计消费表
		DmsTeamTotalConsume teamTotalConsume = new DmsTeamTotalConsume();
		teamTotalConsume.setMemberId(memberId);
		teamTotalConsume.setValue(decimal);
		teamTotalConsumeMapper.insert(teamTotalConsume);
		
		//团队总收入表
		DmsTeamTotalIncome teamTotalIncome = new DmsTeamTotalIncome();
		teamTotalIncome.setMemberId(memberId);
		teamTotalIncome.setValue(decimal);
		teamTotalIncomeMapper.insert(teamTotalIncome);
		
	}
	
}
