Commit 833c2aa6 by 章家宝

fixbug(common_equit):修复门店性质的取值逻辑

1 parent 30a54edc
...@@ -1257,41 +1257,111 @@ class CommonEquityProcessor: ...@@ -1257,41 +1257,111 @@ class CommonEquityProcessor:
if r[0]: if r[0]:
deleted_flag_map[r[0]] = str(r[1] or '').strip() deleted_flag_map[r[0]] = str(r[1] or '').strip()
# 数据源 2:sale_close_store_record 最新一条状态(1 待关店 / 3 已关店) # 数据源 2:sale_close_store_record 关店状态
# 生产库为 MySQL 5.7,不支持 ROW_NUMBER() 等窗口函数, # Java 逻辑(SaleCloseStoreService.getCloseStoreStatusByShopMerchantIds):
# 故改用「自连接取 MAX(updated_date)」的写法;同一门店在同一时间点 # - status=1(待关店):只查上周一到上周日的记录
# 既有待关店又有已关店记录时,取 MAX(status)=3(已关店)优先。 # - status=3(已关店):查所有记录
# - 同一门店取 updated_date 最新的记录
# 计算上周一和上周日的日期
today = date.today()
last_week_end = today - timedelta(days=today.weekday() + 1) # 上周日
last_week_start = last_week_end - timedelta(days=6) # 上周一
close_status_map = {} close_status_map = {}
with eng.connect() as conn: with eng.connect() as conn:
sql = sa_text( # status=3(已关店):查所有数据
"SELECT r.shop_merchant_id, MAX(r.status) AS status " sql_closed = sa_text(
"SELECT r.shop_merchant_id, r.status "
"FROM sale_close_store_record r " "FROM sale_close_store_record r "
"JOIN ( " "JOIN ( "
" SELECT shop_merchant_id, MAX(updated_date) AS max_updated_date " " SELECT shop_merchant_id, MAX(updated_date) AS max_updated_date "
" FROM sale_close_store_record " " FROM sale_close_store_record "
" WHERE deleted_flag = 'N' AND status IN (1, 3) " " WHERE deleted_flag = 'N' AND status = 3 "
f" AND shop_merchant_id IN ({ph}) " f" AND shop_merchant_id IN ({ph}) "
" GROUP BY shop_merchant_id " " GROUP BY shop_merchant_id "
") m ON m.shop_merchant_id = r.shop_merchant_id " ") m ON m.shop_merchant_id = r.shop_merchant_id "
" AND m.max_updated_date = r.updated_date " " AND m.max_updated_date = r.updated_date "
"WHERE r.deleted_flag = 'N' AND r.status IN (1, 3) " "WHERE r.deleted_flag = 'N' AND r.status = 3"
"GROUP BY r.shop_merchant_id"
) )
for r in conn.execute(sql, p).fetchall(): closed_map = {}
for r in conn.execute(sql_closed, p).fetchall():
if r[0]: if r[0]:
close_status_map[r[0]] = int(r[1] or 0) closed_map[r[0]] = 3
# 数据源 3:是否有订单 # status=1(待关店):只查上周一到上周日的记录
with_order = set() p2, ph2 = _in_params(merchant_ids, 'm')
sql_pending = sa_text(
"SELECT r.shop_merchant_id, r.status "
"FROM sale_close_store_record r "
"JOIN ( "
" SELECT shop_merchant_id, MAX(updated_date) AS max_updated_date "
" FROM sale_close_store_record "
" WHERE deleted_flag = 'N' AND status = 1 "
f" AND shop_merchant_id IN ({ph2}) "
" AND updated_date >= :pending_start AND updated_date <= :pending_end "
" GROUP BY shop_merchant_id "
") m ON m.shop_merchant_id = r.shop_merchant_id "
" AND m.max_updated_date = r.updated_date "
"WHERE r.deleted_flag = 'N' AND r.status = 1"
)
p2['pending_start'] = datetime.combine(last_week_start, datetime.min.time())
p2['pending_end'] = datetime.combine(last_week_end, datetime.max.time())
pending_map = {}
for r in conn.execute(sql_pending, p2).fetchall():
if r[0]:
pending_map[r[0]] = 1
# 合并:已关店优先(与 Java 取 MAX(status) 逻辑一致)
for mid in merchant_ids:
if mid in closed_map:
close_status_map[mid] = 3
elif mid in pending_map:
close_status_map[mid] = 1
# 数据源 3:是否有订单(主门店 + 下级商户,与 Java 逻辑一致)
# 先查下级商户(子门店)
sub_merchant_map = {} # {主门店ID: [子门店ID列表]}
with eng.connect() as conn: with eng.connect() as conn:
sql = sa_text( sql = sa_text(
"SELECT DISTINCT merchant_id FROM dj_car_life_goods_order " "SELECT merchant_id, sub_merchant_id "
"FROM dj_car_life_diversion_merchant_ref "
"WHERE deleted_flag = 'N' " "WHERE deleted_flag = 'N' "
f" AND merchant_id IN ({ph})" f" AND merchant_id IN ({ph})"
) )
for r in conn.execute(sql, p).fetchall(): for r in conn.execute(sql, p).fetchall():
if r[0]: if r[0] and r[1]:
with_order.add(r[0]) sub_merchant_map.setdefault(r[0], []).append(r[1])
# 收集所有需要检查订单的门店ID(主门店 + 所有子门店)
all_merchant_ids_for_order = set(merchant_ids)
for sub_ids in sub_merchant_map.values():
all_merchant_ids_for_order.update(sub_ids)
# 查询有订单的门店
p2, ph2 = _in_params(list(all_merchant_ids_for_order), 'm')
with_order = set()
if p2 is not None:
with eng.connect() as conn:
sql = sa_text(
"SELECT DISTINCT merchant_id FROM dj_car_life_goods_order "
"WHERE deleted_flag = 'N' "
f" AND merchant_id IN ({ph2})"
)
order_merchant_ids = set()
for r in conn.execute(sql, p2).fetchall():
if r[0]:
order_merchant_ids.add(r[0])
# 判断每个主门店是否有订单(主门店或任一子门店有订单即可)
for mid in merchant_ids:
if mid in order_merchant_ids:
with_order.add(mid)
else:
# 检查子门店
for sub_id in sub_merchant_map.get(mid, []):
if sub_id in order_merchant_ids:
with_order.add(mid)
break
eng.dispose() eng.dispose()
return deleted_flag_map, close_status_map, with_order return deleted_flag_map, close_status_map, with_order
...@@ -1500,7 +1570,6 @@ class CommonEquityProcessor: ...@@ -1500,7 +1570,6 @@ class CommonEquityProcessor:
week_lights = [LIGHT_RED] * len(week_labels) week_lights = [LIGHT_RED] * len(week_labels)
return { return {
'empty': '',
'special': '非专项', 'special': '非专项',
'province': store['province'], 'province': store['province'],
'director': store['director'], 'director': store['director'],
...@@ -2217,9 +2286,8 @@ class CommonEquityProcessor: ...@@ -2217,9 +2286,8 @@ class CommonEquityProcessor:
# 红黄门店 # 红黄门店
# ------------------------------------------------------------ # ------------------------------------------------------------
# A 列为空列,B~Z 共 25 个字段;W1 起的周列数量随月份变化,追加在末尾 # B~Z 共 25 个字段;W1 起的周列数量随月份变化,追加在末尾
RED_YELLOW_COLUMNS = [ RED_YELLOW_COLUMNS = [
('', 'empty', 'text'),
('对应专项', 'special', 'text'), ('对应专项', 'special', 'text'),
('省份', 'province', 'text'), ('省份', 'province', 'text'),
('区域运营', 'director', 'text'), ('区域运营', 'director', 'text'),
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!