SProductClassRelation.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chenye
  5. * Date: 2017/11/22
  6. * Time: 11:12
  7. */
  8. class SProductClassRelation extends MyActiveRecord {
  9. public static function model($className = __CLASS__)
  10. {
  11. return parent::model($className);
  12. }
  13. public function tableName()
  14. {
  15. return 'product_class_relation';
  16. }
  17. /**
  18. * @param $psId
  19. * @param $currSemester
  20. * @return array|CDbDataReader
  21. * 获取关联班级
  22. */
  23. public function getClassRelation($psId,$currSemester){
  24. $sql = "SELECT c.class_name,c.grade,p.class_id
  25. FROM product_class_relation as p
  26. LEFT JOIN class as c on c.class_id = p.class_id
  27. WHERE p.ps_id = {$psId} AND p.semester_id = {$currSemester}
  28. ";
  29. $result = $this->getDbConnection()->createCommand($sql)->queryAll();
  30. return $result;
  31. }
  32. /**
  33. * @param $psId
  34. * @param $classIdArr
  35. * @param $currSemester
  36. * @param $setType
  37. * @return string
  38. * 新增班级与产品关联
  39. */
  40. public function insertClassRelation($psId,$classIdArr,$currSemester,$setType){
  41. if(is_array($classIdArr)){
  42. $value = array();
  43. //一个班级只能关联产品中的一个
  44. foreach ($classIdArr as $k=>$v){
  45. $value[] = "({$psId},{$v},{$currSemester},{$setType})";
  46. }
  47. //开始事务
  48. $transaction = $this->getDbConnection()->beginTransaction();
  49. try{
  50. //有则删除
  51. $criteria = new CDbCriteria();
  52. $criteria->addInCondition('class_id',$classIdArr);
  53. $criteria->addCondition("ps_id='{$psId}'");
  54. $this->deleteAll($criteria);
  55. $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.implode(',',$value);
  56. $this->getDbConnection()->createCommand($sql)->execute();
  57. $transaction->commit();
  58. return '1';
  59. }catch (Exception $exception){
  60. $transaction->rollback();
  61. return '0';
  62. }
  63. }else{
  64. return '0';
  65. }
  66. }
  67. public function batchInsert($data){
  68. foreach ($data as $k=>$v){
  69. $value[] = "({$v['ps_id']},{$v['class_id']},{$v['semester_id']},{$v['set_type']})";
  70. }
  71. $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.implode(',',$value);
  72. $this->getDbConnection()->createCommand($sql)->execute();
  73. }
  74. /**
  75. * @param $classId
  76. * @param $setType
  77. * @return array|null
  78. * 是否存在同一类型同一班级
  79. */
  80. public function findRelationByClassId($classId,$setType){
  81. $criteria = new CDbCriteria();
  82. $criteria->select = 'ps_id,set_type';
  83. $criteria->addCondition("class_id={$classId} AND set_type={$setType}");
  84. $result = $this->findAll($criteria);
  85. if($result){
  86. $info = getAttributes($result);
  87. }else{
  88. $info = array();
  89. }
  90. return $info;
  91. }
  92. /**
  93. * @param $psIdArr
  94. * @param $classId
  95. * @param $currSemester
  96. * @return string
  97. * 回归到默认设置
  98. */
  99. public function insertDefaultClass($psIdArr,$classId,$currSemester){
  100. if(is_array($psIdArr)){
  101. $value = "({$psIdArr['ps_id']},{$classId},{$currSemester},{$psIdArr['product_type']})";
  102. $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.$value;
  103. $bool = $this->getDbConnection()->createCommand($sql)->execute();
  104. if($bool>0){
  105. return '1';
  106. }else{
  107. return '0';
  108. }
  109. }else{
  110. return '0';
  111. }
  112. }
  113. /**
  114. * @param $psIdArr
  115. * @param $classId
  116. * @param $currSemester
  117. * @return string
  118. * 添加新班级时关联默认产品设置
  119. */
  120. public function insertDefaultPsClass($psIdArr,$classId,$currSemester){
  121. $value = array();
  122. if(is_array($psIdArr)){
  123. foreach ($psIdArr as $k=>$v){
  124. $value[] = "({$v['ps_id']},{$classId},{$currSemester},{$v['product_type']})";
  125. }
  126. $sql = 'INSERT INTO `product_class_relation` (`ps_id`,`class_id`,`semester_id`,`set_type`) VALUES '.implode(',',$value);
  127. $bool = $this->getDbConnection()->createCommand($sql)->execute();
  128. if($bool>0){
  129. return '1';
  130. }else{
  131. return '0';
  132. }
  133. }else{
  134. return '0';
  135. }
  136. }
  137. /**
  138. * @param $psIdArr
  139. * @param $classId
  140. * @param $currSemester
  141. * @return string
  142. * 查询班级
  143. */
  144. public function getPsClass($psIdArr,$classId){
  145. $value = array();
  146. if(is_array($psIdArr)){
  147. $sql = "select ps_id,class_id from `product_class_relation` where ps_id in(".implode(',',$psIdArr).") and class_id in(".implode(',',$classId).")";
  148. $bool = $this->getDbConnection()->createCommand($sql)->queryAll();
  149. return $bool;
  150. }else{
  151. return false;
  152. }
  153. }
  154. }