fix(sync): 修复多设备同步后药瓶使用次数与余量未扣减的问题,改由全量流水动态精准核算
This commit is contained in:
+72
-25
@@ -224,32 +224,79 @@ class SyncHandler(SimpleHTTPRequestHandler):
|
||||
cid = c.get('id')
|
||||
if not cid:
|
||||
continue
|
||||
conn.execute('''
|
||||
INSERT INTO canisters
|
||||
(id, name, total_doses, remaining_doses, opened_at, is_enabled, puffs_per_session, inhalation_seconds, is_steroid, sort_order, note, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
remaining_doses = excluded.remaining_doses,
|
||||
is_enabled = excluded.is_enabled,
|
||||
puffs_per_session = excluded.puffs_per_session,
|
||||
inhalation_seconds = excluded.inhalation_seconds,
|
||||
updated_at = excluded.updated_at
|
||||
''', (
|
||||
cid,
|
||||
c.get('name', ''),
|
||||
c.get('totalDoses', 60),
|
||||
c.get('remainingDoses', 60),
|
||||
c.get('openedAt', now_str),
|
||||
1 if c.get('isEnabled', True) else 0,
|
||||
c.get('puffsPerSession', 2),
|
||||
c.get('inhalationSeconds', 15),
|
||||
1 if c.get('isSteroid', True) else 0,
|
||||
c.get('sortOrder', 1),
|
||||
c.get('note', ''),
|
||||
now_str
|
||||
))
|
||||
incoming_opened_at = c.get('openedAt', now_str)
|
||||
existing = conn.execute('SELECT * FROM canisters WHERE id = ?', (cid,)).fetchone()
|
||||
if not existing:
|
||||
conn.execute('''
|
||||
INSERT INTO canisters
|
||||
(id, name, total_doses, remaining_doses, opened_at, is_enabled, puffs_per_session, inhalation_seconds, is_steroid, sort_order, note, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
''', (
|
||||
cid,
|
||||
c.get('name', ''),
|
||||
c.get('totalDoses', 60),
|
||||
c.get('remainingDoses', 60),
|
||||
incoming_opened_at,
|
||||
1 if c.get('isEnabled', True) else 0,
|
||||
c.get('puffsPerSession', 2),
|
||||
c.get('inhalationSeconds', 15),
|
||||
1 if c.get('isSteroid', True) else 0,
|
||||
c.get('sortOrder', 1),
|
||||
c.get('note', ''),
|
||||
now_str
|
||||
))
|
||||
else:
|
||||
# 如果客户端开瓶时间更新(换了新药瓶),则更新开封时间戳
|
||||
server_opened = existing['opened_at'] or ''
|
||||
target_opened = incoming_opened_at if incoming_opened_at > server_opened else server_opened
|
||||
conn.execute('''
|
||||
UPDATE canisters SET
|
||||
name = ?,
|
||||
total_doses = ?,
|
||||
opened_at = ?,
|
||||
is_enabled = ?,
|
||||
puffs_per_session = ?,
|
||||
inhalation_seconds = ?,
|
||||
sort_order = ?,
|
||||
note = ?,
|
||||
updated_at = ?
|
||||
WHERE id = ?
|
||||
''', (
|
||||
c.get('name', existing['name']),
|
||||
c.get('totalDoses', existing['total_doses']),
|
||||
target_opened,
|
||||
1 if c.get('isEnabled', existing['is_enabled']) else 0,
|
||||
c.get('puffsPerSession', existing['puffs_per_session']),
|
||||
c.get('inhalationSeconds', existing['inhalation_seconds']),
|
||||
c.get('sortOrder', existing['sort_order']),
|
||||
c.get('note', existing['note']),
|
||||
now_str,
|
||||
cid
|
||||
))
|
||||
|
||||
# 3. 汇总拉取全量最新记录并返回给客户端
|
||||
# 3. 依据全量真实流水账本,严格重新核算药瓶剩余喷数(彻底避免不同设备同步导致余量被旧缓存覆盖)
|
||||
for can in conn.execute('SELECT * FROM canisters').fetchall():
|
||||
cid = can['id']
|
||||
is_steroid = can['is_steroid']
|
||||
total_d = can['total_doses']
|
||||
opened = can['opened_at'] or '1970-01-01'
|
||||
|
||||
if is_steroid:
|
||||
res = conn.execute(
|
||||
'SELECT SUM(flixotide_puffs) as s FROM dose_records WHERE start_time >= ?',
|
||||
(opened,)
|
||||
).fetchone()
|
||||
else:
|
||||
res = conn.execute(
|
||||
'SELECT SUM(ventolin_puffs) as s FROM dose_records WHERE start_time >= ?',
|
||||
(opened,)
|
||||
).fetchone()
|
||||
|
||||
consumed = (res['s'] or 0) if res else 0
|
||||
rem = max(0, total_d - consumed)
|
||||
conn.execute('UPDATE canisters SET remaining_doses = ? WHERE id = ?', (rem, cid))
|
||||
|
||||
# 4. 汇总拉取全量最新记录并返回给客户端
|
||||
all_records = []
|
||||
for row in conn.execute('SELECT * FROM dose_records ORDER BY start_time DESC LIMIT 200'):
|
||||
all_records.append({
|
||||
|
||||
Reference in New Issue
Block a user