"""资源购买的有效价格与写事务准备;调用方负责提交/回滚。""" import datetime from tip_transaction import lock_wallet_accounts def effective_resource_price(item): price = item.get("price", 0) pending = item.get("pending_price") effective = item.get("pending_price_effective_at") if pending is not None and effective: when = datetime.datetime.fromisoformat(effective) now = datetime.datetime.now(when.tzinfo) if when.tzinfo else datetime.datetime.now() if now >= when: price = pending if type(price) is float and price.is_integer() and 0 <= price <= 2147483647: price = int(price) if isinstance(price, bool) or not isinstance(price, int) or price < 0: raise ValueError("invalid resource price") return price def lock_resource_purchase_wallets(session, buyer, seller, free): # 免费/作者本人仍需买方钱包行作为跨进程所有权查重的锁锚点。 accounts = (buyer,) if free else (buyer, seller) create = (buyer,) if free else (seller,) wallets = lock_wallet_accounts(session, accounts, create) return wallets[buyer], wallets.get(seller)