$v) { if (strpos($v, 'schoolId=') !== FALSE) { $schoolId = substr($v, strlen('schoolId=')); } if (strpos($v, 'subjectId=') !== FALSE) { $subjectId = substr($v, strlen('subjectId=')); } if (strpos($v, 'productType=') !== FALSE) { $productType = substr($v, strlen('productType=')); } if (strpos($v, 'isBeta=') !== FALSE) { $isBeta = substr($v, strlen('isBeta=')); } if (strpos($v, 'targetType=') !== FALSE) { $targetType = substr($v, strlen('targetType=')); } } $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 { /** 无学科参数默认高中数学 **/ if(isset($subjectId)){ $params['subjectId'] = in_array($subjectId, array(3,6,51))? array(3,6,51) : array($subjectId); }else{ $params['subjectId'] = array(3,6,51); } if(isset($productType)) $params['productType'] = $productType; $params['isBeta'] = isset($isBeta) ? $isBeta : 0; $params['targetType']=isset($targetType)?$targetType:0; self::syncTpl($con, $params); 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; } /** * 同步模板数据 * @param $con object 数据库连接 * @param $params */ private function syncTpl($con, $params) { $subjectIds = implode(',', $params['subjectId']); $sql = "select template_id,template_name,product_type,subject_id,target_type,is_beta,config_text from product_template where subject_id in ({$subjectIds}) "; if(isset($params['productType'])){ $sql .= " and product_type = {$params['productType']}"; } if(isset($params['isBeta'])){ $sql .= " and is_beta = {$params['isBeta']}"; } if(isset($params['targetType']) && $params['targetType']){ $sql .= " and target_type = {$params['targetType']}"; } $templates = $con->createCommand($sql)->queryAll(); foreach ($templates as $template) { //FIXME 目前就数学和物理产品使用product_template表 if(in_array($template['subject_id'], array(3,6,51))){ switch ($template['product_type']){ case ProductDownload::PRODUCT_TYPE_WB: $tplJson = $template['target_type'] == 1 ? ($template['is_beta'] ? ProductMathTpl::MATH_CLASS_WB_BETA : ProductMathTpl::MATH_CLASS_WB) : ($template['is_beta'] ? ProductMathTpl::MATH_STU_WB_BETA : ProductMathTpl::MATH_STU_WB); break; case ProductDownload::PRODUCT_TYPE_ISP: $tplJson = $template['target_type'] == 1 ? ($template['is_beta'] ? ProductMathTpl::MATH_CLASS_ISP_BETA : ProductMathTpl::MATH_CLASS_ISP) : ($template['is_beta'] ? ProductMathTpl::MATH_STU_ISP_BETA : ProductMathTpl::MATH_STU_ISP); break; case ProductDownload::PRODUCT_TYPE_WB_ISP: $tplJson = $template['target_type'] == 1 ? ($template['is_beta'] ? ProductMathTpl::MATH_CLASS_WB_ISP_BETA : ProductMathTpl::MATH_CLASS_WB_ISP) : ($template['is_beta'] ? ProductMathTpl::MATH_STU_WB_ISP_BETA : ProductMathTpl::MATH_STU_WB_ISP); if($template['is_beta']){ $template['config_text']=$this->addLevelDetails($template['config_text'],$template['subject_id']); //3.4班级模板 if($template['target_type']==1) { $template['config_text'] = $this->addTypography($template['config_text'], $template['subject_id']); } } break; case ProductDownload::PRODUCT_TYPE_METHOD: $tplJson = $template['target_type'] == 1 ? ProductMethodTpl::Method_CLASS_WB : ProductMethodTpl::Method_STU_WB; break; } } if($template['subject_id'] == 12){ switch ($template['product_type']){ case ProductDownload::PRODUCT_TYPE_WB: $tplJson = $template['target_type'] == 1 ? ProductPhysicsTpl::PHYSICS_CLASS_WB : ProductPhysicsTpl::PHYSICS_STU_WB; break; case ProductDownload::PRODUCT_TYPE_ISP: $tplJson = $template['target_type'] == 1 ? ProductPhysicsTpl::PHYSICS_CLASS_ISP : ProductPhysicsTpl::PHYSICS_STU_ISP; if($template['target_type']==1) { //增加题组 $template['config_text'] = $this->addTopicGroup($template['config_text'], $template['subject_id']); } break; case ProductDownload::PRODUCT_TYPE_WB_ISP: $tplJson = $template['target_type'] == 1 ? ProductPhysicsTpl::PHYSICS_CLASS_WB_ISP : ProductPhysicsTpl::PHYSICS_STU_WB_ISP; $template['config_text']=$this->addLevelDetails($template['config_text'],$template['subject_id']); if($template['target_type']==1) { $template['config_text'] = $this->addTypography($template['config_text'], $template['subject_id']); //增加题组 $template['config_text'] = $this->addTopicGroup($template['config_text'], $template['subject_id']); } break; } } if(in_array($template['subject_id'], array(3,6,51 ,12))){ $tplJson=$this->filterTplData($tplJson); } if(isset($tplJson)){ $updateJson = json_encode(self::syncJson($tplJson,$template['config_text']),true); $con->createCommand("UPDATE `product_template` SET config_text='{$updateJson}' WHERE template_id={$template['template_id']}")->query(); echo sprintf("模板:%s, id: %s 模板同步完成", $template['template_name'], $template['template_id']).PHP_EOL; } } } /** * 补全tpl json字段 * @param $tpl * @param $newTpl * @return array */ private static function syncJson($tpl, $newTpl) { $_tpl = json_decode($tpl, true); $_newTpl = json_decode($newTpl, true); return array_replace_recursive($_tpl, $_newTpl); } /** * 过滤模板数据(只保留排版数据) */ private function filterTplData($tplJson){ $tplJson=json_decode($tplJson,true); unset($tplJson['studentLevelDivide']['isPushSameTrain']);//特性类型: false 个性(默认) true 共性 unset($tplJson['studentLevelDivide']['studentLevel']);//学生层次 unset($tplJson['studentLevelDivide']['rankBase']);//划分依据 unset($tplJson['studentLevelDivide']['statisticsMethods']);//统计方式 unset($tplJson['studentLevelDivide']['levelDetails']);//等级详情 $tplJson=json_encode($tplJson); return $tplJson; } /** * 等级详情增加数据 * @param $configText * @param int $subjectId * @return string */ private function addLevelDetails($configText,$subjectId=12){ $configTextArr=json_decode($configText,true); if($configTextArr && isset($configTextArr['studentLevelDivide']['levelDetails']) && is_array($configTextArr['studentLevelDivide']['levelDetails'])){ $levelDetails=$configTextArr['studentLevelDivide']['levelDetails']; foreach($levelDetails as &$item){ if(!isset($item['versionType'])){ $item['versionType']=1; } if(isset($item['trainPushDetail']) && is_array($item['trainPushDetail']) && $item['trainPushDetail']){ if($subjectId==12){ if(!isset($item['trainPushDetail']['basics'])) { $item['trainPushDetail']['basics'] = array( "knowledge" => 3, "knowledgePushTopic" => 2 ); } }else{ if(!isset($item['trainPushDetail']['basics'])) { $item['trainPushDetail']['basics'] = array( "knowledge" => 3, "consolidate" => 3 ); } } } } $configTextArr['studentLevelDivide']['levelDetails']=$levelDetails; } return !$configTextArr?$configText:json_encode($configTextArr); } /** * 处理物理某个排版数据按照现有数据来统一 * @param $configText * @param int $subjectId * @return mixed|string */ private function addTypography($configText,$subjectId=12){ $configTextArr=json_decode($configText,true); if($subjectId==12 || $subjectId==3){ if(isset($configTextArr['errorTypeSetting']['titleAnswerMerge']) && !isset($configTextArr['abilityTypeSetting']['titleAnswerMerge']) && !isset($configTextArr['basicTypeSetting']['titleAnswerMerge'])){ $configTextArr['abilityTypeSetting']['titleAnswerMerge']=$configTextArr['errorTypeSetting']['titleAnswerMerge']; $configTextArr['basicTypeSetting']['titleAnswerMerge']=$configTextArr['errorTypeSetting']['titleAnswerMerge']; } } return !$configTextArr?$configText:json_encode($configTextArr); } /** * 只有物理学习方案和学习宝的班级模板增加题组 * @param $configText * @param int $subjectId * @return string */ private function addTopicGroup($configText,$subjectId=12){ $configTextArr=json_decode($configText,true); if($subjectId==12 && $configTextArr){ if(isset($configTextArr['studentLevelDivide']) && $configTextArr['studentLevelDivide'] && !isset($configTextArr['studentLevelDivide']['pushTopicType'])){ $configTextArr['studentLevelDivide']['pushTopicType']=false; } if($configTextArr && isset($configTextArr['studentLevelDivide']['levelDetails']) && is_array($configTextArr['studentLevelDivide']['levelDetails'])){ $levelDetails=$configTextArr['studentLevelDivide']['levelDetails']; foreach($levelDetails as &$item){ if(isset($item['trainPushDetail']) && is_array($item['trainPushDetail']) && $item['trainPushDetail']){ if(!isset($item['trainPushDetail']['topicGroupPushTopic'])) { $item['trainPushDetail']['topicGroupPushTopic']=array( 'topicGroup'=> 1, 'pushDifficulty'=>array( 'select'=>array(0) ), ); } } } $configTextArr['studentLevelDivide']['levelDetails']=$levelDetails; } } return !$configTextArr?$configText:json_encode($configTextArr); } }