package cn.wisenergy.service.app.impl;

import cn.wisenergy.mapper.LastAccountMapper;
import cn.wisenergy.service.app.LastMonthUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LastMonthUserInfoServiceImpl implements LastMonthUserInfoService {

    @Autowired
    private LastAccountMapper lastAccountMapper;

    /**
     * 镜像上一个时间节点的用户表
     *                  直推表
     *                  团队表
     *                  培育奖记录表
     */
    @Override
    public void userInfoMirrorImage() {
        //把用户表user_info备份
        log.info("user_info表进行镜像开始");
        lastAccountMapper.copyTable("user_info_backup", "user_info_image");
        lastAccountMapper.deleteTable("lastmonth_user_info");
        lastAccountMapper.updateTableName("user_info_image", "lastmonth_user_info");
        log.info("user_info表进行镜像成功");

        //recommend_user表备份
        log.info("user_info表进行镜像开始");
        lastAccountMapper.copyTable("recommend_user_backup", "recommend_user_image");
        lastAccountMapper.deleteTable("lastmonth_recommend_user");
        lastAccountMapper.updateTableName("recommend_user_image", "lastmonth_recommend_user");
        log.info("user_info表进行镜像成功");

        //team_user_info表备份
        log.info("user_info表进行镜像开始");
        lastAccountMapper.copyTable("team_user_info_backup", "team_user_info_image");
        lastAccountMapper.deleteTable("lastmonth_team_user_info");
        lastAccountMapper.updateTableName("team_user_info_image", "lastmonth_team_user_info");
        log.info("user_info表进行镜像成功");

        //镜像培育奖记录表
        log.info("cultivating_prize_info表记录镜像开始");
        lastAccountMapper.copyTable("cultivating_prize_info_backup", "cultivating_prize_info_image");
        lastAccountMapper.deleteTable("lastmonth_cultivating_prize_info");
        lastAccountMapper.updateTableName("cultivating_prize_info_image", "lastmonth_cultivating_prize_info");
        log.info("cultivating_prize_info表记录镜像结束");
    }

    /**
     * 用户升级月度任务执行完成以后执行用户表
     *                           直推表
     *                           团队表
     *                           培育奖记录表
     *                                      的备份，以便下次月度定时任务的执行
     */
    @Override
    public void userInfoMirrorImageBack(){
        //把用户表user_info备份
        log.info("user_info表进行镜像开始");
        lastAccountMapper.copyTable("user_info", "user_info_image");
        lastAccountMapper.deleteTable("user_info_backup");
        lastAccountMapper.updateTableName("user_info_image", "user_info_backup");
        log.info("user_info表进行镜像成功");

        //recommend_user表备份
        log.info("recommend_user表进行镜像开始");
        lastAccountMapper.copyTable("recommend_user", "recommend_user_image");
        lastAccountMapper.deleteTable("recommend_user_backup");
        lastAccountMapper.updateTableName("recommend_user_image", "recommend_user_backup");
        log.info("recommend_user表进行镜像成功");

        //team_user_info表备份
        log.info("team_user_info表进行镜像开始");
        lastAccountMapper.copyTable("team_user_info", "team_user_info_image");
        lastAccountMapper.deleteTable("team_user_info_backup");
        lastAccountMapper.updateTableName("team_user_info_image", "team_user_info_backup");
        log.info("team_user_info表进行镜像成功");

        //镜像培育奖记录表
        log.info("cultivating_prize_info表进行镜像开始");
        lastAccountMapper.copyTable("cultivating_prize_info", "cultivating_prize_info_image");
        lastAccountMapper.deleteTable("cultivating_prize_info_backup");
        lastAccountMapper.updateTableName("cultivating_prize_info_image", "cultivating_prize_info_backup");
        log.info("cultivating_prize_info表进行镜像成功");
    }
}
