# -*- coding: utf-8 -*-

import allure
import logging

from zhyy.library.BusinessKw.SZPurchase.PurchaseOrderManage import PurchaseOrder


@allure.feature('深圳采购工作台采购订单页面')
class Test_purchase_order(object):
    test_case = PurchaseOrder()

    def teardown_method(self):
        logging.info("-----------------------------End-------------------------------")

    @allure.story("验证采购工作台采购订单页面列表查询")
    def test_check_purchase_order_page(self):
        purchase_order_code = 'PO251209048' # 采购单号 必填 
        supplier_company_ids = ['334'] # 供应商id 非必填
        payment_status = '0' # 付款状态 非必填
        status = '0' # 采购单状态 非必填
        page_no = 1 # 页码 必填
        page_size = 10 # 每页条数 必填
        response_data = self.test_case.kw_zhyy_get_purchase_page_post(
            note="采购工作台采购订单页面列表查询", 
            user='purchase',
            order_sn=purchase_order_code,
            supplier_company_ids=supplier_company_ids,
            payment_status=payment_status,
            status=status,
            page_no=page_no,
            page_size=page_size
        )
        
        # 断言检查
        assert response_data is not None, "响应数据不能为空"
        assert 'code' in response_data, "响应数据中缺少code字段"
        assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
            response_data.get('code'), response_data.get('msg', '未知错误'))
        assert 'data' in response_data, "响应数据中缺少data字段"
        assert response_data['data'] is not None, "响应数据中的data字段不能为空"
        
        # 如果传入了采购单号，检查返回的数据中是否包含该采购单号
        if purchase_order_code:
            data = response_data.get('data', {})
            order_found = False
            
            # 检查返回的数据结构，可能包含列表或其他结构
            if isinstance(data, dict):
                # 如果data是字典，可能包含records、list、data等字段
                records = data.get('records') or data.get('list') or data.get('data') or []
                if isinstance(records, list) and len(records) > 0:
                    # 检查列表中是否包含指定的采购单号
                    for item in records:
                        if isinstance(item, dict):
                            order_sn = item.get('order_sn') or item.get('orderSn') or item.get('orderSn')
                            if order_sn == purchase_order_code:
                                order_found = True
                                break
                elif isinstance(data, list):
                    # 如果data本身就是列表
                    for item in data:
                        if isinstance(item, dict):
                            order_sn = item.get('order_sn') or item.get('orderSn') or item.get('orderSn')
                            if order_sn == purchase_order_code:
                                order_found = True
                                break
            
            if order_found:
                logging.info("✓ 断言通过：返回的数据中包含采购单号 {}".format(purchase_order_code))
            else:
                logging.warning("⚠ 警告：返回的数据中未找到采购单号: {}，但接口调用成功".format(purchase_order_code))
        
        logging.info("✓ 所有断言检查通过")
        print("✓ 查询成功，响应数据: {}".format(response_data))