/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.comment.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.OrderMapper;
import com.yami.shop.distribution.comment.dao.DistributionLevelAuditingMapper;
import com.yami.shop.distribution.comment.dao.DistributionLevelMapper;
import com.yami.shop.distribution.comment.dao.DistributionUserMapper;
import com.yami.shop.distribution.comment.dto.DistributionUserAchievementDataDto;
import com.yami.shop.distribution.comment.model.DistributionLevel;
import com.yami.shop.distribution.comment.model.DistributionLevelAuditing;
import com.yami.shop.distribution.comment.model.DistributionLevelSet;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.service.DistributionLevelService;
import com.yami.shop.distribution.comment.service.DistributionLevelSetService;
import com.yami.shop.distribution.comment.vo.DistributionAwardDataVO;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionDataVO;
import com.yami.shop.distribution.comment.vo.DistributionLevelConditionsSwitchVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author lgh on 2019/04/04.
 */
@Service
@AllArgsConstructor
public class DistributionLevelServiceImpl extends ServiceImpl<DistributionLevelMapper, DistributionLevel> implements DistributionLevelService {

    private final DistributionUserMapper distributionUserMapper;

    private final OrderMapper orderMapper;

    private final DistributionLevelMapper distributionLevelMapper;


//    @Override
//    public List<DistributionLevel> geyByShopId(Long shopId) {
//        return distributionLevelMapper.geyByShopId(shopId);
//    }



//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public List<DistributionLevel> autoInsertOrUpdateByList(List<DistributionLevel> list) {
//        for(DistributionLevel distributionLevel:list){
//            if(distributionLevel.getLevelId()!=null){
//                distributionLevelMapper.updateById(distributionLevel);
//            }else{
//                distributionLevelMapper.insert(distributionLevel);
//            }
//        }
//
//        return list;
//    }

//    @Override
//    public DistributionAwardDataVO getDistributionAwardDataVOByLevelId(Long levelId) {
//
//        return distributionLevelMapper.getDistributionAwardDataVOByLevelId(levelId);
//    }

//    @Override
//    public List<DistributionLevelDto> getDistributionLevelDtoByShopId(Long shopId) {
//        List<DistributionLevelDto> list=distributionLevelMapper.getDistributionLevelDtoByShopId(shopId);
//        for(DistributionLevelDto distributionLevelDto:list){
//            distributionLevelDto.setDistributionLevelConditionDataVO(Json.parseObject(distributionLevelDto.getConditionData(), DistributionLevelConditionDataVO.class));
//        }
//        return list;
//    }

//    @Override
//    public DistributionLevelDto getDistributionLevelDtoByDistributionUserId(Long distributionUserId) {
//        return distributionLevelMapper.getDistributionLevelDtoByDistributionUserId(distributionUserId);
//    }

    @Override
    public void tryingToUpgrade(DistributionUser distributionUser, DistributionLevelSet distributionLevelSet) {
        DistributionLevel nextDistributionLevelInfo = getLevelInfo(distributionUser.getLevel() +1, distributionLevelSet.getDistributionLevels());
        if (nextDistributionLevelInfo == null) {
            throw new GlobalException("您的等级已经达到上限了");
        }

        //获取要升级等级的条件数据
        DistributionLevelConditionDataVO distributionLevelConditionData = nextDistributionLevelInfo.getDistributionLevelConditionDataVO();
        // 判断升级的开关
        DistributionLevelConditionsSwitchVO levelSetConditionsSwitch = distributionLevelSet.getLevelSetConditionsSwitch();

        //获取分销员与等级条件匹配的数据
        DistributionUserAchievementDataDto distributionUserAchievementDataDto=distributionUserMapper.getLevelDistributionUserAchievementDataByDistributionUserId(distributionUser.getDistributionUserId());

        if(!isConditions(distributionUserAchievementDataDto,distributionLevelConditionData,levelSetConditionsSwitch,distributionUser)){
            throw new GlobalException("您不符合升级条件");
        }
        // 升级
        distributionUserMapper.updateLevel(distributionUser.getDistributionUserId(),nextDistributionLevelInfo.getLevel());
    }



    @Override
    public DistributionLevel getLevelInfo(int level, List<DistributionLevel> distributionLevels) {
        if (level > 0 && level <= distributionLevels.size()) {
            return distributionLevels.get(level -1);
        }
        return null;
    }

    @Override
    public DistributionAwardDataVO getDistributionAwardDataVOByLevelId(Long levelId) {
        return distributionLevelMapper.getDistributionAwardDataVOByLevelId(levelId);
    }

    private boolean isConditions(DistributionUserAchievementDataDto distributionUserAchievementDataDto,
                                 DistributionLevelConditionDataVO distributionLevelConditionData,
                                 DistributionLevelConditionsSwitchVO levelSetConditionsSwitch, DistributionUser distributionUser) {
        //绑定客户数条件
        if(levelSetConditionsSwitch.getBoundCustomers()){
            if(distributionUserAchievementDataDto.getBoundCustomers() <  distributionLevelConditionData.getBoundCustomers()){
                return false;
            }
        }

        //邀请分销员数
        if(levelSetConditionsSwitch.getInvitedVeeker()){
            if(distributionUserAchievementDataDto.getInvitedVeeker() <  distributionLevelConditionData.getInvitedVeeker() ){
                return false;
            }
        }

        //支付单数
        if(levelSetConditionsSwitch.getPayNumber()){
            if(distributionUserAchievementDataDto.getPayNumber() <  distributionLevelConditionData.getPayNumber() ){
                return false;
            }
        }

        //积累收益
        if(levelSetConditionsSwitch.getAddupAmount()){
            if(distributionUserAchievementDataDto.getDistributionUserWallet().getAddupAmount() <  distributionLevelConditionData.getAddupAmount() ){
                return false;
            }
        }

        //成功成交单数
        if(levelSetConditionsSwitch.getSuccessOrderNumber()){
            if(distributionUserAchievementDataDto.getSuccessOrderNumber() <  distributionLevelConditionData.getSuccessOrderNumber() ){
                return false;
            }
        }

        //成功成交金额
        if(levelSetConditionsSwitch.getSuccessTradingVolume()){
            if(distributionUserAchievementDataDto.getSuccessTradingVolume() <  distributionLevelConditionData.getSuccessTradingVolume() ){
                return false;
            }
        }

        //成功成交金额
        if(levelSetConditionsSwitch.getSuccessTradingVolume()){
            if(distributionUserAchievementDataDto.getSuccessTradingVolume() <  distributionLevelConditionData.getSuccessTradingVolume() ){
                return false;
            }
        }

        //积累消费金额
        if(levelSetConditionsSwitch.getSumOfConsumption()){
            if(distributionUserAchievementDataDto.getSumOfConsumption() <  distributionLevelConditionData.getSumOfConsumption() ){
                return false;
            }
        }

        //积累充值金额
//        if(levelSetConditionsSwitch.getRechargeAmount()){
//            if(distributionUserAchievementDataDto.getRechargeAmount() <  distributionLevelConditionData.getRechargeAmount() ){
//                return false;
//            }
//        }

        //购买指定商品
        if(levelSetConditionsSwitch.getCommodity()){
            for(DistributionLevelConditionDataVO.CommodityItemData commodityItemData : distributionLevelConditionData.getCommodity()){
                // 判断有没有没有购买过指定商品
                if (orderMapper.hasBuySuccessProd(commodityItemData.getProdId(),distributionUser.getUserId()) == 0) {
                    return false;
                }
            }
        }
        return true;
    }

//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void upgrade(Long distributionUserId, Long levelId,Integer type) {
//        //修改分销员的等级
//
//    }

//    @Override
//    public DistributionLevelVO getSeniorLevelVOIdByDistributionUserId(Long shopId, Long distributionUserId) {
//        return distributionLevelMapper.getSeniorLevelIdByDistributionUserId(shopId,distributionUserId);
//    }
}
