123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- Yii::import('application.models.*');
- Yii::import('application.components.*');
- class SyncProductSettingCommand extends CConsoleCommand
- {
- public function init()
- {
- parent::init();
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
- public function actionIndex($YII_ENV = 'development')
- {
- echo 'start 产品设置同步脚本.....' . PHP_EOL;
- $schoolId=0;
- foreach ($_SERVER['argv'] as $k => $v) {
- if (strpos($v, 'schoolId=') !== FALSE) {
- $schoolId = substr($v, strlen('schoolId='));
- }
- }
- $databases=$this->getDatabases($schoolId);
- foreach ($databases as $db) {
- echo 'start database_name:' . $db['database_name'] . '...' . PHP_EOL;
- $con = $this->getDbCon($db);
- if (!$con) {
- echo 'database cannot connect' . PHP_EOL;
- continue;
- }
- try {
- self::syncSetting($con);
- echo 'database_name:'.$db['database_name'] . 'done' . PHP_EOL;
- } catch (\Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- continue;
- }
- sleep(0.1);
- }
- echo 'end 处理完成' . PHP_EOL;
- exit;
- }
- /**
- * 库连接
- * @param $db
- * @return bool|CDbConnection
- */
- public function getDbCon($db)
- {
- if (empty($db)) {
- return false;
- }
- $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
- $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
- $my_connection->emulatePrepare = true;
- $my_connection->enableProfiling = true;
- $my_connection->enableParamLogging = true;
- $myDbDsn = null;
- return $my_connection;
- }
- /**
- * 获取学校数据库
- * @return mixed
- */
- private function getDatabases($schoolId)
- {
- if($schoolId) {
- $dbs = BusinessDatabase::model()->findAll('school_id=:sid', array(':sid' => $schoolId));
- }else{
- $dbs= BusinessDatabase::model()->findAll();
- }
- return $dbs;
- }
- /**
- * 获取所有正常可用的学校
- * @return mixed
- */
- private function getSchools()
- {
- $db = Yii::app()->businessDb;
- $sql = "SELECT school_id,school_name FROM `school` WHERE `status`=0 ";
- $schools = $db->createCommand($sql)->queryAll();
- $db->close();
- return $schools;
- }
- /**
- * 学校库连接
- * @param $schoolId
- * @return bool|CDbConnection
- */
- public function getSchoolDbCon($schoolId)
- {
- $db = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
- if (empty($db)) {
- return false;
- }
- $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
- $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
- $my_connection->emulatePrepare = true;
- $my_connection->enableProfiling = true;
- $my_connection->enableParamLogging = true;
- $myDbDsn = null;
- return $my_connection;
- }
- public function syncSetting($con){
- $sql = "select product_type,custom_config from product_setting where product_type = 6 order by ps_id ";
- $templates = $con->createCommand($sql)->queryRow();
- if($templates){
- $templates = json_decode($templates['custom_config'],true);
- if(!isset($templates['setting']['productName'])){
- $templates['setting']['productName'] = '个性化学习宝';
- }
- if(!isset($templates['setting']['oldPaperAnswer'])){
- $templates['setting']['oldPaperAnswer'] = 1;
- }
- //$templates = json_encode($templates,JSON_UNESCAPED_UNICODE);
- $templates = $this->myjson($templates);
- $sql = "update product_setting set custom_config = '$templates' where product_type=6 ";
- $templates = $con->createCommand($sql)->execute();
- echo sprintf("同步完成").PHP_EOL;
- }
- }
- public function myjson($code) {
- $code = json_encode(self::urlencodeAry($code));
- return urldecode($code);
- }
- public static function urlencodeAry($data) {
- if(is_array($data)) {
- foreach($data as $key=>$val) {
- $data[$key] = static::urlencodeAry($val);
- }
- return $data;
- } else {
- return urlencode($data);
- }
- }
- }
|