Product.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. /**
  3. * Controller is the customized base controller class.
  4. * All controller classes for this application should extend from this base class.
  5. */
  6. class Product extends Controller
  7. {
  8. public function init(){
  9. parent::init();
  10. }
  11. protected function initTemplate3(){
  12. //查询是否有默认模板
  13. $wrongBook=array();
  14. $isp=array();
  15. $wbIsp=array();
  16. $methodIsp=array();
  17. $subject_id = isset(Yii::app()->session['session_duoxueke_subject_id']) ? Yii::app()->session['session_duoxueke_subject_id']:3;
  18. // $subject_id = 3;
  19. $isBeta = $this->isBeta($subject_id);
  20. $productTemplate=$this->sConn->createCommand("select template_id,template_name,product_type,target_type,is_default,is_beta from product_template where is_default=1 and subject_id = {$subject_id}")->queryAll();
  21. if($productTemplate){
  22. foreach ($productTemplate as $val){
  23. if($val['product_type']==1 && $val['is_beta']==$isBeta){
  24. if($val['target_type']==1){
  25. $wrongBook['class']=1;
  26. }elseif($val['target_type']==2){
  27. $wrongBook['student']=1;
  28. }
  29. }elseif($val['product_type']==2 && $val['is_beta']==$isBeta){
  30. if($val['target_type']==1){
  31. $isp['class']=1;
  32. }elseif($val['target_type']==2){
  33. $isp['student']=1;
  34. }
  35. }elseif($val['product_type']==3 && $val['is_beta']==$isBeta){
  36. if($val['target_type']==1){
  37. $wbIsp['class']=1;
  38. }elseif($val['target_type']==2){
  39. $wbIsp['student']=1;
  40. }
  41. }elseif($val['product_type']==31){
  42. if($val['target_type']==1){
  43. $methodIsp['class']=1;
  44. }elseif($val['target_type']==2){
  45. $methodIsp['student']=1;
  46. }
  47. }
  48. }
  49. }
  50. //判断默认模板是否存在
  51. if($subject_id == 12){
  52. $wrongBookClassConfig = ProductPhysicsTpl::PHYSICS_CLASS_WB;
  53. $wrongBookStudentConfig = ProductPhysicsTpl::PHYSICS_STU_WB;
  54. $ispClassConfig = ProductPhysicsTpl::PHYSICS_CLASS_ISP;
  55. $ispStudentConfig = ProductPhysicsTpl::PHYSICS_STU_ISP;
  56. $wpIspClassConfig = ProductPhysicsTpl::PHYSICS_CLASS_WB_ISP;
  57. $wpIspStudentConfig = ProductPhysicsTpl::PHYSICS_STU_WB_ISP;
  58. }else{
  59. if(!$isBeta) {
  60. $wrongBookClassConfig = ProductMathTpl::MATH_CLASS_WB;
  61. $wrongBookStudentConfig = ProductMathTpl::MATH_STU_WB;
  62. $ispClassConfig = ProductMathTpl::MATH_CLASS_ISP;
  63. $ispStudentConfig = ProductMathTpl::MATH_STU_ISP;
  64. $wpIspClassConfig = ProductMathTpl::MATH_CLASS_WB_ISP;
  65. $wpIspStudentConfig = ProductMathTpl::MATH_STU_WB_ISP;
  66. $methodIspClassConfig = ProductMethodTpl::Method_CLASS_WB;
  67. $methodIspStudentConfig = ProductMethodTpl::Method_STU_WB;
  68. }else{
  69. $wrongBookClassConfig = ProductMathTpl::MATH_CLASS_WB_BETA;
  70. $wrongBookStudentConfig = ProductMathTpl::MATH_STU_WB_BETA;
  71. $ispClassConfig = ProductMathTpl::MATH_CLASS_ISP_BETA;
  72. $ispStudentConfig = ProductMathTpl::MATH_STU_ISP_BETA;
  73. $wpIspClassConfig = ProductMathTpl::MATH_CLASS_WB_ISP_BETA;
  74. $wpIspStudentConfig = ProductMathTpl::MATH_STU_WB_ISP_BETA;
  75. $methodIspClassConfig = ProductMethodTpl::Method_CLASS_WB;
  76. $methodIspStudentConfig = ProductMethodTpl::Method_STU_WB;
  77. }
  78. }
  79. $values=array();
  80. if(!isset($wrongBook['class'])){
  81. $values[]="('班级默认模板',1,1,'".$wrongBookClassConfig."',".$subject_id.",1,'".time()."',".$isBeta.")";
  82. }
  83. if(!isset($wrongBook['student'])){
  84. $values[]="('学生默认模板',1,2,'".$wrongBookStudentConfig."',".$subject_id.",1,'".time()."',".$isBeta.")";
  85. }
  86. if(!isset($isp['class'])){
  87. $values[]="('班级默认模板',2,1,'".$ispClassConfig."',".$subject_id.",1,'".time()."',".$isBeta.")";
  88. }
  89. if(!isset($isp['student'])){
  90. $values[]="('学生默认模板',2,2,'".$ispStudentConfig."',".$subject_id.",1,'".time()."',".$isBeta.")";
  91. }
  92. if(!isset($wbIsp['class'])){
  93. $values[]="('班级默认模板',3,1,'".$wpIspClassConfig."',".$subject_id.",1,'".time()."',".$isBeta.")";
  94. }
  95. if(!isset($wbIsp['student'])){
  96. $values[]="('学生默认模板',3,2,'".$wpIspStudentConfig."',".$subject_id.",1,'".time()."',".$isBeta.")";
  97. }
  98. if($subject_id == 3) {//只有高中数学才有方法宝设置
  99. if (!isset($methodIsp['class'])) {
  100. $values[] = "('班级默认模板',31,1,'" . $methodIspClassConfig . "'," . $subject_id . ",1,'" . time() . "',0)";
  101. }
  102. if (!isset($methodIsp['student'])) {
  103. $values[] = "('学生默认模板',31,2,'" . $methodIspStudentConfig . "'," . $subject_id . ",1,'" . time() . "',0)";
  104. }
  105. }
  106. if($values){
  107. $sql="insert into product_template(`template_name`,`product_type`,`target_type`,`config_text`,`subject_id`,`is_default`,`create_time`,`is_beta`) values ".implode(',',$values);
  108. $this->sConn->createCommand($sql)->execute();
  109. }
  110. }
  111. //根据类型读取模板
  112. public function actionGetTemplateByType(){
  113. $type = intval(Req::post('type')); //模板类型 1-错题本 2-个性化学习方案 3-个性化学习宝
  114. $targetType=intval(Req::post('target_type')); //使用类型:1班级模板,2学生模板
  115. $post_subject_id = intval(Req::post('subject_id')); //接口传的subject_id
  116. $session_subject_id = isset(Yii::app()->session['session_duoxueke_subject_id'])?Yii::app()->session['session_duoxueke_subject_id']:3;
  117. $subject_id = $post_subject_id?$post_subject_id:$session_subject_id;
  118. if(!$type ){
  119. $result['msg']='参数错误';
  120. exit(json_encode($result));
  121. }
  122. $sql="select template_id,template_name,target_type from product_template where product_type='{$type} ' and subject_id={$subject_id} ";
  123. if($targetType && in_array($targetType,array(1,2))){
  124. $sql.=" and target_type='".$targetType."'";
  125. }
  126. $productTemplate=$this->sConn->createCommand($sql)->queryAll();
  127. $result['status'] = 1;
  128. $result['msg']='操作成功';
  129. $result['data']=$productTemplate;
  130. exit(json_encode($result));
  131. }
  132. //将提交的json和模板结构进行对比
  133. protected function checkoutJson($jsonArr,$productType,$targetType,$subject_id=3){
  134. // $subject_id = isset(Yii::app()->session['session_duoxueke_subject_id']) ? Yii::app()->session['session_duoxueke_subject_id']:3;
  135. if(!$jsonArr) return false;
  136. $tempJson='';
  137. $isBeta = $this->isBeta($subject_id);
  138. if($productType==1){
  139. if($targetType==1){
  140. if($subject_id == 12){
  141. $tempJson = ProductPhysicsTpl::PHYSICS_CLASS_WB;
  142. }else{
  143. $tempJson = $isBeta ? ProductMathTpl::MATH_CLASS_WB_BETA : ProductMathTpl::MATH_CLASS_WB;
  144. }
  145. }elseif($targetType==2){
  146. if($subject_id == 12){
  147. $tempJson = ProductPhysicsTpl::PHYSICS_STU_WB;
  148. }else{
  149. $tempJson = $isBeta ? ProductMathTpl::MATH_STU_WB_BETA: ProductMathTpl::MATH_STU_WB;
  150. }
  151. }
  152. }elseif($productType==2){
  153. if($targetType==1){
  154. if($subject_id == 12){
  155. $tempJson = ProductPhysicsTpl::PHYSICS_CLASS_ISP;
  156. }else{
  157. $tempJson = $isBeta ? ProductMathTpl::MATH_CLASS_ISP_BETA : ProductMathTpl::MATH_CLASS_ISP;
  158. }
  159. }elseif($targetType==2){
  160. if($subject_id == 12){
  161. $tempJson = ProductPhysicsTpl::PHYSICS_STU_ISP;
  162. }else{
  163. $tempJson = $isBeta ? ProductMathTpl::MATH_STU_ISP_BETA : ProductMathTpl::MATH_STU_ISP;
  164. }
  165. }
  166. }elseif($productType==3){
  167. if($targetType==1){
  168. if($subject_id == 12){
  169. $tempJson = ProductPhysicsTpl::PHYSICS_CLASS_WB_ISP;
  170. }else{
  171. $tempJson = $isBeta ? ProductMathTpl::MATH_CLASS_WB_ISP_BETA : ProductMathTpl::MATH_CLASS_WB_ISP;
  172. }
  173. }elseif($targetType==2){
  174. if($subject_id == 12){
  175. $tempJson = ProductPhysicsTpl::PHYSICS_STU_WB_ISP;
  176. }else{
  177. $tempJson = $isBeta ? ProductMathTpl::MATH_STU_WB_ISP_BETA : ProductMathTpl::MATH_STU_WB_ISP;
  178. }
  179. }
  180. }elseif($productType==31){
  181. if($targetType==1){
  182. $tempJson = ProductMethodTpl::Method_CLASS_WB;
  183. }elseif($targetType==2){
  184. $tempJson = ProductMethodTpl::Method_STU_WB;
  185. }
  186. }
  187. if(!$tempJson) return false;
  188. $jsonTemplate=json_decode($tempJson,true);
  189. foreach ($jsonTemplate as $key =>$value){
  190. if(!isset($jsonArr[$key])) return false;
  191. if(is_array($value)){
  192. foreach ($value as $key_2 =>$val ){
  193. if(!isset($jsonArr[$key][$key_2])) return false;
  194. if(is_array($val)){
  195. foreach ($val as $key_3 =>$v){
  196. if($key_2=='levelDetails' && $key_3>0){
  197. continue;
  198. }
  199. if(!isset($jsonArr[$key][$key_2][$key_3])) return false;
  200. if(is_array($v)){
  201. foreach ($v as $key_4 =>$vv){
  202. if(!isset($jsonArr[$key][$key_2][$key_3][$key_4])) return false;
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. return true;
  211. }
  212. //获取学期学年
  213. public function actionGetSemester(){
  214. $semesterId = $this->semesterId;
  215. $data = array();
  216. if($semesterId){
  217. $result=$this->sConn->createCommand("select semester_name,school_year from semester where semester_id='{$semesterId}'")->queryRow();
  218. if($result){
  219. $data['semester_name'] = $result['semester_name'];
  220. $data['school_year'] = $result['school_year'];
  221. }
  222. }
  223. echo json_encode(array('status'=>1,'data'=>$data));exit;
  224. }
  225. public function getProductEdition()
  226. {
  227. $switch = 2;
  228. $data=$this->sConn->createCommand("select switch from producut_set_edition where id=1")->queryRow();
  229. if($data){
  230. $switch = $data['switch'];
  231. }
  232. return $switch;
  233. }
  234. //FIXME
  235. public function isBeta($subject_id=3)
  236. {
  237. if(!in_array($subject_id,Yii::app()->params['mathSubjectId'])){
  238. return 0;
  239. }
  240. $switch = $this->getProductEdition();
  241. return $switch == 4 ? 1 :0;
  242. }
  243. //切换学科
  244. public function actionChangeSubject(){
  245. $subjectId=Req::post('subjectId')?Req::post('subjectId'):0;
  246. if(!$subjectId){
  247. echo json_encode(array('status'=>0,'msg'=>'学科不正确 '));exit;
  248. }else{
  249. Yii::app()->session['session_duoxueke_subject_id'] = $subjectId;
  250. Yii::app()->session['session_subject_id'] = $subjectId;
  251. echo json_encode(array('status'=>1));exit;
  252. }
  253. }
  254. //根据年级读取班级
  255. public function actionGetClassByGrade(){
  256. $grade_id = Req::post("grade_id");
  257. $class_array = array();
  258. if(!in_array($grade_id,array(0,1,2,3)))
  259. {
  260. echo json_encode(array('status'=>0,'result'=>array()));exit;
  261. }
  262. if($grade_id){
  263. $class_data = ClassModel::model()->findAll('grade=:grade and semester_id=:semester_id and class_type=:class_type',array(':grade' => $grade_id,':semester_id'=>$this->semesterId,':class_type'=>1));
  264. }else{
  265. $class_data = ClassModel::model()->findAll(' semester_id=:semester_id and class_type=:class_type',array(':semester_id'=>$this->semesterId ,':class_type'=>1));
  266. }
  267. if($class_data){
  268. foreach($class_data as $v )
  269. {
  270. $class_array[] = $v->attributes;
  271. }
  272. echo json_encode(array('status'=>1,'result'=>$class_array));exit;
  273. }
  274. echo json_encode(array('status'=>0,'result'=>array()));exit;
  275. }
  276. /**
  277. * 查询行政班级名字
  278. * @param $stu_ids
  279. * @return mixed
  280. */
  281. protected function getStudentXzClassName($stu_ids)
  282. {
  283. $student_class_info =$this->sConn->createCommand("select c.class_name,scr.student_id from student_class_relation as scr left join class as c on scr.class_id = c.class_id where scr.status = 0 and c.class_type = 1 and scr.student_id in (".implode(',',$stu_ids).")")->queryAll();
  284. if ($student_class_info) {
  285. foreach ($student_class_info as $value) {
  286. $stu_rel_class[$value['student_id']] = $value['class_name'];
  287. }
  288. }
  289. unset($student_class_info);
  290. return $stu_rel_class;
  291. }
  292. //重命名
  293. public function actionReNameTemplate(){
  294. $id = intval(Req::post('id'));
  295. $name=Req::post('name');
  296. $result['status'] = 0;
  297. $session_subject_id = isset(Yii::app()->session['session_duoxueke_subject_id'])?Yii::app()->session['session_duoxueke_subject_id']:3;
  298. if(!$id ){
  299. $result['msg']='请选择模板';
  300. exit(json_encode($result));
  301. }
  302. if(!$name){
  303. $result['msg']='名称不能为空';
  304. exit(json_encode($result));
  305. }
  306. $productTemplate=$this->sConn->createCommand("select template_id,product_type from product_template where template_id='{$id}'")->queryRow();
  307. if(!$productTemplate){
  308. $result['msg']='模板id不正确';
  309. exit(json_encode($result));
  310. }
  311. $productTemplate=$this->sConn->createCommand("select template_id from product_template where template_name='{$name}' and product_type='{$productTemplate['product_type']}' and subject_id='{$session_subject_id}'")->queryRow();
  312. if($productTemplate){
  313. $result['msg']='模板名称已存在';
  314. exit(json_encode($result));
  315. }
  316. $criteria = new CDbCriteria();
  317. $criteria->addCondition("template_id={$id}");
  318. if(SProductTemplate::model()->updateAll(array('template_name'=>$name),$criteria)){
  319. $result['status'] = 1;
  320. $result['msg']='操作成功';
  321. }else{
  322. $result['msg']='操作失败';
  323. }
  324. exit(json_encode($result));
  325. }
  326. //删除
  327. public function actionDelTemplate(){
  328. $id = intval(Req::post('id'));
  329. $result['status'] = 0;
  330. if(!$id ){
  331. $result['msg']='请选择模板';
  332. exit(json_encode($result));
  333. }
  334. $productTemplate=$this->sConn->createCommand("select template_id,is_default,product_type,target_type from product_template where template_id='{$id}'")->queryRow();
  335. if(!$productTemplate){
  336. $result['msg']='模板id不正确';
  337. exit(json_encode($result));
  338. }
  339. if($productTemplate['is_default']==1){
  340. $result['msg']='默认模板不可以删除';
  341. exit(json_encode($result));
  342. }
  343. //删除模板
  344. $trans = $this->sConn->beginTransaction();
  345. try{
  346. if($productTemplate['product_type']==1){
  347. if($productTemplate['target_type']==1){
  348. $this->sConn->createCommand("update product_class_set set wrong_book=0 where wrong_book='{$id}'")->execute();
  349. }if($productTemplate['target_type']==2){
  350. $this->sConn->createCommand("update product_student_set set wrong_book=0 where wrong_book='{$id}'")->execute();
  351. }
  352. }elseif($productTemplate['product_type']==2){
  353. if($productTemplate['target_type']==1){
  354. $this->sConn->createCommand("update product_class_set set isp=0 where isp='{$id}'")->execute();
  355. }if($productTemplate['target_type']==2){
  356. $this->sConn->createCommand("update product_student_set set isp=0 where isp='{$id}'")->execute();
  357. }
  358. }elseif($productTemplate['product_type']==3){
  359. if($productTemplate['target_type']==1){
  360. $this->sConn->createCommand("update product_class_set set wb_isp=0 where wb_isp='{$id}'")->execute();
  361. }if($productTemplate['target_type']==2){
  362. $this->sConn->createCommand("update product_student_set set wb_isp=0 where wb_isp='{$id}'")->execute();
  363. }
  364. }else{
  365. if($productTemplate['target_type']==1){
  366. $this->sConn->createCommand("update product_class_set set template_id=0 where template_id='{$id}'")->execute();
  367. }if($productTemplate['target_type']==2){
  368. $this->sConn->createCommand("update product_student_set set template_id=0 where template_id='{$id}'")->execute();
  369. }
  370. }
  371. $this->sConn->createCommand("delete from product_template where template_id='{$id}'")->execute();
  372. $trans->commit();
  373. $result['status'] = 1;
  374. $result['msg'] = "删除成功";
  375. }catch (Exception $e){
  376. $trans->rollBack();
  377. $result['status'] = 0;
  378. $result['msg'] = "删除失败";
  379. }
  380. exit(json_encode($result));
  381. }
  382. //删除模板之前预判断
  383. //返回值 :1正常可删除 -1 默认模板 -2 已有使用 0 参数异常
  384. public function actionBeforeDelTemplate(){
  385. $id = intval(Req::post('id'));
  386. $result['status'] = 0;
  387. if(!$id ){
  388. $result['msg']='请选择模板';
  389. exit(json_encode($result));
  390. }
  391. $productTemplate=$this->sConn->createCommand("select template_id,is_default,product_type,target_type from product_template where template_id='{$id}'")->queryRow();
  392. if(!$productTemplate){
  393. $result['msg']='模板id不正确';
  394. exit(json_encode($result));
  395. }
  396. if($productTemplate['is_default']==1){
  397. $result['status'] = -1;
  398. exit(json_encode($result));
  399. }
  400. if($productTemplate['product_type']==1){
  401. if($productTemplate['target_type']==1){
  402. $used=$this->sConn->createCommand("select class_id from product_class_set where wrong_book='{$id}'")->queryRow();
  403. }if($productTemplate['target_type']==2){
  404. $used=$this->sConn->createCommand("select class_id from product_student_set where wrong_book='{$id}'")->queryRow();
  405. }
  406. }elseif($productTemplate['product_type']==2){
  407. if($productTemplate['target_type']==1){
  408. $used=$this->sConn->createCommand("select class_id from product_class_set where isp='{$id}'")->queryRow();
  409. }if($productTemplate['target_type']==2){
  410. $used=$this->sConn->createCommand("select class_id from product_student_set where isp='{$id}'")->queryRow();
  411. }
  412. }elseif($productTemplate['product_type']==3){
  413. if($productTemplate['target_type']==1){
  414. $used=$this->sConn->createCommand("select class_id from product_class_set where wb_isp='{$id}'")->queryRow();
  415. }if($productTemplate['target_type']==2){
  416. $used=$this->sConn->createCommand("select class_id from product_student_set where wb_isp='{$id}'")->queryRow();
  417. }
  418. }else{
  419. if($productTemplate['target_type']==1){
  420. $used=$this->sConn->createCommand("select class_id from product_class_set where template_id='{$id}'")->queryRow();
  421. }if($productTemplate['target_type']==2){
  422. $used=$this->sConn->createCommand("select class_id from product_student_set where template_id='{$id}'")->queryRow();
  423. }
  424. }
  425. if($used){
  426. $result['status'] = -2;
  427. exit(json_encode($result));
  428. }
  429. $result['status'] = 1;
  430. exit(json_encode($result));
  431. }
  432. }