ClientController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. class ClientController extends Controller
  3. {
  4. private $redis;
  5. public function __construct(){
  6. // $this->redis = new Redis();
  7. // $server = Yii::app()->params["redis"]["server"];
  8. // $this->redis->connect($server["host"], $server["port"]);
  9. // $this->redis->select(2);
  10. $this->delExpire();
  11. }
  12. /**
  13. * 重置错题本
  14. * @throws CDbException
  15. * @throws CException
  16. */
  17. public function actionWbhtml(){
  18. list($status,$msg) = $this->resetExam(1);
  19. echo $status;
  20. exit(0);
  21. }
  22. /**
  23. * 重置二步个性化学习宝
  24. * @throws CDbException
  25. * @throws CException
  26. */
  27. public function actionIsp2html(){
  28. list($status,$msg) = $this->resetExam(2);
  29. echo $status;
  30. exit(0);
  31. }
  32. /**
  33. * 重置三步个性化学习宝
  34. * @throws CDbException
  35. * @throws CException
  36. */
  37. public function actionIsp3html(){
  38. list($status,$msg) = $this->resetExam(3);
  39. echo $status;
  40. exit(0);
  41. }
  42. /************* 重置多个班级 *************/
  43. /**
  44. * 重置多个班级错题本
  45. * @throws CDbException
  46. * @throws CException
  47. */
  48. public function actionMulWbhtml(){
  49. $classExam = Yii::app()->request->getParam('classExam');
  50. foreach($classExam as $value){
  51. list($status,$msg) = $this->resetExam(1,$value);
  52. if (!$status) {
  53. break;
  54. }
  55. }
  56. echo $status;
  57. }
  58. /**
  59. * 重置多个班级二步个性化学习宝
  60. * @throws CDbException
  61. * @throws CException
  62. */
  63. public function actionMulIsp2html(){
  64. $classExam = Yii::app()->request->getParam('classExam');
  65. foreach($classExam as $value){
  66. list($status,$msg) = $this->resetExam(2,$value);
  67. if (!$status) {
  68. break;
  69. }
  70. }
  71. echo $status;
  72. }
  73. /**
  74. * 重置多个班级三步个性化学习宝
  75. * @throws CDbException
  76. * @throws CException
  77. */
  78. public function actionMulIsp3html(){
  79. $classExam = Yii::app()->request->getParam('classExam');
  80. foreach($classExam as $value){
  81. list($status,$msg) = $this->resetExam(3,$value);
  82. if (!$status) {
  83. break;
  84. }
  85. }
  86. echo $status;
  87. }
  88. /**
  89. * 新考试类型产品重置(初高中方法宝等)
  90. */
  91. public function actionMulExamProducthtml(){
  92. $productType = Req::get('productType');
  93. $classExam = Yii::app()->request->getParam('classExam');
  94. foreach($classExam as $value){
  95. list($status,$msg) = $this->resetExam($productType,$value,true);
  96. if (!$status) {
  97. break;
  98. }
  99. }
  100. echo $status;
  101. }
  102. /**
  103. * 重置产品方法
  104. * @param $proType 产品类型(对应ims商品类目)
  105. * @param array $examClassIdArr array(0=>classId,1=>examId)
  106. * @param boolean $isNewExamProduct
  107. * @return array
  108. * @throws CDbException
  109. * @throws CException
  110. */
  111. private function resetExam($proType,$examClassIdArr = array(), $isNewExamProduct=false)
  112. {
  113. $error = array();
  114. if ($examClassIdArr && count($examClassIdArr) == 2) {
  115. $classId = $examClassIdArr[0];
  116. $examId = $examClassIdArr[1];
  117. }else{
  118. $examId = (string)Yii::app()->request->getParam('examId');
  119. $classId = (string) Yii::app()->request->getParam('classId');
  120. }
  121. if(! $examId){
  122. $error[] = '请传入examId参数';
  123. }
  124. if(! $classId ){
  125. $error[] = '请传入classId参数';
  126. }
  127. // if(!in_array($proType,array(1,2,3))){
  128. // $error[] = '产品类型不正确';
  129. // }
  130. $feildArr = array();
  131. if (!$error) {
  132. //FIXME 新考试类型产品
  133. if($isNewExamProduct){
  134. $feildArr['exam_product_status'] = array(
  135. 'is_create_html' => 90,
  136. 'is_create_pdf' => 90,
  137. 'is_download' => 0,
  138. 'pdf_page_num' => 0,
  139. 'pdf_sheet_num' => 0,
  140. );
  141. $feildArr['exam'] = array(
  142. 'is_common_topic' => 90,
  143. );
  144. }else{
  145. $feildArr['student_paper_relation_property'] = array(
  146. 'is_radar_img' => 90,
  147. 'is_studytrend_img' => 90,
  148. 'is_enc_lang' => 90,
  149. 'is_studytrend' => 90,
  150. 'is_kps_analy' => 90,
  151. 'is_scoring_detail' => 90,
  152. 'is_tips' => 90,
  153. );
  154. if ($proType == 1) {
  155. $feildArr['student_paper_relation'] = array(
  156. 'is_wrongbook_html' => 90,
  157. 'is_wrongbook_pdf' => 0,
  158. 'is_studytrend_img' => 90,
  159. 'is_wrong_plan_created' => 0,
  160. 'wb_page_num' => 0,
  161. 'is_wrongbook_download' => 0,
  162. );
  163. $feildArr['student_paper_relation_property']['is_wb'] = 90;
  164. $feildArr['exam'] = array(
  165. 'wb_group_id' => 0,
  166. 'is_common_topic' => 90,
  167. );
  168. }elseif ($proType == 2) {
  169. $feildArr['student_paper_relation'] = array(
  170. 'is_isp_html' => 90,
  171. 'is_isp_pdf' => 0,
  172. 'is_plan_created' => 0,
  173. 'is_top_plan_created' => 0,
  174. 'is_studytrend_img' => 90,
  175. 'is_wrong_plan_created' => 0,
  176. 'isp_page_num' =>0,
  177. 'is_two_isp_download' => 0,
  178. );
  179. $feildArr['student_paper_relation_property']['is_isp'] = 90;
  180. $feildArr['exam'] = array(
  181. 'is_preparation_created' => 90,
  182. 'preparation_plan_id' => 0,
  183. 'isp_group_id' => 0,
  184. 'is_common_topic' => 90,
  185. );
  186. }elseif ($proType == 3) {
  187. $feildArr['student_paper_relation'] = array(
  188. 'is_wbisp_html' => 90,
  189. 'is_wbisp_pdf' => 0,
  190. 'is_plan_created' => 0,
  191. 'is_top_plan_created' => 0,
  192. 'is_studytrend_img' => 90,
  193. 'is_wrong_plan_created' => 0,
  194. 'wbisp_page_num' =>0,
  195. 'is_three_isp_download' => 0,
  196. );
  197. $feildArr['student_paper_relation_property']['is_wbisp'] = 90;
  198. $feildArr['exam'] = array(
  199. 'is_preparation_created' => 90,
  200. 'preparation_plan_id' => 0,
  201. 'wbisp_group_id' => 0,
  202. 'is_common_topic' => 90,
  203. );
  204. }
  205. }
  206. //查询重置日志
  207. $examGroup=$this->sConn->createCommand("select exam_group_id,subject_id from exam where exam_id='{$examId}'")->queryRow();
  208. $resetLog=$this->sConn->createCommand("select reset_count,learn_tube_reset_count from product_reset_log where exam_id='{$examId}' and product_type='".$proType."'")->queryRow();
  209. $logSql='';
  210. if(!$resetLog){
  211. $logSql="insert into product_reset_log(`exam_group_id`,`exam_id`,`class_id`,`subject_id`,`product_type`,`semester_id`,`learn_tube_reset_count`,`learn_tube_reset_time`)";
  212. if(!isset(Yii::app()->session['testFlag']) || Yii::app()->session['testFlag']!=1 ) {
  213. $logSql .= " values('" . $examGroup['exam_group_id'] . "','" . $examId . "','" . $classId . "','" . $this->subjectId . "','" . $proType . "','" . $this->semesterId . "',1," . time() . ")";
  214. }else{
  215. $logSql .= " values('" . $examGroup['exam_group_id'] . "','" . $examId . "','" . $classId . "','" . $this->subjectId . "','" . $proType . "','" . $this->semesterId . "',0," . 0 . ")";
  216. }
  217. }else{
  218. $count=intval($resetLog['reset_count'])+1;
  219. if(!isset(Yii::app()->session['testFlag']) || Yii::app()->session['testFlag']!=1 ){
  220. $larnTubeResetCount=intval($resetLog['learn_tube_reset_count'])+1;
  221. //学管最多重置3次
  222. //$larnTubeResetCount=$larnTubeResetCount>3?3:$larnTubeResetCount;
  223. $learnTubeResetTime=time();
  224. $logSql="update product_reset_log set reset_count=".$count." ,learn_tube_reset_count={$larnTubeResetCount},learn_tube_reset_time={$learnTubeResetTime} where exam_id='{$examId}' and product_type='".$proType."'";
  225. }else{
  226. $logSql="update product_reset_log set reset_count=".$count." where exam_id='{$examId}' and product_type='".$proType."'";
  227. }
  228. }
  229. $dbConn = SStudentPaperRelation::model()->getDbConnection();
  230. $transaction = $dbConn->beginTransaction();
  231. try
  232. {
  233. //重置后清除已存在的下载任务
  234. $task_sql = "delete from pack_product_task where unique_key ={$examId} and school_id ={$this->schoolId} and class_id={$classId} and product_type={$proType}";
  235. $this->conn->createCommand($task_sql)->execute();
  236. $sqlArr = $this->getUpdateSql($feildArr, $examId, $classId, $proType);
  237. foreach ($sqlArr as $tableName => $sql) {
  238. $bool = $dbConn->createCommand($sql)->execute();
  239. if ($bool === false) {
  240. throw new Exception("表{$tableName}更新失败");
  241. }
  242. }
  243. $bool = $this->saveResetExam($dbConn,$examId,$proType);
  244. if ($bool === false) {
  245. throw new Exception("表数据失败");
  246. }
  247. //更新产品重置日志表
  248. if($logSql){
  249. $this->sConn->createCommand($logSql)->execute();
  250. }
  251. $transaction->commit();
  252. }catch(Exception $ex){
  253. $transaction->rollback();
  254. $error[] = $ex->getMessage();
  255. }
  256. }
  257. if ($error) {
  258. return array(0, implode(PHP_EOL, $error));
  259. }else{
  260. //发送消息
  261. $sendMsg=array(
  262. 'school_id'=>$this->schoolId,
  263. 'msg_type'=>90,
  264. 'subject_id'=>$examGroup['subject_id'],
  265. 'send_type'=>'exam',
  266. 'product_type'=>array($proType),
  267. 'exam_group_id'=>$examGroup['exam_group_id'],
  268. 'exam_ids'=>array($examId)
  269. );
  270. sendDataToKafka('xbkc-php-product-html',$sendMsg);
  271. return array(1, '');
  272. }
  273. }
  274. /**
  275. * 生成sql语句
  276. * @param $feildArr
  277. * @param $examId
  278. * @param $classId
  279. * @param $productType
  280. * @return array
  281. */
  282. private function getUpdateSql($feildArr,$examId,$classId,$productType)
  283. {
  284. $sqlArr = array();
  285. $where = " where class_id={$classId} and exam_id={$examId} ";
  286. $whereExam = " where exam_id={$examId} ";
  287. foreach ($feildArr as $tableName => $_feilds) {
  288. $set_feild_arr = array();
  289. foreach ($_feilds as $k => $v) {
  290. $set_feild_arr[] = "{$k} = {$v}";
  291. }
  292. $set_feild_str = implode(',', $set_feild_arr);
  293. if($tableName == 'exam' || $tableName == 'exam_product_status'){
  294. $sql = "update {$tableName} set {$set_feild_str} {$whereExam}";
  295. $sqlArr[$tableName] = $tableName == 'exam_product_status' ? $sql. " and product_type={$productType}" : $sql;
  296. }else{
  297. $sqlArr[$tableName] = "update {$tableName} set {$set_feild_str} {$where}";
  298. }
  299. }
  300. return $sqlArr;
  301. }
  302. /**
  303. * 重置考试和学生保存到表里
  304. * @param $dbConn
  305. * @param $examId
  306. * @param $proType
  307. * @return bool
  308. */
  309. private function saveResetExam(&$dbConn,$examId,$proType){
  310. $sql = 'select student_id,is_feedback,is_complete,is_del from student_paper_relation where exam_id = :exam_id';
  311. $studentPaper = SStudentPaperRelation::model()->findAllBySql($sql,array(':exam_id'=>$examId));
  312. $exam_obj = new SExam();
  313. $subjectId = $exam_obj->getExamSubjectId($examId);
  314. unset($exam_obj);
  315. $resetData = array();
  316. if($studentPaper){
  317. foreach ($studentPaper as $v){
  318. if(isset(Yii::app()->session['session_duoxueke_subject_id']) && Yii::app()->session['session_duoxueke_subject_id'] == 8){
  319. if($v->is_feedback == 1 && $v->is_complete == 1){
  320. if(!isset($resetData[$examId])){
  321. $resetData[$examId] = array();
  322. }
  323. $resetData[$examId][] = array(
  324. 'exam_id'=>$examId,
  325. 'subject_id'=>$subjectId,
  326. 'student_id'=>$v->student_id,
  327. 'product_type'=>$proType,
  328. 'status'=>90,
  329. 'create_time'=>time(),
  330. );
  331. }
  332. }else{
  333. if(!($v->is_feedback == 0 && $v->is_complete == 0 && $v->is_del == 0)){
  334. if(!isset($resetData[$examId])){
  335. $resetData[$examId] = array();
  336. }
  337. $resetData[$examId][] = array(
  338. 'exam_id'=>$examId,
  339. 'subject_id'=>$subjectId,
  340. 'student_id'=>$v->student_id,
  341. 'product_type'=>$proType,
  342. 'status'=>90,
  343. 'create_time'=>time(),
  344. );
  345. }
  346. }
  347. }
  348. }
  349. unset($studentPaper);
  350. $bool = false;
  351. if($resetData){
  352. foreach ($resetData as $examId => $value){
  353. $sql = 'insert into can_generate_product (exam_id,subject_id,student_id,product_type,status,create_time) values ';
  354. foreach ($value as $k => $v){
  355. $sql .= ($k ? ',':'')."({$examId},{$v['subject_id']},{$v['student_id']},{$v['product_type']},{$v['status']},{$v['create_time']})";
  356. }
  357. $del_sql = 'delete from can_generate_product where exam_id = ' . $examId . ' and product_type = ' . $proType;
  358. $dbConn->createCommand($del_sql)->execute();
  359. $bool = $dbConn->createCommand($sql)->execute();
  360. }
  361. }
  362. return $bool;
  363. }
  364. /**
  365. * 删除过期重置记录
  366. */
  367. private function delExpire()
  368. {
  369. //删除七天前的
  370. $time = time() - 7 * 24 * 3600;
  371. $del_sql = 'delete from can_generate_product where create_time < ' . $time;
  372. $dbConn = SCanGenerateProduct::model()->getDbConnection();
  373. $dbConn->createCommand($del_sql)->execute();
  374. }
  375. }