ProductMethodController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * 产品设置 方法宝.
  4. * User: li
  5. * Date: 2021/10/20
  6. * Time: 11:05
  7. */
  8. class ProductMethodController extends Product {
  9. public $layout = false;
  10. /**
  11. * 首页
  12. */
  13. public function actionIndex()
  14. {
  15. exit('1');
  16. }
  17. /*方法宝模板中心*/
  18. public function actionAjaxGetTemplate(){
  19. $data=array();
  20. $subject_id = isset(Yii::app()->session['session_duoxueke_subject_id']) ? Yii::app()->session['session_duoxueke_subject_id']:3;
  21. $productTemplate=$this->sConn->createCommand("select template_id,template_name,product_type,target_type,is_default from product_template where subject_id='{$subject_id}' and product_type=31")->queryAll();
  22. if($productTemplate){
  23. foreach ($productTemplate as $val){
  24. $data['list'][$val['product_type']]=$val;
  25. }
  26. }else{
  27. $this->initTemplate3();
  28. $productTemplate=$this->sConn->createCommand("select template_id,template_name,product_type,target_type,is_default from product_template where subject_id='{$subject_id}' and product_type=31")->queryAll();
  29. foreach ($productTemplate as $val){
  30. $data['list'][$val['product_type']]=$val;
  31. }
  32. }
  33. $result['status'] = 1;
  34. $result['data']=$data['list'];
  35. exit(json_encode($result));
  36. }
  37. public function actionAjaxGetTemplateById(){
  38. $id = intval(Req::post('id'));
  39. $result['status'] = 0;
  40. if(!$id){
  41. $result['msg']='模板ID不正确';
  42. exit(json_encode($result));
  43. }
  44. $productTemplate=$this->sConn->createCommand("select template_id,template_name,product_type,target_type,config_text,subject_id,is_default,create_time from product_template where template_id='{$id}'")->queryRow();
  45. if(!$productTemplate){
  46. $result['msg']='模板id不正确';
  47. exit(json_encode($result));
  48. }else{
  49. $result['status'] = 1;
  50. $result['data'] = $productTemplate;
  51. exit(json_encode($result));
  52. }
  53. }
  54. /*方法宝保存模板设置*/
  55. public function actionSaveTemplate(){
  56. $id = intval(Req::post('id'));
  57. $config=Req::post('config');
  58. $result['status'] = 0;
  59. $subject_id = 3;
  60. if(!$id || !$config){
  61. $result['msg']='模板参数不正确';
  62. exit(json_encode($result));
  63. }
  64. //
  65. $config=str_replace('&quot;','"',$config);
  66. $configArr=json_decode($config,true);
  67. if(!$configArr){
  68. $result['msg']='设置数据格式不正确';
  69. exit(json_encode($result));
  70. }
  71. $productTemplate=$this->sConn->createCommand("select template_id,product_type,target_type,subject_id from product_template where template_id='{$id}'")->queryRow();
  72. if(!$productTemplate){
  73. $result['msg']='模板id不正确';
  74. exit(json_encode($result));
  75. }else{
  76. $subject_id = $productTemplate["subject_id"];
  77. }
  78. if(!$this->checkoutJson($configArr,$productTemplate['product_type'],$productTemplate['target_type'],$subject_id)){
  79. $result['msg']='设置数据格式不正确';
  80. exit(json_encode($result));
  81. }
  82. if($productTemplate['target_type']==1){
  83. //班级模板
  84. if(isset($configArr['studentLevelDivide']['levelDetails'])){
  85. foreach ($configArr['studentLevelDivide']['levelDetails'] as $val){
  86. if(isset($val['trainPushDetail']) && !$val['trainPushDetail']['pushDifficulty']){
  87. $result['msg']='推送配置难度不能为空';
  88. exit(json_encode($result));
  89. }
  90. }
  91. }else{
  92. $result['msg']='设置数据格式不正确';
  93. exit(json_encode($result));
  94. }
  95. }
  96. $criteria = new CDbCriteria();
  97. $criteria->addCondition("template_id={$id}");
  98. SProductTemplate::model()->updateAll(array('config_text'=>$config),$criteria);
  99. $result['status'] = 1;
  100. $result['msg']='操作成功';
  101. exit(json_encode($result));
  102. }
  103. //方法宝保存班级定制
  104. public function actionSaveClassSet(){
  105. $classIds=Req::post('classes'); //班级id组 包含年级
  106. $template_id=Req::post('template_id');
  107. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  108. $session_subject_id = isset(Yii::app()->session['session_duoxueke_subject_id'])?Yii::app()->session['session_duoxueke_subject_id']:3;
  109. $subject_id = $post_subject_id?$post_subject_id:$session_subject_id;
  110. $result['status'] = 0;
  111. if(!$classIds){
  112. $result['msg']='请选择班级';
  113. exit(json_encode($result));
  114. }
  115. if(!$template_id){
  116. $result['msg']='请选择模板';
  117. exit(json_encode($result));
  118. }
  119. $templateInfo=$this->sConn->createCommand("select product_type,target_type from product_template where template_id='{$template_id}' ")->queryRow();
  120. if(!$templateInfo){
  121. $result['msg']='模板id不正确';
  122. exit(json_encode($result));
  123. }
  124. $updateSql=array();
  125. $insertSql=array();
  126. foreach ($classIds as $val){
  127. if(!isset($val['class_id']) || !$val['class_id']){
  128. $result['msg']='班级数据错误';
  129. exit(json_encode($result));
  130. }
  131. $classInfo=$this->sConn->createCommand("select class_id,grade from class where class_id='{$val['class_id']}'")->queryRow();
  132. if(!$classInfo){
  133. $result['msg']='选择的班级不存在';
  134. exit(json_encode($result));
  135. }
  136. $classSet=$this->sConn->createCommand("select class_id from product_class_set where class_id='{$val['class_id']}' and subject_id='{$subject_id}' and product_type=31 ")->queryRow();
  137. if($classSet){
  138. $updateSql[]="update product_class_set set `template_id`='".$template_id."',semester_id='".$this->semesterId."' where class_id='".$val['class_id']."' and subject_id='{$subject_id}' and product_type=31";
  139. }else{
  140. $insertSql[]="insert into product_class_set(`class_id`,`semester_id`,`grade`,`template_id`,`subject_id`,`product_type`) values('".$val['class_id']."','".$this->semesterId."','".$classInfo['grade']."','".$template_id."','".$subject_id."','31')";
  141. }
  142. }
  143. $trans = $this->sConn->beginTransaction();
  144. try{
  145. if($updateSql){
  146. foreach ($updateSql as $sql){
  147. $this->sConn->createCommand($sql)->execute();
  148. }
  149. }
  150. if($insertSql){
  151. foreach ($insertSql as $sql){
  152. $this->sConn->createCommand($sql)->execute();
  153. }
  154. }
  155. $trans->commit();
  156. $result['status'] = 1;
  157. $result['msg'] = "更新成功";
  158. }catch (Exception $e){
  159. $trans->rollBack();
  160. $result['status'] = 0;
  161. $result['msg'] = "更新失败";
  162. }
  163. exit(json_encode($result));
  164. }
  165. //班级定制列表
  166. public function actionProductClassSet(){
  167. $page=intval(Req::post('page'));
  168. $class_id=Req::post('class_id');
  169. $grade_id=Req::post('grade_id');
  170. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  171. $session_subject_id = isset(Yii::app()->session['session_duoxueke_subject_id'])?Yii::app()->session['session_duoxueke_subject_id']:3;
  172. $subject_id = $post_subject_id?$post_subject_id:$session_subject_id;
  173. if(!$page) $page=1;
  174. $pageLimit =10;
  175. $offset=($page-1)*$pageLimit;
  176. $semesterId=$this->semesterId;
  177. $pages=array();
  178. //wrong_book 或wb ,isp ,wb_isp
  179. if($class_id){
  180. $data=$this->sConn->createCommand("select class_id,grade,template_id from product_class_set where class_id='{$class_id}' and subject_id='{$subject_id}' and product_type=31")->queryAll();
  181. }else{
  182. if($grade_id){
  183. $count=$this->sConn->createCommand("select count(*) as count from product_class_set where semester_id='{$semesterId}' and grade='{$grade_id}' and subject_id='{$subject_id}' and product_type=31")->queryRow();
  184. $total=$count['count'];
  185. $totalPage=ceil($total/$pageLimit);
  186. $data=$this->sConn->createCommand("select class_id,grade,template_id from product_class_set where semester_id='{$semesterId}' and grade='{$grade_id}' and subject_id='{$subject_id}' and product_type=31 limit {$offset},{$pageLimit}")->queryAll();
  187. }else{
  188. $count=$this->sConn->createCommand("select count(*) as count from product_class_set where semester_id='{$semesterId}' and subject_id='{$subject_id}' and product_type=31")->queryRow();
  189. $total=$count['count'];
  190. $totalPage=ceil($total/$pageLimit);
  191. $data=$this->sConn->createCommand("select class_id,grade,template_id from product_class_set where semester_id='{$semesterId}' and subject_id='{$subject_id}' and product_type=31 limit {$offset},{$pageLimit}")->queryAll();
  192. }
  193. $pages=array(
  194. 'totalPage'=>$totalPage,
  195. 'total'=>$total,
  196. 'nowPage'=>$page
  197. );
  198. }
  199. if($data){
  200. //读取默认模板
  201. $defaultTemplate=$this->sConn->createCommand("select template_name,product_type,target_type from product_template where is_default=1 and target_type=1 and subject_id='{$subject_id}' and product_type=31")->queryRow();
  202. foreach ($data as $key => $val){
  203. $classInfo=$this->sConn->createCommand("select class_name,level from class where class_id='".$val['class_id']."'")->queryRow();
  204. $data[$key]['class_name']=$classInfo['class_name'];
  205. $data[$key]['level']=$classInfo['level'];
  206. //读取模板名
  207. if($val['template_id']){
  208. $Template=$this->sConn->createCommand("select template_name from product_template where template_id='{$val['template_id']}'")->queryRow();
  209. $data[$key]['template_name']=$Template['template_name'];
  210. }else{
  211. $data[$key]['template_name']=$defaultTemplate['template_name'];
  212. }
  213. }
  214. }
  215. $result['list']=$data;
  216. $result['pages']=$pages;
  217. exit(json_encode(array('status'=>1,'data'=>$result)));
  218. }
  219. //班级恢复默认
  220. public function actionResetClassSet(){
  221. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  222. $subject_id = isset(Yii::app()->session['session_duoxueke_subject_id']) ? Yii::app()->session['session_duoxueke_subject_id']:3;
  223. $subject_id = $post_subject_id?$post_subject_id:$subject_id;
  224. $sql = "select class_id from product_class_set where subject_id = '{$subject_id}' and product_type=31";
  225. $class_set = $this->sConn->createCommand($sql)->queryRow();
  226. if(!$class_set){
  227. echo json_encode(array('status'=>1,'msg'=>'操作成功'));exit;
  228. }
  229. if($this->sConn->createCommand("update product_class_set set template_id=0 where subject_id = '{$subject_id}' and product_type=31")->execute()){
  230. echo json_encode(array('status'=>1,'msg'=>'操作成功'));exit;
  231. }else{
  232. echo json_encode(array('status'=>0,'msg'=>'操作失败'));exit;
  233. }
  234. }
  235. //学生定制列表
  236. public function actionProductStudentSet(){
  237. $page=intval(Req::post('page'));
  238. $class_id=Req::post('class_id');
  239. $grade_id=Req::post('grade_id');
  240. $student_name=Req::post('student_name');
  241. if(!$page) $page=1;
  242. $pageLimit =10;
  243. $offset=($page-1)*$pageLimit;
  244. $semesterId=$this->semesterId;
  245. $subject_id=$this->subjectId;
  246. $where=array();
  247. $where[]=" semester_id='{$semesterId}'";
  248. $where[]=" product_type=31";
  249. $where[]=" subject_id='{$subject_id}'";
  250. if($class_id){
  251. $where[]=" class_id='{$class_id}'";
  252. }
  253. if($grade_id){
  254. $where[]=" grade='{$grade_id}'";
  255. }
  256. if($student_name){
  257. $where[]=" student_name like '%".$student_name."%'";
  258. }
  259. if($subject_id){
  260. $where[]=" subject_id='{$subject_id}'";
  261. }
  262. $count=$this->sConn->createCommand("select count(*) as count from product_student_set where ".implode(' and ',$where))->queryRow();
  263. $total=$count['count'];
  264. $totalPage=ceil($total/$pageLimit);
  265. $data=$this->sConn->createCommand("select student_id,class_id,grade,wrong_book,isp,wb_isp,student_name,template_id from product_student_set where ".implode(' and ',$where)." limit {$offset},{$pageLimit}")->queryAll();
  266. $pages=array(
  267. 'totalPage'=>$totalPage,
  268. 'total'=>$total,
  269. 'nowPage'=>$page
  270. );
  271. if($data){
  272. //取出所有学生,再查询行政班级名字
  273. $student_arr = array();
  274. foreach ($data as $value) {
  275. $student_arr[$value['student_id']] = $value['student_id'];
  276. }
  277. $stu_rel_class = $this->getStudentXzClassName(array_keys($student_arr));
  278. //读取默认模板
  279. $defaultTemplate=$this->sConn->createCommand("select template_name,product_type,target_type from product_template where is_default=1 and target_type=1 and subject_id='{$subject_id}' and product_type=31")->queryRow();
  280. foreach ($data as $key => $val){
  281. $classInfo=$this->sConn->createCommand("select class_name,level from class where class_id='".$val['class_id']."'")->queryRow();
  282. if (isset($stu_rel_class[$val['student_id']])) {
  283. $data[$key]['class_name']=$stu_rel_class[$val['student_id']];
  284. }else{
  285. $data[$key]['class_name']=$classInfo['class_name'];
  286. }
  287. $data[$key]['level']=$classInfo['level'];
  288. //读取系统准考证号
  289. $student=$this->conn->createCommand("select student_card from student where student_id='".$val['student_id']."'")->queryRow();
  290. $data[$key]['student_card']=$student['student_card'];
  291. $classTemplateData=$this->sConn->createCommand("select template_id from product_class_set where class_id='".$val['class_id']."' and subject_id = '{$subject_id}' and product_type=31")->queryRow();
  292. //读取模板名
  293. if($val['template_id']){
  294. $Template=$this->sConn->createCommand("select template_name from product_template where template_id='{$val['template_id']}' ")->queryRow();
  295. $data[$key]['template_name']=$Template['template_name'];
  296. }else{
  297. if($classTemplateData['template_id']){
  298. $Template=$this->sConn->createCommand("select template_name from product_template where template_id='{$classTemplateData['template_id']}' ")->queryRow();
  299. $data[$key]['template_name']=$Template['template_name'];
  300. }else{
  301. $data[$key]['template_name']=$defaultTemplate['template_name'];
  302. }
  303. }
  304. }
  305. }
  306. $result['list']=$data;
  307. $result['pages']=$pages;
  308. exit(json_encode(array('status'=>1,'data'=>$result)));
  309. }
  310. //保存学生定制
  311. public function actionSaveStudentSet(){
  312. $studentIds=Req::post('students');
  313. $template_id=Req::post('template_id');
  314. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  315. $session_subject_id = isset(Yii::app()->session['session_duoxueke_subject_id'])?Yii::app()->session['session_duoxueke_subject_id']:3;
  316. $subject_id = $post_subject_id?$post_subject_id:$session_subject_id;
  317. $result['status'] = 0;
  318. if(!$studentIds){
  319. $result['msg']='请选择学生';
  320. exit(json_encode($result));
  321. }
  322. if(!$template_id){
  323. $result['msg']='请选择模板';
  324. exit(json_encode($result));
  325. }
  326. $templateInfo=$this->sConn->createCommand("select product_type,target_type from product_template where template_id='{$template_id}' ")->queryRow();
  327. if($templateInfo['product_type']!=31){
  328. $result['msg']='模板类型不正确';
  329. exit(json_encode($result));
  330. }
  331. $updateSql=array();
  332. $insertSql=array();
  333. foreach ($studentIds as $val){
  334. if(!$val['student_id'] || !$val['realname'] || !$val['class_id'] || !$val['grade_id']){
  335. $result['msg']='学生数据错误';
  336. exit(json_encode($result));
  337. }
  338. $classInfo=$this->sConn->createCommand("select class_id,grade from class where class_id='{$val['class_id']}'")->queryRow();
  339. if(!$classInfo){
  340. $result['msg']='选择的班级不存在';
  341. exit(json_encode($result));
  342. }
  343. $studentSet=$this->sConn->createCommand("select student_id,student_name,use_custom,semester_id from product_student_set where student_id='{$val['student_id']}' and subject_id='{$subject_id}' and product_type=31")->queryRow();
  344. if($studentSet){
  345. $updateSql[]="update product_student_set set `template_id`='".$template_id."',semester_id='".$this->semesterId."' where student_id='".$val['student_id']."' and subject_id='".$subject_id."' and product_type=31";
  346. }else{
  347. $insertSql[]="insert into product_student_set(`student_id`,`student_name`,`semester_id`,`class_id`,`grade`,`template_id`,`subject_id`,`product_type`) values('".$val['student_id']."','".$val['realname']."','".$this->semesterId."','".$val['class_id']."','".$classInfo['grade']."','".$template_id."','".$subject_id."','31')";
  348. }
  349. }
  350. $trans = $this->sConn->beginTransaction();
  351. try{
  352. if($updateSql){
  353. foreach ($updateSql as $sql){
  354. $this->sConn->createCommand($sql)->execute();
  355. }
  356. }
  357. if($insertSql){
  358. foreach ($insertSql as $sql){
  359. $this->sConn->createCommand($sql)->execute();
  360. }
  361. }
  362. $trans->commit();
  363. $result['status'] = 1;
  364. $result['msg']='更新完成';
  365. }catch (Exception $e){
  366. $trans->rollBack();
  367. $result['status'] = 0;
  368. $result['msg'] = "更新失败";
  369. }
  370. exit(json_encode($result));
  371. }
  372. //学生恢复默认
  373. public function actionResetStudentSet(){
  374. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  375. $subject_id = isset(Yii::app()->session['session_duoxueke_subject_id']) ? Yii::app()->session['session_duoxueke_subject_id']:3;
  376. $subject_id = $post_subject_id?$post_subject_id:$subject_id;
  377. $sql = "select student_id from product_student_set where subject_id = '{$subject_id}' and product_type=31";
  378. $student_set = $this->sConn->createCommand($sql)->queryRow();
  379. if(!$student_set){
  380. echo json_encode(array('status'=>1,'msg'=>'操作成功'));exit;
  381. }
  382. if($this->sConn->createCommand("update product_student_set set template_id=0 where subject_id='{$subject_id}' and product_type=31")->execute()){
  383. echo json_encode(array('status'=>1,'msg'=>'操作成功'));exit;
  384. }else{
  385. echo json_encode(array('status'=>0,'msg'=>'操作失败'));exit;
  386. }
  387. }
  388. //复用模板设置
  389. public function actionCopyTemplate(){
  390. $id = intval(Req::post('id'));
  391. $result['status'] = 0;
  392. if(!$id){
  393. $result['msg']='模板ID不能为空';
  394. exit(json_encode($result));
  395. }
  396. $productTemplate=$this->sConn->createCommand("select template_name,product_type,target_type,config_text,subject_id from product_template where template_id='{$id}'")->queryRow();
  397. if(!$productTemplate){
  398. $result['msg']='请选择复用的模板';
  399. exit(json_encode($result));
  400. }
  401. //查询模板名称
  402. $templateName=$productTemplate['template_name'].'副本';
  403. $repeat=$this->sConn->createCommand("select template_name from product_template where template_name like '".$templateName."%' and product_type='".$productTemplate['product_type']."' and target_type='".$productTemplate['target_type']."' and subject_id='".$productTemplate['subject_id']."'")->queryAll();
  404. if($repeat){
  405. $preg='/\(\d+\)/is';
  406. $cloneCount=0;
  407. foreach ($repeat as $value){
  408. $str=str_replace($templateName,'',$value['template_name']);
  409. preg_match_all($preg,$str,$match);
  410. if(isset($match[0]) && count($match[0])==1){
  411. $cloneCount++;
  412. }
  413. }
  414. $templateName.='('.($cloneCount+1).')';
  415. }else{
  416. $templateName.='(1)';
  417. }
  418. $ps = new SProductTemplate();
  419. $ps->template_name = $templateName;
  420. $ps->product_type = $productTemplate['product_type'];
  421. $ps->target_type = $productTemplate['target_type'];
  422. $ps->config_text = $productTemplate['config_text'];
  423. $ps->subject_id = $productTemplate['subject_id'];
  424. $ps->is_default = 0;
  425. $ps->create_time = time();
  426. if($bool = $ps->save()){
  427. $result['status'] = 1;
  428. $result['msg']='操作成功';
  429. $result['data']=array(
  430. 'id'=>$ps->template_id,
  431. 'name'=>$templateName
  432. );
  433. }else{
  434. $result['msg']='操作失败';
  435. }
  436. exit(json_encode($result));
  437. }
  438. //根据类型读取模板
  439. public function actionGetTemplateByType(){
  440. $targetType=intval(Req::post('target_type')); //使用类型:1班级模板,2学生模板
  441. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  442. $session_subject_id = isset(Yii::app()->session['session_duoxueke_subject_id'])?Yii::app()->session['session_duoxueke_subject_id']:3;
  443. $subject_id = $post_subject_id?$post_subject_id:$session_subject_id;
  444. $isBeta = $this->isBeta($subject_id);
  445. $sql="select template_id,template_name,target_type from product_template where product_type='31 ' and subject_id={$subject_id} ";
  446. if($targetType && in_array($targetType,array(1,2))){
  447. $sql.=" and target_type='".$targetType."'";
  448. }
  449. $productTemplate=$this->sConn->createCommand($sql)->queryAll();
  450. if(!$productTemplate){
  451. $this->initTemplate3();
  452. $productTemplate=$this->sConn->createCommand($sql)->queryAll();
  453. }
  454. $result['status'] = 1;
  455. $result['msg']='操作成功';
  456. $result['data']=$productTemplate;
  457. exit(json_encode($result));
  458. }
  459. }