package com.bizzan.bitrade.service;

import com.bizzan.bitrade.dao.MemberWeightUpperDao;
import com.bizzan.bitrade.entity.Member;
import com.bizzan.bitrade.entity.MemberWeightUpper;
import com.bizzan.bitrade.pagination.Criteria;
import com.bizzan.bitrade.pagination.Restrictions;
import com.bizzan.bitrade.service.Base.BaseService;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Visitor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author sulinxin
 */
@Service
public class MemberWeightUpperService extends BaseService {

    @Autowired
    private MemberWeightUpperDao memberWeightUpperDao;
    @Autowired
    private MemberService memberService;

    public MemberWeightUpper findMemberWeightUpperByMemberId(Long memberId) {
        Criteria<MemberWeightUpper> specification = new Criteria<MemberWeightUpper>();
        specification.add(Restrictions.eq("memberId", memberId, false));
        return memberWeightUpperDao.findOne(specification);
    }
    
    public int findMinUpperWcRate(Long memberId) {
    	MemberWeightUpper memberWeightUpper = this.findMemberWeightUpperByMemberId(memberId);
    	int minWcRate = 100;
    	if(memberWeightUpper.getAllUppers() == null)
    		return minWcRate;
    	List<MemberWeightUpper> allUppers = findAllByUpperIds(memberWeightUpper.getAllUppers());
    	for(MemberWeightUpper upper: allUppers) {
    		if(upper.getWcRate()!=null && upper.getWcRate()!=0 && upper.getWcRate()<minWcRate) {
    			minWcRate = upper.getWcRate();
    		}
    	}
    	if(minWcRate==100)
    		return 0;
    	else
    		return minWcRate;
    }

    public List<MemberWeightUpper> findAllByUpperIds(String uppers) {
        String[] idss = uppers.split(",");
        List<Long> ids = new ArrayList<>();
        for(String id:idss){
            ids.add(Long.parseLong(id));
        }
        return memberWeightUpperDao.findAllByUpperIds(ids);
    }
    public List<MemberWeightUpper> findAllByUpper(Long upper) {
        return memberWeightUpperDao.findAllByUpper(upper+"");
    }
    
    public List<MemberWeightUpper> findAllByAllUppers(Long upper) {
        return memberWeightUpperDao.findAllByAllUppers(upper+"");
    }
    
    public List<Long> findMemberIdListByAllUppers(Long upper) {
    	List<MemberWeightUpper> subList = memberWeightUpperDao.findAllByAllUppers(upper+"");
    	List<Long> subIds = new ArrayList<Long>();
        for(MemberWeightUpper sub: subList)
        	subIds.add(sub.getMemberId());
        return subIds;
    }
    
    public int countRegisteredMembersByMemberIdsAndTime(List<Long> ids, Date startDate, Date endDate) {
    	return memberWeightUpperDao.countRegisteredMembersByMemberIdsAndTime(startDate, endDate, ids);
    }
    
    public void cancelSuperPartner(Long upper) {
    	List<Long> ids = this.findMemberIdListByAllUppers(upper);
    	ids.add(upper);
    	memberWeightUpperDao.cancelSuperPartner(ids);
    	for(Long id: ids) {
    		memberService.updateSuperPartner(id, "0");
    	}
    }
    
    public void save(MemberWeightUpper memberWeightUpper) {
    	memberWeightUpperDao.save(memberWeightUpper);
        memberService.updateSuperPartner(memberWeightUpper.getMemberId(), (memberWeightUpper.getRate()==0?"0":"1"));
    }

    public MemberWeightUpper saveMemberWeightUpper(Member member) {
        MemberWeightUpper memberWeightUpper = this.findMemberWeightUpperByMemberId(member.getId());
        if(memberWeightUpper!=null){
        	memberWeightUpper.setUpper(member.getInviterId()+"");
        	
            MemberWeightUpper upper = this.findMemberWeightUpperByMemberId(member.getInviterId());
            
            String allUppers = upper.getAllUppers();
            if(allUppers==null || "".equals(allUppers.trim())){
            	allUppers = upper.getMemberId().toString();
            }else {
            	allUppers = allUppers+","+upper.getMemberId();
            }
            memberWeightUpper.setAllUppers(allUppers);
            memberWeightUpperDao.save(memberWeightUpper);
            
            List<MemberWeightUpper> lowerUppers = this.findAllByAllUppers(member.getId());
            for(MemberWeightUpper lowerUpper: lowerUppers) {
	            lowerUpper.setAllUppers(allUppers+","+lowerUpper.getAllUppers());
	        	
	        	memberWeightUpperDao.save(lowerUpper);
            }
            return memberWeightUpper;
        }
        memberWeightUpper = new MemberWeightUpper();
        //找上级 如果有上级
        if(member.getInviterId()!=null){
            Member inviter = memberService.findOne(member.getInviterId());
            //有上级
            MemberWeightUpper upper = this.findMemberWeightUpperByMemberId(member.getInviterId());
            memberWeightUpper.setFirstMemberId(upper.getFirstMemberId());
            memberWeightUpper.setRate(0);
            memberWeightUpper.setFollowRate(0);
            memberWeightUpper.setDividendRate(0);
            memberWeightUpper.setMemberId(member.getId());
            String allUppers = upper.getAllUppers();
            if(allUppers==null || "".equals(allUppers.trim())){
            	allUppers = upper.getMemberId().toString();
            }else {
            	allUppers = allUppers+","+upper.getMemberId();
            }
            memberWeightUpper.setUpper(member.getInviterId()+"");
            memberWeightUpper.setAllUppers(allUppers);
        }else {
            //最上级
            //if("1".equals(member.getSuperPartner())){
            //    memberWeightUpper.setRate(100);
            //}else {
            memberWeightUpper.setRate(0);
            memberWeightUpper.setFollowRate(0);
            memberWeightUpper.setDividendRate(0);
            //}
            memberWeightUpper.setFirstMemberId(member.getId());
            memberWeightUpper.setMemberId(member.getId());
            memberWeightUpper.setUpper(null);

        }
        memberWeightUpper = memberWeightUpperDao.save(memberWeightUpper);
        return memberWeightUpper;
    }
}
