package service import ( "fmt" "os/exec" "strings" "billai-server/config" ) // CleanOptions 清洗选项 type CleanOptions struct { Year string // 年份筛选 Month string // 月份筛选 Start string // 起始日期 End string // 结束日期 Format string // 输出格式: csv/json } // CleanResult 清洗结果 type CleanResult struct { BillType string // 检测到的账单类型: alipay/wechat Output string // Python 脚本输出 } // RunCleanScript 执行 Python 清洗脚本 // inputPath: 输入文件路径 // outputPath: 输出文件路径 // opts: 清洗选项 func RunCleanScript(inputPath, outputPath string, opts *CleanOptions) (*CleanResult, error) { // 构建命令参数 cleanScriptAbs := config.ResolvePath(config.Global.CleanScript) args := []string{cleanScriptAbs, inputPath, outputPath} if opts != nil { if opts.Year != "" { args = append(args, "--year", opts.Year) } if opts.Month != "" { args = append(args, "--month", opts.Month) } if opts.Start != "" { args = append(args, "--start", opts.Start) } if opts.End != "" { args = append(args, "--end", opts.End) } if opts.Format != "" { args = append(args, "--format", opts.Format) } } // 执行 Python 脚本 fmt.Printf("🐍 执行清洗脚本...\n") pythonPathAbs := config.ResolvePath(config.Global.PythonPath) cmd := exec.Command(pythonPathAbs, args...) cmd.Dir = config.Global.ProjectRoot output, err := cmd.CombinedOutput() outputStr := string(output) if err != nil { return nil, fmt.Errorf("清洗脚本执行失败: %w\n输出: %s", err, outputStr) } // 从输出中检测账单类型 billType := DetectBillTypeFromOutput(outputStr) return &CleanResult{ BillType: billType, Output: outputStr, }, nil } // DetectBillTypeFromOutput 从 Python 脚本输出中检测账单类型 func DetectBillTypeFromOutput(output string) string { if strings.Contains(output, "支付宝") { return "alipay" } if strings.Contains(output, "微信") { return "wechat" } return "" }