SyncProductTplCommand.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. Yii::import('application.models.*');
  3. Yii::import('application.components.*');
  4. /**
  5. * 同步产品设置模板数据
  6. */
  7. class SyncProductTplCommand extends CConsoleCommand
  8. {
  9. public function init()
  10. {
  11. parent::init();
  12. @ini_set('memory_limit', '1024M');
  13. set_time_limit(0);
  14. }
  15. /***
  16. * 脚本可选参数 学校id:schoolId | 产品类型: productType | 是否抢先版:isBeta
  17. *
  18. *
  19. * 数学能力宝基础宝配置同步脚本/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
  20. *
  21. * 物理能力宝基础宝配置同步脚本/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
  22. *
  23. */
  24. public function actionIndex($YII_ENV = 'development')
  25. {
  26. echo 'start 产品设置同步脚本.....' . PHP_EOL;
  27. $schoolId=0;
  28. foreach ($_SERVER['argv'] as $k => $v) {
  29. if (strpos($v, 'schoolId=') !== FALSE) {
  30. $schoolId = substr($v, strlen('schoolId='));
  31. }
  32. if (strpos($v, 'subjectId=') !== FALSE) {
  33. $subjectId = substr($v, strlen('subjectId='));
  34. }
  35. if (strpos($v, 'productType=') !== FALSE) {
  36. $productType = substr($v, strlen('productType='));
  37. }
  38. if (strpos($v, 'isBeta=') !== FALSE) {
  39. $isBeta = substr($v, strlen('isBeta='));
  40. }
  41. if (strpos($v, 'targetType=') !== FALSE) {
  42. $targetType = substr($v, strlen('targetType='));
  43. }
  44. }
  45. $databases=$this->getDatabases($schoolId);
  46. foreach ($databases as $db) {
  47. echo 'start database_name:' . $db['database_name'] . '...' . PHP_EOL;
  48. $con = $this->getDbCon($db);
  49. if (!$con) {
  50. echo 'database cannot connect' . PHP_EOL;
  51. continue;
  52. }
  53. try {
  54. /** 无学科参数默认高中数学 **/
  55. if(isset($subjectId)){
  56. $params['subjectId'] = in_array($subjectId, array(3,6,51))? array(3,6,51) : array($subjectId);
  57. }else{
  58. $params['subjectId'] = array(3,6,51);
  59. }
  60. if(isset($productType)) $params['productType'] = $productType;
  61. $params['isBeta'] = isset($isBeta) ? $isBeta : 0;
  62. $params['targetType']=isset($targetType)?$targetType:0;
  63. self::syncTpl($con, $params);
  64. echo 'database_name:'.$db['database_name'] . 'done' . PHP_EOL;
  65. } catch (\Exception $e) {
  66. echo $e->getMessage() . PHP_EOL;
  67. continue;
  68. }
  69. sleep(0.1);
  70. }
  71. echo 'end 处理完成' . PHP_EOL;
  72. exit;
  73. }
  74. /**
  75. * 库连接
  76. * @param $db
  77. * @return bool|CDbConnection
  78. */
  79. public function getDbCon($db)
  80. {
  81. if (empty($db)) {
  82. return false;
  83. }
  84. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  85. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  86. $my_connection->emulatePrepare = true;
  87. $my_connection->enableProfiling = true;
  88. $my_connection->enableParamLogging = true;
  89. $myDbDsn = null;
  90. return $my_connection;
  91. }
  92. /**
  93. * 获取学校数据库
  94. * @return mixed
  95. */
  96. private function getDatabases($schoolId)
  97. {
  98. if($schoolId) {
  99. $dbs = BusinessDatabase::model()->findAll('school_id=:sid', array(':sid' => $schoolId));
  100. }else{
  101. $dbs= BusinessDatabase::model()->findAll();
  102. }
  103. return $dbs;
  104. }
  105. /**
  106. * 获取所有正常可用的学校
  107. * @return mixed
  108. */
  109. private function getSchools()
  110. {
  111. $db = Yii::app()->businessDb;
  112. $sql = "SELECT school_id,school_name FROM `school` WHERE `status`=0 ";
  113. $schools = $db->createCommand($sql)->queryAll();
  114. $db->close();
  115. return $schools;
  116. }
  117. /**
  118. * 学校库连接
  119. * @param $schoolId
  120. * @return bool|CDbConnection
  121. */
  122. public function getSchoolDbCon($schoolId)
  123. {
  124. $db = BusinessDatabase::model()->find('school_id=:sid', array(':sid' => $schoolId));
  125. if (empty($db)) {
  126. return false;
  127. }
  128. $myDbDsn = 'mysql:host=' . $db->database_host . ';dbname=' . $db->database_name;
  129. $my_connection = new CDbConnection($myDbDsn, $db->database_user, $db->database_password);
  130. $my_connection->emulatePrepare = true;
  131. $my_connection->enableProfiling = true;
  132. $my_connection->enableParamLogging = true;
  133. $myDbDsn = null;
  134. return $my_connection;
  135. }
  136. /**
  137. * 同步模板数据
  138. * @param $con object 数据库连接
  139. * @param $params
  140. */
  141. private function syncTpl($con, $params)
  142. {
  143. $subjectIds = implode(',', $params['subjectId']);
  144. $sql = "select template_id,template_name,product_type,subject_id,target_type,is_beta,config_text from product_template where subject_id in ({$subjectIds}) ";
  145. if(isset($params['productType'])){
  146. $sql .= " and product_type = {$params['productType']}";
  147. }
  148. if(isset($params['isBeta'])){
  149. $sql .= " and is_beta = {$params['isBeta']}";
  150. }
  151. if(isset($params['targetType']) && $params['targetType']){
  152. $sql .= " and target_type = {$params['targetType']}";
  153. }
  154. $templates = $con->createCommand($sql)->queryAll();
  155. foreach ($templates as $template)
  156. {
  157. //FIXME 目前就数学和物理产品使用product_template表
  158. if(in_array($template['subject_id'], array(3,6,51))){
  159. switch ($template['product_type']){
  160. case ProductDownload::PRODUCT_TYPE_WB:
  161. $tplJson = $template['target_type'] == 1 ? ($template['is_beta'] ? ProductMathTpl::MATH_CLASS_WB_BETA : ProductMathTpl::MATH_CLASS_WB)
  162. : ($template['is_beta'] ? ProductMathTpl::MATH_STU_WB_BETA : ProductMathTpl::MATH_STU_WB);
  163. break;
  164. case ProductDownload::PRODUCT_TYPE_ISP:
  165. $tplJson = $template['target_type'] == 1 ? ($template['is_beta'] ? ProductMathTpl::MATH_CLASS_ISP_BETA : ProductMathTpl::MATH_CLASS_ISP)
  166. : ($template['is_beta'] ? ProductMathTpl::MATH_STU_ISP_BETA : ProductMathTpl::MATH_STU_ISP);
  167. break;
  168. case ProductDownload::PRODUCT_TYPE_WB_ISP:
  169. $tplJson = $template['target_type'] == 1 ? ($template['is_beta'] ? ProductMathTpl::MATH_CLASS_WB_ISP_BETA : ProductMathTpl::MATH_CLASS_WB_ISP)
  170. : ($template['is_beta'] ? ProductMathTpl::MATH_STU_WB_ISP_BETA : ProductMathTpl::MATH_STU_WB_ISP);
  171. if($template['is_beta']){
  172. $template['config_text']=$this->addLevelDetails($template['config_text'],$template['subject_id']);
  173. //3.4班级模板
  174. if($template['target_type']==1) {
  175. $template['config_text'] = $this->addTypography($template['config_text'], $template['subject_id']);
  176. }
  177. }
  178. break;
  179. case ProductDownload::PRODUCT_TYPE_METHOD:
  180. $tplJson = $template['target_type'] == 1 ? ProductMethodTpl::Method_CLASS_WB : ProductMethodTpl::Method_STU_WB;
  181. break;
  182. }
  183. }
  184. if($template['subject_id'] == 12){
  185. switch ($template['product_type']){
  186. case ProductDownload::PRODUCT_TYPE_WB:
  187. $tplJson = $template['target_type'] == 1 ? ProductPhysicsTpl::PHYSICS_CLASS_WB : ProductPhysicsTpl::PHYSICS_STU_WB;
  188. break;
  189. case ProductDownload::PRODUCT_TYPE_ISP:
  190. $tplJson = $template['target_type'] == 1 ? ProductPhysicsTpl::PHYSICS_CLASS_ISP : ProductPhysicsTpl::PHYSICS_STU_ISP;
  191. if($template['target_type']==1) {
  192. //增加题组
  193. $template['config_text'] = $this->addTopicGroup($template['config_text'], $template['subject_id']);
  194. }
  195. break;
  196. case ProductDownload::PRODUCT_TYPE_WB_ISP:
  197. $tplJson = $template['target_type'] == 1 ? ProductPhysicsTpl::PHYSICS_CLASS_WB_ISP : ProductPhysicsTpl::PHYSICS_STU_WB_ISP;
  198. $template['config_text']=$this->addLevelDetails($template['config_text'],$template['subject_id']);
  199. if($template['target_type']==1) {
  200. $template['config_text'] = $this->addTypography($template['config_text'], $template['subject_id']);
  201. //增加题组
  202. $template['config_text'] = $this->addTopicGroup($template['config_text'], $template['subject_id']);
  203. }
  204. break;
  205. }
  206. }
  207. if(in_array($template['subject_id'], array(3,6,51 ,12))){
  208. $tplJson=$this->filterTplData($tplJson);
  209. }
  210. if(isset($tplJson)){
  211. $updateJson = json_encode(self::syncJson($tplJson,$template['config_text']),true);
  212. $con->createCommand("UPDATE `product_template` SET config_text='{$updateJson}' WHERE template_id={$template['template_id']}")->query();
  213. echo sprintf("模板:%s, id: %s 模板同步完成", $template['template_name'], $template['template_id']).PHP_EOL;
  214. }
  215. }
  216. }
  217. /**
  218. * 补全tpl json字段
  219. * @param $tpl
  220. * @param $newTpl
  221. * @return array
  222. */
  223. private static function syncJson($tpl, $newTpl)
  224. {
  225. $_tpl = json_decode($tpl, true);
  226. $_newTpl = json_decode($newTpl, true);
  227. return array_replace_recursive($_tpl, $_newTpl);
  228. }
  229. /**
  230. * 过滤模板数据(只保留排版数据)
  231. */
  232. private function filterTplData($tplJson){
  233. $tplJson=json_decode($tplJson,true);
  234. unset($tplJson['studentLevelDivide']['isPushSameTrain']);//特性类型: false 个性(默认) true 共性
  235. unset($tplJson['studentLevelDivide']['studentLevel']);//学生层次
  236. unset($tplJson['studentLevelDivide']['rankBase']);//划分依据
  237. unset($tplJson['studentLevelDivide']['statisticsMethods']);//统计方式
  238. unset($tplJson['studentLevelDivide']['levelDetails']);//等级详情
  239. $tplJson=json_encode($tplJson);
  240. return $tplJson;
  241. }
  242. /**
  243. * 等级详情增加数据
  244. * @param $configText
  245. * @param int $subjectId
  246. * @return string
  247. */
  248. private function addLevelDetails($configText,$subjectId=12){
  249. $configTextArr=json_decode($configText,true);
  250. if($configTextArr && isset($configTextArr['studentLevelDivide']['levelDetails']) && is_array($configTextArr['studentLevelDivide']['levelDetails'])){
  251. $levelDetails=$configTextArr['studentLevelDivide']['levelDetails'];
  252. foreach($levelDetails as &$item){
  253. if(!isset($item['versionType'])){
  254. $item['versionType']=1;
  255. }
  256. if(isset($item['trainPushDetail']) && is_array($item['trainPushDetail']) && $item['trainPushDetail']){
  257. if($subjectId==12){
  258. if(!isset($item['trainPushDetail']['basics'])) {
  259. $item['trainPushDetail']['basics'] = array(
  260. "knowledge" => 3,
  261. "knowledgePushTopic" => 2
  262. );
  263. }
  264. }else{
  265. if(!isset($item['trainPushDetail']['basics'])) {
  266. $item['trainPushDetail']['basics'] = array(
  267. "knowledge" => 3,
  268. "consolidate" => 3
  269. );
  270. }
  271. }
  272. }
  273. }
  274. $configTextArr['studentLevelDivide']['levelDetails']=$levelDetails;
  275. }
  276. return !$configTextArr?$configText:json_encode($configTextArr);
  277. }
  278. /**
  279. * 处理物理某个排版数据按照现有数据来统一
  280. * @param $configText
  281. * @param int $subjectId
  282. * @return mixed|string
  283. */
  284. private function addTypography($configText,$subjectId=12){
  285. $configTextArr=json_decode($configText,true);
  286. if($subjectId==12 || $subjectId==3){
  287. if(isset($configTextArr['errorTypeSetting']['titleAnswerMerge']) && !isset($configTextArr['abilityTypeSetting']['titleAnswerMerge']) && !isset($configTextArr['basicTypeSetting']['titleAnswerMerge'])){
  288. $configTextArr['abilityTypeSetting']['titleAnswerMerge']=$configTextArr['errorTypeSetting']['titleAnswerMerge'];
  289. $configTextArr['basicTypeSetting']['titleAnswerMerge']=$configTextArr['errorTypeSetting']['titleAnswerMerge'];
  290. }
  291. }
  292. return !$configTextArr?$configText:json_encode($configTextArr);
  293. }
  294. /**
  295. * 只有物理学习方案和学习宝的班级模板增加题组
  296. * @param $configText
  297. * @param int $subjectId
  298. * @return string
  299. */
  300. private function addTopicGroup($configText,$subjectId=12){
  301. $configTextArr=json_decode($configText,true);
  302. if($subjectId==12 && $configTextArr){
  303. if(isset($configTextArr['studentLevelDivide']) && $configTextArr['studentLevelDivide'] && !isset($configTextArr['studentLevelDivide']['pushTopicType'])){
  304. $configTextArr['studentLevelDivide']['pushTopicType']=false;
  305. }
  306. if($configTextArr && isset($configTextArr['studentLevelDivide']['levelDetails']) && is_array($configTextArr['studentLevelDivide']['levelDetails'])){
  307. $levelDetails=$configTextArr['studentLevelDivide']['levelDetails'];
  308. foreach($levelDetails as &$item){
  309. if(isset($item['trainPushDetail']) && is_array($item['trainPushDetail']) && $item['trainPushDetail']){
  310. if(!isset($item['trainPushDetail']['topicGroupPushTopic'])) {
  311. $item['trainPushDetail']['topicGroupPushTopic']=array(
  312. 'topicGroup'=> 1,
  313. 'pushDifficulty'=>array(
  314. 'select'=>array(0)
  315. ),
  316. );
  317. }
  318. }
  319. }
  320. $configTextArr['studentLevelDivide']['levelDetails']=$levelDetails;
  321. }
  322. }
  323. return !$configTextArr?$configText:json_encode($configTextArr);
  324. }
  325. }