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

import com.buy507.mall.mapper.*;
import com.buy507.mall.model.*;
import com.buy507.mall.portal.service.MemberCommissionService;
import com.google.common.collect.Lists;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class MemberCommissionServiceImpl implements MemberCommissionService {
    /**
     * 会员上下关系
     */
    @Resource
    private UmsMemberRelationTreeMapper umsMemberRelationTreeMapper;

    @Resource
    private UmsMemberMapper memberMapper;

    @Resource
    private OmsOrderMapper orderMapper;

    @Async
    @Override
    public void computeCommission(UmsMember higherMember) {
        //计算会员升级
        UmsMemberRelationTreeExample treeExample  = new UmsMemberRelationTreeExample();
        treeExample.createCriteria().andHigherLevelIdEqualTo(higherMember.getId());
        List<UmsMemberRelationTree> directMemberList = umsMemberRelationTreeMapper.selectByExample(treeExample);

        if(directMemberList.isEmpty()){
            return;
        }
        switch (higherMember.getMemberLevel()) {
            case Consumer:
                //vip会员升级逻辑
                this.vipMemberUpgrade(higherMember, directMemberList);
                break;
            case Partner:
                //店长会员升级逻辑
                this.partnerMemberUpgrade(higherMember, directMemberList);
                break;
            case Diamond:
                //代理会员升级逻辑
                this.diamondMemberUpgrade(higherMember, directMemberList);
                break;
        }
    }

    private static BigDecimal VIP_UPGRADE_STANDARDS_AMOUNT  = new BigDecimal("19000");

    private void vipMemberUpgrade(UmsMember higherMember, List<UmsMemberRelationTree> directMemberList) {
        int i = 0;
        //达标下属数量
        //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
        List<Integer> stats = Lists.newArrayList(0, 4,5);
        for (UmsMemberRelationTree relationTree : directMemberList) {
            OmsOrderExample orderExample = new OmsOrderExample();
            orderExample.createCriteria().andMemberIdEqualTo(relationTree.getMemberId())
                    .andStatusNotIn(stats);
            List<OmsOrder> sumOrder = orderMapper.selectByExample(orderExample);
            if(!sumOrder.isEmpty()){
                //进行累加
                Optional<BigDecimal> payAmountSum = sumOrder.stream().map(OmsOrder::getPayAmount)
                        .reduce(BigDecimal::add);

                if (payAmountSum.isPresent()) {
                    if(VIP_UPGRADE_STANDARDS_AMOUNT.compareTo(payAmountSum.get()) < 0){
                        i++;
                        /**
                         * 有2个以上的会员下单达到19000
                         * vip会员升级为店长
                         */
                        if(i > 1){
                            higherMember.setMemberLevel(MemberLevel.Partner);
                            higherMember.setMemberLevelId((long)MemberLevel.Partner.getValue());
                            memberMapper.updateByPrimaryKeySelective(higherMember);
                            return;
                        }

                    };

                }
            }
        }



    }

    private void partnerMemberUpgrade(UmsMember higherMember, List<UmsMemberRelationTree> directMemberList) {


        List<Long> ids = directMemberList.stream().map(UmsMemberRelationTree::getId).collect(Collectors.toList());
        UmsMemberExample memberExample = new UmsMemberExample();
        memberExample.createCriteria().andIdIn(ids).andMemberLevelEqualTo(MemberLevel.Partner.getValue());
        //达标下属数量
        long number = memberMapper.countByExample(memberExample);
        if(number > 1){
            higherMember.setMemberLevel(MemberLevel.Diamond);
            higherMember.setMemberLevelId((long)MemberLevel.Diamond.getValue());
            memberMapper.updateByPrimaryKeySelective(higherMember);
        }
    }

    private void diamondMemberUpgrade(UmsMember higherMember, List<UmsMemberRelationTree> directMemberList) {

        List<Long> ids = directMemberList.stream().map(UmsMemberRelationTree::getId).collect(Collectors.toList());
        UmsMemberExample memberExample = new UmsMemberExample();
        memberExample.createCriteria().andIdIn(ids).andMemberLevelEqualTo(MemberLevel.Diamond.getValue());
        //达标下属数量
        long number = memberMapper.countByExample(memberExample);
        if(number > 1){
            higherMember.setMemberLevel(MemberLevel.OneDiamond);
            higherMember.setMemberLevelId((long)MemberLevel.OneDiamond.getValue());
            memberMapper.updateByPrimaryKeySelective(higherMember);
        }
    }






}
