SyncProductSettingCommand.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. Yii::import('application.models.*');
  3. Yii::import('application.components.*');
  4. class SyncProductSettingCommand extends CConsoleCommand
  5. {
  6. public function init()
  7. {
  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 产品设置同步脚本.....' . PHP_EOL;
  15. $schoolId=0;
  16. foreach ($_SERVER['argv'] as $k => $v) {
  17. if (strpos($v, 'schoolId=') !== FALSE) {
  18. $schoolId = substr($v, strlen('schoolId='));
  19. }
  20. }
  21. $databases=$this->getDatabases($schoolId);
  22. foreach ($databases as $db) {
  23. echo 'start database_name:' . $db['database_name'] . '...' . PHP_EOL;
  24. $con = $this->getDbCon($db);
  25. if (!$con) {
  26. echo 'database cannot connect' . PHP_EOL;
  27. continue;
  28. }
  29. try {
  30. self::syncSetting($con);
  31. echo 'database_name:'.$db['database_name'] . 'done' . PHP_EOL;
  32. } catch (\Exception $e) {
  33. echo $e->getMessage() . PHP_EOL;
  34. continue;
  35. }
  36. sleep(0.1);
  37. }
  38. echo 'end 处理完成' . PHP_EOL;
  39. exit;
  40. }
  41. /**
  42. * 库连接
  43. * @param $db
  44. * @return bool|CDbConnection
  45. */
  46. public function getDbCon($db)
  47. {
  48. if (empty($db)) {
  49. return false;
  50. }
  51. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  52. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  53. $my_connection->emulatePrepare = true;
  54. $my_connection->enableProfiling = true;
  55. $my_connection->enableParamLogging = true;
  56. $myDbDsn = null;
  57. return $my_connection;
  58. }
  59. /**
  60. * 获取学校数据库
  61. * @return mixed
  62. */
  63. private function getDatabases($schoolId)
  64. {
  65. if($schoolId) {
  66. $dbs = BusinessDatabase::model()->findAll('school_id=:sid', array(':sid' => $schoolId));
  67. }else{
  68. $dbs= BusinessDatabase::model()->findAll();
  69. }
  70. return $dbs;
  71. }
  72. /**
  73. * 获取所有正常可用的学校
  74. * @return mixed
  75. */
  76. private function getSchools()
  77. {
  78. $db = Yii::app()->businessDb;
  79. $sql = "SELECT school_id,school_name FROM `school` WHERE `status`=0 ";
  80. $schools = $db->createCommand($sql)->queryAll();
  81. $db->close();
  82. return $schools;
  83. }
  84. /**
  85. * 学校库连接
  86. * @param $schoolId
  87. * @return bool|CDbConnection
  88. */
  89. public function getSchoolDbCon($schoolId)
  90. {
  91. $db = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
  92. if (empty($db)) {
  93. return false;
  94. }
  95. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  96. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  97. $my_connection->emulatePrepare = true;
  98. $my_connection->enableProfiling = true;
  99. $my_connection->enableParamLogging = true;
  100. $myDbDsn = null;
  101. return $my_connection;
  102. }
  103. public function syncSetting($con){
  104. $sql = "select product_type,custom_config from product_setting where product_type = 6 order by ps_id ";
  105. $templates = $con->createCommand($sql)->queryRow();
  106. if($templates){
  107. $templates = json_decode($templates['custom_config'],true);
  108. if(!isset($templates['setting']['productName'])){
  109. $templates['setting']['productName'] = '个性化学习宝';
  110. }
  111. if(!isset($templates['setting']['oldPaperAnswer'])){
  112. $templates['setting']['oldPaperAnswer'] = 1;
  113. }
  114. //$templates = json_encode($templates,JSON_UNESCAPED_UNICODE);
  115. $templates = $this->myjson($templates);
  116. $sql = "update product_setting set custom_config = '$templates' where product_type=6 ";
  117. $templates = $con->createCommand($sql)->execute();
  118. echo sprintf("同步完成").PHP_EOL;
  119. }
  120. }
  121. public function myjson($code) {
  122. $code = json_encode(self::urlencodeAry($code));
  123. return urldecode($code);
  124. }
  125. public static function urlencodeAry($data) {
  126. if(is_array($data)) {
  127. foreach($data as $key=>$val) {
  128. $data[$key] = static::urlencodeAry($val);
  129. }
  130. return $data;
  131. } else {
  132. return urlencode($data);
  133. }
  134. }
  135. }