123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- /**
- * Created by PhpStorm.
- * User: chenye
- * Date: 2017/11/22
- * Time: 11:12
- */
- class SProductClassRelation extends MyActiveRecord {
- public static function model($className = __CLASS__)
- {
- return parent::model($className);
- }
- public function tableName()
- {
- return 'product_class_relation';
- }
- /**
- * @param $psId
- * @param $currSemester
- * @return array|CDbDataReader
- * 获取关联班级
- */
- public function getClassRelation($psId,$currSemester){
- $sql = "SELECT c.class_name,c.grade,p.class_id
- FROM product_class_relation as p
- LEFT JOIN class as c on c.class_id = p.class_id
- WHERE p.ps_id = {$psId} AND p.semester_id = {$currSemester}
- ";
- $result = $this->getDbConnection()->createCommand($sql)->queryAll();
- return $result;
- }
- /**
- * @param $psId
- * @param $classIdArr
- * @param $currSemester
- * @param $setType
- * @return string
- * 新增班级与产品关联
- */
- public function insertClassRelation($psId,$classIdArr,$currSemester,$setType){
- if(is_array($classIdArr)){
- $value = array();
- //一个班级只能关联产品中的一个
- foreach ($classIdArr as $k=>$v){
- $value[] = "({$psId},{$v},{$currSemester},{$setType})";
- }
- //开始事务
- $transaction = $this->getDbConnection()->beginTransaction();
- try{
- //有则删除
- $criteria = new CDbCriteria();
- $criteria->addInCondition('class_id',$classIdArr);
- $criteria->addCondition("ps_id='{$psId}'");
- $this->deleteAll($criteria);
- $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.implode(',',$value);
- $this->getDbConnection()->createCommand($sql)->execute();
- $transaction->commit();
- return '1';
- }catch (Exception $exception){
- $transaction->rollback();
-
- return '0';
- }
- }else{
- return '0';
- }
- }
- public function batchInsert($data){
- foreach ($data as $k=>$v){
- $value[] = "({$v['ps_id']},{$v['class_id']},{$v['semester_id']},{$v['set_type']})";
- }
- $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.implode(',',$value);
- $this->getDbConnection()->createCommand($sql)->execute();
- }
- /**
- * @param $classId
- * @param $setType
- * @return array|null
- * 是否存在同一类型同一班级
- */
- public function findRelationByClassId($classId,$setType){
- $criteria = new CDbCriteria();
- $criteria->select = 'ps_id,set_type';
- $criteria->addCondition("class_id={$classId} AND set_type={$setType}");
- $result = $this->findAll($criteria);
- if($result){
- $info = getAttributes($result);
- }else{
- $info = array();
- }
- return $info;
- }
- /**
- * @param $psIdArr
- * @param $classId
- * @param $currSemester
- * @return string
- * 回归到默认设置
- */
- public function insertDefaultClass($psIdArr,$classId,$currSemester){
- if(is_array($psIdArr)){
- $value = "({$psIdArr['ps_id']},{$classId},{$currSemester},{$psIdArr['product_type']})";
- $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.$value;
- $bool = $this->getDbConnection()->createCommand($sql)->execute();
- if($bool>0){
- return '1';
- }else{
- return '0';
- }
- }else{
- return '0';
- }
- }
- /**
- * @param $psIdArr
- * @param $classId
- * @param $currSemester
- * @return string
- * 添加新班级时关联默认产品设置
- */
- public function insertDefaultPsClass($psIdArr,$classId,$currSemester){
- $value = array();
- if(is_array($psIdArr)){
- foreach ($psIdArr as $k=>$v){
- $value[] = "({$v['ps_id']},{$classId},{$currSemester},{$v['product_type']})";
- }
- $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.implode(',',$value);
- $bool = $this->getDbConnection()->createCommand($sql)->execute();
- if($bool>0){
- return '1';
- }else{
- return '0';
- }
- }else{
- return '0';
- }
- }
- /**
- * @param $psIdArr
- * @param $classId
- * @param $currSemester
- * @return string
- * 查询班级
- */
- public function getPsClass($psIdArr,$classId){
- $value = array();
- if(is_array($psIdArr)){
- $sql = "select ps_id,class_id from `product_class_relation` where ps_id in(".implode(',',$psIdArr).") and class_id in(".implode(',',$classId).")";
- $bool = $this->getDbConnection()->createCommand($sql)->queryAll();
- return $bool;
- }else{
- return false;
- }
- }
- }
|