SubjectProductSettingFixCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/8/29
  6. * Time: 16:59
  7. */
  8. class SubjectProductSettingFixCommand extends CConsoleCommand
  9. {
  10. public function init()
  11. {
  12. parent::init();
  13. @ini_set('memory_limit', '1024M');
  14. set_time_limit(0);
  15. }
  16. /***
  17. *
  18. * 学科产品设置数据同步脚本/usr/local/php5.3/bin/php /home/www/vhosts/zxhx.testing/zsyas2/protected/shell/yiic.php subjectproductsettingfix index schoolId=3980 --YII_ENV=testing
  19. *
  20. */
  21. public function actionIndex($YII_ENV = 'development')
  22. {
  23. echo 'start 学科产品设置数据同步脚本.....' . PHP_EOL;
  24. $schoolId=0;
  25. foreach ($_SERVER['argv'] as $k => $v) {
  26. if (strpos($v, 'schoolId=') !== FALSE) {
  27. $schoolId = substr($v, strlen('schoolId='));
  28. }
  29. }
  30. $databases=$this->getDatabases($schoolId);
  31. foreach($databases as $db){
  32. echo 'start database_name:' . $db['database_name'] . '...' . PHP_EOL;
  33. $con = $this->getDbCon($db);
  34. if (!$con) {
  35. echo ' database cannot connect' . PHP_EOL;
  36. continue;
  37. }
  38. try {
  39. self::syncTpl($con);
  40. echo 'database_name:'.$db['database_name'] . 'done' . PHP_EOL;
  41. } catch (\Exception $e) {
  42. echo $e->getMessage() . PHP_EOL;
  43. continue;
  44. }
  45. sleep(0.1);
  46. }
  47. echo 'end 处理完成' . PHP_EOL;
  48. exit;
  49. }
  50. /**
  51. * 库连接
  52. * @param $db
  53. * @return bool|CDbConnection
  54. */
  55. public function getDbCon($db)
  56. {
  57. if (empty($db)) {
  58. return false;
  59. }
  60. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  61. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  62. $my_connection->emulatePrepare = true;
  63. $my_connection->enableProfiling = true;
  64. $my_connection->enableParamLogging = true;
  65. $myDbDsn = null;
  66. return $my_connection;
  67. }
  68. /**
  69. * 获取学校数据库
  70. * @return mixed
  71. */
  72. private function getDatabases($schoolId)
  73. {
  74. if($schoolId) {
  75. $dbs = BusinessDatabase::model()->findAll('school_id=:sid', array(':sid' => $schoolId));
  76. }else{
  77. $dbs= BusinessDatabase::model()->findAll();
  78. }
  79. return $dbs;
  80. }
  81. /**
  82. * 同步学科数据
  83. * @param $con object 数据库连接
  84. */
  85. private function syncTpl($con)
  86. {
  87. $spsIds=array(1,2,3,4);
  88. foreach($spsIds as $spsId){
  89. $sql="select * from subject_product_setting where sps_id={$spsId}";
  90. $subjectProductStting = $con->createCommand($sql)->queryRow();
  91. $time=time();
  92. if(!$subjectProductStting){
  93. $insertSql='';
  94. if($spsId==1){
  95. $insertSql="INSERT INTO `subject_product_setting` VALUES (1, 3, '数学', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
  96. }elseif($spsId==2){
  97. $insertSql="INSERT INTO `subject_product_setting` VALUES (2, 12, '物理', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
  98. }elseif($spsId==3){
  99. $insertSql="INSERT INTO `subject_product_setting` VALUES (3, 8, '英语', '{\"new_version_size\":false}', {$time}, 0)";
  100. }elseif($spsId==4){
  101. $insertSql="INSERT INTO `subject_product_setting` VALUES (4, 0, '其他学科', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
  102. }
  103. if($insertSql){
  104. echo $insertSql.PHP_EOL;
  105. $con->createCommand($insertSql)->execute();
  106. }
  107. }
  108. }
  109. }
  110. }