DataBak.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/4/28
  6. * Time: 下午6:38
  7. */
  8. namespace common\helpers;
  9. use common\components\ActiveRecord;
  10. use common\models\User;
  11. use common\models\UserInfo;
  12. use common\models\UserNetwork;
  13. use common\models\UserRelation;
  14. use yii\base\Exception;
  15. class DataBak
  16. {
  17. const LIMIT = 100;
  18. /**
  19. * 备份数据
  20. * @param $periodNum
  21. * @throws \yii\db\Exception
  22. */
  23. public static function backup($periodNum) {
  24. // 删除并创建会员表
  25. self::dropBakTable('USER_ALL', $periodNum);
  26. self::createdBakTable('USER_ALL', $periodNum);
  27. // 删除并创建接点表
  28. self::dropBakTable('USER_NETWORK', $periodNum);
  29. self::createdBakTable('USER_NETWORK', $periodNum);
  30. // 删除并创建推荐表
  31. self::dropBakTable('USER_RELATION', $periodNum);
  32. self::createdBakTable('USER_RELATION', $periodNum);
  33. // 备份会员数据
  34. self::backupUserTableData($periodNum);
  35. // self::backupNetTableData('network', $periodNum);
  36. // self::backupNetTableData('relation', $periodNum);
  37. }
  38. /**
  39. * 备份会员表
  40. * @param $periodNum
  41. * @param int $offset
  42. * @throws \yii\db\Exception
  43. */
  44. public static function backupUserTableData($periodNum, $offset = 0) {
  45. $allUserData = User::findUseDbCalc()->where('1=1')->offset($offset)->limit(self::LIMIT)->asArray()->all();
  46. if($allUserData){
  47. $insertData = [];
  48. foreach($allUserData as $data){
  49. unset($data['ROWNUMID'], $data['PARTITION_DATE'], $data['PAY_PASSWORD'], $data['PASSWORD_HASH']);
  50. $userInfoData = UserInfo::findUseDbCalc()->where('USER_ID=:USER_ID', [':USER_ID' => $data['ID']])->asArray()->one();
  51. if($userInfoData){
  52. unset($userInfoData['ROWNUMID']);
  53. } else {
  54. continue;
  55. }
  56. $insertData[] = array_merge($data, $userInfoData);
  57. }
  58. ActiveRecord::batchInsert($insertData, '{{%USER_ALL_'.$periodNum.'}}', 'dbNetPoint');
  59. unset($allUserData, $insertData);
  60. self::backupUserTableData($periodNum, $offset + self::LIMIT);
  61. }
  62. }
  63. /**
  64. * 备份网络数据
  65. * @param $netType
  66. * @param $periodNum
  67. * @param int $offset
  68. * @throws \yii\db\Exception
  69. */
  70. public static function backupNetTableData($netType, $periodNum, $offset = 0){
  71. if($netType == 'network'){
  72. $modelClass = UserNetwork::class;
  73. $tableName = '{{%USER_NETWORK_'.$periodNum.'}}';
  74. } else {
  75. $modelClass = UserRelation::class;
  76. $tableName = '{{%USER_RELATION_'.$periodNum.'}}';
  77. }
  78. $allData = $modelClass::findUseDbCalc()->where('1=1')->offset($offset)->limit(self::LIMIT)->asArray()->all();
  79. if($allData){
  80. $insertData = [];
  81. foreach($allData as $data){
  82. unset($data['ROWNUMID']);
  83. $insertData[] = $data;
  84. }
  85. ActiveRecord::batchInsert($insertData, $tableName, 'dbNetPoint');
  86. unset($allData, $insertData);
  87. self::backupNetTableData($netType, $periodNum, $offset + self::LIMIT);
  88. }
  89. }
  90. /**
  91. * 创建备份表
  92. * @param $tableName
  93. * 不含 AR_
  94. * @param $periodNum
  95. * 取数
  96. */
  97. public static function createdBakTable($tableName, $periodNum) {
  98. if($tableName == 'USER_ALL') {
  99. $fromTableName = '{{%USER_ALL_TEMPLATE}}';
  100. $fromDb = 'dbNetPoint';
  101. } else {
  102. $fromTableName = '{{%'.$tableName.'}}';
  103. $fromDb = 'db';
  104. }
  105. $createTableName = '{{%'.$tableName.'_'.$periodNum.'}}';
  106. if(!ActiveRecord::isExistsTable($createTableName, 'dbNetPoint')){
  107. // 创建表
  108. ActiveRecord::createTableFromTable($createTableName, $fromTableName, [], 'dbNetPoint', $fromDb);
  109. }
  110. }
  111. /**
  112. * 删除备份的表
  113. * @param $tableName
  114. * 不含 AR_
  115. * @param $periodNum
  116. * 取数
  117. */
  118. public static function dropBakTable($tableName, $periodNum){
  119. $dropTableName = '{{%'.$tableName.'_'.$periodNum.'}}';
  120. if(ActiveRecord::isExistsTable($dropTableName, 'dbNetPoint')){
  121. ActiveRecord::deleteTable($dropTableName, 'dbNetPoint');
  122. }
  123. }
  124. }