init xiaoxiang project

This commit is contained in:
2026-04-15 18:12:30 +08:00
commit f539f1bece
160 changed files with 981970 additions and 0 deletions
+162
View File
@@ -0,0 +1,162 @@
# -*- coding: utf-8 -*-
"""
从 product_data 同步插入到 data_completed(字段按语义对应)。
源表无 s_id / station_id / shop_id / image / token 时,用下方常量或环境变量填充;
可按需修改 DEFAULT_* 或设置环境变量后再运行。
"""
from __future__ import annotations
import os
from decimal import Decimal
from typing import Any, Optional
import pymysql
from newsetting import All_Setting
# ---------- 与 data_completed 中缺失字段对应的默认值(请按实际业务修改) ----------
DEFAULT_S_ID = os.environ.get("ELEPHANT_S_ID", "")
DEFAULT_STATION_ID = os.environ.get("ELEPHANT_STATION_ID", "")
DEFAULT_SHOP_ID = os.environ.get("ELEPHANT_SHOP_ID", "")
DEFAULT_TOKEN = os.environ.get("ELEPHANT_TOKEN", "")
def _d(v: Any) -> Decimal:
if v is None:
return Decimal("0.00")
if isinstance(v, Decimal):
return v
try:
return Decimal(str(v))
except Exception:
return Decimal("0.00")
def _tiny_int_next_day_to_immediate(next_day: Any) -> int:
"""
product_data.next_day_delivery: 是否次日达 (0/1)
data_completed.is_immediate: 是否立即发货
简单约定:次日达则非立即发货 -> is_immediate = 0;否则为 1。
"""
if next_day is None:
return 1
try:
n = int(next_day)
except (TypeError, ValueError):
return 1
return 0 if n == 1 else 1
def row_to_completed(row: dict[str, Any]) -> tuple[Any, ...]:
"""将 SELECT 别名行转为插入元组。"""
next_day = row.get("next_day_delivery")
return (
row.get("work_id") or "",
row.get("sku_id") or "",
DEFAULT_S_ID,
DEFAULT_STATION_ID,
row.get("show_contents"),
row.get("one_id") or "",
row.get("one_name") or "",
row.get("label"),
None, # image
_tiny_int_next_day_to_immediate(next_day),
_d(row.get("origin_price")),
_d(row.get("sale_price")),
row.get("sale_type"),
row.get("two_name") or "",
DEFAULT_SHOP_ID,
row.get("two_id") or "",
row.get("two_name") or "",
row.get("shop_name") or "",
DEFAULT_TOKEN,
_d(row.get("member_price")),
)
INSERT_SQL = """
INSERT INTO data_completed (
work_id, goods_id, s_id, station_id, `desc`, f_id, f_name, goods_tag, image,
is_immediate, original_price, price, price_tag, s_name, shop_id, t_id, t_name,
title, token, vip_price
) VALUES (
%s, %s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s, %s, %s, %s, %s, %s,
%s, %s, %s
)
ON DUPLICATE KEY UPDATE
s_id = VALUES(s_id),
station_id = VALUES(station_id),
`desc` = VALUES(`desc`),
f_id = VALUES(f_id),
f_name = VALUES(f_name),
goods_tag = VALUES(goods_tag),
image = VALUES(image),
is_immediate = VALUES(is_immediate),
original_price = VALUES(original_price),
price = VALUES(price),
price_tag = VALUES(price_tag),
s_name = VALUES(s_name),
shop_id = VALUES(shop_id),
t_id = VALUES(t_id),
t_name = VALUES(t_name),
title = VALUES(title),
token = VALUES(token),
vip_price = VALUES(vip_price)
"""
def fetch_product_rows(
conn: pymysql.connections.Connection,
work_id: Optional[str] = None,
limit: Optional[int] = None,
) -> list[dict[str, Any]]:
sql = """
SELECT
work_id, sku_id, label, sale_type, show_contents, sale_price, member_price,
origin_price, one_name, one_id, two_name, two_id, next_day_delivery, shop_name
FROM product_data
"""
params: list[Any] = []
if work_id:
sql += " WHERE work_id = %s"
params.append(work_id)
sql += " ORDER BY id"
if limit is not None:
sql += " LIMIT %s"
params.append(int(limit))
with conn.cursor(pymysql.cursors.DictCursor) as cur:
cur.execute(sql, params)
return list(cur.fetchall())
def main() -> None:
# 与项目内其他脚本一致;可按需改 host/port/db
tent = All_Setting(host="183.6.7.91", port=33016, db="tk_test")
work_id_filter = os.environ.get("MIGRATE_WORK_ID", "").strip() or None
conn = pymysql.connect(
host=tent.host,
port=tent.port,
user="root",
password="Aliyun@7646!",
db=tent.db,
charset="utf8mb4",
)
try:
rows = fetch_product_rows(conn, work_id=work_id_filter)
if not rows:
print("无待迁移数据(请检查 product_data 或 MIGRATE_WORK_ID)。")
return
batch = [row_to_completed(r) for r in rows]
with conn.cursor() as cur:
cur.executemany(INSERT_SQL, batch)
conn.commit()
print(f"完成:写入/更新 {len(batch)} 条 -> data_completed(主键 work_id+goods_id)。")
finally:
conn.close()
if __name__ == "__main__":
main()