#!/usr/bin/env python3 """把机械文件里金额 setter 的实参包成 Money.of(...)(括号配平),并补 import。 仅用于不含金额算术公式的文件。""" import os, re BASE = "/Users/qiu/Desktop/ERP/oa-backend/src/main/java/com/kaidi/oa" SETTERS = [ "setAmount", "setPaidAmount", "setInvoicedAmount", "setBudget", "setAppliedAmount", "setGrantedAmount", "setControlPrice", "setBidAmount", "setDeposit", "setTaxAmount", "setTotal", "setAnnualFee", "setBudgetAmount", "setActualAmount", "setBalance", "setAmountBefore", "setAmountAfter", "setBudgetTotal", "setActualTotal", "setMaterialCost", "setLaborCost", "setOverheadCost", "setTotalStandard", "setActualCost", "setVariance", "setUnitPrice", "setFundAmount", ] FILES = [ "web/BudgetController.java", "web/PaymentController.java", "web/RdProjectController.java", "web/OpportunityController.java", "web/ContractController.java", "web/BidController.java", "web/PolicyApplicationController.java", ] def find_call(s, start): """返回 (open_idx, close_idx, setter) 第一个未包装的金额 setter 调用,从 start 起。""" best = None for setter in SETTERS: i = s.find("." + setter + "(", start) if i != -1 and (best is None or i < best[0]): best = (i, setter) if best is None: return None i, setter = best open_idx = s.index("(", i) depth, j = 0, open_idx while j < len(s): if s[j] == "(": depth += 1 elif s[j] == ")": depth -= 1 if depth == 0: return (open_idx, j, setter) j += 1 return None def wrap_file(path): s = open(path, encoding="utf-8").read() out = [] pos = 0 count = 0 while True: call = find_call(s, pos) if call is None: out.append(s[pos:]) break open_idx, close_idx, setter = call inner = s[open_idx + 1:close_idx] out.append(s[pos:open_idx + 1]) if inner.strip().startswith("Money.of(") and inner.strip().endswith(")"): out.append(inner) # 已包装,跳过 else: out.append("Money.of(" + inner + ")") count += 1 out.append(")") pos = close_idx + 1 res = "".join(out) if count and "import com.kaidi.oa.common.Money;" not in res: res = re.sub(r"(package [^\n]+\n)", r"\1\nimport com.kaidi.oa.common.Money;\n", res, count=1) if res != s: open(path, "w", encoding="utf-8").write(res) return count def main(): for f in FILES: p = os.path.join(BASE, f) if not os.path.exists(p): print(f" !! 缺 {f}") continue n = wrap_file(p) print(f"{f}: 包装 {n} 处金额 setter") if __name__ == "__main__": main()