package cn.wisenergy.service.app.impl;

import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
import cn.wisenergy.model.enums.TradeRecordEnum;
import cn.wisenergy.service.app.MonthUserLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Slf4j
public class MonthUserLevelServiceImpl implements MonthUserLevelService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private CultivatingPrizeMapper cultivatingPrizeMapper;

    @Autowired
    private TradeRecordMapper tradeRecordMapper;

    @Autowired
    private LastMonthUserInfoMapper lastMonthUserInfoMapper;

    @Autowired
    private LastMonthRecommendUserMapper lastMonthRecommendUserMapper;

    @Autowired
    private LastMonthTeamUserInfoMapper lastMonthTeamUserInfoMapper;

    @Autowired
    private LastMonthCultivatingPrizeInfoMapper lastMonthCultivatingPrizeInfoMapper;

    //系统推荐人邀请码
    private static final String monthSysInviteCode1 = "1";
    private static final String monthSysInviteCode2 = "88888888";

    @Transactional
    @Override
    public void monthUserLevelUpgrade(String userId) {
        //当前用户对象
        LastMonthUserInfo user = lastMonthUserInfoMapper.getByUserId(userId);
        //当前用户直推表对象
        LastMonthRecommendUser recommendUser = lastMonthRecommendUserMapper.getByUserId(userId);
        //当前用户团队信息表对象
//        TeamUserInfo teamUserInfoMapperByUserId = lastMonthTeamUserInfoMapper.getByUserId(userId);
        LastMonthTeamUserInfo teamUserInfoMapperByUserId = lastMonthTeamUserInfoMapper.getByUserId(userId);
        //当前用户等级
        Integer userLevel = user.getUserLevel();
        //当前用户的累计消费
        BigDecimal performanceByUserid = new BigDecimal(0);
        if (userLevel == 0){
            //计算当前用户业绩
            performanceByUserid = orderMapper.getPerformanceByUserid(userId);
            if (null == performanceByUserid) {
                performanceByUserid = new BigDecimal(0);
            }
        }
        //当前用户的推荐人邀请码
        String beInvitedCode = user.getBeInvitedCode();
        //
        String inviteUserId = "";
        LastMonthRecommendUser inviteRecommendUser = new LastMonthRecommendUser();
        //若当前用户的推荐人邀请码是  “1” 时 则当前用户没有邀请人
        if (!monthSysInviteCode1.equals(beInvitedCode) && !monthSysInviteCode2.equals(beInvitedCode)) {
            //通过当前用户推荐人邀请码获取邀请人的userId
            LastMonthUserInfo byBeInvitedCode = lastMonthUserInfoMapper.getByBeInvitedCode(beInvitedCode);
            if (null != byBeInvitedCode) {
                inviteUserId = byBeInvitedCode.getUserId();
                //邀请人的直推用户信息
                inviteRecommendUser = lastMonthRecommendUserMapper.getByUserId(inviteUserId);
            }
        }
        //用户等级升级为幼苗 1
        //若当前用户等级为0（普通用户）且跨境额度（业绩消费金额）>= 3980 进行升级0-->1
        //todo:根据业绩去计算  查一次表获取业绩
        if (userLevel == 0 && performanceByUserid.intValue() >= 3980) {
            user.setUserLevel(1);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
            //todo: accountInfo 和TeamPerformance 需要镜像吗
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            //用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //推荐人直推信息表数据维护
                //若邀请人直推用户信息中的普通用户数量为0是则不用变化，否则普通用户数量-1
                if (inviteRecommendUser.getNormalUserNum() == 0) {
                    inviteRecommendUser.setNormalUserNum(inviteRecommendUser.getNormalUserNum());
                } else {
                    //普通用户-1
                    inviteRecommendUser.setNormalUserNum(inviteRecommendUser.getNormalUserNum() - 1);
                }
                //邀请人直推用户信息中的幼苗用户数量为+1
                inviteRecommendUser.setSeedlingNum(inviteRecommendUser.getSeedlingNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoNormalToSeed(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 1;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }

        }
        //用户等级升级为青铜树 2
        //当前用户等级为幼苗用户（1）时 且当前用户直推人数中幼苗等级>=5人<20人时
        if (userLevel == 1 && recommendUser.getSeedlingNum() >= 3
                && recommendUser.getSeedlingNum() < 15) {
            //用户升级成功
            user.setUserLevel(2);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            ////用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //若邀请人直推用户信息中的幼苗用户数量为0是则不用变化，否则幼苗用户数量-1
                if (inviteRecommendUser.getSeedlingNum() == 0) {
                    inviteRecommendUser.setSeedlingNum(inviteRecommendUser.getSeedlingNum());
                } else {
                    //幼苗用户-1
                    inviteRecommendUser.setSeedlingNum(inviteRecommendUser.getSeedlingNum() - 1);
                }
                //邀请人直推用户信息中的青铜树用户数量为+1
                inviteRecommendUser.setBronzeTreeNum(inviteRecommendUser.getBronzeTreeNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoSeedToBronze(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 2;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }
        }
        //用户等级升级为白银树 3
        if (userLevel == 2 && recommendUser.getSeedlingNum() >= 15
                && recommendUser.getSeedlingNum() < 20) {
            user.setUserLevel(3);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            //用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //推荐人直推信息表数据维护
                //若邀请人直推用户信息中的青铜树用户数量为0是则不用变化，否则青铜树用户数量-1
                if (inviteRecommendUser.getBronzeTreeNum() == 0) {
                    inviteRecommendUser.setBronzeTreeNum(inviteRecommendUser.getBronzeTreeNum());
                } else {
                    //青铜树用户-1
                    inviteRecommendUser.setBronzeTreeNum(inviteRecommendUser.getBronzeTreeNum() - 1);
                }
                //邀请人直推用户信息中的白银用户数量 + 1
                inviteRecommendUser.setSilverTreeNum(inviteRecommendUser.getSilverTreeNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoBronzeToSilver(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 3;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }

        }
        //用户等级升级为黄金树 4
        //:todo 条件:团队中青铜树数量>=20
        if (userLevel == 3 && recommendUser.getSeedlingNum() >= 20
                && recommendUser.getSilverTreeNum() >= 2
                && teamUserInfoMapperByUserId.getSeedlingNum() >= 30
                && teamUserInfoMapperByUserId.getBronzeTreeNum() >= 20) {
            user.setUserLevel(4);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            ////用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //推荐人直推信息表数据维护
                //若邀请人直推用户信息中的白银树用户数量为0是则不用变化，否则白银树用户数量-1
                if (inviteRecommendUser.getSilverTreeNum() == 0) {
                    inviteRecommendUser.setSilverTreeNum(inviteRecommendUser.getSilverTreeNum());
                } else {
                    //白银树用户-1
                    inviteRecommendUser.setSilverTreeNum(inviteRecommendUser.getSilverTreeNum() - 1);
                }
                //邀请人直推用户信息中的黄金用户数量 + 1
                inviteRecommendUser.setGoldTreeNum(inviteRecommendUser.getGoldTreeNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoSilverToGoldTree(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 4;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }
        }
        //用户等级级升级为农场主 5
        //:todo 条件:团队中白银树数量>=40
        if (userLevel == 4 && recommendUser.getSeedlingNum() >= 30
                && recommendUser.getGoldTreeNum() >= 2
                && teamUserInfoMapperByUserId.getSeedlingNum() >=70
                && teamUserInfoMapperByUserId.getSilverTreeNum() >= 40) {
            user.setUserLevel(5);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            //用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //推荐人直推信息表数据维护
                //若邀请人直推用户信息中的黄金树用户数量为0是则不用变化，否则黄金树用户数量-1
                if (inviteRecommendUser.getGoldTreeNum() == 0) {
                    inviteRecommendUser.setGoldTreeNum(inviteRecommendUser.getGoldTreeNum());
                } else {
                    //黄金树用户-1
                    inviteRecommendUser.setGoldTreeNum(inviteRecommendUser.getGoldTreeNum() - 1);
                }
                //邀请人直推用户信息中的农场主用户数量 + 1
                inviteRecommendUser.setGoldTreeNum(inviteRecommendUser.getFarmerNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoGoldTreeToFarmer(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 5;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }
        }

        //用户等级升级为森林之星 6
        if (userLevel == 5 && recommendUser.getSeedlingNum() >= 40
                && recommendUser.getFarmerNum() >= 2
                && teamUserInfoMapperByUserId.getSeedlingNum() >=80
                && teamUserInfoMapperByUserId.getSilverTreeNum() >= 70
                && teamUserInfoMapperByUserId.getGoldTreeNum() >= 30) {
            user.setUserLevel(6);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            //用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //推荐人直推信息表数据维护
                //若邀请人直推用户信息中的农场主用户数量为0是则不用变化，否则农场主用户数量-1
                if (inviteRecommendUser.getFarmerNum() == 0) {
                    inviteRecommendUser.setFarmerNum(inviteRecommendUser.getFarmerNum());
                } else {
                    //农场主用户-1
                    inviteRecommendUser.setFarmerNum(inviteRecommendUser.getFarmerNum() - 1);
                }
                //邀请人直推用户信息中的森林之星用户数量 + 1
                inviteRecommendUser.setForestStartNum(inviteRecommendUser.getForestStartNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoFarmerToForestStart(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 6;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }
        }
        //用户等级升级为西田森合伙人 7
        //:todo 条件:团队中白银树数量>=90 && 条件:团队中黄金树数量>=50 条件:团队中农场主数量>=30
        if (userLevel == 6 && recommendUser.getSeedlingNum() >= 50
                && recommendUser.getForestStartNum() >= 2
                && teamUserInfoMapperByUserId.getSeedlingNum() >= 100
                && teamUserInfoMapperByUserId.getSilverTreeNum() >= 90
                && teamUserInfoMapperByUserId.getGoldTreeNum() >= 50
                && teamUserInfoMapperByUserId.getFarmerNum() >= 30) {
            user.setUserLevel(7);
            lastMonthUserInfoMapper.updateById(user);
            //同步accountInfo 和TeamPerformance 的用户等级
//            accountAndTeamPerformanceUserLevelUpdate(userId);
            //用户的推荐人邀请码为1或者88888888时  没有上级用户
            if (monthSysInviteCode1.equals(beInvitedCode) || monthSysInviteCode2.equals(beInvitedCode)) {
                return;
            } else {
                //推荐人直推信息表数据维护
                //若邀请人直推用户信息中的森林之星用户数量为0是则不用变化，否则森林之星用户数量-1
                if (inviteRecommendUser.getForestStartNum() == 0) {
                    inviteRecommendUser.setForestStartNum(inviteRecommendUser.getForestStartNum());
                } else {
                    //森林之星用户-1
                    inviteRecommendUser.setForestStartNum(inviteRecommendUser.getForestStartNum() - 1);
                }
                //邀请人直推用户信息中的西田森合伙人用户数量 + 1
                inviteRecommendUser.setPartnerNum(inviteRecommendUser.getPartnerNum() + 1);
                lastMonthRecommendUserMapper.updateById(inviteRecommendUser);
                //团队用户表数据维护
                lastmonthTeamUserInfoForestStartToPartner(inviteUserId);
                //上级用户获得幼苗等级的培育奖
                int level = 7;
                lastmonthCultivatingPrizeToInvitedUser(userId, inviteUserId, level);
                //用户升级成功时，进行邀请人等级自查是否达到升级条件
                monthUserLevelUpgrade(inviteUserId);
            }
        }
    }
    /**
     * 团队用户表数据维护(普通用户升级至幼苗用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoNormalToSeed(String inviteUserId) {
        if (!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo intiveUserByUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            //邀请人的推荐人邀请码
            String inviteCode = intiveUserByUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                LastMonthUserInfo byBeInvitedCode = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode);
                if (null != byBeInvitedCode) {
                    beinvitedCodeUserId = byBeInvitedCode.getUserId();
                }
            }
            if (teamNumInfoBuyId.getNormalUserNum() == 0) {
                teamNumInfoBuyId.setNormalUserNum(teamNumInfoBuyId.getNormalUserNum());
            } else {
                //普通用户数量-1
                teamNumInfoBuyId.setNormalUserNum(teamNumInfoBuyId.getNormalUserNum() - 1);
            }
            //邀请人的团队用户信息表中幼苗数量 + 1
            //第一波用户是谁邀请的
            teamNumInfoBuyId.setSeedlingNum(teamNumInfoBuyId.getSeedlingNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            //递归向上修改团队用户信息表
            if (!"".equals(beinvitedCodeUserId)) {
                lastmonthTeamUserInfoNormalToSeed(beinvitedCodeUserId);
            }
        }
    }

    /**
     * 团队用户表数据维护(幼苗用户升级至青铜用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoSeedToBronze(String inviteUserId) {
        if (!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            String inviteCode = byUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                beinvitedCodeUserId = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode).getUserId();
            }
            //通过邀请人的用户信息获取邀请人的推荐人邀请码
            //String beinvitedCodeUserId = usersMapper.getByBeInvitedCode(byUserId.getBeInvitedCode()).getUserId();
            //邀请人团队信息表中幼苗用户数量 = 0 时，幼苗用户数量不用变化，否则幼苗用户数量 - 1
            if (teamNumInfoBuyId.getSeedlingNum() == 0) {
                teamNumInfoBuyId.setSeedlingNum(teamNumInfoBuyId.getSeedlingNum());
            } else {
                //幼苗用户数量-1
                teamNumInfoBuyId.setSeedlingNum(teamNumInfoBuyId.getSeedlingNum() - 1);
            }
            //邀请人的团队用户信息表中青铜树数量 + 1
            teamNumInfoBuyId.setBronzeTreeNum(teamNumInfoBuyId.getBronzeTreeNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            //递归向上修改团队用户信息表
            if ("".equals(beinvitedCodeUserId)){

                lastmonthTeamUserInfoSeedToBronze(beinvitedCodeUserId);
            }
        }
    }

    /**
     * 团队用户表数据维护(青铜树用户升级至白银树用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoBronzeToSilver(String inviteUserId) {
        if (!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            String inviteCode = byUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                beinvitedCodeUserId = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode).getUserId();
            }
            //通过邀请人的用户信息获取邀请人的推荐人邀请码
            //String beinvitedCodeUserId = usersMapper.getByBeInvitedCode(byUserId.getBeInvitedCode()).getUserId();
            //邀请人团队信息表中青铜树用户数量 = 0 时，青铜树用户数量不用变化，否则青铜树用户数量 - 1
            if (teamNumInfoBuyId.getBronzeTreeNum() == 0) {
                teamNumInfoBuyId.setBronzeTreeNum(teamNumInfoBuyId.getBronzeTreeNum());
            } else {
                //青铜树用户数量 - 1
                teamNumInfoBuyId.setBronzeTreeNum(teamNumInfoBuyId.getBronzeTreeNum() - 1);
            }
            //白银树用户数量+1
            teamNumInfoBuyId.setSilverTreeNum(teamNumInfoBuyId.getSilverTreeNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            //递归向上修改团队用户信息表
            if ("".equals(beinvitedCodeUserId)){

                lastmonthTeamUserInfoBronzeToSilver(beinvitedCodeUserId);
            }
        }
    }

    /**
     * 团队用户表数据维护(白银树用户升级至黄金树用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoSilverToGoldTree(String inviteUserId) {
        if(!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            String inviteCode = byUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                beinvitedCodeUserId = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode).getUserId();
            }
            //通过邀请人的用户信息获取邀请人的推荐人邀请码
            //String beinvitedCodeUserId = usersMapper.getByBeInvitedCode(byUserId.getBeInvitedCode()).getUserId();
            //邀请人团队信息表中白银树用户数量 = 0 时，白银树用户数量不用变化，否则白银树用户数量 - 1
            if (teamNumInfoBuyId.getSilverTreeNum() == 0) {
                teamNumInfoBuyId.setSilverTreeNum(teamNumInfoBuyId.getSilverTreeNum());
            } else {
                //白银树用户数量 - 1
                teamNumInfoBuyId.setSilverTreeNum(teamNumInfoBuyId.getSilverTreeNum() - 1);
            }
            //黄金树用户数量 - 1
            teamNumInfoBuyId.setGoldTreeNum(teamNumInfoBuyId.getGoldTreeNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            //递归向上修改团队用户信息表
            if ("".equals(beinvitedCodeUserId)){

                lastmonthTeamUserInfoSilverToGoldTree(beinvitedCodeUserId);
            }
        }
    }

    /**
     * 团队用户表数据维护(黄金树用户升级至农场主用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoGoldTreeToFarmer(String inviteUserId) {
        if (!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            String inviteCode = byUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                beinvitedCodeUserId = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode).getUserId();
            }
            //通过邀请人的用户信息获取邀请人的推荐人邀请码
            // String beinvitedCodeUserId = usersMapper.getByBeInvitedCode(byUserId.getBeInvitedCode()).getUserId();
            //邀请人团队信息表中黄金树用户数量 = 0 时，黄金树用户数量不用变化，否则黄金树用户数量 - 1
            if (teamNumInfoBuyId.getGoldTreeNum() == 0) {
                teamNumInfoBuyId.setGoldTreeNum(teamNumInfoBuyId.getGoldTreeNum());
            } else {
                //黄金树用户数量 - 1
                teamNumInfoBuyId.setGoldTreeNum(teamNumInfoBuyId.getGoldTreeNum() - 1);
            }
            //农场主数量 + 1
            teamNumInfoBuyId.setFarmerNum(teamNumInfoBuyId.getFarmerNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            //递归向上修改团队用户信息表
            if ("".equals(beinvitedCodeUserId)){

                lastmonthTeamUserInfoGoldTreeToFarmer(beinvitedCodeUserId);
            }
        }
    }

    /**
     * 团队用户表数据维护(农场主用户升级至森林之星用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoFarmerToForestStart(String inviteUserId) {
        if (!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            String inviteCode = byUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                beinvitedCodeUserId = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode).getUserId();
            }
            //通过邀请人的用户信息获取邀请人的推荐人邀请码
            //String beinvitedCodeUserId = usersMapper.getByBeInvitedCode(byUserId.getBeInvitedCode()).getUserId();
            //邀请人团队信息表中农场主用户数量 = 0 时，农场主用户数量不用变化，否则农场主用户数量 - 1
            if (teamNumInfoBuyId.getFarmerNum() == 0) {
                teamNumInfoBuyId.setFarmerNum(teamNumInfoBuyId.getFarmerNum());
            } else {
                //农场主用户数量 - 1
                teamNumInfoBuyId.setFarmerNum(teamNumInfoBuyId.getFarmerNum() - 1);
            }
            //森林之星用户数量 + 1
            teamNumInfoBuyId.setForestStartNum(teamNumInfoBuyId.getForestStartNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            //递归向上修改团队用户信息表
            if ("".equals(beinvitedCodeUserId)){

                lastmonthTeamUserInfoFarmerToForestStart(beinvitedCodeUserId);
            }
        }
    }

    /**
     * 团队用户表数据维护(森林之星用户升级至西田森合伙人用户时)
     *
     * @param inviteUserId 邀请人userid
     */
    private void lastmonthTeamUserInfoForestStartToPartner(String inviteUserId) {
        if (!"".equals(inviteUserId)){

            //邀请人的团队信息表对象
//            TeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            LastMonthTeamUserInfo teamNumInfoBuyId = lastMonthTeamUserInfoMapper.getByUserId(inviteUserId);
            //邀请人的用户表对象
            LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(teamNumInfoBuyId.getUserId());

            String beinvitedCodeUserId = "";
            String inviteCode = byUserId.getBeInvitedCode();
            if (!monthSysInviteCode1.equals(inviteUserId) && !monthSysInviteCode2.equals(inviteUserId)) {
                //通过邀请人的用户信息获取邀请人的推荐人邀请码
                beinvitedCodeUserId = lastMonthUserInfoMapper.getByBeInvitedCode(inviteCode).getUserId();
            }
            //通过邀请人的用户信息获取邀请人的推荐人邀请码
            //String beinvitedCodeUserId = usersMapper.getByBeInvitedCode(byUserId.getBeInvitedCode()).getUserId();
            //邀请人团队信息表中森林之星用户数量 = 0 时，森林之星用户数量不用变化，否则森林之星用户数量 - 1
            if (teamNumInfoBuyId.getForestStartNum() == 0) {
                teamNumInfoBuyId.setForestStartNum(teamNumInfoBuyId.getForestStartNum());
            } else {
                //森林之星用户数量 - 1
                teamNumInfoBuyId.setForestStartNum(teamNumInfoBuyId.getForestStartNum() - 1);
            }
            //西田森合伙人用户数量 + 1
            teamNumInfoBuyId.setPartnerNum(teamNumInfoBuyId.getPartnerNum() + 1);
            lastMonthTeamUserInfoMapper.updateById(teamNumInfoBuyId);
            if (monthSysInviteCode1.equals(inviteUserId) || monthSysInviteCode2.equals(inviteUserId)) {
                return;
            }
            if ("".equals(beinvitedCodeUserId)){

                lastmonthTeamUserInfoForestStartToPartner(beinvitedCodeUserId);
            }
        }
    }



    /**
     * 培育奖给到上级用户
     *
     * @param userId       当前用户id
     * @param inviteUserId 上级用户id
     * @param level        当前用户升级后的等级
     */
    public void lastmonthCultivatingPrizeToInvitedUser(String userId, String inviteUserId, int level) {
        //当前用户的培育奖记录
        LastMonthCultivatingPrizeInfo cultivatingPrizeInfo = lastMonthCultivatingPrizeInfoMapper.getOneByUserId(userId);
        //判断当前用户等级
        if (1 == level) {
            //获取等级对应的培育奖记录状态
            int seedling = cultivatingPrizeInfo.getSeedling();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();
            if (0 == seedling) {
                //1、判断推荐人的等级是否大与当前用户等级
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                int inviteUserLevel = inviteUser.getUserLevel();
                if (null != inviteUser) {
                    if (inviteUserLevel >= level) {
                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        seedling = 1;
                        cultivatingPrizeInfo.setSeedling(seedling);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    } else {
                        //3.修改培育奖记录状态 ==2
                        seedling = 2;
                        cultivatingPrizeInfo.setSeedling(seedling);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }

                }
            }
        } else if (2 == level) {
            //培育奖记录状态
            int bronzeTree = cultivatingPrizeInfo.getBronzeTree();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();

            if (0 == bronzeTree) {
                //1、判断推荐人的等级是否大与当前用户等级
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                if (null != inviteUser) {
                    int inviteUserLevel = inviteUser.getUserLevel();
                    if (inviteUserLevel >= level) {
                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        bronzeTree = 1;
                        cultivatingPrizeInfo.setBronzeTree(bronzeTree);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    } else {
                        //3.修改培育奖记录状态 ==2
                        bronzeTree = 2;
                        cultivatingPrizeInfo.setBronzeTree(bronzeTree);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }

                }
            }
        } else if (3 == level) {
            //获取当前等级的培育奖状态
            int silverTree = cultivatingPrizeInfo.getSilverTree();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();

            if (0 == silverTree) {
                //1、判断推荐人的等级是否大与当前用户等级
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                if (null != inviteUser) {
                    int inviteUserLevel = inviteUser.getUserLevel();
                    if (inviteUserLevel >= level) {
                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        silverTree = 1;
                        cultivatingPrizeInfo.setBronzeTree(silverTree);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    } else {
                        //3.修改培育奖记录状态 ==2
                        silverTree = 2;
                        cultivatingPrizeInfo.setBronzeTree(silverTree);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }
                }
            }
        } else if (4 == level) {
            int goldTree = cultivatingPrizeInfo.getGoldTree();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();

            if (0 == goldTree) {
                //1、判断推荐人的等级是否大与当前用户等级
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                if (null != inviteUser) {
                    int userLevel = inviteUser.getUserLevel();
                    if (userLevel >= level){

                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        goldTree = 1;
                        cultivatingPrizeInfo.setGoldTree(goldTree);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    }else {
                        //3.修改培育奖记录状态 ==2
                        goldTree = 2;
                        cultivatingPrizeInfo.setGoldTree(goldTree);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }
                }
            }
        } else if (5 == level) {
            int farmer = cultivatingPrizeInfo.getFarmer();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();

            if (0 == farmer) {
                //上级用户
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                if (null != inviteUser) {
                    int userLevel = inviteUser.getUserLevel();
                    if (userLevel >= level){

                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        farmer = 1;
                        cultivatingPrizeInfo.setGoldTree(farmer);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    }else {
                        //3.修改培育奖记录状态 ==2
                        farmer = 2;
                        cultivatingPrizeInfo.setGoldTree(farmer);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }
                }
            }
        } else if (6 == level) {
            int forestStart = cultivatingPrizeInfo.getForestStart();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();

            if (0 == forestStart) {
                //上级用户
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                if (null != inviteUser) {
                    int userLevel = inviteUser.getUserLevel();
                    if (userLevel >= level){

                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        forestStart = 1;
                        cultivatingPrizeInfo.setGoldTree(forestStart);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    }else {
                        //3.修改培育奖记录状态 ==2
                        forestStart = 2;
                        cultivatingPrizeInfo.setGoldTree(forestStart);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }
                }
            }
        } else if (7 == level) {
            int partner = cultivatingPrizeInfo.getPartner();
            //用户等级对应的培育奖金额
            BigDecimal cultivatingPrize = cultivatingPrizeMapper.getcultivatingPrizeByUserLevel(level).getCultivatingPrize();

            if (0 == partner) {
                //上级用户
                LastMonthUserInfo inviteUser = lastMonthUserInfoMapper.getByUserId(inviteUserId);
                if (null != inviteUser) {
                    int userLevel = inviteUser.getUserLevel();
                    if (userLevel >= level ){

                        //2.交易流水新增一条数据
                        TradeRecord tradeRecord = new TradeRecord();
                        tradeRecord.setUserId(inviteUserId);
                        tradeRecord.setMoney(cultivatingPrize);
                        tradeRecord.setTradeNo(null);
                        tradeRecord.setStatus(1);
                        tradeRecord.setTradeType(TradeRecordEnum.CULTIVATING_PRIZE.getCode());
                        tradeRecordMapper.add(tradeRecord);

                        //3.修改培育奖记录状态 ==1
                        partner = 1;
                        cultivatingPrizeInfo.setGoldTree(partner);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                        accountAddFromCultivatingPrize(inviteUserId,cultivatingPrize);
                    }else {
                        //3.修改培育奖记录状态 ==2
                        partner = 2;
                        cultivatingPrizeInfo.setGoldTree(partner);
                        lastMonthCultivatingPrizeInfoMapper.updateById(cultivatingPrizeInfo);
                    }
                }
            }
        }
    }

    /**
     * 月度升级定时任务校准执行完成以后若有下级用户升级成功后
     *          需要将用户的培育奖收益与账户表进行累加
     *
     */
    public void accountAddFromCultivatingPrize(String userId,BigDecimal cultivatingPrize){

        AccountInfo accountMapperByUserId = accountMapper.getByUserId(userId);
        //可提现金额
        BigDecimal extractMoney = accountMapperByUserId.getExtractMoney();
        //本月收益
        BigDecimal earningsMonth = accountMapperByUserId.getEarningsMonth();
        //累计收益
        BigDecimal earningsTotal = accountMapperByUserId.getEarningsTotal();

        accountMapperByUserId.setExtractMoney(extractMoney.add(cultivatingPrize));
        accountMapperByUserId.setEarningsMonth(earningsMonth.add(cultivatingPrize));
        accountMapperByUserId.setEarningsTotal(earningsTotal.add(cultivatingPrize));

        accountMapper.edit(accountMapperByUserId);

    }

}
