TextbookSyncCommand.php 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. Yii::import('application.models.*');
  3. /**
  4. * 处理晨读词汇历史批次id数据脚本(一次性)
  5. */
  6. class TextbookSyncCommand extends CConsoleCommand {
  7. public function init() {
  8. parent::init();
  9. @ini_set('memory_limit', '1024M');
  10. set_time_limit(0);
  11. }
  12. public function actionIndex($YII_ENV='development')
  13. {
  14. echo "start 同步教材数据.....\n";
  15. if(YII_ENV == "production"){
  16. $url = "http://zsybrain.zhixinhuixue.com/rest/all_textbook/tree";
  17. }else{
  18. if(YII_ENV == 'development'){
  19. $url = "http://zsybrain.dev.xueping.com/rest/all_textbook/tree";
  20. }else{
  21. $url = "http://zsybrain.testing.xueping.com/rest/all_textbook/tree";
  22. }
  23. }
  24. $json_material = Curl::post($url."/subjectId/3");
  25. $json_material = json_decode($json_material, true);
  26. $textbooksData=isset($json_material['status']) &&$json_material['status'] && isset($json_material['data'])?$json_material['data']:array();
  27. $textbooks=array();
  28. foreach($textbooksData as $item){
  29. if(isset($textbooks[$item['textbook_name']])){
  30. echo '教材名称已存在';
  31. exit;
  32. }
  33. $textbooks[$item['textbook_name']]=$item['textbook_id'];
  34. }
  35. if(!$textbooks){
  36. echo "获取全学科教材失败!";
  37. exit;
  38. }
  39. //先删除高三数据
  40. SchoolTextbook::model()->deleteAll("grade=:grade",array(':grade'=>3,));
  41. $textbookMap=array(
  42. array('old_textbook_id'=>43,'old_textbook_name'=>'新人教A版','new_textbook_name'=>'人教A版(2019)'),
  43. array('old_textbook_id'=>13,'old_textbook_name'=>'人教A版','new_textbook_name'=>'人教A版'),
  44. array('old_textbook_id'=>44,'old_textbook_name'=>'新人教B版','new_textbook_name'=>'人教B版(2019)'),
  45. array('old_textbook_id'=>14,'old_textbook_name'=>'人教B版','new_textbook_name'=>'人教B版'),
  46. array('old_textbook_id'=>45,'old_textbook_name'=>'新北师大版','new_textbook_name'=>'北师大版(2019)'),
  47. array('old_textbook_id'=>16,'old_textbook_name'=>'北师大版','new_textbook_name'=>'北师大版'),
  48. array('old_textbook_id'=>47,'old_textbook_name'=>'新湘教版','new_textbook_name'=>'湘教版(2019)'),
  49. array('old_textbook_id'=>32,'old_textbook_name'=>'湘教版','new_textbook_name'=>'湘教版'),
  50. array('old_textbook_id'=>46,'old_textbook_name'=>'新苏教版','new_textbook_name'=>'苏教版(2019)'),
  51. array('old_textbook_id'=>15,'old_textbook_name'=>'苏教版','new_textbook_name'=>'苏教版'),
  52. );
  53. $textbookIds=array();
  54. foreach($textbookMap as &$item){
  55. if(!isset($textbooks[$item['new_textbook_name']])){
  56. echo '无教材id:'.$item['new_textbook_name'].'\n';
  57. continue;
  58. }
  59. $item['new_textbook_id']=$textbooks[$item['new_textbook_name']];
  60. $textbookIds[$item['old_textbook_id']]=$textbooks[$item['new_textbook_name']];
  61. }
  62. //线上教材数据自动转换为全学科题库教材版本数据 数据同步
  63. foreach($textbookIds as $oldTextbookid=>$newTextbookId){
  64. if(!SchoolTextbook::model()->updateAll(array('textbook_id'=>$newTextbookId,'subject_id'=>3),"textbook_id=:textbook_id",array(':textbook_id'=>$oldTextbookid))){
  65. echo "教材id:{$oldTextbookid}无更新\n";
  66. }
  67. }
  68. //线上教材数据自动转换为全学科题库教材版本数据 缓存同步
  69. $sql="select school_id from school_textbook group by school_id";
  70. $schoolIds=SchoolTextbook::model()->getDbConnection()->createCommand($sql)->queryAll();
  71. foreach($schoolIds as $item){
  72. //更新缓存
  73. $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$item['school_id']));
  74. $redisData=array();
  75. foreach ($check as $item){
  76. $redisData[]=array(
  77. 'grade'=>$item['grade'],
  78. 'textbook_id'=>$item['textbook_id'],
  79. 'subject_id'=>$item['subject_id'],
  80. 'school_id'=>$item['school_id']
  81. );
  82. }
  83. Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$item['school_id'],array('setting'=>jsonEncode($redisData)));
  84. echo "更新缓存数组school_id:{$item['school_id']}\n";
  85. }
  86. echo 'end 同步教材数据\n';
  87. }
  88. }