# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/22
# 描述: 采购OP
import os
from datetime import datetime
from time import sleep

import pytest
import allure
import logging

from ddt import ddt, file_data

from component.GuKeYiTiHua.StorageManage.StorageIn.warehouse_accept import WarehouseVerify
from component.GuKeYiTiHua.StorageManage.StorageIn.warehouse_take import WarehouseTake
from component.GuKeYiTiHua.StorageManage.StorageIn.warehouse_up import WarehouseUp
from component.GuKeYiTiHua.StorageManage.StorageInfo.serial_storage import SerialStorage
from component.GuKeYiTiHua.StorageManage.StorageOut.warehouse_check import WarehouseCheck
from component.GuKeYiTiHua.StorageManage.StorageOut.warehouse_delivery import WarehouseDelivery
from component.GuKeYiTiHua.StorageManage.StorageOut.warehouse_pick import WarehousePick
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.orthopedic_lend import OrthopedicLend
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.surgery_distribute import SurgeryDistribute
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.surgery_require import SurgeryRequire
from component.GuKeYiTiHua.purchaseManage.purchaseIn.purchase_op import PurchaseOP
from page.GuKeYiTiHua.DBQuery.db_query import DBQuery
from page.GuKeYiTiHua.Public.common import CommonElement
from unit.public.DataDic import DataDic

log = logging.getLogger(__name__)


@pytest.mark.incremental
@ddt
class TestPurchaseOrder(object):
    dd = DataDic()

    def setup_class(self):
        self.dbq = DBQuery()
        self.common = CommonElement()
        self.po = PurchaseOP()
        self.wt = WarehouseTake()
        self.wa = WarehouseVerify()
        self.wu = WarehouseUp()
        self.ss = SerialStorage()

    @allure.feature('采购管理')
    @allure.story('采购OP')
    @allure.title('测试标题')
    # @pytest.mark.skip(reason='调试通过')
    @allure.description('测试时间：{}'.format(datetime.now()))
    @pytest.mark.purchase
    @pytest.mark.run(order=10)
    @file_data('{}/data/{}TestData.yaml'.format(os.getcwd(), dd.get_value('module')))
    def test_purchase_op(self, **kwargs):
        with allure.step("测试数据准备..."):
            log.info(kwargs)
            company = self.dd.get_value('company')
            warehouse = self.dd.get_value('warehouse')
            if company is not None and warehouse is not None:
                kwargs['company'] = company
                kwargs['warehouse'] = warehouse
            elif (company is None and warehouse is not None) or (company is not None and warehouse is None):
                pytest.fail("公司及仓库参数需同时传入.")
            cust_good = self.dd.get_value('custGood')
            if cust_good is not None:
                kwargs['goods'] = [[cust_good, int(self.dd.get_value('custNum'))]]
            self.dd.set_value('test_data_info', kwargs)
            purchase_op_code = None

        with allure.step("采购订单（OP）"):
            self.po.open_menu_purchase_op()
            self.po.purchase_op_create(purchase_op_code)
            purchase_op_code = self.dd.get_value('purchase_op_code')
            self.po.purchase_op_transact(purchase_op_code)
            self.common.close_tap()

        with allure.step("仓库：收货"):
            self.wt.take_menu_open()
            self.wt.take_pool_take(purchase_op_code)
            self.wt.take_confirm(purchase_op_code)
            self.common.close_tap()

        with allure.step("仓库：验收"):
            self.wa.open_verify_menu()
            self.wa.verify_edit_and_confirm(purchase_op_code)
            self.common.close_tap()

        with allure.step("仓库：上架"):
            self.wu.open_up_menu()
            self.wu.up_confirm(purchase_op_code)
            self.common.close_tap()

        with allure.step("序列号库存查询"):
            self.ss.open_menu_serial_storage()
            total = 0
            for good in kwargs['goods']:
                if self.dbq.get_serial_flag(warehouse, good[0]) == 'Y':
                    total += good[1]
            self.ss.serial_storage_search(text=purchase_op_code, count=total)
            self.common.close_tap()
