#!/usr/bin/env python3 """Add pagination to all list endpoints + change status filter to deleted_at.""" # ========== 1. Controller: request/response structs + handlers ========== ctrl_path = '/Users/xujiaqian/GolandProjects/HDWL/data-engine/controller/report/report_controller.go' with open(ctrl_path) as f: ctrl = f.read() # 1a. listBusinessesReq - add PageReq embed ctrl = ctrl.replace( 'type listBusinessesReq struct {\n\t\tg.Meta `path:"/businesses" method:"get" tags:"报表引擎" summary:"业务列表"`\n\t}', 'type listBusinessesReq struct {\n\t\tg.Meta `path:"/businesses" method:"get" tags:"报表引擎" summary:"业务列表"`\n\t\tmodel.PageReq\n\t}' ) # 1b. listBusinessesRes - add PageRes embed + rename List ctrl = ctrl.replace( 'type listBusinessesRes struct {\n\t\tList []model.BusinessConfig `json:"list"`\n\t}', 'type listBusinessesRes struct {\n\t\tList []model.BusinessConfig `json:"list"`\n\t\tmodel.PageRes\n\t}' ) # 1c. ListBusinesses handler - pass page params + calc TotalPages ctrl = ctrl.replace( '\t\tlist, err := svc().GetAllBusinesses(ctx)', '\t\tlist, total, err := svc().GetAllBusinesses(ctx, req.PageNum, req.PageSize)' ) ctrl = ctrl.replace( '\t\treturn &listBusinessesRes{List: list}, nil', '\t\ttotalPages := (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' ) # 1d. listReportsReq - add PageReq embed ctrl = ctrl.replace( 'type listReportsReq struct {\n\t\tg.Meta `path:"/reports" method:"get" tags:"报表引擎" summary:"报表列表"`\n\t\tBusinessCode string `json:"businessCode" v:"required"`\n\t}', 'type listReportsReq struct {\n\t\tg.Meta `path:"/reports" method:"get" tags:"报表引擎" summary:"报表列表"`\n\t\tBusinessCode string `json:"businessCode" v:"required"`\n\t\tmodel.PageReq\n\t}' ) # 1e. listReportsRes - add PageRes embed ctrl = ctrl.replace( 'type listReportsRes struct {\n\t\tList []model.ReportConfig `json:"list"`\n\t}', 'type listReportsRes struct {\n\t\tList []model.ReportConfig `json:"list"`\n\t\tmodel.PageRes\n\t}' ) # 1f. ListReports handler ctrl = ctrl.replace( '\t\tlist, err := svc().GetAllReports(ctx, req.BusinessCode)', '\t\tlist, total, err := svc().GetAllReports(ctx, req.BusinessCode, req.PageNum, req.PageSize)' ) ctrl = ctrl.replace( '\t\treturn &listReportsRes{List: list}, nil', '\t\ttotalPages := (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' ) # 1g. getExtractConfigsReq - add PageReq embed ctrl = ctrl.replace( 'type getExtractConfigsReq struct {\n\t\tg.Meta `path:"/extractConfigs" method:"get" tags:"报表引擎" summary:"抽取配置列表"`\n\t\tBusinessCode string `json:"businessCode" v:"required"`\n\t\tReportCode string `json:"reportCode" v:"required"`\n\t}', 'type getExtractConfigsReq struct {\n\t\tg.Meta `path:"/extractConfigs" method:"get" tags:"报表引擎" summary:"抽取配置列表"`\n\t\tBusinessCode string `json:"businessCode" v:"required"`\n\t\tReportCode string `json:"reportCode" v:"required"`\n\t\tmodel.PageReq\n\t}' ) # 1h. getExtractConfigsRes - add PageRes embed ctrl = ctrl.replace( 'type getExtractConfigsRes struct {\n\t\tList []model.ExtractConfig `json:"list"`\n\t}', 'type getExtractConfigsRes struct {\n\t\tList []model.ExtractConfig `json:"list"`\n\t\tmodel.PageRes\n\t}' ) # 1i. GetExtractConfigs handler ctrl = ctrl.replace( '\t\tlist, err := svc().GetExtractConfigs(ctx, req.BusinessCode, req.ReportCode)', '\t\tlist, total, err := svc().GetExtractConfigs(ctx, req.BusinessCode, req.ReportCode, req.PageNum, req.PageSize)' ) ctrl = ctrl.replace( '\t\treturn &getExtractConfigsRes{List: list}, nil', '\t\ttotalPages := (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(ctrl_path, 'w') as f: f.write(ctrl) print('✅ Controller done') # ========== 2. API service layer ========== api_path = '/Users/xujiaqian/GolandProjects/HDWL/data-engine/common/report/api.go' with open(api_path) as f: api = f.read() # 2a. GetAllBusinesses api = api.replace( 'func (s *ReportService) GetAllBusinesses(ctx context.Context) ([]model.BusinessConfig, error) {\n\t\treturn s.configLoader.GetAllBusinesses(ctx)', 'func (s *ReportService) GetAllBusinesses(ctx context.Context, pageNum, pageSize int) ([]model.BusinessConfig, int, error) {\n\t\treturn s.configLoader.GetAllBusinesses(ctx, pageNum, pageSize)' ) # 2b. GetAllReports api = api.replace( 'func (s *ReportService) GetAllReports(ctx context.Context, businessCode string) ([]model.ReportConfig, error) {\n\t\treturn s.configLoader.GetAllReports(ctx, businessCode)', 'func (s *ReportService) GetAllReports(ctx context.Context, businessCode string, pageNum, pageSize int) ([]model.ReportConfig, int, error) {\n\t\treturn s.configLoader.GetAllReports(ctx, businessCode, pageNum, pageSize)' ) # 2c. GetExtractConfigs api = api.replace( 'func (s *ReportService) GetExtractConfigs(ctx context.Context, businessCode, reportCode string) ([]model.ExtractConfig, error) {\n\t\treturn s.configLoader.GetExtractConfigs(ctx, businessCode, reportCode)', 'func (s *ReportService) GetExtractConfigs(ctx context.Context, businessCode, reportCode string, pageNum, pageSize int) ([]model.ExtractConfig, int, error) {\n\t\treturn s.configLoader.GetExtractConfigs(ctx, businessCode, reportCode, pageNum, pageSize)' ) with open(api_path, 'w') as f: f.write(api) print('✅ Service done') # ========== 3. Loader layer ========== loader_path = '/Users/xujiaqian/GolandProjects/HDWL/data-engine/common/report/config/loader.go' with open(loader_path) as f: loader = f.read() # 3a. GetAllBusinesses - add deleted_at + pagination old_biz = '''func (l *ConfigLoader) GetAllBusinesses(ctx context.Context) ([]model.BusinessConfig, error) { \tr, err := gfdb.DB(ctx).GetAll(ctx, \t\t"SELECT * FROM report_business_config WHERE status = $1 ORDER BY id ASC", \t\tmodel.StatusActive) \tif err != nil { \t\treturn nil, 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, err \t\t} \t\tbusinesses = append(businesses, biz) \t} \treturn businesses, nil }''' new_biz = '''func (l *ConfigLoader) GetAllBusinesses(ctx context.Context, pageNum, pageSize int) ([]model.BusinessConfig, int, error) { \t// 查询总数 \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 }''' loader = loader.replace(old_biz, new_biz, 1) # 3b. GetAllReports - add deleted_at + pagination old_rpt = '''func (l *ConfigLoader) GetAllReports(ctx context.Context, businessCode string) ([]model.ReportConfig, error) { \tr, err := gfdb.DB(ctx).GetAll(ctx, \t\t"SELECT * FROM report_report_config WHERE business_code = $1 ORDER BY id ASC", \t\tbusinessCode) \tif err != nil { \t\treturn nil, 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, err \t\t} \t\treports = append(reports, rpt) \t} \treturn reports, nil }''' new_rpt = '''func (l *ConfigLoader) GetAllReports(ctx context.Context, businessCode string, pageNum, pageSize int) ([]model.ReportConfig, int, error) { \t// 查询总数 \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 }''' loader = loader.replace(old_rpt, new_rpt, 1) # 3c. GetExtractConfigs - add deleted_at + pagination old_ec = '''func (l *ConfigLoader) GetExtractConfigs(ctx context.Context, businessCode, reportCode string) ([]model.ExtractConfig, error) { \tr, err := gfdb.DB(ctx).GetAll(ctx, \t\t"SELECT * FROM report_extract_config WHERE business_code = $1 AND report_code = $2 AND status = $3 AND is_enabled = $4", \t\tbusinessCode, reportCode, model.StatusActive, true) \tif err != nil { \t\treturn nil, 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, err \t\t} \t\tconfigs = append(configs, ec) \t} \tl.mu.Lock() \tl.extractCache[businessCode+":"+reportCode] = configs \tl.mu.Unlock() \treturn configs, nil }''' new_ec = '''func (l *ConfigLoader) GetExtractConfigs(ctx context.Context, businessCode, reportCode string, pageNum, pageSize int) ([]model.ExtractConfig, int, error) { \t// 查询总数 \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} \treturn configs, count, nil }''' loader = loader.replace(old_ec, new_ec, 1) with open(loader_path, 'w') as f: f.write(loader) print('✅ Loader done')