Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
233 lines
8.3 KiB
Go
233 lines
8.3 KiB
Go
package service
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"academy/internal/assets/trainingtemplates"
|
|
"academy/internal/errors"
|
|
"academy/internal/types"
|
|
"github.com/xuri/excelize/v2"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 配置中心 · 导入模板管理:
|
|
// 模板按业务键(demand_collection/question_bank)管理,租户上传的自定义版本存
|
|
// tr_templates 并全端生效;未上传或「恢复默认」后下载回退二进制内置文件
|
|
// (internal/assets/trainingtemplates,源 docs/moban)。
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// templateMaxSize 模板 Excel 上限(10MB,业务模板均为几十 KB 级,防误传大文件)。
|
|
const templateMaxSize = 10 << 20
|
|
|
|
// ListTrainingTemplates returns metadata of all managed templates,
|
|
// merging tenant custom versions over bundled defaults.
|
|
func (s *trainingService) ListTrainingTemplates(ctx context.Context, tenantID uint64) ([]*types.TrainingTemplateMeta, error) {
|
|
customs := map[string]*types.TrainingTemplate{}
|
|
if tpl, err := s.repo.GetTrainingTemplate(ctx, tenantID, types.TrainingTemplateDemandCollection); err != nil {
|
|
return nil, err
|
|
} else if tpl != nil {
|
|
customs[tpl.TemplateKey] = tpl
|
|
}
|
|
if tpl, err := s.repo.GetTrainingTemplate(ctx, tenantID, types.TrainingTemplateQuestionBank); err != nil {
|
|
return nil, err
|
|
} else if tpl != nil {
|
|
customs[tpl.TemplateKey] = tpl
|
|
}
|
|
|
|
keys := []string{types.TrainingTemplateDemandCollection, types.TrainingTemplateQuestionBank}
|
|
out := make([]*types.TrainingTemplateMeta, 0, len(keys))
|
|
for _, key := range keys {
|
|
meta := &types.TrainingTemplateMeta{
|
|
TemplateKey: key,
|
|
Source: "default",
|
|
}
|
|
if name, ok := trainingtemplates.DefaultFileName(key); ok {
|
|
meta.FileName = name
|
|
}
|
|
if data, err := trainingtemplates.Default(key); err == nil && data != nil {
|
|
meta.FileSize = int64(len(data))
|
|
}
|
|
if custom := customs[key]; custom != nil {
|
|
meta.FileName = custom.FileName
|
|
meta.FileSize = custom.FileSize
|
|
meta.UpdatedAt = custom.UpdatedAt
|
|
meta.UpdatedBy = custom.UpdatedBy
|
|
meta.Source = "custom"
|
|
}
|
|
out = append(out, meta)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// TrainingTemplateFile resolves the effective template bytes:
|
|
// tenant custom version first, bundled default otherwise.
|
|
// 收集模板(demand_collection)在返回前注入组织架构下拉(见 enrichDemandTemplate)。
|
|
func (s *trainingService) TrainingTemplateFile(ctx context.Context, tenantID uint64, key string) ([]byte, string, error) {
|
|
if !trainingtemplates.IsKnown(key) {
|
|
return nil, "", errors.NewBadRequestError("未知模板类型")
|
|
}
|
|
fileName, _ := trainingtemplates.DefaultFileName(key)
|
|
var data []byte
|
|
if tpl, err := s.repo.GetTrainingTemplate(ctx, tenantID, key); err != nil {
|
|
return nil, "", err
|
|
} else if tpl != nil && len(tpl.FileContent) > 0 {
|
|
data = tpl.FileContent
|
|
if tpl.FileName != "" {
|
|
fileName = tpl.FileName
|
|
}
|
|
} else {
|
|
var err error
|
|
data, err = trainingtemplates.Default(key)
|
|
if err != nil || data == nil {
|
|
return nil, "", errors.NewBadRequestError("模板文件缺失")
|
|
}
|
|
}
|
|
if key == types.TrainingTemplateDemandCollection {
|
|
data = s.enrichDemandTemplate(ctx, tenantID, data)
|
|
}
|
|
return data, fileName, nil
|
|
}
|
|
|
|
// demandTemplateOptionCols 收集模板下拉辅助列(明细表内右侧隐藏列,
|
|
// 同表引用规避跨表公式与 255 字符内联列表限制)。
|
|
const (
|
|
demandOptColDept = "CA" // 部门单位(组织架构部门)
|
|
demandOptColJoin = "CB" // 参加人员(员工档案)
|
|
demandOptColLect = "CC" // 培训讲师(员工档案)
|
|
demandTargetRows = 500 // 下拉覆盖的数据行数(第 4~503 行)
|
|
demandMemberLimit = 2000 // 员工档案下拉取值上限
|
|
)
|
|
|
|
// enrichDemandTemplate 给教育训练需求表模板(内置默认/自定义 xlsx 版)注入
|
|
// 组织架构下拉:
|
|
// - 「部门」(B 列)下拉=组织架构部门;
|
|
// - 「参加人员」(D 列)「讲师」(I 列)下拉=员工档案人员姓名(联查 users 优先取
|
|
// display_name 真实姓名,回退 username 工号;去重去空);
|
|
//
|
|
// 取值写入同表隐藏辅助列(CA/CB/CC),以数据验证下拉呈现(数据区 4~503 行)。
|
|
// 基础文件不可解析(如旧 .xls 自定义版)或数据源失败时原样返回,不阻断下载。
|
|
func (s *trainingService) enrichDemandTemplate(ctx context.Context, tenantID uint64, base []byte) []byte {
|
|
f, err := excelize.OpenReader(bytes.NewReader(base))
|
|
if err != nil {
|
|
return base
|
|
}
|
|
sheets := f.GetSheetList()
|
|
if len(sheets) == 0 {
|
|
return base
|
|
}
|
|
sheet := sheets[0]
|
|
|
|
// 下拉取值:组织架构部门 + 员工档案人员(去重、去空)
|
|
deptNames := []string{}
|
|
if depts, err := s.org.ListDepts(ctx, tenantID); err == nil {
|
|
for _, d := range depts {
|
|
if d != nil && strings.TrimSpace(d.Name) != "" {
|
|
deptNames = append(deptNames, strings.TrimSpace(d.Name))
|
|
}
|
|
}
|
|
}
|
|
empNames := []string{}
|
|
if members, _, err := s.org.ListEmployees(ctx, tenantID, types.OrgModuleTraining, &types.OrgListQuery{
|
|
Page: 1, PageSize: demandMemberLimit,
|
|
}); err == nil {
|
|
seen := map[string]bool{}
|
|
for _, m := range members {
|
|
if m == nil {
|
|
continue
|
|
}
|
|
name := strings.TrimSpace(m.UserName)
|
|
if name == "" || seen[name] {
|
|
continue
|
|
}
|
|
seen[name] = true
|
|
empNames = append(empNames, name)
|
|
}
|
|
}
|
|
|
|
// 写隐藏辅助列并以数据验证下拉挂到对应列。
|
|
// 下拉为非阻断式(ShowErrorMessage=false,NewDataValidation 默认):单元格可
|
|
// 自由填写——参加人员可填多人(、/,分隔),讲师可填系统外姓名。
|
|
// prompt 为选中单元格时浮出的输入提示,引导多选与外部讲师填写方式。
|
|
applyList := func(optCol, header string, values []string, targetRange string, prompt string) {
|
|
if len(values) == 0 {
|
|
return
|
|
}
|
|
if err := f.SetCellValue(sheet, optCol+"1", header); err != nil {
|
|
return
|
|
}
|
|
for i, v := range values {
|
|
if err := f.SetCellValue(sheet, fmt.Sprintf("%s%d", optCol, i+2), v); err != nil {
|
|
return
|
|
}
|
|
}
|
|
// 隐藏辅助列(excelize v2 用 SetColVisible=false 表达隐藏)
|
|
_ = f.SetColVisible(sheet, optCol, false)
|
|
dv := excelize.NewDataValidation(true)
|
|
dv.SetSqref(targetRange)
|
|
dv.Type = "list"
|
|
dv.Formula1 = fmt.Sprintf("$%s$2:$%s$%d", optCol, optCol, len(values)+1)
|
|
if prompt != "" {
|
|
dv.SetInput(header, prompt)
|
|
}
|
|
_ = f.AddDataValidation(sheet, dv)
|
|
}
|
|
applyList(demandOptColDept, "部门(组织架构)", deptNames, fmt.Sprintf("B4:B%d", demandTargetRows+3), "")
|
|
applyList(demandOptColJoin, "参加人员(员工档案)", empNames,
|
|
fmt.Sprintf("D4:D%d", demandTargetRows+3), "可从下拉选择,也可直接填写多个姓名,用、或,分隔")
|
|
applyList(demandOptColLect, "培训讲师(员工档案)", empNames,
|
|
fmt.Sprintf("I4:I%d", demandTargetRows+3), "可从下拉选择系统员工,也可直接填写外部讲师姓名")
|
|
|
|
buf, err := f.WriteToBuffer()
|
|
if err != nil {
|
|
return base
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// SaveTrainingTemplate stores a tenant custom template (content-type validated upstream).
|
|
func (s *trainingService) SaveTrainingTemplate(ctx context.Context, tenantID uint64, key, fileName string, data []byte, updaterID string) (*types.TrainingTemplateMeta, error) {
|
|
if !trainingtemplates.IsKnown(key) {
|
|
return nil, errors.NewBadRequestError("未知模板类型")
|
|
}
|
|
if len(data) == 0 {
|
|
return nil, errors.NewBadRequestError("模板文件为空")
|
|
}
|
|
if len(data) > templateMaxSize {
|
|
return nil, errors.NewBadRequestError(fmt.Sprintf("模板文件超过 %dMB 上限", templateMaxSize>>20))
|
|
}
|
|
if fileName == "" {
|
|
fileName, _ = trainingtemplates.DefaultFileName(key)
|
|
}
|
|
tpl := &types.TrainingTemplate{
|
|
TenantID: tenantID,
|
|
TemplateKey: key,
|
|
FileName: fileName,
|
|
FileSize: int64(len(data)),
|
|
FileContent: data,
|
|
UpdatedBy: updaterID,
|
|
}
|
|
if err := s.repo.UpsertTrainingTemplate(ctx, tpl); err != nil {
|
|
return nil, err
|
|
}
|
|
return &types.TrainingTemplateMeta{
|
|
TemplateKey: key,
|
|
FileName: tpl.FileName,
|
|
FileSize: tpl.FileSize,
|
|
UpdatedAt: time.Now(),
|
|
UpdatedBy: updaterID,
|
|
Source: "custom",
|
|
}, nil
|
|
}
|
|
|
|
// ResetTrainingTemplate deletes the custom template so downloads fall back to default.
|
|
func (s *trainingService) ResetTrainingTemplate(ctx context.Context, tenantID uint64, key, operatorID string) error {
|
|
if !trainingtemplates.IsKnown(key) {
|
|
return errors.NewBadRequestError("未知模板类型")
|
|
}
|
|
return s.repo.DeleteTrainingTemplate(ctx, tenantID, key)
|
|
}
|