TextbookController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * 教材管理控制器类
  4. * @author li
  5. * @date 2021-11-29 17:30:00
  6. * @company 上海互教教育有限公司.
  7. */
  8. class TextbookController extends Controller
  9. {
  10. public $textbookMap = array(
  11. 43 => array('old_textbook_id' => 43, 'old_textbook_name' => '新人教A版', 'new_textbook_name' => '人教A版(2019)', 'new_textbook_id' => 6101, 'subject_id' => 51,),
  12. 13 => array('old_textbook_id' => 13, 'old_textbook_name' => '人教A版', 'new_textbook_name' => '人教A版', 'new_textbook_id' => 6100, 'subject_id' => 3,),
  13. 44 => array('old_textbook_id' => 44, 'old_textbook_name' => '新人教B版', 'new_textbook_name' => '人教B版(2019)', 'new_textbook_id' => 6103, 'subject_id' => 51,),
  14. 14 => array('old_textbook_id' => 14, 'old_textbook_name' => '人教B版', 'new_textbook_name' => '人教B版', 'new_textbook_id' => 6102, 'subject_id' => 3,),
  15. 45 => array('old_textbook_id' => 45, 'old_textbook_name' => '新北师大版', 'new_textbook_name' => '北师大版(2019)', 'new_textbook_id' => 6105, 'subject_id' => 51,),
  16. 16 => array('old_textbook_id' => 16, 'old_textbook_name' => '北师大版', 'new_textbook_name' => '北师大版', 'new_textbook_id' => 6104, 'subject_id' => 3,),
  17. 47 => array('old_textbook_id' => 47, 'old_textbook_name' => '新湘教版', 'new_textbook_name' => '湘教版(2019)', 'new_textbook_id' => 6109, 'subject_id' => 51,),
  18. 32 => array('old_textbook_id' => 32, 'old_textbook_name' => '湘教版', 'new_textbook_name' => '湘教版', 'new_textbook_id' => 6108, 'subject_id' => 3,),
  19. 46 => array('old_textbook_id' => 46, 'old_textbook_name' => '新苏教版', 'new_textbook_name' => '苏教版(2019)', 'new_textbook_id' => 6107, 'subject_id' => 51,),
  20. 15 => array('old_textbook_id' => 15, 'old_textbook_name' => '苏教版', 'new_textbook_name' => '苏教版', 'new_textbook_id' => 6106, 'subject_id' => 3,),
  21. );
  22. // 教材列表
  23. public function actionIndex()
  24. {
  25. //判断全学科试用学校
  26. $rs = http('/cms/api/qxk-math-trial-school/' . $this->schoolId, 'GET', $this->schoolId, array(), 'cgi');
  27. $rs = json_decode($rs, true);
  28. //是不是试用全学科学校 0:不是 1:是。跟school_textbook表is_qxk:是否全学科教材:0-不是1-是,默认0 值一致
  29. $isTrial = 0;
  30. if (isset($rs['errCode']) && $rs['errCode'] == '00' && $rs['data'] == true) {
  31. $isTrial = 1;
  32. }
  33. $schoolTextBookSet = SchoolTextbook::model()->findAll("school_id=:sid and is_qxk=:is_qxk", array(':sid' => $this->schoolId,':is_qxk'=>$isTrial));
  34. $textbookArr=array();
  35. if (!$isTrial) {
  36. if ($schoolTextBookSet) {
  37. foreach ($schoolTextBookSet as $key => $val) {
  38. $textbookArrTemp = array(
  39. 'grade_id' => $val['grade'],
  40. 'subject_id' => $val['subject_id'],
  41. 'textbook_id' => $val['textbook_id'],
  42. 'grade' => Yii::app()->params['grade_config'][$val['grade']]['grade_name'],
  43. 'subject' => Yii::app()->params['subjectId'][$val['subject_id']],
  44. );
  45. if (isset($this->textBookType['3'][$val['textbook_id']])) {
  46. $textbookArrTemp['textbook_name'] = $this->textBookType['3'][$val['textbook_id']];
  47. }
  48. if (isset($this->textBookType['51'][$val['textbook_id']])) {
  49. $textbookArrTemp['textbook_name'] = $this->textBookType['51'][$val['textbook_id']];
  50. }
  51. if (isset($this->textBookType['6'][$val['textbook_id']])) {
  52. $textbookArrTemp['textbook_name'] = $this->textBookType['6'][$val['textbook_id']];
  53. }
  54. if (empty($textbookArrTemp['textbook_name'])) {
  55. $textbookArrTemp['textbook_name'] = '';
  56. }
  57. $textbookArr[] = $textbookArrTemp;
  58. }
  59. } else {
  60. //初始化一条高三
  61. $model = new SchoolTextbook();
  62. $model->school_id = $this->schoolId;
  63. $model->subject_id = 6;
  64. $model->grade = 3;
  65. $model->textbook_id = 28;
  66. $model->save();
  67. $textbookArr[] = array(
  68. 'grade_id' => 3,
  69. 'subject_id' => 6,
  70. 'textbook_id' => 28,
  71. 'grade' => '高三',
  72. 'subject' => '数学',
  73. 'textbook_name' => '高三通用',
  74. );
  75. }
  76. $view = 'index';
  77. $data = array('data' => $textbookArr, 'textbookArr' => $this->textBookType);
  78. } else {
  79. $json_material = $this->apiPost('all_textbook/tree', array('subjectId' => 3));
  80. $json_material = json_decode(json_encode($json_material), true);
  81. $textbooksData = isset($json_material['status']) && $json_material['status'] && isset($json_material['data']) ? $json_material['data'] : array();
  82. $textbooks = array();
  83. foreach ($textbooksData as $item) {
  84. $textbooks[$item['textbook_id']] = $item['textbook_name'];
  85. }
  86. foreach ($schoolTextBookSet as $key => $val) {
  87. $textbookArr[] = array(
  88. 'grade_id' => $val['grade'],
  89. 'subject_id' => $val['subject_id'],
  90. 'textbook_id' => $val['textbook_id'],
  91. 'grade' => Yii::app()->params['grade_config'][$val['grade']]['grade_name'],
  92. 'subject' => Yii::app()->params['subjectId'][$val['subject_id']],
  93. 'textbook_name' => isset($textbooks[$val['textbook_id']]) ? $textbooks[$val['textbook_id']] : '',
  94. );
  95. }
  96. $view = 'index2';
  97. $data = array('data' => $textbookArr, 'textbookArr' => $textbooks);
  98. }
  99. $this->render($view, $data);
  100. }
  101. //添加教材
  102. public function actionAdd(){
  103. $subject = Req::post('subject');
  104. $grade = Req::post('grade');
  105. $textbook = Req::post('textbook');
  106. $isQxk = Req::post('isQxk');
  107. $result['status']=0;
  108. //判断科目id
  109. if(!$textbook){
  110. exit(jsonEncode($result));
  111. }
  112. if(!$isQxk) {
  113. foreach ($this->textBookType as $key => $val) {
  114. if (isset($val[$textbook])) {
  115. $subject = $key;
  116. }
  117. }
  118. }else{
  119. $subject=3;
  120. }
  121. $check=SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk",array(':sid'=>$this->schoolId,':grade'=>$grade,':is_qxk'=>$isQxk));
  122. if($check){
  123. $result['msg']=Yii::app()->params['grade_config'][$grade]['grade_name'].Yii::app()->params['subjectId'][$subject].'已经有设置内容,不能重复设置';
  124. exit(jsonEncode($result));
  125. }else{
  126. $transaction = $this->conn->beginTransaction();
  127. try {
  128. $sql="insert into school_textbook(school_id,grade,textbook_id,subject_id,is_qxk) values({$this->schoolId},{$grade},{$textbook},{$subject},{$isQxk})";
  129. $this->conn->createCommand($sql)->execute();
  130. if(!$isQxk && isset($this->textbookMap[$textbook])){
  131. $newTextbookId=$this->textbookMap[$textbook]['new_textbook_id'];
  132. $qxkTextbook = SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk", array(':sid' => $this->schoolId, ':grade' => $grade, ':is_qxk' => 1));
  133. if($qxkTextbook){
  134. if($qxkTextbook['textbook_id']!=$newTextbookId){
  135. $sql="update school_textbook set textbook_id={$newTextbookId} where school_id={$this->schoolId} and grade={$grade} and is_qxk=1";
  136. $this->conn->createCommand($sql)->execute();
  137. }
  138. }else{
  139. $sql="insert into school_textbook(school_id,grade,textbook_id,subject_id,is_qxk) values({$this->schoolId},{$grade},{$newTextbookId},3,1)";
  140. $this->conn->createCommand($sql)->execute();
  141. }
  142. }
  143. $transaction->commit();
  144. $result['status']=1;
  145. }catch (\Exception $e){
  146. $transaction->rollBack();
  147. $result['msg']=$e->getMessage();
  148. exit(jsonEncode($result));
  149. }
  150. }
  151. if($result['status']==1){
  152. //更新缓存
  153. $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$this->schoolId));
  154. $redisData=array();
  155. foreach ($check as $item){
  156. $redisData[]=array(
  157. 'grade'=>$item['grade'],
  158. 'textbook_id'=>$item['textbook_id'],
  159. 'subject_id'=>$item['subject_id'],
  160. 'school_id'=>$this->schoolId,
  161. 'is_qxk'=>$item['is_qxk'],
  162. );
  163. }
  164. Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$this->schoolId,array('setting'=>jsonEncode($redisData)));
  165. }
  166. exit(jsonEncode($result));
  167. }
  168. //编辑教材
  169. public function actionEdit(){
  170. $subject = Req::post('subject');
  171. $grade = Req::post('grade');
  172. $textbook = Req::post('textbook');
  173. $isQxk = Req::post('isQxk');
  174. $result['status']=0;
  175. //判断科目id
  176. if(!$textbook){
  177. exit(jsonEncode($result));
  178. }
  179. if(!$isQxk) {
  180. foreach ($this->textBookType as $key => $val) {
  181. if (isset($val[$textbook])) {
  182. $subject = $key;
  183. }
  184. }
  185. }else{
  186. $subject=3;
  187. }
  188. $check=SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk",array(':sid'=>$this->schoolId,':grade'=>$grade,':is_qxk'=>$isQxk));
  189. if($check){
  190. if($check['textbook_id']==$textbook ){
  191. $result['msg']=Yii::app()->params['grade_config'][$grade]['grade_name'].Yii::app()->params['subjectId'][$subject].'教材已有相同设置,不能重复设置';
  192. exit(jsonEncode($result));
  193. }else{
  194. $transaction = $this->conn->beginTransaction();
  195. try {
  196. $sql="update school_textbook set textbook_id={$textbook},subject_id={$subject} where school_id={$this->schoolId} and grade={$grade} and is_qxk={$isQxk}";
  197. $this->conn->createCommand($sql)->execute();
  198. if(!$isQxk && isset($this->textbookMap[$textbook])){
  199. $newTextbookId=$this->textbookMap[$textbook]['new_textbook_id'];
  200. $qxkTextbook = SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk", array(':sid' => $this->schoolId, ':grade' => $grade, ':is_qxk' => 1));
  201. if($qxkTextbook){
  202. if($qxkTextbook['textbook_id']!=$newTextbookId){
  203. $sql="update school_textbook set textbook_id={$newTextbookId} where school_id={$this->schoolId} and grade={$grade} and is_qxk=1";
  204. $this->conn->createCommand($sql)->execute();
  205. }
  206. }else{
  207. $sql="insert into school_textbook(school_id,grade,textbook_id,subject_id,is_qxk) values({$this->schoolId},{$grade},{$newTextbookId},3,1)";
  208. $this->conn->createCommand($sql)->execute();
  209. }
  210. }
  211. $transaction->commit();
  212. $result['status']=1;
  213. }catch (\Exception $e){
  214. $transaction->rollBack();
  215. $result['msg']=$e->getMessage();
  216. exit(jsonEncode($result));
  217. }
  218. }
  219. }
  220. if($result['status']==1){
  221. //更新缓存
  222. $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$this->schoolId));
  223. $redisData=array();
  224. foreach ($check as $item){
  225. $redisData[]=array(
  226. 'grade'=>$item['grade'],
  227. 'textbook_id'=>$item['textbook_id'],
  228. 'subject_id'=>$item['subject_id'],
  229. 'school_id'=>$this->schoolId,
  230. 'is_qxk'=>$item['is_qxk'],
  231. );
  232. }
  233. Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$this->schoolId,array('setting'=>jsonEncode($redisData)));
  234. }
  235. exit(jsonEncode($result));
  236. }
  237. }