package cn.wisenergy.service.app.impl;

import cn.wisenergy.mapper.*;
import cn.wisenergy.model.app.*;
import cn.wisenergy.service.app.LastMonthUserInfoService;
import cn.wisenergy.service.app.MonthUserLevelService;
import cn.wisenergy.service.app.UserLevelService;
import cn.wisenergy.service.app.UserLevelTaskService;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.model.IndexedUDFFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class UserLevelTaskServiceImpl implements UserLevelTaskService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UserLevelService userLevelService;

    @Autowired
    private MonthUserLevelService monthUserLevelService;

    @Autowired
    private LastMonthUserInfoMapper lastMonthUserInfoMapper;

    @Autowired
    private LastMonthRecommendUserMapper lastMonthRecommendUserMapper;

    @Autowired
    private LastMonthTeamUserInfoMapper lastMonthTeamUserInfoMapper;

    @Autowired
    private LastMonthUserInfoService lastMonthUserInfoService;

    @Autowired
    private UserDataInfoMapper userDataInfoMapper;

    //获取订单
    @XxlJob(value = "userLevelUpjobhandler")
    @Override
    public void userLevelUp() {
        //1.获取数据库订单数据
        log.info("------------------------日定时任务每5分钟执行一次：用户升级开始----------------------------------");
        List<OrderInfo> ordersByLevelStatus = orderMapper.getByLevelStatus(0);
        for (OrderInfo orderInfo : ordersByLevelStatus) {
            String userId = orderInfo.getUserId();
            userLevelService.userLevelUpgrade(userId);
            //当前订单升级状态置为1
            orderInfo.setLevelStatus(1);
            orderMapper.updateById(orderInfo);
        }
        log.info("-------------------------日定时任务每5分钟执行一次：用户升级结束--------------------------------------");
    }

    @XxlJob("monthUserLevelUpjobhandler")
    @Override
    public void monthUserLevelUp(){

        /**
         * 根据上月的镜像表镜像lastmonth_userinfo
         *                  lastmonth_recommend_user
         *                   lastmonth_team_user_info
         *                    lastmonth_cultivating_prize_info
         */
        lastMonthUserInfoService.userInfoMirrorImage();

        /**
         * 1、获取订单数据  根据订单对应的userId进行升级以及升级后直推数据与团队数据维护
         */
        log.info("------------------------月定时任务：每月执行一次 ====用户升级开始----------------------------------");
        List<OrderInfo> ordersByLevelStatus = orderMapper.getByMonthOrderStatus(0);
        for (OrderInfo orderInfo : ordersByLevelStatus) {
            //用户信息
            String userId = orderInfo.getUserId();
            //月度升级功能实现
            monthUserLevelService.monthUserLevelUpgrade(userId);
            //当前订单升级状态置为1
            orderInfo.setMonthOrderStatus(1);
            orderMapper.updateById(orderInfo);
        }

        /**
         * 根据用户填写邀请码的行为记录表
         *          进行用户的直推团队数据维护 之后进行升级
         */
        List<UserDataInfo> userDataInfoList = userDataInfoMapper.getListByLevelStatus(0);
        for (UserDataInfo userDataInfo : userDataInfoList) {
            String userId = userDataInfo.getUserId();
            String beInvitedCode = userDataInfo.getBeInvitedCode();

            LastMonthUserInfo userByUserId = lastMonthUserInfoMapper.getUserByUserId(userId);
            userByUserId.setBeInvitedCode(beInvitedCode);
            lastMonthUserInfoMapper.updateById(userByUserId);
            LastMonthUserInfo user = lastMonthUserInfoMapper.getuserByBeInvitedCode(beInvitedCode);
            if (null != user){
                String inviteUserId = user.getUserId();
                lastmonthUserSetBeinviteCode(userId,inviteUserId);
            }
            userDataInfo.setLevelStatus(1);
            userDataInfoMapper.updateById(userDataInfo);
        }

        /**
         * 将月度定时任务的执行结果同步至线上user_info表中
         */
        List<LastMonthUserInfo> lastMonthUserInfoList = lastMonthUserInfoMapper.getAll();
        if (null != lastMonthUserInfoList){
            for (LastMonthUserInfo lastMonthUserInfo : lastMonthUserInfoList) {
                String userId = lastMonthUserInfo.getUserId();
                int userLevel = lastMonthUserInfo.getUserLevel();
                User userByUserId = usersMapper.getUserByUserId(userId);
                if (null != userByUserId){
                    userByUserId.setUserLevel(userLevel);
                }
                usersMapper.updateById(userByUserId);

            }
        }

        /**
         * 镜像用户信息表
         *      直推表
         *      团队表
         *      培育奖记录表
         */
        lastMonthUserInfoService.userInfoMirrorImageBack();

        log.info("-------------------------月定时任务：每月执行一次 ====用户升级结束--------------------------------------");
    }

    private void lastmonthUserSetBeinviteCode(String userId, String inviteUserId) {
        //3、上级用户的直推表数据维护
        lastmonthRecommendInfoDataPreserve(userId,inviteUserId);
        //4、用户团队数据维护
        lastmonthTeamDataPreserve(userId,inviteUserId);
        //记录用户设置邀请码的行为数据
//        userDataInfoManager.addUserSetBeInvitedCodeDoc(userId, inviteUserId);
        //5、查看当前用户是否要进行升级
//        userLevelService.userLevelUpgrade(inviteUserId);
        monthUserLevelService.monthUserLevelUpgrade(inviteUserId);
    }

    private void lastmonthRecommendInfoDataPreserve(String userId, String inviteUserId) {
        LastMonthUserInfo byUserId = lastMonthUserInfoMapper.getByUserId(userId);
        int userLevel = byUserId.getUserLevel();
        LastMonthRecommendUser recommendUserMapperByRandUser = lastMonthRecommendUserMapper.getByUserId(inviteUserId);
        if (userLevel == 0){
            //普通用户数量 + 1
            Integer normalUserNum = recommendUserMapperByRandUser.getNormalUserNum();
            recommendUserMapperByRandUser.setNormalUserNum(normalUserNum +1);
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 1){
            //幼苗用户数量  + 1
            Integer seedlingNum = recommendUserMapperByRandUser.getSeedlingNum();
            recommendUserMapperByRandUser.setSeedlingNum(seedlingNum + 1);
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 2){
            //青铜用户数量  + 1
            Integer bronzeTreeNum = recommendUserMapperByRandUser.getBronzeTreeNum();
            recommendUserMapperByRandUser.setBronzeTreeNum(bronzeTreeNum + 1 );
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 3){
            //白银用户数量  + 1
            Integer silverTreeNum = recommendUserMapperByRandUser.getSilverTreeNum();
            recommendUserMapperByRandUser.setSilverTreeNum(silverTreeNum + 1 );
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 4){
            //黄金用户数量  + 1
            Integer goldTreeNum = recommendUserMapperByRandUser.getGoldTreeNum();
            recommendUserMapperByRandUser.setGoldTreeNum(goldTreeNum + 1);
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 5){
            //农场主用户数量  + 1
            Integer farmerNum = recommendUserMapperByRandUser.getFarmerNum();
            recommendUserMapperByRandUser.setFarmerNum(farmerNum + 1);
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 6){
            //森林之星用户数量  + 1
            Integer forestStartNum = recommendUserMapperByRandUser.getForestStartNum();
            recommendUserMapperByRandUser.setForestStartNum(forestStartNum + 1);
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }else if (userLevel == 7){
            //西田森合伙人用户数量  + 1
            Integer partnerNum = recommendUserMapperByRandUser.getPartnerNum();
            recommendUserMapperByRandUser.setFarmerNum(partnerNum + 1);
            lastMonthRecommendUserMapper.updateById(recommendUserMapperByRandUser);
        }
    }

    /**
     * 团队数据维护
     * @param userId
     * @param userInvitedId
     */
    private void lastmonthTeamDataPreserve(String userId, String userInvitedId) {

        LastMonthUserInfo userByUserId = lastMonthUserInfoMapper.getUserByUserId(userId);
        //用户等级
        int userLevel = userByUserId.getUserLevel();
        //当前用户的团队各人数等级
//        TeamUserInfo userTeamUserInfoMapperByUserId = lastMonthTeamUserInfoMapper.getByUserId(userId);
        LastMonthTeamUserInfo userTeamUserInfoMapperByUserId = lastMonthTeamUserInfoMapper.getByUserId(userId);
        if (null != userTeamUserInfoMapperByUserId ){
            Integer normalUserNum = userTeamUserInfoMapperByUserId.getNormalUserNum();
            Integer seedlingNum = userTeamUserInfoMapperByUserId.getSeedlingNum();
            Integer bronzeTreeNum = userTeamUserInfoMapperByUserId.getBronzeTreeNum();
            Integer silverTreeNum = userTeamUserInfoMapperByUserId.getSilverTreeNum();
            Integer goldTreeNum = userTeamUserInfoMapperByUserId.getGoldTreeNum();
            Integer farmerNum = userTeamUserInfoMapperByUserId.getFarmerNum();
            Integer forestStartNum = userTeamUserInfoMapperByUserId.getForestStartNum();
            Integer partnerNum = userTeamUserInfoMapperByUserId.getPartnerNum();

            //推荐人的用户的团队各等级人数
//            TeamUserInfo inviteTeamUserInfoMapperByUserId = lastMonthTeamUserInfoMapper.getByUserId(userInvitedId);
            LastMonthTeamUserInfo inviteTeamUserInfoMapperByUserId = lastMonthTeamUserInfoMapper.getByUserId(userInvitedId);
            if (null != inviteTeamUserInfoMapperByUserId){

                if (0 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum + 1 );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (1 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum +  1 );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (2 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum + 1 );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (3 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum + 1 );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (4 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum + 1 );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (5 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum + 1 );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (6 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum + 1 );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum );
                }else if (7 == userLevel){
                    inviteTeamUserInfoMapperByUserId.setNormalUserNum(inviteTeamUserInfoMapperByUserId.getNormalUserNum() + normalUserNum );
                    inviteTeamUserInfoMapperByUserId.setSeedlingNum(inviteTeamUserInfoMapperByUserId.getSeedlingNum() + seedlingNum );
                    inviteTeamUserInfoMapperByUserId.setBronzeTreeNum(inviteTeamUserInfoMapperByUserId.getBronzeTreeNum() + bronzeTreeNum );
                    inviteTeamUserInfoMapperByUserId.setSilverTreeNum(inviteTeamUserInfoMapperByUserId.getSilverTreeNum() + silverTreeNum );
                    inviteTeamUserInfoMapperByUserId.setGoldTreeNum(inviteTeamUserInfoMapperByUserId.getGoldTreeNum() + goldTreeNum );
                    inviteTeamUserInfoMapperByUserId.setFarmerNum(inviteTeamUserInfoMapperByUserId.getFarmerNum() + farmerNum );
                    inviteTeamUserInfoMapperByUserId.setForestStartNum(inviteTeamUserInfoMapperByUserId.getForestStartNum() + forestStartNum );
                    inviteTeamUserInfoMapperByUserId.setPartnerNum(inviteTeamUserInfoMapperByUserId.getPartnerNum() + partnerNum + 1);
                }
                lastMonthTeamUserInfoMapper.updateById(inviteTeamUserInfoMapperByUserId);

                //推荐人的推荐人id
                LastMonthUserInfo invitedUserByUserId = lastMonthUserInfoMapper.getUserByUserId(userInvitedId);
                if (null != invitedUserByUserId){
                    String beInvitedCode = invitedUserByUserId.getBeInvitedCode();
                    LastMonthUserInfo user = lastMonthUserInfoMapper.getuserByBeInvitedCode(beInvitedCode);
                    if (null != user){
                        String upUserId = user.getUserId();
                        lastmonthTeamDataPreserve(userId,upUserId);
                    }
                }

            }
        }
    }

}
