123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- <?php
- Yii::import('application.models.*');
- Yii::import('application.components.*');
- /**
- * 同步产品设置模板数据
- */
- class SyncProductTplCommand extends CConsoleCommand
- {
- public function init()
- {
- parent::init();
- @ini_set('memory_limit', '1024M');
- set_time_limit(0);
- }
- /***
- * 脚本可选参数 学校id:schoolId | 产品类型: productType | 是否抢先版:isBeta
- *
- *
- * 数学能力宝基础宝配置同步脚本/usr/local/php5.3/bin/php /home/www/vhosts/zxhx.testing/zsyas2/protected/shell/yiic.php syncproducttpl index subjectId=3 productType=3 isBeta=1 --YII_ENV=testing
- *
- * 物理能力宝基础宝配置同步脚本/usr/local/php5.3/bin/php /home/www/vhosts/zxhx.testing/zsyas2/protected/shell/yiic.php syncproducttpl index subjectId=12 productType=3 --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='));
- }
- 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);
- }
- }
|