feat(order): 지오영 주문도 선별 주문 적용

- quick_order로 장바구니 담기 (auto_confirm 제거)
- submit_order_selective로 선별 주문
- 기존 품목 보존, 이번에 담은 것만 주문
This commit is contained in:
thug0bin 2026-03-06 22:00:50 +09:00
parent be95f8b3d1
commit 760aea6f89

View File

@ -287,33 +287,30 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
geo_session = get_geo_session()
# 1단계: 모든 품목을 장바구니에만 담기 (auto_confirm=False)
for item in items:
kd_code = item.get('kd_code') or item.get('drug_code')
order_qty = item['order_qty']
spec = item.get('specification', '')
try:
# 지오영 주문 실행
# cart_only=True: 장바구니만 (auto_confirm=False)
# cart_only=False: 주문 확정까지 (auto_confirm=True)
result = geo_session.full_order(
# 장바구니에만 담기 (주문 확정은 나중에 한번에)
result = geo_session.quick_order(
kd_code=kd_code,
quantity=order_qty,
specification=spec if spec else None,
check_stock=True,
auto_confirm=not cart_only, # cart_only면 확정 안함
memo=f"자동주문 - {item.get('product_name', '')}"
check_stock=True
)
if result.get('success'):
status = 'success'
result_code = 'CART_ADDED' if cart_only else 'OK'
result_message = '장바구니 추가 완료' if cart_only else result.get('message', '주문 완료')
result_code = 'CART_ADDED'
result_message = '장바구니 추가 완료'
success_count += 1
else:
status = 'failed'
result_code = result.get('error', 'UNKNOWN')
result_message = result.get('message', '주문 실패')
result_message = result.get('message', '장바구니 추가 실패')
failed_count += 1
except Exception as e:
@ -321,10 +318,14 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
result_code = 'ERROR'
result_message = str(e)
failed_count += 1
result = {}
update_item_result(item['id'], status, result_code, result_message)
# AI 학습용 컨텍스트 저장 (실제 주문)
# quick_order 결과에서 product_code 가져오기
product_code = result.get('product', {}).get('product_code') if result.get('success') else None
# AI 학습용 컨텍스트 저장
save_order_context(item['id'], {
'drug_code': item['drug_code'],
'product_name': item['product_name'],
@ -332,7 +333,9 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
'usage_7d': item.get('usage_qty', 0),
'ordered_spec': spec,
'ordered_qty': order_qty,
'selection_reason': 'user_order'
'selection_reason': 'user_order',
'wholesaler_id': 'geoyoung',
'product_code': product_code
})
results.append({
@ -343,32 +346,51 @@ def submit_geoyoung_order(order: dict, dry_run: bool, cart_only: bool = True) ->
'order_qty': order_qty,
'status': status,
'result_code': result_code,
'result_message': result_message
'result_message': result_message,
'product_code': product_code # 선별 주문용
})
# 주문 상태 업데이트
if cart_only:
# 장바구니만 담은 경우
# 2단계: cart_only=False면 선별 주문 (기존 품목 보존)
if not cart_only and success_count > 0:
try:
# 이번에 담은 품목의 product_code만 수집
ordered_codes = [r['product_code'] for r in results
if r['status'] == 'success' and r.get('product_code')]
if ordered_codes:
# 선별 주문: 기존 품목은 건드리지 않고, 이번에 담은 것만 주문
confirm_result = geo_session.submit_order_selective(ordered_codes)
if confirm_result.get('success'):
restored_info = f", 기존 {confirm_result.get('restored_count', 0)}개 복원" if confirm_result.get('restored_count', 0) > 0 else ""
update_order_status(order_id, 'submitted',
f'지오영 주문 확정 완료: {success_count}{restored_info}')
# 결과 메시지 업데이트
for r in results:
if r['status'] == 'success':
r['result_code'] = 'OK'
r['result_message'] = '주문 확정 완료'
else:
update_order_status(order_id, 'partial',
f'지오영 장바구니 담김, 확정 실패: {confirm_result.get("error", "알 수 없는 오류")}')
else:
update_order_status(order_id, 'partial',
f'지오영 장바구니 담김, product_code 없음')
except Exception as e:
logger.error(f"지오영 주문 확정 오류: {e}")
update_order_status(order_id, 'partial',
f'지오영 장바구니 담김, 확정 중 오류: {str(e)}')
elif success_count > 0:
# cart_only=True: 장바구니만
if failed_count == 0:
update_order_status(order_id, 'pending',
f'지오영 장바구니 추가 완료: {success_count}개 (사이트에서 확정 필요)')
elif success_count == 0:
update_order_status(order_id, 'failed',
f'장바구니 추가 실패: {failed_count}개 품목')
else:
update_order_status(order_id, 'partial',
f'부분 성공: {success_count}개 장바구니, {failed_count}개 실패')
else:
# 실제 주문까지 한 경우
if failed_count == 0:
update_order_status(order_id, 'submitted',
f'주문 제출 완료: {success_count}개 품목')
elif success_count == 0:
update_order_status(order_id, 'failed',
f'주문 실패: {failed_count}개 품목')
else:
update_order_status(order_id, 'partial',
f'부분 주문: {success_count}개 성공, {failed_count}개 실패')
f'지오영 주문 실패: {failed_count}')
return {
'success': True,