Browse Source

fix: 修复查询当前班级绑定教师列表的bug

sakura 1 năm trước cách đây
mục cha
commit
58e9a3bfcf

+ 2 - 2
school-in-out-admin/src/main/java/com/schoolinout/web/controller/system/ClazzController.java

@@ -110,8 +110,8 @@ public class ClazzController extends BaseController {
     @PreAuthorize("@ss.hasPermi('system:clazz:edit')")
     @Log(title = "班级管理", businessType = BusinessType.UPDATE)
     @PutMapping
-    public AjaxResult edit(@RequestBody Clazz clazz) {
-        return toAjax(clazzService.updateClazz(clazz));
+    public AjaxResult edit(@Validated @RequestBody AddClassDto form) {
+        return toAjax(clazzService.updateClazz(form));
     }
 
     /**

+ 10 - 0
school-in-out-system/src/main/java/com/schoolinout/system/domain/dto/AddClassDto.java

@@ -15,6 +15,8 @@ import java.util.List;
 public class AddClassDto implements Serializable {
     private static final long serialVersionUID = 1L;
 
+    private Long id;
+
     /**
      * 学校id
      */
@@ -56,6 +58,14 @@ public class AddClassDto implements Serializable {
                 '}';
     }
 
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
     public Long getSchoolId() {
         return schoolId;
     }

+ 6 - 0
school-in-out-system/src/main/java/com/schoolinout/system/mapper/TeacherClassRelationMapper.java

@@ -75,4 +75,10 @@ public interface TeacherClassRelationMapper extends BaseMapper<TeacherClassRelat
     void updateTeacherMainBindRelation(@Param("schoolId") Long schoolId, @Param("classId") Long classId, @Param("teacherId") Long teacherId, @Param("teacherMain") String teacherMain, @Param("setTeacherMain") String setTeacherMain);
 
     TeacherClassRelation selectTeacherMain(@Param("schoolId") Long schoolId, @Param("teacherId") Long teacherId, @Param("classId") Long classId);
+
+    void deleteRelationBatch(@Param("schoolId") Long schoolId, @Param("classId") Long classId, @Param("list") List<Long> teacherRelationToRemove);
+
+    void insertRelationBatch(@Param("schoolId") Long schoolId, @Param("classId") Long classId, @Param("list") List<Long> teacherIds);
+
+    void deleteAllRelationsForClass(@Param("schoolId") Long schoolId, @Param("classId") Long classId);
 }

+ 3 - 1
school-in-out-system/src/main/java/com/schoolinout/system/service/IClazzService.java

@@ -45,7 +45,7 @@ public interface IClazzService {
      * @param clazz 班级管理
      * @return 结果
      */
-    public int updateClazz(Clazz clazz);
+    public int updateClazz(AddClassDto form);
 
     /**
      * 批量删除班级管理
@@ -64,4 +64,6 @@ public interface IClazzService {
     public int deleteClazzById(Long id);
 
     public int saveClassAndRelation(AddClassDto form);
+
+    public void handlerClassTeacherRelation(AddClassDto form, Long classId, List<Long> newTeacherIds);
 }

+ 0 - 2
school-in-out-system/src/main/java/com/schoolinout/system/service/ITeacherClassRelationService.java

@@ -74,8 +74,6 @@ public interface ITeacherClassRelationService {
     /**
      * 查询当前教师绑定的所有班级
      *
-     * @param schoolId  学校id
-     * @param teacherId 教师id
      * @return 结果
      */
     public List<TeacherClassInfoVo> queryTeacherBindClassList(RelationListDto form);

+ 57 - 5
school-in-out-system/src/main/java/com/schoolinout/system/service/impl/ClazzServiceImpl.java

@@ -6,6 +6,7 @@ import com.schoolinout.common.utils.DateUtils;
 import com.schoolinout.common.utils.SecurityUtils;
 import com.schoolinout.common.utils.bean.BeanUtils;
 import com.schoolinout.system.domain.Clazz;
+import com.schoolinout.system.domain.TeacherClassRelation;
 import com.schoolinout.system.domain.dto.AddClassDto;
 import com.schoolinout.system.domain.dto.InsertTeacherClassRelationDto;
 import com.schoolinout.system.domain.vo.OptionVo;
@@ -19,6 +20,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.rmi.ServerException;
 import java.util.Date;
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
@@ -29,6 +31,9 @@ import java.util.stream.Collectors;
  */
 @Service
 public class ClazzServiceImpl implements IClazzService {
+
+    @Autowired
+    private IClazzService curProxy;
     @Autowired
     private ClazzMapper clazzMapper;
     @Autowired
@@ -76,15 +81,62 @@ public class ClazzServiceImpl implements IClazzService {
     /**
      * 修改班级管理
      *
-     * @param clazz 班级管理
      * @return 结果
      */
     @Override
-    public int updateClazz(Clazz clazz) {
-        clazz.setUpdateTime(DateUtils.getNowDate());
+    @Transactional(rollbackFor = RuntimeException.class)
+    public int updateClazz(AddClassDto form) {
+        Long classId = form.getId();
+        List<Long> newTeacherIds = form.getTeacherIds();
+
+        // 处理班级与教师直接的关系
+        curProxy.handlerClassTeacherRelation(form, classId, newTeacherIds);
+
+        // 更新班级信息
+        Clazz clazz = new Clazz();
+        BeanUtils.copyBeanProp(clazz, form);
         return clazzMapper.updateClazz(clazz);
     }
 
+    @Transactional(rollbackFor = RuntimeException.class)
+    public void handlerClassTeacherRelation(AddClassDto form, Long classId, List<Long> newTeacherIds) {
+        // 先查询原班级关系数据
+        TeacherClassRelation queryRelation = new TeacherClassRelation();
+        queryRelation.setClassId(classId);
+        List<TeacherClassRelation> existingRelations = relationMapper.selectTeacherClassRelationList(queryRelation);
+
+        // 将现有关系映射为教师ID集合
+        Set<Long> existingTeacherIds = existingRelations.stream()
+                .map(TeacherClassRelation::getTeacherId)
+                .collect(Collectors.toSet());
+
+        // 如果新的教师列表为空,或者需要移除所有现有教师
+        if (newTeacherIds == null || newTeacherIds.isEmpty()) {
+            // 删除所有现有关系
+            relationMapper.deleteAllRelationsForClass(form.getSchoolId(), classId);
+        } else {
+            // 计算需要删除的教师ID
+            List<Long> teacherIdsToRemove = existingTeacherIds.stream()
+                    .filter(teacherId -> !newTeacherIds.contains(teacherId))
+                    .collect(Collectors.toList());
+
+            // 计算需要添加的教师ID
+            List<Long> teacherIdsToAdd = newTeacherIds.stream()
+                    .filter(teacherId -> !existingTeacherIds.contains(teacherId))
+                    .collect(Collectors.toList());
+
+            // 执行批量删除
+            if (!teacherIdsToRemove.isEmpty()) {
+                relationMapper.deleteRelationBatch(form.getSchoolId(), classId, teacherIdsToRemove);
+            }
+
+            // 执行批量插入
+            if (!teacherIdsToAdd.isEmpty()) {
+                relationMapper.insertRelationBatch(form.getSchoolId(), classId, teacherIdsToAdd);
+            }
+        }
+    }
+
     /**
      * 批量删除班级管理
      *
@@ -128,7 +180,7 @@ public class ClazzServiceImpl implements IClazzService {
 
         // step3:如果存在绑定老师,则新增关系的绑定
         List<Long> teacherIds = form.getTeacherIds();
-        if (!teacherIds.isEmpty()) {
+        if (teacherIds != null && !teacherIds.isEmpty()) {
             // 插入数据
             Clazz finalClazz = clazz;
             List<InsertTeacherClassRelationDto> insertList = teacherIds.stream()
@@ -137,6 +189,6 @@ public class ClazzServiceImpl implements IClazzService {
             return relationMapper.insertTeacherClassRelationBatch(insertList);
         }
 
-        return 0;
+        return 1;
     }
 }

+ 0 - 1
school-in-out-system/src/main/java/com/schoolinout/system/service/impl/TeacherClassRelationServiceImpl.java

@@ -159,7 +159,6 @@ public class TeacherClassRelationServiceImpl implements ITeacherClassRelationSer
             ans = "解绑成功";
         }
 
-
         return ans;
     }
 }

+ 4 - 2
school-in-out-system/src/main/resources/mapper/system/ClazzMapper.xml

@@ -47,8 +47,9 @@
         tb_school_class.update_by,
         tb_school_class.update_time
         from tb_school_class
-        LEFT JOIN tb_school_teacher_class_relation relation on tb_school_class.school_id = relation.school_id AND
-        relation.teacher_main = 'Y'
+        LEFT JOIN tb_school_teacher_class_relation relation on tb_school_class.school_id = relation.school_id
+        AND tb_school_class.id = relation.class_id
+        AND relation.teacher_main = 'Y'
         LEFT JOIN tb_school_teacher teacher on relation.teacher_id = teacher.id
         <where>
             <if test="schoolId != null ">and school_id = #{schoolId}</if>
@@ -56,6 +57,7 @@
             <if test="classCount != null ">and class_count = #{classCount}</if>
             <if test="phone != null  and phone != ''">and phone = #{phone}</if>
         </where>
+        ORDER BY tb_school_class.create_time DESC
     </select>
 
     <select id="selectClazzById" parameterType="Long" resultMap="ClazzResult">

+ 39 - 3
school-in-out-system/src/main/resources/mapper/system/TeacherClassRelationMapper.xml

@@ -53,9 +53,12 @@
         relation.teacher_main,
         class.class_name,
         teacher.teacher_name
-        FROM tb_school_teacher_class_relation relation
-        JOIN tb_school_class class ON relation.class_id = class.id
-        JOIN tb_school_teacher teacher ON relation.school_id = teacher.school_id
+        FROM
+        tb_school_teacher_class_relation relation
+        JOIN
+        tb_school_class class ON relation.class_id = class.id AND relation.school_id = class.school_id
+        JOIN
+        tb_school_teacher teacher ON relation.teacher_id = teacher.id AND relation.school_id = teacher.school_id
         <where>
             <if test="schoolId != null">
                 AND relation.school_id = #{schoolId}
@@ -116,6 +119,15 @@
             (#{item.schoolId}, #{item.teacherId}, #{item.classId})
         </foreach>
     </insert>
+    <insert id="insertRelationBatch">
+        INSERT INTO tb_school_teacher_class_relation(school_id, teacher_id, class_id)
+        VALUES
+        <if test="list != null">
+            <foreach collection="list" item="item" separator=",">
+                (#{schoolId}, #{item}, #{classId})
+            </foreach>
+        </if>
+    </insert>
 
     <update id="updateTeacherClassRelation" parameterType="TeacherClassRelation">
         update tb_school_teacher_class_relation
@@ -168,4 +180,28 @@
         WHERE school_id = #{schoolId}
           AND teacher_id = #{teacherId}
     </delete>
+    <delete id="deleteRelationBatch">
+        DELETE FROM tb_school_teacher_class_relation
+        <where>
+            WHERE school_id = 2 and class_id = 1 and teacher_id = 1;
+            <if test="schoolId != null">
+                AND school_id = #{schoolId}
+            </if>
+            <if test="classId != null">
+                AND class_id = #{classId}
+            </if>
+            <if test="list != null">
+                AND teacher_id IN
+                <foreach collection="list" item="item" separator="," open="(" close=")">
+                    #{item}
+                </foreach>
+            </if>
+        </where>
+    </delete>
+    <delete id="deleteAllRelationsForClass">
+        DELETE
+        FROM tb_school_teacher_class_relation
+        WHERE school_id = #{schoolId}
+          AND class_id = #{classId}
+    </delete>
 </mapper>

+ 2 - 2
school-in-out-ui/src/views/system/clazz/index.vue

@@ -263,13 +263,13 @@ export default {
             this.$modal.msgSuccess("修改成功");
             this.$refs.clazzForm.open = false
             this.getList();
-          });
+          }).finally(() =>  this.$refs.clazzForm.loading = false)
         } else {
           addClazz(data).then(response => {
             this.$modal.msgSuccess("新增成功");
             this.$refs.clazzForm.open = false
             this.getList();
-          });
+          }).finally(() =>  this.$refs.clazzForm.loading = false);
         }
     },
     /** 删除按钮操作 */

+ 8 - 4
school-in-out-ui/src/views/system/clazz/module/BindTeacherClass.vue

@@ -6,6 +6,7 @@
     element-loading-background="rgba(0, 0, 0, 0.8)"
     title="绑定班主任" width="450px" 
     :visible.sync="open"
+    @close="closeDialog"
   >
     <el-form 
         :model="form" 
@@ -56,11 +57,13 @@ export default {
     methods: {
         openDialog() {
             this.open = true;
+
         },
         closeDialog() {
             this.open = false;
             this.reset();
             this.$refs.bindRuleForm.resetFields()
+            this.teacherOptions = []
         },
         dialogFormVisibleChange(val) {
             this.dialogFormVisible = val;
@@ -70,10 +73,10 @@ export default {
         },
         searchTeacherOption(openSelect) {
             if (openSelect) {
+                this.bindOptionLoding = true
                 // 发送请求
-                console.log("搜索数据");
                 getRelationInfoList(this.form).then(res => {
-                    console.log(res);
+                
                     let data = res.data;
                     this.teacherOptions = data.map(item => {
                         let obj = {};
@@ -81,8 +84,9 @@ export default {
                         obj.value = item.teacherId;
                         return obj;
                     })
-                 
-                })
+                }).finally(() =>
+                    this.bindOptionLoding = false
+                )
             }
         },
         submitForm() {

+ 17 - 4
school-in-out-ui/src/views/system/clazz/module/ClazzForm.vue

@@ -1,7 +1,17 @@
 <template>
     <div class="box">
         <!-- 添加或修改班级管理对话框 -->
-        <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
+        <el-dialog 
+            v-loading="loading"  
+            :element-loading-text="'正在' + title + '...'"
+            element-loading-spinner="el-icon-loading"
+            element-loading-background="rgba(0, 0, 0, 0.8)"
+            :title="title" 
+            :close-on-click-modal="false" 
+            :visible.sync="open" 
+            width="500px" 
+            append-to-body
+        >
             <el-form ref="Cform" :model="form" :rules="rules" label-width="80px">
                 <el-form-item label="所属学校" prop="schoolId">
                     <el-select 
@@ -61,6 +71,7 @@ export default {
     mixins: [Textyixia],
     data() {
         return {
+            loading: false,
             open: false,
             title: '',
             // 教师下拉框数据
@@ -110,9 +121,13 @@ export default {
         },
         submitForm() {
             this.$refs.Cform.validate(isOK=>{
+                clearTimeout(this.timer)
                 if(isOK){
                     console.log(this.form);
-            this.$emit('submit', this.form);
+                    this.loading = true
+                    this.timer = setTimeout(()=>{
+                        this.$emit('submit', this.form);
+                    },500)
                 }else{
                     console.log('====');
                 }
@@ -143,8 +158,6 @@ export default {
 }
 </script>
                 
-}
-</script>
 
 <style lang="scss" scoped>
 .box {