TextbookSyncQxkCommand.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. Yii::import('application.models.*');
  3. /**
  4. * 同步数据,旧教材对应新增新教材
  5. */
  6. class TextbookSyncQxkCommand extends CConsoleCommand
  7. {
  8. public function init()
  9. {
  10. parent::init();
  11. @ini_set('memory_limit', '1024M');
  12. set_time_limit(0);
  13. }
  14. public $textbookMap = array(
  15. 43 => array('old_textbook_id' => 43, 'old_textbook_name' => '新人教A版', 'new_textbook_name' => '人教A版(2019)', 'new_textbook_id' => 6101, 'subject_id' => 51,),
  16. 13 => array('old_textbook_id' => 13, 'old_textbook_name' => '人教A版', 'new_textbook_name' => '人教A版', 'new_textbook_id' => 6100, 'subject_id' => 3,),
  17. 44 => array('old_textbook_id' => 44, 'old_textbook_name' => '新人教B版', 'new_textbook_name' => '人教B版(2019)', 'new_textbook_id' => 6103, 'subject_id' => 51,),
  18. 14 => array('old_textbook_id' => 14, 'old_textbook_name' => '人教B版', 'new_textbook_name' => '人教B版', 'new_textbook_id' => 6102, 'subject_id' => 3,),
  19. 45 => array('old_textbook_id' => 45, 'old_textbook_name' => '新北师大版', 'new_textbook_name' => '北师大版(2019)', 'new_textbook_id' => 6105, 'subject_id' => 51,),
  20. 16 => array('old_textbook_id' => 16, 'old_textbook_name' => '北师大版', 'new_textbook_name' => '北师大版', 'new_textbook_id' => 6104, 'subject_id' => 3,),
  21. 47 => array('old_textbook_id' => 47, 'old_textbook_name' => '新湘教版', 'new_textbook_name' => '湘教版(2019)', 'new_textbook_id' => 6109, 'subject_id' => 51,),
  22. 32 => array('old_textbook_id' => 32, 'old_textbook_name' => '湘教版', 'new_textbook_name' => '湘教版', 'new_textbook_id' => 6108, 'subject_id' => 3,),
  23. 46 => array('old_textbook_id' => 46, 'old_textbook_name' => '新苏教版', 'new_textbook_name' => '苏教版(2019)', 'new_textbook_id' => 6107, 'subject_id' => 51,),
  24. 15 => array('old_textbook_id' => 15, 'old_textbook_name' => '苏教版', 'new_textbook_name' => '苏教版', 'new_textbook_id' => 6106, 'subject_id' => 3,),
  25. );
  26. public function actionIndex($YII_ENV = 'development')
  27. {
  28. echo "start 教材数据.....\n";
  29. //旧教材
  30. $sql="select school_id,grade,textbook_id,subject_id from school_textbook where textbook_id<100 order by school_id ";
  31. //$sql="select school_id,grade,textbook_id,subject_id from school_textbook where textbook_id<100 and school_id=3980 order by school_id ";
  32. $textbooks=SchoolTextbook::model()->getDbConnection()->createCommand($sql)->queryAll();
  33. $qxkTextbooks=array();
  34. echo '开始整理数据'.PHP_EOL;
  35. foreach($textbooks as $textbook){
  36. if(isset($this->textbookMap[$textbook['textbook_id']])){
  37. $qxkTextbook=$this->textbookMap[$textbook['textbook_id']];
  38. $qxkTextbooks[]=array(
  39. 'school_id'=>$textbook['school_id'],
  40. 'grade'=>$textbook['grade'],
  41. 'textbook_id'=>$qxkTextbook['new_textbook_id'],
  42. 'subject_id'=>3,
  43. 'is_qxk'=>1,
  44. );
  45. }
  46. }
  47. echo '结束整理数据'.PHP_EOL;
  48. $conn = $this->getDbConnection(Yii::app()->params["default_server"]['addr'], Yii::app()->params["default_db"]['name'], Yii::app()->params["default_server"]['username'], Yii::app()->params["default_server"]['password']);
  49. if(!$conn){
  50. echo '连接失败'.PHP_EOL;
  51. exit;
  52. }
  53. echo '开始插入数据'.PHP_EOL;
  54. if($qxkTextbooks){
  55. $textbooksColumn = 'school_id,grade,textbook_id,subject_id,is_qxk';
  56. $textbooksSql='';
  57. foreach($qxkTextbooks as $item){
  58. $textbooksSql .= "('" . implode("','", $item) . "'),";
  59. }
  60. $textbooksSql=trim($textbooksSql,',');
  61. $sql = "REPLACE INTO `school_textbook` ($textbooksColumn) VALUES $textbooksSql";
  62. $conn->createCommand($sql)->execute();
  63. }
  64. echo '结束插入数据'.PHP_EOL;
  65. //线上教材数据自动转换为全学科题库教材版本数据 缓存同步
  66. $sql="select school_id from school_textbook group by school_id";
  67. //$sql="select school_id from school_textbook where school_id=3980 group by school_id";
  68. $schools=SchoolTextbook::model()->getDbConnection()->createCommand($sql)->queryAll();
  69. foreach($schools as $school){
  70. //更新缓存
  71. $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$school['school_id']));
  72. $redisData=array();
  73. foreach ($check as $item){
  74. $redisData[]=array(
  75. 'grade'=>$item['grade'],
  76. 'textbook_id'=>$item['textbook_id'],
  77. 'subject_id'=>$item['subject_id'],
  78. 'school_id'=>$item['school_id'],
  79. 'is_qxk'=>$item['is_qxk'],
  80. );
  81. }
  82. Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$item['school_id'],array('setting'=>jsonEncode($redisData)));
  83. echo "更新缓存数组school_id:{$school['school_id']}".PHP_EOL;
  84. }
  85. echo 'end'.PHP_EOL;
  86. }
  87. public function getDbConnection($database_host,$database_name,$database_user,$database_password)
  88. {
  89. if($database_host && $database_name && $database_user && $database_password){
  90. $myDbDsn = 'mysql:host=' . $database_host . ';dbname=' . $database_name;
  91. $my_connection = new CDbConnection($myDbDsn, $database_user, $database_password);
  92. $my_connection->emulatePrepare = true;
  93. $my_connection->enableProfiling = true;
  94. $my_connection->enableParamLogging = true;
  95. $myDbDsn = null;
  96. return $my_connection;
  97. }else{
  98. return null;
  99. }
  100. }
  101. }