123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2022/8/29
- * Time: 16:59
- */
- class SubjectProductSettingFixCommand extends CConsoleCommand
- {
- public function init()
- {
- parent::init();
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
- /***
- *
- * 学科产品设置数据同步脚本/usr/local/php5.3/bin/php /home/www/vhosts/zxhx.testing/zsyas2/protected/shell/yiic.php subjectproductsettingfix index schoolId=3980 --YII_ENV=testing
- *
- */
- 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::syncTpl($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;
- }
- /**
- * 同步学科数据
- * @param $con object 数据库连接
- */
- private function syncTpl($con)
- {
- $spsIds=array(1,2,3,4);
- foreach($spsIds as $spsId){
- $sql="select * from subject_product_setting where sps_id={$spsId}";
- $subjectProductStting = $con->createCommand($sql)->queryRow();
- $time=time();
- if(!$subjectProductStting){
- $insertSql='';
- if($spsId==1){
- $insertSql="INSERT INTO `subject_product_setting` VALUES (1, 3, '数学', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
- }elseif($spsId==2){
- $insertSql="INSERT INTO `subject_product_setting` VALUES (2, 12, '物理', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
- }elseif($spsId==3){
- $insertSql="INSERT INTO `subject_product_setting` VALUES (3, 8, '英语', '{\"new_version_size\":false}', {$time}, 0)";
- }elseif($spsId==4){
- $insertSql="INSERT INTO `subject_product_setting` VALUES (4, 0, '其他学科', '{\"new_version_size\":false,\"cut_blank_answer_area\":true}', {$time}, 0)";
- }
- if($insertSql){
- echo $insertSql.PHP_EOL;
- $con->createCommand($insertSql)->execute();
- }
- }
- }
- }
- }
|