1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
- Yii::import('application.models.*');
- /**
- * 处理晨读词汇历史批次id数据脚本(一次性)
- */
- class TextbookSyncCommand extends CConsoleCommand {
- public function init() {
- parent::init();
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
- public function actionIndex($YII_ENV='development')
- {
- echo "start 同步教材数据.....\n";
- if(YII_ENV == "production"){
- $url = "http://zsybrain.zhixinhuixue.com/rest/all_textbook/tree";
- }else{
- if(YII_ENV == 'development'){
- $url = "http://zsybrain.dev.xueping.com/rest/all_textbook/tree";
- }else{
- $url = "http://zsybrain.testing.xueping.com/rest/all_textbook/tree";
- }
- }
- $json_material = Curl::post($url."/subjectId/3");
- $json_material = json_decode($json_material, true);
- $textbooksData=isset($json_material['status']) &&$json_material['status'] && isset($json_material['data'])?$json_material['data']:array();
- $textbooks=array();
- foreach($textbooksData as $item){
- if(isset($textbooks[$item['textbook_name']])){
- echo '教材名称已存在';
- exit;
- }
- $textbooks[$item['textbook_name']]=$item['textbook_id'];
- }
- if(!$textbooks){
- echo "获取全学科教材失败!";
- exit;
- }
- //先删除高三数据
- SchoolTextbook::model()->deleteAll("grade=:grade",array(':grade'=>3,));
- $textbookMap=array(
- array('old_textbook_id'=>43,'old_textbook_name'=>'新人教A版','new_textbook_name'=>'人教A版(2019)'),
- array('old_textbook_id'=>13,'old_textbook_name'=>'人教A版','new_textbook_name'=>'人教A版'),
- array('old_textbook_id'=>44,'old_textbook_name'=>'新人教B版','new_textbook_name'=>'人教B版(2019)'),
- array('old_textbook_id'=>14,'old_textbook_name'=>'人教B版','new_textbook_name'=>'人教B版'),
- array('old_textbook_id'=>45,'old_textbook_name'=>'新北师大版','new_textbook_name'=>'北师大版(2019)'),
- array('old_textbook_id'=>16,'old_textbook_name'=>'北师大版','new_textbook_name'=>'北师大版'),
- array('old_textbook_id'=>47,'old_textbook_name'=>'新湘教版','new_textbook_name'=>'湘教版(2019)'),
- array('old_textbook_id'=>32,'old_textbook_name'=>'湘教版','new_textbook_name'=>'湘教版'),
- array('old_textbook_id'=>46,'old_textbook_name'=>'新苏教版','new_textbook_name'=>'苏教版(2019)'),
- array('old_textbook_id'=>15,'old_textbook_name'=>'苏教版','new_textbook_name'=>'苏教版'),
- );
- $textbookIds=array();
- foreach($textbookMap as &$item){
- if(!isset($textbooks[$item['new_textbook_name']])){
- echo '无教材id:'.$item['new_textbook_name'].'\n';
- continue;
- }
- $item['new_textbook_id']=$textbooks[$item['new_textbook_name']];
- $textbookIds[$item['old_textbook_id']]=$textbooks[$item['new_textbook_name']];
- }
- //线上教材数据自动转换为全学科题库教材版本数据 数据同步
- foreach($textbookIds as $oldTextbookid=>$newTextbookId){
- if(!SchoolTextbook::model()->updateAll(array('textbook_id'=>$newTextbookId,'subject_id'=>3),"textbook_id=:textbook_id",array(':textbook_id'=>$oldTextbookid))){
- echo "教材id:{$oldTextbookid}无更新\n";
- }
- }
- //线上教材数据自动转换为全学科题库教材版本数据 缓存同步
- $sql="select school_id from school_textbook group by school_id";
- $schoolIds=SchoolTextbook::model()->getDbConnection()->createCommand($sql)->queryAll();
- foreach($schoolIds as $item){
- //更新缓存
- $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$item['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']
- );
- }
- Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$item['school_id'],array('setting'=>jsonEncode($redisData)));
- echo "更新缓存数组school_id:{$item['school_id']}\n";
- }
- echo 'end 同步教材数据\n';
- }
- }
|