<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/4/28
 * Time: 下午6:38
 */

namespace common\helpers;


use common\components\ActiveRecord;
use common\models\User;
use common\models\UserInfo;
use common\models\UserNetwork;
use common\models\UserRelation;
use yii\base\Exception;

class DataBak
{
    const LIMIT = 100;

    /**
     * 备份数据
     * @param $periodNum
     * @throws \yii\db\Exception
     */
    public static function backup($periodNum) {
        // 删除并创建会员表
        self::dropBakTable('USER_ALL', $periodNum);
        self::createdBakTable('USER_ALL', $periodNum);
        // 删除并创建接点表
        self::dropBakTable('USER_NETWORK', $periodNum);
        self::createdBakTable('USER_NETWORK', $periodNum);
        // 删除并创建推荐表
        self::dropBakTable('USER_RELATION', $periodNum);
        self::createdBakTable('USER_RELATION', $periodNum);
        // 备份会员数据
        self::backupUserTableData($periodNum);
//        self::backupNetTableData('network', $periodNum);
//        self::backupNetTableData('relation', $periodNum);
    }

    /**
     * 备份会员表
     * @param $periodNum
     * @param int $offset
     * @throws \yii\db\Exception
     */
    public static function backupUserTableData($periodNum, $offset = 0) {
        $allUserData = User::findUseDbCalc()->where('1=1')->offset($offset)->limit(self::LIMIT)->asArray()->all();
        if($allUserData){
            $insertData = [];
            foreach($allUserData as $data){
                unset($data['ROWNUMID'], $data['PARTITION_DATE'], $data['PAY_PASSWORD'], $data['PASSWORD_HASH']);
                $userInfoData = UserInfo::findUseDbCalc()->where('USER_ID=:USER_ID', [':USER_ID' => $data['ID']])->asArray()->one();
                if($userInfoData){
                    unset($userInfoData['ROWNUMID']);
                } else {
                    continue;
                }
                $insertData[] = array_merge($data, $userInfoData);
            }
            ActiveRecord::batchInsert($insertData, '{{%USER_ALL_'.$periodNum.'}}', 'dbNetPoint');
            unset($allUserData, $insertData);
            self::backupUserTableData($periodNum, $offset + self::LIMIT);
        }
    }

    /**
     * 备份网络数据
     * @param $netType
     * @param $periodNum
     * @param int $offset
     * @throws \yii\db\Exception
     */
    public static function backupNetTableData($netType, $periodNum, $offset = 0){
        if($netType == 'network'){
            $modelClass = UserNetwork::class;
            $tableName = '{{%USER_NETWORK_'.$periodNum.'}}';
        } else {
            $modelClass = UserRelation::class;
            $tableName = '{{%USER_RELATION_'.$periodNum.'}}';
        }
        $allData = $modelClass::findUseDbCalc()->where('1=1')->offset($offset)->limit(self::LIMIT)->asArray()->all();
        if($allData){
            $insertData = [];
            foreach($allData as $data){
                unset($data['ROWNUMID']);
                $insertData[] = $data;
            }
                ActiveRecord::batchInsert($insertData, $tableName, 'dbNetPoint');
            unset($allData, $insertData);
            self::backupNetTableData($netType, $periodNum, $offset + self::LIMIT);
        }
    }

    /**
     * 创建备份表
     * @param $tableName
     * 不含 AR_
     * @param $periodNum
     * 取数
     */
    public static function createdBakTable($tableName, $periodNum) {
        if($tableName == 'USER_ALL') {
            $fromTableName = '{{%USER_ALL_TEMPLATE}}';
            $fromDb = 'dbNetPoint';
        } else {
            $fromTableName = '{{%'.$tableName.'}}';
            $fromDb = 'db';
        }
        $createTableName = '{{%'.$tableName.'_'.$periodNum.'}}';
        if(!ActiveRecord::isExistsTable($createTableName, 'dbNetPoint')){
            // 创建表
            ActiveRecord::createTableFromTable($createTableName, $fromTableName, [], 'dbNetPoint', $fromDb);
        }
    }

    /**
     * 删除备份的表
     * @param $tableName
     * 不含 AR_
     * @param $periodNum
     * 取数
     */
    public static function dropBakTable($tableName, $periodNum){
        $dropTableName =  '{{%'.$tableName.'_'.$periodNum.'}}';
        if(ActiveRecord::isExistsTable($dropTableName, 'dbNetPoint')){
            ActiveRecord::deleteTable($dropTableName, 'dbNetPoint');
        }
    }

}