deploy-hxtesting / build-and-deploy (push) Failing after 0s
核验状态(verify_status,迁移 000157): - 新字段 pending 待核验(初始)/confirmed 已确认,附核验人/核验时间审计列,存量数据回填待核验 - 新增/导入/编辑(含重新上传课件)一律重置为待核验,原核验结论作废 - 发布前置校验:课程发布、月培训计划「发布/保存并发布」关联课程必须已确认,否则友好提示核验入口 核验/删除权限(仅本部门课长/经理): - 新端点 POST /training/course-library/verify 单个/批量共用,越权/已确认行跳过不阻断 - 核验与删除严格按课长身份所在部门收窄,人事/讲师/学员/空间管理员均无跨部门操作权 - 删除仅限待核验状态(已确认课程不可删,须走核验流程) 列表数据范围与页签: - 课程管理列表按角色注入过滤:人事/空间管理员全部,课长/经理本部门,讲师/学员仅「与我相关」(本人创建或本人授课) - 课程管理页签对学员开放;无维护角色用户仅可编辑本人创建/授课课程(不可新增) 前端: - 发布状态「草稿」文案改为「待发布」;新增「状态」列固定右侧置于操作列前 - 列表勾选 + 批量核验/批量删除(仅本部门待核验行提交);发布按钮前置核验校验未核验友好提示;按钮按角色/部门/状态动态显隐
152 lines
7.0 KiB
Go
152 lines
7.0 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)
|
|
}
|
|
}
|
|
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 工号)。
|
|
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").
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|