forked from wangziqi/gongxue-base
refactor: replace UserDingMapping with StudentDingMapping entity
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { DataSource, Repository } from 'typeorm';
|
||||
import { SyncLog, SyncState, UserDingMapping, ClassStudent, ClassTeacher, Department } from '../entities';
|
||||
import { SyncLog, SyncState, StudentDingMapping, ClassStudent, ClassTeacher, Department } from '../entities';
|
||||
import { Class as ClassEntity } from '../entities/class.entity';
|
||||
import type { SyncPlatform, SyncType, SyncStatus } from '../entities/sync-log.entity';
|
||||
import { DingTalkService } from '../integration/dingtalk.service';
|
||||
@@ -30,8 +30,8 @@ export class SyncService {
|
||||
private readonly syncLogRepo: Repository<SyncLog>,
|
||||
@InjectRepository(SyncState)
|
||||
private readonly syncStateRepo: Repository<SyncState>,
|
||||
@InjectRepository(UserDingMapping)
|
||||
private readonly mappingRepo: Repository<UserDingMapping>,
|
||||
@InjectRepository(StudentDingMapping)
|
||||
private readonly studentDingMappingRepo: Repository<StudentDingMapping>,
|
||||
@InjectRepository(User)
|
||||
private readonly userRepo: Repository<User>,
|
||||
@InjectRepository(Student)
|
||||
@@ -123,7 +123,7 @@ export class SyncService {
|
||||
/**
|
||||
* 从钉钉导入用户:roleId 非 null → 老师(User + 指定角色),roleId null → 学生(User + Student)。
|
||||
* 支持同时创建班级并建立师生关联。
|
||||
* 已存在 UserDingMapping 的记录跳过。
|
||||
* 已存在 StudentDingMapping 的记录跳过。
|
||||
*/
|
||||
async importDingTalkUsers(
|
||||
users: ImportUserDto[],
|
||||
@@ -190,7 +190,7 @@ export class SyncService {
|
||||
|
||||
// 2. 导入用户(逐用户)
|
||||
for (const u of users) {
|
||||
const existingMapping = await manager.findOne(UserDingMapping, {
|
||||
const existingMapping = await manager.findOne(StudentDingMapping, {
|
||||
where: { dingUserId: u.dingUserId },
|
||||
});
|
||||
|
||||
@@ -199,7 +199,7 @@ export class SyncService {
|
||||
|
||||
if (existingMapping) {
|
||||
skipped++;
|
||||
userId = existingMapping.userId;
|
||||
userId = existingMapping.studentId;
|
||||
// 判断是老师还是学生:查角色
|
||||
const existingUser = await manager.findOne(User, {
|
||||
where: { id: userId },
|
||||
@@ -258,15 +258,13 @@ export class SyncService {
|
||||
}
|
||||
|
||||
// 钉钉映射(幂等:可能已被 syncAll 创建)
|
||||
const existingMappingForUser = await manager.findOne(UserDingMapping, {
|
||||
const existingMappingForUser = await manager.findOne(StudentDingMapping, {
|
||||
where: { dingUserId: u.dingUserId },
|
||||
});
|
||||
if (!existingMappingForUser) {
|
||||
const mapping = manager.create(UserDingMapping, {
|
||||
const mapping = manager.create(StudentDingMapping, {
|
||||
dingUserId: u.dingUserId,
|
||||
userId: user.id,
|
||||
dingName: u.name,
|
||||
dingMobile: u.mobile,
|
||||
studentId: user.id,
|
||||
});
|
||||
await manager.save(mapping);
|
||||
}
|
||||
@@ -422,7 +420,7 @@ export class SyncService {
|
||||
const end = endDate.toISOString().slice(0, 10);
|
||||
|
||||
this.logger.log(`Importing DingTalk attendance: ${start} ~ ${end}`);
|
||||
const mappings = await this.mappingRepo.find();
|
||||
const mappings = await this.studentDingMappingRepo.find();
|
||||
const userIds = mappings.map((m) => m.dingUserId);
|
||||
const importResult = await this.attendanceImportService.importFromDingTalk({
|
||||
startDate: start,
|
||||
|
||||
Reference in New Issue
Block a user