256 lines
9.3 KiB
Python
256 lines
9.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Final pass: apply all list-pagination + deleted_at filter changes."""
|
|
import re
|
|
|
|
# ============= LOADER =============
|
|
lp = '/Users/xujiaqian/GolandProjects/HDWL/data-engine/common/report/config/loader.go'
|
|
with open(lp) as f:
|
|
l = f.read()
|
|
|
|
# 1. GetAllBusinesses: status=ACTIVE -> deleted_at IS NULL
|
|
l = l.replace(
|
|
'"SELECT * FROM report_business_config WHERE status = $1 ORDER BY id ASC"',
|
|
'"SELECT * FROM report_business_config WHERE deleted_at IS NULL ORDER BY id ASC"'
|
|
)
|
|
l = l.replace('\t\tmodel.StatusActive)', ')\n\t\t// (removed status filter)')
|
|
|
|
# 2. GetAllReports: add deleted_at IS NULL (keep existing)
|
|
# Already has no status filter, just verify
|
|
# (The user already removed status filter from GetAllReports earlier)
|
|
|
|
# 3. GetFields: status=ACTIVE -> deleted_at IS NULL
|
|
l = l.replace(
|
|
'"SELECT * FROM report_field_config WHERE business_code = $1 AND report_code = $2 AND status = $3 ORDER BY sort_order ASC"',
|
|
'"SELECT * FROM report_field_config WHERE business_code = $1 AND report_code = $2 AND deleted_at IS NULL ORDER BY sort_order ASC"'
|
|
)
|
|
# Remove StatusActive arg from GetFields call
|
|
l = l.replace(
|
|
'businessCode, reportCode, model.StatusActive)',
|
|
'businessCode, reportCode)'
|
|
)
|
|
|
|
# 4. GetExtractConfigs: status=ACTIVE AND is_enabled=true -> deleted_at IS NULL
|
|
l = l.replace(
|
|
'"SELECT * FROM report_extract_config WHERE business_code = $1 AND report_code = $2 AND status = $3 AND is_enabled = $4"',
|
|
'"SELECT * FROM report_extract_config WHERE business_code = $1 AND report_code = $2 AND deleted_at IS NULL ORDER BY id ASC"'
|
|
)
|
|
l = l.replace(
|
|
'businessCode, reportCode, model.StatusActive, true)',
|
|
'businessCode, reportCode)'
|
|
)
|
|
|
|
# 5. Add GetAllFields (for edit query, returns all non-deleted regardless of status)
|
|
if 'GetAllFields' not in l:
|
|
# Add after GetFields function ends
|
|
marker = 'return fields, nil\n}\n\n// GetFieldMap 获取字段配置Map'
|
|
add = '''return fields, nil
|
|
}
|
|
|
|
// GetAllFields 获取报表全部字段(含 INACTIVE,不含已删除,用于编辑回显)
|
|
func (l *ConfigLoader) GetAllFields(ctx context.Context, businessCode, reportCode string) ([]model.FieldConfig, error) {
|
|
\tr, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT * FROM report_field_config WHERE business_code = $1 AND report_code = $2 AND deleted_at IS NULL ORDER BY sort_order ASC",
|
|
\t\tbusinessCode, reportCode)
|
|
\tif err != nil {
|
|
\t\treturn nil, err
|
|
\t}
|
|
\tvar fields []model.FieldConfig
|
|
\tfor _, record := range r {
|
|
\t\tvar f model.FieldConfig
|
|
\t\tif err := record.Struct(&f); err != nil {
|
|
\t\t\treturn nil, err
|
|
\t\t}
|
|
\t\tif f.ValidAggregates == nil {
|
|
\t\t\tf.ValidAggregates = []string{}
|
|
\t\t}
|
|
\t\tif f.FilterOperators == nil {
|
|
\t\t\tf.FilterOperators = []string{"=", "!=", ">", "<", ">=", "<=", "IN", "LIKE", "BETWEEN"}
|
|
\t\t}
|
|
\t\tfields = append(fields, f)
|
|
\t}
|
|
\treturn fields, nil
|
|
}
|
|
|
|
// GetFieldMap 获取字段配置Map'''
|
|
l = l.replace(marker, add, 1)
|
|
|
|
# 6. Add paginated list methods
|
|
# ListBusinesses
|
|
if 'ListBusinesses' not in l:
|
|
add = '''// ListBusinesses 分页获取业务列表
|
|
func (l *ConfigLoader) ListBusinesses(ctx context.Context, pageNum, pageSize int) ([]model.BusinessConfig, int, error) {
|
|
\ttotal, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT COUNT(*) AS cnt FROM report_business_config WHERE deleted_at IS NULL")
|
|
\tif err != nil {
|
|
\t\treturn nil, 0, err
|
|
\t}
|
|
\tcount := 0
|
|
\tif !total.IsEmpty() {
|
|
\t\tcount = total[0]["cnt"].Int()
|
|
\t}
|
|
\toffset := (pageNum - 1) * pageSize
|
|
\tr, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT * FROM report_business_config WHERE deleted_at IS NULL ORDER BY id ASC LIMIT $1 OFFSET $2",
|
|
\t\tpageSize, offset)
|
|
\tif err != nil {
|
|
\t\treturn nil, 0, err
|
|
\t}
|
|
\tvar businesses []model.BusinessConfig
|
|
\tfor _, record := range r {
|
|
\t\tvar biz model.BusinessConfig
|
|
\t\tif err := record.Struct(&biz); err != nil {
|
|
\t\t\treturn nil, 0, err
|
|
\t\t}
|
|
\t\tbusinesses = append(businesses, biz)
|
|
\t}
|
|
\treturn businesses, count, nil
|
|
}
|
|
|
|
// ListReports 分页获取报表列表
|
|
func (l *ConfigLoader) ListReports(ctx context.Context, businessCode string, pageNum, pageSize int) ([]model.ReportConfig, int, error) {
|
|
\ttotal, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT COUNT(*) AS cnt FROM report_report_config WHERE business_code = $1 AND deleted_at IS NULL",
|
|
\t\tbusinessCode)
|
|
\tif err != nil {
|
|
\t\treturn nil, 0, err
|
|
\t}
|
|
\tcount := 0
|
|
\tif !total.IsEmpty() {
|
|
\t\tcount = total[0]["cnt"].Int()
|
|
\t}
|
|
\toffset := (pageNum - 1) * pageSize
|
|
\tr, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT * FROM report_report_config WHERE business_code = $1 AND deleted_at IS NULL ORDER BY id ASC LIMIT $2 OFFSET $3",
|
|
\t\tbusinessCode, pageSize, offset)
|
|
\tif err != nil {
|
|
\t\treturn nil, 0, err
|
|
\t}
|
|
\tvar reports []model.ReportConfig
|
|
\tfor _, record := range r {
|
|
\t\tvar rpt model.ReportConfig
|
|
\t\tif err := record.Struct(&rpt); err != nil {
|
|
\t\t\treturn nil, 0, err
|
|
\t\t}
|
|
\t\treports = append(reports, rpt)
|
|
\t}
|
|
\treturn reports, count, nil
|
|
}
|
|
|
|
// ListExtractConfigs 分页获取抽取配置列表
|
|
func (l *ConfigLoader) ListExtractConfigs(ctx context.Context, businessCode, reportCode string, pageNum, pageSize int) ([]model.ExtractConfig, int, error) {
|
|
\ttotal, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT COUNT(*) AS cnt FROM report_extract_config WHERE business_code = $1 AND report_code = $2 AND deleted_at IS NULL",
|
|
\t\tbusinessCode, reportCode)
|
|
\tif err != nil {
|
|
\t\treturn nil, 0, err
|
|
\t}
|
|
\tcount := 0
|
|
\tif !total.IsEmpty() {
|
|
\t\tcount = total[0]["cnt"].Int()
|
|
\t}
|
|
\toffset := (pageNum - 1) * pageSize
|
|
\tr, err := gfdb.DB(ctx).GetAll(ctx,
|
|
\t\t"SELECT * FROM report_extract_config WHERE business_code = $1 AND report_code = $2 AND deleted_at IS NULL ORDER BY id ASC LIMIT $3 OFFSET $4",
|
|
\t\tbusinessCode, reportCode, pageSize, offset)
|
|
\tif err != nil {
|
|
\t\treturn nil, 0, err
|
|
\t}
|
|
\tvar configs []model.ExtractConfig
|
|
\tfor _, record := range r {
|
|
\t\tvar ec model.ExtractConfig
|
|
\t\tif err := record.Struct(&ec); err != nil {
|
|
\t\t\treturn nil, 0, err
|
|
\t\t}
|
|
\t\tconfigs = append(configs, ec)
|
|
\t}
|
|
\tl.mu.Lock()
|
|
\tl.extractCache[businessCode+":"+reportCode] = configs
|
|
\tl.mu.Unlock()
|
|
\treturn configs, count, nil
|
|
}
|
|
'''
|
|
# Insert before the "//" comment separator near the end
|
|
marker2 = '\n// GetReportFields 获取报表可用字段(按角色分类)'
|
|
if marker2 in l:
|
|
l = l.replace(marker2, add + marker2, 1)
|
|
else:
|
|
l += add
|
|
|
|
with open(lp, 'w') as f:
|
|
f.write(l)
|
|
print('✅ Loader')
|
|
|
|
# ============= SERVICE (api.go) =============
|
|
ap = '/Users/xujiaqian/GolandProjects/HDWL/data-engine/common/report/api.go'
|
|
with open(ap) as f:
|
|
a = f.read()
|
|
|
|
# Add ListBusinesses/ListReports/ListExtractConfigs service methods
|
|
if 'func (s *ReportService) ListBusinesses' not in a:
|
|
add_svc = '''
|
|
// ListBusinesses 分页获取业务列表
|
|
func (s *ReportService) ListBusinesses(ctx context.Context, pageNum, pageSize int) ([]model.BusinessConfig, int, error) {
|
|
\treturn s.configLoader.ListBusinesses(ctx, pageNum, pageSize)
|
|
}
|
|
|
|
// ListReports 分页获取报表列表
|
|
func (s *ReportService) ListReports(ctx context.Context, businessCode string, pageNum, pageSize int) ([]model.ReportConfig, int, error) {
|
|
\treturn s.configLoader.ListReports(ctx, businessCode, pageNum, pageSize)
|
|
}
|
|
|
|
// ListExtractConfigs 分页获取抽取配置列表
|
|
func (s *ReportService) ListExtractConfigs(ctx context.Context, businessCode, reportCode string, pageNum, pageSize int) ([]model.ExtractConfig, int, error) {
|
|
\treturn s.configLoader.ListExtractConfigs(ctx, businessCode, reportCode, pageNum, pageSize)
|
|
}
|
|
'''
|
|
# Insert after the service type definition
|
|
a = a.replace(
|
|
's.configLoader.GetAllBusinesses(ctx)\n}',
|
|
's.configLoader.GetAllBusinesses(ctx)\n}' + add_svc
|
|
)
|
|
|
|
with open(ap, 'w') as f:
|
|
f.write(a)
|
|
print('✅ Service')
|
|
|
|
# ============= CONTROLLER =============
|
|
cp = '/Users/xujiaqian/GolandProjects/HDWL/data-engine/controller/report/report_controller.go'
|
|
with open(cp) as f:
|
|
c = f.read()
|
|
|
|
# Fix ListBusinesses handler
|
|
c = c.replace(
|
|
'list, err := svc().GetAllBusinesses(ctx)',
|
|
'list, total, err := svc().ListBusinesses(ctx, req.PageNum, req.PageSize)'
|
|
)
|
|
c = c.replace(
|
|
'return &listBusinessesRes{List: list}, nil',
|
|
'totalPages := (total + req.PageSize - 1) / req.PageSize\n\t\treturn &listBusinessesRes{List: list, PageRes: model.PageRes{Total: total, Page: req.PageNum, PageSize: req.PageSize, TotalPages: totalPages}}, nil'
|
|
)
|
|
|
|
# Fix ListReports handler
|
|
c = c.replace(
|
|
'list, err := svc().GetAllReports(ctx, req.BusinessCode)',
|
|
'list, total, err := svc().ListReports(ctx, req.BusinessCode, req.PageNum, req.PageSize)'
|
|
)
|
|
c = c.replace(
|
|
'return &listReportsRes{List: list}, nil',
|
|
'totalPages := (total + req.PageSize - 1) / req.PageSize\n\t\treturn &listReportsRes{List: list, PageRes: model.PageRes{Total: total, Page: req.PageNum, PageSize: req.PageSize, TotalPages: totalPages}}, nil'
|
|
)
|
|
|
|
# Fix GetExtractConfigs handler
|
|
c = c.replace(
|
|
'list, err := svc().GetExtractConfigs(ctx, req.BusinessCode, req.ReportCode)',
|
|
'list, total, err := svc().ListExtractConfigs(ctx, req.BusinessCode, req.ReportCode, req.PageNum, req.PageSize)'
|
|
)
|
|
c = c.replace(
|
|
'return &getExtractConfigsRes{List: list}, nil',
|
|
'totalPages := (total + req.PageSize - 1) / req.PageSize\n\t\treturn &getExtractConfigsRes{List: list, PageRes: model.PageRes{Total: total, Page: req.PageNum, PageSize: req.PageSize, TotalPages: totalPages}}, nil'
|
|
)
|
|
|
|
with open(cp, 'w') as f:
|
|
f.write(c)
|
|
print('✅ Controller')
|
|
|
|
print('\nAll done!')
|