# -*- coding:utf-8 -*-
# 作者：齐振鋆
# 日期：2023/2/15
# 描述:
import logging
import os
from datetime import datetime

import allure
import pytest
from ddt import ddt, file_data

from cases.UI.GuKeYiTiHua import PROJECTNAME
from component.GuKeYiTiHua.StorageManage.StorageIn.warehouse_accept import WarehouseVerify
from component.GuKeYiTiHua.StorageManage.StorageIn.warehouse_up import WarehouseUp
from component.GuKeYiTiHua.StorageManage.StorageOut.storage_out_integration import StorageOut
from component.GuKeYiTiHua.StorageManage.StorageOut.warehouse_pick import WarehousePick
from component.GuKeYiTiHua.SurgeryFollow.SurgeryBack.apolegamy_back import ApolegamyBack
from component.GuKeYiTiHua.SurgeryFollow.SurgeryBack.apolegamy_out import ApolegamyOut
from component.GuKeYiTiHua.SurgeryFollow.SurgeryBack.orthopedic_take import OrthopedicTake
from component.GuKeYiTiHua.SurgeryFollow.SurgeryBack.surgery_back import SurgeryBack
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.apolegamy_borrow import ApolegamyBorrow
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.apolegamy_take import ApolegamyTake
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.orthopedic_lend import OrthopedicLend
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.out_ledger import OutLedger
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.surgery_distribute import SurgeryDistribute
from component.GuKeYiTiHua.SurgeryFollow.SurgeryLend.surgery_require import SurgeryRequire
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
@allure.epic(PROJECTNAME)
@allure.feature('手术跟台模块')
class TestSurgeryFollow(object):
    dd = DataDic()

    def setup_class(self):
        self.dbq = DBQuery()
        self.common = CommonElement()
        self.sr = SurgeryRequire()
        self.sd = SurgeryDistribute()
        self.ol = OrthopedicLend()
        self.wp = WarehousePick()
        self.so = StorageOut()
        self.ole = OutLedger()
        self.ab = ApolegamyBorrow()
        self.at = ApolegamyTake()

        self.sb = SurgeryBack()
        self.ot = OrthopedicTake()

        self.wa = WarehouseVerify()
        self.wu = WarehouseUp()

        self.abk = ApolegamyBack()
        self.aot = ApolegamyOut()

    @allure.story('手术跟台流程')
    @allure.title('借出-还回-销售')
    @allure.severity(allure.severity_level.BLOCKER)
    @allure.description('测试时间：{}'.format(datetime.now()))
    @pytest.mark.surgery
    @pytest.mark.run(order=10)
    @file_data('{}/data/{}TestData.yaml'.format(os.getcwd(), dd.get_value('module')))
    # @file_data(dd.get_value('test_data_file'))
    def test_surgery_normal(self, **kwargs):
        with allure.step('测试数据准备...'):
            log.info(kwargs)
            self.dd.set_value('test_data_info', kwargs)
            goods = kwargs['goods']
            process_type = kwargs['process_type']

        if process_type == 1:
            with allure.step('手术请领单'):
                self.sr.surgery_require_menu_open()
                self.sr.surgery_require_create()
                ql_code = self.dd.get_value('ql_code')
                self.sr.surgery_require_search('已办', ql_code)
                self.common.close_tap()

            with allure.step('请领单配货'):
                self.sd.surgery_distribute_menu_open()
                self.sd.surgery_distribute_search(order_code=ql_code)
                self.sd.surgery_require_distribute(order_code=ql_code)
                self.sd.surgery_distribute_search(tap_name='已办', order_code=ql_code)
                self.common.close_tap()

        with allure.step('手术借出单'):
            self.ol.orthopedic_lend_menu_open()
            if process_type == 1:
                self.ol.orthopedic_lend_search(ot='QL', order_code=ql_code)
                self.ol.orthopedic_lend_edit_submit()
            else:
                self.ol.orthopedic_lend_create()
            jc_code = self.dd.get_value('jc_code')
            self.ol.orthopedic_lend_search(order_code=jc_code)
            self.ol.orthopedic_lend_transact(jc_code)
            self.ol.orthopedic_lend_search('已办', order_code=jc_code)
            self.common.close_tap()

        with allure.step("仓库：发货-拣货-复核"):
            self.so.storage_out_integration(jc_code, len(goods))

        with allure.step("借出单出库台账"):
            self.ole.open_menu_out_ledger()
            self.ole.out_ledger_search(order_code=jc_code)
            self.common.close_tap()

        with allure.step("选配类借入单"):
            self.ab.open_menu_apolegamy_borrow()
            self.ab.apolegamy_borrow_search(order_code=jc_code)
            self.ab.get_apolegamy_borrow_code(jc_code)
            self.common.close_tap()

        with allure.step("选配类收货单"):
            self.at.open_menu_apolegamy_take()
            jr_code = self.dd.get_value('jr_code')
            self.at.apolegamy_take_search(order_code=jr_code)
            self.common.close_tap()

        with allure.step("手术跟台还回单"):
            self.sb.open_menu_surgery_back()
            self.sb.surgery_back_search(order_code=jc_code)
            self.sb.get_surgery_back_code(jc_code)
            self.common.close_tap()

        with allure.step("骨科仓归还收货"):
            self.ot.open_menu_orthopedic_take()
            follow = kwargs['follow']
            self.ot.orthopedic_take_create(None, jc_code, follow)
            self.ot.orthopedic_take_search(tap_name='已办', order_code=self.dd.get_value('hhsh_code'))
            self.common.close_tap()

        with allure.step("仓库：验收"):
            self.wa.open_verify_menu()
            hh_code = self.dd.get_value('hh_code')
            self.wa.verify_edit_and_confirm(order_code=hh_code, order_type='HH') if follow == 'N' else 0
            self.wa.verify_order_search('已办', order_code=hh_code)
            self.common.close_tap()

        with allure.step("仓库：上架"):
            self.wu.open_up_menu()
            self.wu.up_confirm(order_code=hh_code) if follow == 'N' else 0
            self.wu.up_order_search('已办', order_code=hh_code)
            self.common.close_tap()

        with allure.step("选配类还回出库"):
            self.abk.open_menu_apolegamy_back()
            self.abk.apolegamy_back_search(order_code=hh_code)
            self.abk.get_apolegamy_out_code(order_code=hh_code)
            self.common.close_tap()

        with allure.step("选配类还回出库单出库"):
            self.aot.open_menu_apolegamy_out()
            self.aot.apolegamy_out_search(order_code=self.dd.get_value('fh_code'))
            self.common.close_tap()

        # 连台新流程
        if follow == 'Y':
            with allure.step("连台：请领单"):
                self.sr.surgery_require_menu_open()
                new_ql_code = self.dbq.get_follow_ql_code(hh_code)
                log.info('连台生成请领单号：【{}】'.format(new_ql_code))
                self.sr.surgery_require_search('已办', new_ql_code)
                self.common.close_tap()

            with allure.step("连台：借出单"):
                self.ol.orthopedic_lend_menu_open()
                self.ol.orthopedic_lend_search(ot='QL', order_code=new_ql_code)
                self.ol.orthopedic_lend_edit_submit(follow)
                self.common.close_tap()

            with allure.step("连台：仓库：拣货"):
                self.wp.open_menu_warehouse_pick()
                self.wp.pick_order_search(tap_name='待办', order_code=self.dd.get_value('new_jc_code'))
                self.common.close_tap()

        # 选配类转销售单
