SubjectProductSettingCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/8/29
  6. * Time: 16:59
  7. */
  8. class SubjectProductSettingCommand 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 subjectproductsetting index schoolId=3980 --YII_ENV=testing
  19. *
  20. */
  21. public function actionIndex($YII_ENV = 'development')
  22. {
  23. echo 'start 学科产品设置数据同步脚本.....' . PHP_EOL;
  24. foreach ($_SERVER['argv'] as $k => $v) {
  25. if (strpos($v, 'schoolId=') !== FALSE) {
  26. $schoolId = substr($v, strlen('schoolId='));
  27. }
  28. }
  29. $schools = isset($schoolId) ? array(0=>array('school_id'=> $schoolId)) : $this->getSchools();
  30. foreach ($schools as $school) {
  31. echo 'start school_id:' . $school['school_id'] . '...' . PHP_EOL;
  32. $con = $this->getSchoolDbCon($school['school_id']);
  33. if (!$con) {
  34. echo 'school database cannot connect' . PHP_EOL;
  35. continue;
  36. }
  37. try {
  38. self::syncTpl($con);
  39. echo 'school_id:'.$school['school_id'] . 'done' . PHP_EOL;
  40. } catch (\Exception $e) {
  41. echo $e->getMessage() . PHP_EOL;
  42. continue;
  43. }
  44. sleep(0.1);
  45. }
  46. echo 'end 处理完成' . PHP_EOL;
  47. exit;
  48. }
  49. /**
  50. * 获取所有正常可用的学校
  51. * @return mixed
  52. */
  53. private function getSchools()
  54. {
  55. $db = Yii::app()->businessDb;
  56. $sql = "SELECT school_id,school_name FROM `school` WHERE `status`=0 ";
  57. $schools = $db->createCommand($sql)->queryAll();
  58. $db->close();
  59. return $schools;
  60. }
  61. /**
  62. * 学校库连接
  63. * @param $schoolId
  64. * @return bool|CDbConnection
  65. */
  66. public function getSchoolDbCon($schoolId)
  67. {
  68. $db = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
  69. if (empty($db)) {
  70. return false;
  71. }
  72. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  73. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  74. $my_connection->emulatePrepare = true;
  75. $my_connection->enableProfiling = true;
  76. $my_connection->enableParamLogging = true;
  77. $myDbDsn = null;
  78. return $my_connection;
  79. }
  80. /**
  81. * 同步学科数据
  82. * @param $con object 数据库连接
  83. */
  84. private function syncTpl($con)
  85. {
  86. $spsIds=array(1,2,3,4);
  87. foreach($spsIds as $spsId){
  88. $sql="select * from subject_product_setting where sps_id={$spsId}";
  89. $subjectProductStting = $con->createCommand($sql)->queryRow();
  90. $time=time();
  91. if(!$subjectProductStting){
  92. $insertSql='';
  93. if($spsId==1){
  94. $insertSql="INSERT INTO `subject_product_setting` VALUES (1, 3, '数学', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
  95. }elseif($spsId==2){
  96. $insertSql="INSERT INTO `subject_product_setting` VALUES (2, 12, '物理', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
  97. }elseif($spsId==3){
  98. $insertSql="INSERT INTO `subject_product_setting` VALUES (3, 8, '英语', '{\"new_version_size\":false}', {$time}, 0)";
  99. }elseif($spsId==4){
  100. $insertSql="INSERT INTO `subject_product_setting` VALUES (4, 0, '其他学科', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
  101. }
  102. if($insertSql){
  103. echo $insertSql.PHP_EOL;
  104. $con->createCommand($insertSql)->execute();
  105. }
  106. }
  107. }
  108. }
  109. }