HistoryBonusForm.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Cache;
  5. use common\helpers\Date;
  6. use common\helpers\user\Info;
  7. use common\models\HistoryBonus;
  8. use common\models\UserBonus;
  9. use common\models\Withdraw;
  10. /**
  11. * 历史奖金余额表单
  12. */
  13. class HistoryBonusForm extends Model {
  14. private $_query;
  15. private $_totalCount;
  16. private $_limit = 100;
  17. private $_backupCompleted = false;
  18. /**
  19. * 自动备份
  20. * @return bool
  21. * @throws \yii\base\InvalidConfigException
  22. */
  23. public function taskAutoBackup(){
  24. //HistoryWalletHelper::setAutoBackupRunning();
  25. $this->handleQuery();
  26. try{
  27. foreach($this->_query->batch($this->_limit) as $list){
  28. if($list){
  29. $insert = [];
  30. foreach($list as $value){
  31. if(!$info = Info::baseInfoWithNet($value['USER_ID'])) continue;
  32. $insert[] = [
  33. 'USER_ID' => $value['USER_ID'],
  34. 'USER_NAME' => $info['USER_NAME'],
  35. 'REAL_NAME' => $info['REAL_NAME'],
  36. 'DEC_LV' => $info['DEC_LV'],
  37. 'EMP_LV' => $info['EMP_LV'],
  38. 'IS_DEC' => $info['IS_DEC'],
  39. 'DEC_ROLE_ID' => $info['DEC_ROLE_ID'],
  40. 'SYSTEM_NAME' => $info['SYSTEM_NAME'],
  41. 'BONUS' => $value['BONUS'],
  42. 'CF' => $value['CF'],
  43. 'LX' => $value['LX'],
  44. 'WITHDRAW' => Withdraw::getWithdrawTotal($value['USER_ID'], Withdraw::STATUS_WAIT_PAID),
  45. 'WITHDRAW_TAX' => 0.00,//todo 待提现开发
  46. 'WITHDRAW_DEDUCT' => 0.00,//todo 待提现开发
  47. 'WITHDRAW_REAL' => 0.00,//todo 待提现开发
  48. 'WITHDRAW_FAIL' => Withdraw::getWithdrawTotal($value['USER_ID'], Withdraw::STATUS_PAID_FALSE),
  49. 'USER_STATUS' => $info['STATUS'],
  50. 'USER_STATUS_AT' => $info['STATUS_AT'],
  51. 'HIGHEST_EMP_LV' => $info['HIGHEST_EMP_LV'],
  52. 'PERIOD_AT' => $info['PERIOD_AT'],
  53. 'DEC_DEC_ROLE_ID' => $info['DEC_DEC_ROLE_ID'],
  54. 'DEC_USER_NAME' => $info['DEC_USER_NAME'],
  55. 'DEC_REAL_NAME' => $info['DEC_REAL_NAME'],
  56. 'MOBILE' => $info['MOBILE'],
  57. 'TEL' => $info['TEL'],
  58. 'PROVINCE' => $info['PROVINCE'],
  59. 'CITY' => $info['CITY'],
  60. 'COUNTY' => $info['COUNTY'],
  61. 'SUB_COM_ID' => $info['SUB_COM_ID'],
  62. 'IS_DIRECT_SELLER' => $info['IS_DIRECT_SELLER'],
  63. 'BACKUP_AT' => Date::nowTime(),
  64. 'PARTITION_DATE' => Date::ociToDate(),
  65. ];
  66. }
  67. if($insert){
  68. HistoryBonus::batchInsert($insert);
  69. }
  70. }
  71. }
  72. $this->backupCompleted();
  73. }catch (\PDOException $e) {
  74. $error = $e->getMessage();
  75. // 说明数据读取完成了
  76. if (strpos($error, 'fetch out of sequence') !== false) {
  77. $this->backupCompleted();
  78. } else {
  79. //todo log
  80. //Logger::error($this->getThrowMessage($e), 'historyBonus');
  81. }
  82. }catch(\Exception $e){
  83. print_r($e->getMessage());
  84. //todo log
  85. //Logger::error($this->getThrowMessage($e), 'historyBonus');
  86. }
  87. // 开始删除
  88. if($this->_backupCompleted){
  89. try{
  90. $this->_deleteHistoryByLimit();
  91. return true;
  92. }catch(\Exception $e){
  93. //todo log
  94. //Logger::error('[deleteHistoryByLimit]' . $this->getThrowMessage($e), 'historyBonus');
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. /**
  101. * 完成
  102. */
  103. public function backupCompleted(){
  104. // todo log
  105. //Logger::error('备份完成[success]于 ' . date('Y-m-d H:i:s', Date::nowTime()), 'historyBonus');
  106. $this->_backupCompleted = true;
  107. }
  108. /**
  109. *
  110. */
  111. public function handleQuery(){
  112. UserBonus::prepare();
  113. $this->_query = UserBonus::$query;
  114. $countQuery = clone $this->_query;
  115. $this->_totalCount = $this->_query->count();
  116. unset($countQuery);
  117. }
  118. /**
  119. * 删除
  120. */
  121. private function _deleteHistoryByLimit(){
  122. $config = Cache::getSystemConfig();
  123. $historyBonusLimit = (int)$config['historyBonusLimit']['VALUE'];
  124. if($historyBonusLimit > 0){
  125. // 604800 = 7 * 86400
  126. $limit = 604800 * $historyBonusLimit;
  127. // 多少周以前
  128. $timestamp = Date::nowTime() - $limit;
  129. // todo logger
  130. //Logger::error('开始删除于 ' . date('Y-m-d H:i:s', Date::nowTime()), 'historyBonus');
  131. HistoryBonus::deleteAll("BACKUP_AT<=:BACKUP_AT", ['BACKUP_AT' => $timestamp]);
  132. // todo logger
  133. //Logger::error('删除结束于 ' . date('Y-m-d H:i:s', Date::nowTime()), 'historyBonus');
  134. }
  135. }
  136. }