123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * 教材管理控制器类
- * @author li
- * @date 2021-11-29 17:30:00
- * @company 上海互教教育有限公司.
- */
- class TextbookController extends Controller
- {
- public $textbookMap = array(
- 43 => array('old_textbook_id' => 43, 'old_textbook_name' => '新人教A版', 'new_textbook_name' => '人教A版(2019)', 'new_textbook_id' => 6101, 'subject_id' => 51,),
- 13 => array('old_textbook_id' => 13, 'old_textbook_name' => '人教A版', 'new_textbook_name' => '人教A版', 'new_textbook_id' => 6100, 'subject_id' => 3,),
- 44 => array('old_textbook_id' => 44, 'old_textbook_name' => '新人教B版', 'new_textbook_name' => '人教B版(2019)', 'new_textbook_id' => 6103, 'subject_id' => 51,),
- 14 => array('old_textbook_id' => 14, 'old_textbook_name' => '人教B版', 'new_textbook_name' => '人教B版', 'new_textbook_id' => 6102, 'subject_id' => 3,),
- 45 => array('old_textbook_id' => 45, 'old_textbook_name' => '新北师大版', 'new_textbook_name' => '北师大版(2019)', 'new_textbook_id' => 6105, 'subject_id' => 51,),
- 16 => array('old_textbook_id' => 16, 'old_textbook_name' => '北师大版', 'new_textbook_name' => '北师大版', 'new_textbook_id' => 6104, 'subject_id' => 3,),
- 47 => array('old_textbook_id' => 47, 'old_textbook_name' => '新湘教版', 'new_textbook_name' => '湘教版(2019)', 'new_textbook_id' => 6109, 'subject_id' => 51,),
- 32 => array('old_textbook_id' => 32, 'old_textbook_name' => '湘教版', 'new_textbook_name' => '湘教版', 'new_textbook_id' => 6108, 'subject_id' => 3,),
- 46 => array('old_textbook_id' => 46, 'old_textbook_name' => '新苏教版', 'new_textbook_name' => '苏教版(2019)', 'new_textbook_id' => 6107, 'subject_id' => 51,),
- 15 => array('old_textbook_id' => 15, 'old_textbook_name' => '苏教版', 'new_textbook_name' => '苏教版', 'new_textbook_id' => 6106, 'subject_id' => 3,),
- );
- // 教材列表
- public function actionIndex()
- {
- //判断全学科试用学校
- $rs = http('/cms/api/qxk-math-trial-school/' . $this->schoolId, 'GET', $this->schoolId, array(), 'cgi');
- $rs = json_decode($rs, true);
- //是不是试用全学科学校 0:不是 1:是。跟school_textbook表is_qxk:是否全学科教材:0-不是1-是,默认0 值一致
- $isTrial = 0;
- if (isset($rs['errCode']) && $rs['errCode'] == '00' && $rs['data'] == true) {
- $isTrial = 1;
- }
- $schoolTextBookSet = SchoolTextbook::model()->findAll("school_id=:sid and is_qxk=:is_qxk", array(':sid' => $this->schoolId,':is_qxk'=>$isTrial));
- $textbookArr=array();
- if (!$isTrial) {
- if ($schoolTextBookSet) {
- foreach ($schoolTextBookSet as $key => $val) {
- $textbookArrTemp = array(
- 'grade_id' => $val['grade'],
- 'subject_id' => $val['subject_id'],
- 'textbook_id' => $val['textbook_id'],
- 'grade' => Yii::app()->params['grade_config'][$val['grade']]['grade_name'],
- 'subject' => Yii::app()->params['subjectId'][$val['subject_id']],
- );
- if (isset($this->textBookType['3'][$val['textbook_id']])) {
- $textbookArrTemp['textbook_name'] = $this->textBookType['3'][$val['textbook_id']];
- }
- if (isset($this->textBookType['51'][$val['textbook_id']])) {
- $textbookArrTemp['textbook_name'] = $this->textBookType['51'][$val['textbook_id']];
- }
- if (isset($this->textBookType['6'][$val['textbook_id']])) {
- $textbookArrTemp['textbook_name'] = $this->textBookType['6'][$val['textbook_id']];
- }
- if (empty($textbookArrTemp['textbook_name'])) {
- $textbookArrTemp['textbook_name'] = '';
- }
- $textbookArr[] = $textbookArrTemp;
- }
- } else {
- //初始化一条高三
- $model = new SchoolTextbook();
- $model->school_id = $this->schoolId;
- $model->subject_id = 6;
- $model->grade = 3;
- $model->textbook_id = 28;
- $model->save();
- $textbookArr[] = array(
- 'grade_id' => 3,
- 'subject_id' => 6,
- 'textbook_id' => 28,
- 'grade' => '高三',
- 'subject' => '数学',
- 'textbook_name' => '高三通用',
- );
- }
- $view = 'index';
- $data = array('data' => $textbookArr, 'textbookArr' => $this->textBookType);
- } else {
- $json_material = $this->apiPost('all_textbook/tree', array('subjectId' => 3));
- $json_material = json_decode(json_encode($json_material), true);
- $textbooksData = isset($json_material['status']) && $json_material['status'] && isset($json_material['data']) ? $json_material['data'] : array();
- $textbooks = array();
- foreach ($textbooksData as $item) {
- $textbooks[$item['textbook_id']] = $item['textbook_name'];
- }
- foreach ($schoolTextBookSet as $key => $val) {
- $textbookArr[] = array(
- 'grade_id' => $val['grade'],
- 'subject_id' => $val['subject_id'],
- 'textbook_id' => $val['textbook_id'],
- 'grade' => Yii::app()->params['grade_config'][$val['grade']]['grade_name'],
- 'subject' => Yii::app()->params['subjectId'][$val['subject_id']],
- 'textbook_name' => isset($textbooks[$val['textbook_id']]) ? $textbooks[$val['textbook_id']] : '',
- );
- }
- $view = 'index2';
- $data = array('data' => $textbookArr, 'textbookArr' => $textbooks);
- }
- $this->render($view, $data);
- }
- //添加教材
- public function actionAdd(){
- $subject = Req::post('subject');
- $grade = Req::post('grade');
- $textbook = Req::post('textbook');
- $isQxk = Req::post('isQxk');
- $result['status']=0;
- //判断科目id
- if(!$textbook){
- exit(jsonEncode($result));
- }
- if(!$isQxk) {
- foreach ($this->textBookType as $key => $val) {
- if (isset($val[$textbook])) {
- $subject = $key;
- }
- }
- }else{
- $subject=3;
- }
- $check=SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk",array(':sid'=>$this->schoolId,':grade'=>$grade,':is_qxk'=>$isQxk));
- if($check){
- $result['msg']=Yii::app()->params['grade_config'][$grade]['grade_name'].Yii::app()->params['subjectId'][$subject].'已经有设置内容,不能重复设置';
- exit(jsonEncode($result));
- }else{
- $transaction = $this->conn->beginTransaction();
- try {
- $sql="insert into school_textbook(school_id,grade,textbook_id,subject_id,is_qxk) values({$this->schoolId},{$grade},{$textbook},{$subject},{$isQxk})";
- $this->conn->createCommand($sql)->execute();
- if(!$isQxk && isset($this->textbookMap[$textbook])){
- $newTextbookId=$this->textbookMap[$textbook]['new_textbook_id'];
- $qxkTextbook = SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk", array(':sid' => $this->schoolId, ':grade' => $grade, ':is_qxk' => 1));
- if($qxkTextbook){
- if($qxkTextbook['textbook_id']!=$newTextbookId){
- $sql="update school_textbook set textbook_id={$newTextbookId} where school_id={$this->schoolId} and grade={$grade} and is_qxk=1";
- $this->conn->createCommand($sql)->execute();
- }
- }else{
- $sql="insert into school_textbook(school_id,grade,textbook_id,subject_id,is_qxk) values({$this->schoolId},{$grade},{$newTextbookId},3,1)";
- $this->conn->createCommand($sql)->execute();
- }
- }
- $transaction->commit();
- $result['status']=1;
- }catch (\Exception $e){
- $transaction->rollBack();
- $result['msg']=$e->getMessage();
- exit(jsonEncode($result));
- }
- }
- if($result['status']==1){
- //更新缓存
- $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$this->schoolId));
- $redisData=array();
- foreach ($check as $item){
- $redisData[]=array(
- 'grade'=>$item['grade'],
- 'textbook_id'=>$item['textbook_id'],
- 'subject_id'=>$item['subject_id'],
- 'school_id'=>$this->schoolId,
- 'is_qxk'=>$item['is_qxk'],
- );
- }
- Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$this->schoolId,array('setting'=>jsonEncode($redisData)));
- }
- exit(jsonEncode($result));
- }
- //编辑教材
- public function actionEdit(){
- $subject = Req::post('subject');
- $grade = Req::post('grade');
- $textbook = Req::post('textbook');
- $isQxk = Req::post('isQxk');
- $result['status']=0;
- //判断科目id
- if(!$textbook){
- exit(jsonEncode($result));
- }
- if(!$isQxk) {
- foreach ($this->textBookType as $key => $val) {
- if (isset($val[$textbook])) {
- $subject = $key;
- }
- }
- }else{
- $subject=3;
- }
- $check=SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk",array(':sid'=>$this->schoolId,':grade'=>$grade,':is_qxk'=>$isQxk));
- if($check){
- if($check['textbook_id']==$textbook ){
- $result['msg']=Yii::app()->params['grade_config'][$grade]['grade_name'].Yii::app()->params['subjectId'][$subject].'教材已有相同设置,不能重复设置';
- exit(jsonEncode($result));
- }else{
- $transaction = $this->conn->beginTransaction();
- try {
- $sql="update school_textbook set textbook_id={$textbook},subject_id={$subject} where school_id={$this->schoolId} and grade={$grade} and is_qxk={$isQxk}";
- $this->conn->createCommand($sql)->execute();
- if(!$isQxk && isset($this->textbookMap[$textbook])){
- $newTextbookId=$this->textbookMap[$textbook]['new_textbook_id'];
- $qxkTextbook = SchoolTextbook::model()->find("school_id=:sid and grade=:grade and is_qxk=:is_qxk", array(':sid' => $this->schoolId, ':grade' => $grade, ':is_qxk' => 1));
- if($qxkTextbook){
- if($qxkTextbook['textbook_id']!=$newTextbookId){
- $sql="update school_textbook set textbook_id={$newTextbookId} where school_id={$this->schoolId} and grade={$grade} and is_qxk=1";
- $this->conn->createCommand($sql)->execute();
- }
- }else{
- $sql="insert into school_textbook(school_id,grade,textbook_id,subject_id,is_qxk) values({$this->schoolId},{$grade},{$newTextbookId},3,1)";
- $this->conn->createCommand($sql)->execute();
- }
- }
- $transaction->commit();
- $result['status']=1;
- }catch (\Exception $e){
- $transaction->rollBack();
- $result['msg']=$e->getMessage();
- exit(jsonEncode($result));
- }
- }
- }
- if($result['status']==1){
- //更新缓存
- $check=SchoolTextbook::model()->findAll("school_id=:sid ",array(':sid'=>$this->schoolId));
- $redisData=array();
- foreach ($check as $item){
- $redisData[]=array(
- 'grade'=>$item['grade'],
- 'textbook_id'=>$item['textbook_id'],
- 'subject_id'=>$item['subject_id'],
- 'school_id'=>$this->schoolId,
- 'is_qxk'=>$item['is_qxk'],
- );
- }
- Yii::app()->school_textbook_setting->hmset('content:school_textbook:'.$this->schoolId,array('setting'=>jsonEncode($redisData)));
- }
- exit(jsonEncode($result));
- }
- }
|