deploy-hxtesting / build-and-deploy (push) Successful in 1m18s
2.创建课程时课程默认是”线上“,发布课程时必须要有课件! 3.课程管理中的课件上传预览下载交互样式,按维护”试题“交互样式风格来!列表中增加一列”试题“,列表中”试题“、”课件“按实际情况只展示”有/无“即可!
182 lines
8.9 KiB
Go
182 lines
8.9 KiB
Go
package repository
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"academy/internal/types"
|
||
"academy/internal/types/interfaces"
|
||
)
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// 课程管理(课程资产主数据,tr_course_library,000151)。
|
||
// 月培训计划的「计划名称」从本表选择,选中后经 tr_courses.library_course_id 关联。
|
||
// ---------------------------------------------------------------------------
|
||
|
||
// ListCourseLibrary returns a page of course-library items with keyword filter
|
||
// (分类/类型等前端本地过滤,数据量小全量加载)。
|
||
func (r *trainingRepository) ListCourseLibrary(ctx context.Context, tenantID uint64, q *interfaces.TrainingListQuery) ([]*types.TrainingCourseLibrary, int64, error) {
|
||
// 列名统一以 tr_course_library 限定:JOIN users 后 tenant_id/updated_at 等
|
||
// 同名列不加限定会报 SQLSTATE 42702 歧义(与 ListCourses 同口径)。
|
||
tx := r.db.WithContext(ctx).Model(&types.TrainingCourseLibrary{}).Where("tr_course_library.tenant_id = ?", tenantID)
|
||
if q != nil {
|
||
if q.Keyword != "" {
|
||
tx = tx.Where("tr_course_library.name ILIKE ?", "%"+q.Keyword+"%")
|
||
}
|
||
// 数据范围过滤(handler 按调用方角色强制注入,000157):dept_id=课长/经理
|
||
// 本部门;related_user_id=无维护角色的用户仅见自己创建或授课的课程。
|
||
if q.DeptID != "" {
|
||
tx = tx.Where("tr_course_library.owner_dept_id = ?", q.DeptID)
|
||
}
|
||
if q.RelatedUserID != "" {
|
||
tx = tx.Where("tr_course_library.creator_id = ? OR tr_course_library.lecturer_id = ?", q.RelatedUserID, q.RelatedUserID)
|
||
}
|
||
// 学员目录口径(000160,scope=catalog):仅已发布 + 适合人员覆盖调用方。
|
||
// suitable='全部' 全员可见;000159 多部门口径:值按「、」拆分(逗号/分号
|
||
// 兼容归一)后与调用方部门名做成员匹配;未设置适合人员的存量课程沿用
|
||
// 原部门范围口径(所属部门为空=全员,否则须与调用方部门一致)。调用方
|
||
// 无部门档案时只见「全部」与未设置的课程。
|
||
if q.CatalogOnly {
|
||
tx = tx.Where("tr_course_library.publish_status = ?", "published")
|
||
tx = tx.Where(
|
||
"tr_course_library.suitable_members = ? "+
|
||
"OR (? <> '' AND ? = ANY(string_to_array(replace(replace(replace(replace(tr_course_library.suitable_members, ',', '、'), ',', '、'), ';', '、'), ';', '、'), '、'))) "+
|
||
"OR (tr_course_library.suitable_members = '' AND (tr_course_library.owner_dept_id = '' "+
|
||
"OR (? <> '' AND tr_course_library.owner_dept_id = ?)))",
|
||
"全部", q.CatalogDeptName, q.CatalogDeptName, q.CatalogDeptID, q.CatalogDeptID,
|
||
)
|
||
}
|
||
}
|
||
var total int64
|
||
if err := tx.Count(&total).Error; err != nil {
|
||
return nil, 0, err
|
||
}
|
||
page, pageSize := 1, 100
|
||
if q != nil {
|
||
if q.Page > 0 {
|
||
page = q.Page
|
||
}
|
||
if q.PageSize > 0 {
|
||
pageSize = q.PageSize
|
||
}
|
||
}
|
||
var items []*types.TrainingCourseLibrary
|
||
// lecturer_name 联查真实姓名(讲师可为任意员工,外部讲师直接落姓名列);
|
||
// owner_dept_name 联查部门名(所属部门存 org_depts id);
|
||
// creator_name 联查创建人姓名(display_name 优先,回退 username 工号);
|
||
// question_bank_count 关联题库数(相关子查询,软删不计;列表「试题」有/无)。
|
||
err := tx.Select("tr_course_library.*, COALESCE(NULLIF(lu.display_name, ''), lu.username, '') AS lecturer_name, COALESCE(od.name, '') AS owner_dept_name, COALESCE(NULLIF(cu.display_name, ''), cu.username, '') AS creator_name, "+
|
||
"(SELECT COUNT(*) FROM tr_question_banks qb WHERE qb.library_course_id = tr_course_library.id AND qb.tenant_id = tr_course_library.tenant_id AND qb.deleted_at IS NULL) AS question_bank_count").
|
||
Joins("LEFT JOIN users lu ON lu.id = tr_course_library.lecturer_id").
|
||
Joins("LEFT JOIN org_depts od ON od.id = tr_course_library.owner_dept_id AND od.tenant_id = tr_course_library.tenant_id AND od.deleted_at IS NULL").
|
||
Joins("LEFT JOIN users cu ON cu.id = tr_course_library.creator_id").
|
||
Order("tr_course_library.updated_at DESC").
|
||
Offset((page - 1) * pageSize).Limit(pageSize).
|
||
Find(&items).Error
|
||
return items, total, err
|
||
}
|
||
|
||
// FindCourseLibraryByName fetches one course-library item by exact course name
|
||
// (需求侧课程自动匹配,000158:按课程名称精准查找,同名存量取最早一条)。
|
||
func (r *trainingRepository) FindCourseLibraryByName(ctx context.Context, tenantID uint64, name string) (*types.TrainingCourseLibrary, error) {
|
||
var item types.TrainingCourseLibrary
|
||
if err := r.db.WithContext(ctx).
|
||
Where("tenant_id = ? AND name = ?", tenantID, name).
|
||
Order("created_at ASC").
|
||
First(&item).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return &item, nil
|
||
}
|
||
|
||
// GetCourseLibraryItem fetches one course-library item.
|
||
func (r *trainingRepository) GetCourseLibraryItem(ctx context.Context, tenantID uint64, id string) (*types.TrainingCourseLibrary, error) {
|
||
var item types.TrainingCourseLibrary
|
||
if err := r.db.WithContext(ctx).
|
||
Select("tr_course_library.*, COALESCE(NULLIF(lu.display_name, ''), lu.username, '') AS lecturer_name, COALESCE(od.name, '') AS owner_dept_name, COALESCE(NULLIF(cu.display_name, ''), cu.username, '') AS creator_name").
|
||
Joins("LEFT JOIN users lu ON lu.id = tr_course_library.lecturer_id").
|
||
Joins("LEFT JOIN org_depts od ON od.id = tr_course_library.owner_dept_id AND od.tenant_id = tr_course_library.tenant_id AND od.deleted_at IS NULL").
|
||
Joins("LEFT JOIN users cu ON cu.id = tr_course_library.creator_id").
|
||
Where("tr_course_library.tenant_id = ? AND tr_course_library.id = ?", tenantID, id).First(&item).Error; err != nil {
|
||
return nil, err
|
||
}
|
||
return &item, nil
|
||
}
|
||
|
||
// CreateCourseLibraryItem inserts one course-library item.
|
||
func (r *trainingRepository) CreateCourseLibraryItem(ctx context.Context, item *types.TrainingCourseLibrary) error {
|
||
return r.db.WithContext(ctx).Create(item).Error
|
||
}
|
||
|
||
// UpdateCourseLibraryItem updates mutable course-library fields (file columns included:
|
||
// 课件文件上传后随行更新,清空即置空)。
|
||
func (r *trainingRepository) UpdateCourseLibraryItem(ctx context.Context, item *types.TrainingCourseLibrary) error {
|
||
return r.db.WithContext(ctx).Model(&types.TrainingCourseLibrary{}).
|
||
Where("tenant_id = ? AND id = ?", item.TenantID, item.ID).
|
||
Updates(map[string]interface{}{
|
||
"name": item.Name,
|
||
"owner_dept_id": item.OwnerDeptID,
|
||
"description": item.Description,
|
||
"suitable_members": item.SuitableMembers,
|
||
"category": item.Category,
|
||
"type": item.Type,
|
||
"mode": item.Mode,
|
||
"lecturer_id": item.LecturerID,
|
||
"lecturer_external": item.LecturerExternal,
|
||
"hours": item.Hours,
|
||
"material_name": item.MaterialName,
|
||
"file_key": item.FileKey,
|
||
"file_name": item.FileName,
|
||
"file_size": item.FileSize,
|
||
// 课件知识库文档 id(上传同步后回填;code 编号列只读不在更新列)。
|
||
"knowledge_id": item.KnowledgeID,
|
||
// 核验状态(000157):编辑/换课件后由服务层重置为待核验并清空核验痕迹。
|
||
"verify_status": item.VerifyStatus,
|
||
"confirmed_by": item.ConfirmedBy,
|
||
"confirmed_at": item.ConfirmedAt,
|
||
}).Error
|
||
}
|
||
|
||
// ConfirmCourseLibraryItems 批量核验课程条目(000157):仅待核验行置为已确认并
|
||
// 回填核验人/时间(幂等:已确认行不重复更新),返回实际核验行数。
|
||
func (r *trainingRepository) ConfirmCourseLibraryItems(ctx context.Context, tenantID uint64, ids []string, verifierID string) (int64, error) {
|
||
if len(ids) == 0 {
|
||
return 0, nil
|
||
}
|
||
tx := r.db.WithContext(ctx).Model(&types.TrainingCourseLibrary{}).
|
||
Where("tenant_id = ? AND id IN ? AND verify_status = ?", tenantID, ids, types.CourseLibraryVerifyPending).
|
||
Updates(map[string]interface{}{
|
||
"verify_status": types.CourseLibraryVerifyConfirmed,
|
||
"confirmed_by": verifierID,
|
||
"confirmed_at": time.Now(),
|
||
"updated_at": time.Now(),
|
||
})
|
||
return tx.RowsAffected, tx.Error
|
||
}
|
||
|
||
// UpdateCourseLibraryPublishStatus 更新课程管理条目发布状态(000154):
|
||
// published 已发布 / offline 已下线,供培训中心「和信课程」目录过滤。
|
||
func (r *trainingRepository) UpdateCourseLibraryPublishStatus(ctx context.Context, tenantID uint64, id, status string) error {
|
||
return r.db.WithContext(ctx).Model(&types.TrainingCourseLibrary{}).
|
||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||
Updates(map[string]interface{}{
|
||
"publish_status": status,
|
||
"updated_at": time.Now(),
|
||
}).Error
|
||
}
|
||
|
||
// BatchCreateCourseLibrary inserts course-library items in one batch (Excel 导入)。
|
||
func (r *trainingRepository) BatchCreateCourseLibrary(ctx context.Context, items []*types.TrainingCourseLibrary) error {
|
||
if len(items) == 0 {
|
||
return nil
|
||
}
|
||
return r.db.WithContext(ctx).Create(&items).Error
|
||
}
|
||
|
||
// DeleteCourseLibraryItem soft-deletes one course-library item.
|
||
func (r *trainingRepository) DeleteCourseLibraryItem(ctx context.Context, tenantID uint64, id string) error {
|
||
return r.db.WithContext(ctx).
|
||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||
Delete(&types.TrainingCourseLibrary{}).Error
|
||
}
|