123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2022/8/29
- * Time: 16:59
- */
- class SubjectProductSettingCommand 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 subjectproductsetting index schoolId=3980 --YII_ENV=testing
- *
- */
- public function actionIndex($YII_ENV = 'development')
- {
- echo 'start 学科产品设置数据同步脚本.....' . PHP_EOL;
- foreach ($_SERVER['argv'] as $k => $v) {
- if (strpos($v, 'schoolId=') !== FALSE) {
- $schoolId = substr($v, strlen('schoolId='));
- }
- }
- $schools = isset($schoolId) ? array(0=>array('school_id'=> $schoolId)) : $this->getSchools();
- foreach ($schools as $school) {
- echo 'start school_id:' . $school['school_id'] . '...' . PHP_EOL;
- $con = $this->getSchoolDbCon($school['school_id']);
- if (!$con) {
- echo 'school database cannot connect' . PHP_EOL;
- continue;
- }
- try {
- self::syncTpl($con);
- echo 'school_id:'.$school['school_id'] . 'done' . PHP_EOL;
- } catch (\Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- continue;
- }
- sleep(0.1);
- }
- echo 'end 处理完成' . PHP_EOL;
- exit;
- }
- /**
- * 获取所有正常可用的学校
- * @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;
- }
- /**
- * 同步学科数据
- * @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();
- }
- }
- }
- }
- }
|