123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- Yii::import('application.models.*');
- /**
- * 同步数据,旧教材对应新增新教材
- */
- class TextbookSyncQxkCommand extends CConsoleCommand
- {
- public function init()
- {
- parent::init();
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
- public $textbookMap = array(
- 43 => array('old_textbook_id' => 43, 'old_textbook_name' => '新人教A版', 'new_textbook_name' => '人教A版(2019)', 'new_textbook_id' => 6101, 'subject_id' => 51,),
- 13 => array('old_textbook_id' => 13, 'old_textbook_name' => '人教A版', 'new_textbook_name' => '人教A版', 'new_textbook_id' => 6100, 'subject_id' => 3,),
- 44 => array('old_textbook_id' => 44, 'old_textbook_name' => '新人教B版', 'new_textbook_name' => '人教B版(2019)', 'new_textbook_id' => 6103, 'subject_id' => 51,),
- 14 => array('old_textbook_id' => 14, 'old_textbook_name' => '人教B版', 'new_textbook_name' => '人教B版', 'new_textbook_id' => 6102, 'subject_id' => 3,),
- 45 => array('old_textbook_id' => 45, 'old_textbook_name' => '新北师大版', 'new_textbook_name' => '北师大版(2019)', 'new_textbook_id' => 6105, 'subject_id' => 51,),
- 16 => array('old_textbook_id' => 16, 'old_textbook_name' => '北师大版', 'new_textbook_name' => '北师大版', 'new_textbook_id' => 6104, 'subject_id' => 3,),
- 47 => array('old_textbook_id' => 47, 'old_textbook_name' => '新湘教版', 'new_textbook_name' => '湘教版(2019)', 'new_textbook_id' => 6109, 'subject_id' => 51,),
- 32 => array('old_textbook_id' => 32, 'old_textbook_name' => '湘教版', 'new_textbook_name' => '湘教版', 'new_textbook_id' => 6108, 'subject_id' => 3,),
- 46 => array('old_textbook_id' => 46, 'old_textbook_name' => '新苏教版', 'new_textbook_name' => '苏教版(2019)', 'new_textbook_id' => 6107, 'subject_id' => 51,),
- 15 => array('old_textbook_id' => 15, 'old_textbook_name' => '苏教版', 'new_textbook_name' => '苏教版', 'new_textbook_id' => 6106, 'subject_id' => 3,),
- );
- public function actionIndex($YII_ENV = 'development')
- {
- echo "start 教材数据.....\n";
- //旧教材
- $sql="select school_id,grade,textbook_id,subject_id from school_textbook where textbook_id<100 order by school_id ";
- //$sql="select school_id,grade,textbook_id,subject_id from school_textbook where textbook_id<100 and school_id=3980 order by school_id ";
- $textbooks=SchoolTextbook::model()->getDbConnection()->createCommand($sql)->queryAll();
- $qxkTextbooks=array();
- echo '开始整理数据'.PHP_EOL;
- foreach($textbooks as $textbook){
- if(isset($this->textbookMap[$textbook['textbook_id']])){
- $qxkTextbook=$this->textbookMap[$textbook['textbook_id']];
- $qxkTextbooks[]=array(
- 'school_id'=>$textbook['school_id'],
- 'grade'=>$textbook['grade'],
- 'textbook_id'=>$qxkTextbook['new_textbook_id'],
- 'subject_id'=>3,
- 'is_qxk'=>1,
- );
- }
- }
- echo '结束整理数据'.PHP_EOL;
- $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']);
- if(!$conn){
- echo '连接失败'.PHP_EOL;
- exit;
- }
- echo '开始插入数据'.PHP_EOL;
- if($qxkTextbooks){
- $textbooksColumn = 'school_id,grade,textbook_id,subject_id,is_qxk';
- $textbooksSql='';
- foreach($qxkTextbooks as $item){
- $textbooksSql .= "('" . implode("','", $item) . "'),";
- }
- $textbooksSql=trim($textbooksSql,',');
- $sql = "REPLACE INTO `school_textbook` ($textbooksColumn) VALUES $textbooksSql";
- $conn->createCommand($sql)->execute();
- }
- echo '结束插入数据'.PHP_EOL;
- //线上教材数据自动转换为全学科题库教材版本数据 缓存同步
- $sql="select school_id from school_textbook group by school_id";
- //$sql="select school_id from school_textbook where school_id=3980 group by school_id";
- $schools=SchoolTextbook::model()->getDbConnection()->createCommand($sql)->queryAll();
- foreach($schools as $school){
- //更新缓存
- $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$school['school_id']));
- $redisData=array();
- foreach ($check as $item){
- $redisData[]=array(
- 'grade'=>$item['grade'],
- 'textbook_id'=>$item['textbook_id'],
- 'subject_id'=>$item['subject_id'],
- 'school_id'=>$item['school_id'],
- 'is_qxk'=>$item['is_qxk'],
- );
- }
- Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$item['school_id'],array('setting'=>jsonEncode($redisData)));
- echo "更新缓存数组school_id:{$school['school_id']}".PHP_EOL;
- }
- echo 'end'.PHP_EOL;
- }
- public function getDbConnection($database_host,$database_name,$database_user,$database_password)
- {
- if($database_host && $database_name && $database_user && $database_password){
- $myDbDsn = 'mysql:host=' . $database_host . ';dbname=' . $database_name;
- $my_connection = new CDbConnection($myDbDsn, $database_user, $database_password);
- $my_connection->emulatePrepare = true;
- $my_connection->enableProfiling = true;
- $my_connection->enableParamLogging = true;
- $myDbDsn = null;
- return $my_connection;
- }else{
- return null;
- }
- }
- }
|