import datetime
import logging
import threading
import time
import traceback

import os
import sys

cur_path = os.path.abspath(os.path.dirname(__file__))  # 获取当前文件的目录
proj_path = cur_path[:cur_path.find('testcase')]  # 获取根目录
sys.path.append(proj_path)

# from airtest.core.api import *
# from selenium.webdriver import DesiredCapabilities
# from airtest_selenium.proxy import *

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import Chrome, ChromeOptions
from airtest.core.api import *
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from public.mail1 import send_email1
from public.tools import *

# opt.headless = False  # 把 Chrome 设置成无界面模式，windows/Linux 皆可
# opt.add_argument('--headless')# 把 Chrome 设置成无界面模式

# opt = ChromeOptions()  # 创建 Chrome 参数对象
# opt.add_argument('--start-maximized')
# opt.add_argument('--window-size=1280x1024')
# chrome = webdriver.Chrome(options=opt)

chrome = webdriver.Chrome()
chrome.maximize_window()
wait = WebDriverWait(chrome, 20, poll_frequency=0.2)

SIT_URL = 'http://scdev.cmic.com.cn:8088/beta/'  # SIT环境地址
# 通行证
usr1, pwd1 = 'bj_01', 'Gyxc1234'
usr2, pwd2 = 'bj_02', 'Gyxc1234'
usr3, pwd3 = 'bj_03', 'Gyxc1234'
usr4, pwd4 = 'bj_04', 'Gyxc1234'
usr5, pwd5 = 'bj_05', 'Gyxc1234'
warehousename = '北京强生非WMS仓'  # 仓库名称
contractnumber = 41007  # 合同号
salesman = '杨春刚'  # 业务员名称
productcode = 10156326  # 商品编码
productdate = '2023-10-01'  # 生产日期
expirdate = '2026-09-30'  # 失效日期
errmsg1 = '流程一：采购_采退流程异常，请检查！'  # 流程异常提示信息


class LoginSys():  ###登录
    def loginsys(self, url, usr, pwd):
        chrome.get(url)
        sleep(2)
        chrome.find_element(By.ID, 'input1').clear()
        #chrome.find_element_by_id('input1').clear()
        chrome.find_element_by_id('input1').send_keys(usr)
        chrome.find_element_by_id('input2').clear()
        chrome.find_element_by_id('input2').send_keys(pwd)
        chrome.find_element_by_id('input2').send_keys(Keys.ENTER)
        locator = (By.XPATH, '//span[text()="退出"]')
        wait.until(ec.presence_of_element_located(locator))
        # while True:
        #     try:
        #         if(chrome.find_element_by_xpath('//span[text()="退出"]').is_displayed()==True):
        #             break
        #     except:
        #         continue


class PurchaseOrder():  ###普通采购单界面操作
    def data_add_purchase(self):  # 填写表单数据
        chrome.implicitly_wait(10)
        ###打开普通采购单###
        order_link = chrome.find_element_by_xpath('//span[text()="普通采购订单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(2)
        # 点击新建按钮
        chrome.find_element_by_xpath('//span[text()="新建"]').click()
        sleep(4)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(2)

        ###获取订单号###
        global orderno1
        orderno1 = chrome.find_element_by_xpath('//div[text() = "订单号 "]/following-sibling::div/span').text
        print('普通采购单号为:', orderno1)

        ###填写仓库信息###
        # 点击仓库名称，输入仓库名并查询出结果
        chrome.find_element_by_xpath(
            '//div[text()="仓库名称 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:仓库名称,仓库编码"]').send_keys(warehousename)
        chrome.find_element_by_xpath('//input[@placeholder="模糊检索:仓库名称,仓库编码"]').send_keys(Keys.ENTER)
        sleep(1)
        # 勾选查询出的仓库并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="022bc5d182335c5146b3817a500c2cbc7"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//button[@class="el-button el-button--primary el-button--default"])[2]').click()
        sleep(1)

        ###填写合同信息###
        # 切到合同信息页
        chrome.find_element_by_xpath('//div[text()="合同信息"]').click()
        sleep(1)
        # 点击采购合同/协议号并输入信息，查询出结果
        chrome.find_element_by_xpath(
            '//div[text()="采购合同/协议号 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:合同协议号,合同号,项目编码,项目名称,业态编码,对方编码,产品线编码"]').send_keys(
            contractnumber)
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:合同协议号,合同号,项目编码,项目名称,业态编码,对方编码,产品线编码"]').send_keys(
            Keys.ENTER)
        sleep(1)
        # chrome.find_element_by_xpath(
        #     '//div[@class="multiple-header string_col" and text()="38443"]/ancestor-or-self::td/preceding-sibling::td/div/label/span/span').click()
        # 勾选查询出的合同号并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="03992a2172b8f7327a077e60ac1da11b8"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//button[@class="el-button el-button--primary el-button--default"])[2]').click()
        sleep(1)

        # 点击业务员名称并输入查询条件，查询出结果
        chrome.find_element_by_xpath(
            '//div[text()="业务员名称 "]/following-sibling::div/div/div/div/div/div/div/div/span/span/i').click()
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:业务员编码,业务员名称,部门编码,部门名称"]').send_keys(salesman)
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:业务员编码,业务员名称,部门编码,部门名称"]').send_keys(Keys.ENTER)
        sleep(1)
        # 勾选查询出的业务员并点击确定按钮
        btn_div = chrome.find_element_by_xpath(
            '//span/input[@type="radio" and @value="06c32a810e3dabf2a50d1ab040c7951c8"]')
        chrome.execute_script("arguments[0].click();", btn_div)
        sleep(1)
        chrome.find_element_by_xpath('(//span[@class="ensureBtn"]/button[1])[3]').click()
        sleep(1)

        ###切换到其他信息页签###
        chrome.find_element_by_xpath('//div[text()="其他信息"]').click()
        sleep(1)
        ###子表信息录入###
        chrome.find_element_by_xpath('//div[text()="主体信息"]').click()
        sleep(1)
        # 点击添加商品并输入商品编码，查询出结果
        chrome.find_element_by_xpath('//span[text()="添加商品"]').click()
        sleep(1)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:商品编码,商品名称,规格型号,厂家物料编码"]').send_keys(productcode)
        sleep(0.5)
        chrome.find_element_by_xpath(
            '//input[@placeholder="模糊检索:商品编码,商品名称,规格型号,厂家物料编码"]').send_keys(Keys.ENTER)
        sleep(2)
        # 勾选查询出的商品并点击确定按钮
        chrome.find_element_by_xpath(
            '//th[@class="el-table_15_column_199 multiple-column el-table-column--selection is-leaf headerCellClassName el-table__cell"]/div/label/span/span').click()
        sleep(0.5)
        chrome.find_element_by_xpath('(//div[@class="el-dialog__body"]/div[2]/span/button[1]/span)[4]').click()
        sleep(5)

        ###填写数量价格###
        for i in range(1):
            wait.until(ec.visibility_of_element_located(
                (By.XPATH, '(//td[@colid="col_165" and @f="F_QUANTITY_ORD" and @i=%s])' % i)))
            # 定位数量框并输入数量
            element = chrome.find_element_by_xpath('(//td[@colid="col_165" and @f="F_QUANTITY_ORD" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys('1')
            sleep(1)
            # 点击其它地方让数量框失焦，让数量真正填入数量框(平台设计如此，不用纠结为啥要此步)
            chrome.find_element_by_xpath('(//td[@colid="col_172" and @f="F_PURUNIT_TXT" and @i=%s])' % i).click()
            sleep(1)

    def return_pchno(self):  # 返回采购单号
        return orderno1

    def closetab(self):  # 关闭菜单
        chrome.switch_to.default_content()
        sleep(1)
        # chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        # chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        # chrome.find_element_by_class_name('awsui-tabs-icon close').click()
        chrome.find_element_by_xpath('//span[@class="awsui-tabs-icon close"]').click()
        sleep(1)

    def button_click1(self, type=2, el1='el-loading-spinner', el2='//p[text()="办理成功"]'):  # 保存/提交订单：type=1为保存，否则为提交
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="保存"]')))
        if type == 1:  # 保存
            # 点击保存按钮
            chrome.find_element_by_xpath('//span[text()="保存"]').click()
            sleep(1)
            # 元素el1不可见时认为保存成功
            locator = (By.CLASS_NAME, f'{el1}')
            wait.until_not(ec.presence_of_element_located(locator))
        elif type == 2:  # 提交(提交后界面无弹框)
            # 点击提交按钮
            chrome.find_element_by_xpath('//span[text()="提交"]').click()
            sleep(1)
            # 元素el2出现时认为提交成功
            locator = (By.XPATH, f'{el2}')
            wait.until(ec.presence_of_element_located(locator))
        elif type == 3:  # 提交(提交后界面还有弹框)
            # 点击保存按钮
            chrome.find_element_by_xpath('//span[text()="提交"]').click()
            sleep(1)
            chrome.find_element_by_xpath('//span[text()="确定"][1]').click()
            # 元素el2出现时认为提交成功
            locator = (By.XPATH, f'{el2}')
            wait.until(ec.presence_of_element_located(locator))

        self.closetab()

    def query_click(self, menu_name, orderno, el, qrcond='订单号'):  # 查询出订单并点击
        # 打开菜单
        sleep(1)
        order_link = chrome.find_element_by_xpath('//span[text()="%s"]' % menu_name)
        chrome.execute_script("arguments[0].click();", order_link)
        sleep(5)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))

        # 输入orderno并查询出订单
        wait.until(
            ec.visibility_of_element_located((By.XPATH, '//label[text()="%s"]/parent::div/div/div/div/input' % qrcond)))
        sleep(0.5)
        chrome.find_element_by_xpath('//label[text()="%s"]/parent::div/div/div/div/input' % qrcond).send_keys(orderno)
        sleep(1)
        chrome.find_element_by_xpath('//label[text()="%s"]/parent::div/div/div/div/input' % qrcond).send_keys(
            Keys.ENTER)
        sleep(2)
        # locator = (By.CLASS_NAME, 'el-loading-spinner')
        # wait.until_not(ec.presence_of_element_located(locator))
        # sleep(1)
        # 点击采购单
        # chrome.find_element_by_xpath(
        #     '//span[text()="%s"]//parent::div/parent::td//preceding-sibling::td[1]/div/span' % orderno1).click()
        # 点击订单，进入表单视图
        wait.until(ec.visibility_of_element_located((By.XPATH, '//td[@class="%s"]' % el)))
        sleep(2)
        chrome.find_element_by_xpath('//td[@class="%s"]/div/span' % el).click()
        sleep(1)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(0.5)

    def commit_order(self, type=2):  # 保存/提交订单：type=1为保存，否则为提交
        # 调用query_click查询并打开订单，进入表单视图
        self.query_click('普通采购订单', orderno1, 'vxe-body--column col_19 col--last col--ellipsis dw-click-cell')
        # 等待表单视图加载完
        locator = (By.XPATH, '//span[text()="提交"]')
        wait.until(ec.presence_of_element_located(locator))
        sleep(2)
        # 调用button_click1保存/提交订单，type=1为保存，否则为提交
        self.button_click1(type)

    def switch_loggin(self, usr, pwd):  # 切换登录
        chrome.switch_to.default_content()
        sleep(0.5)
        # 退出系统
        chrome.find_element_by_xpath('//span[text()="退出"]').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//button[text()="确定"]').click()
        sleep(1)
        # 输入用户名密码登录
        chrome.find_element_by_id('input1').clear()
        chrome.find_element_by_id('input1').send_keys(usr)
        chrome.find_element_by_id('input2').clear()
        chrome.find_element_by_id('input2').send_keys(pwd)
        chrome.find_element_by_id('input2').send_keys(Keys.ENTER)
        locator = (By.XPATH, '//span[text()="退出"]')
        wait.until(ec.presence_of_element_located(locator))

    def button_click2(self, type=1, msg1='办理成功'):  # 审批/驳回订单：type=1为审批/其余为驳回订单
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="审核通过"]')))
        sleep(1)
        if type == 1:  # 审核流程
            # 点击审核通过
            chrome.find_element_by_xpath('//span[text()="审核通过"]').click()
            locator = (By.XPATH, '//textarea[@placeholder="意见留言"]')
            wait.until(ec.presence_of_element_located(locator))
            sleep(0.5)
            # 填写审核通过信息
            chrome.find_element_by_xpath('//textarea[@placeholder="意见留言"]').send_keys('审核通过')
            sleep(0.5)
            # 点击发送按钮
            chrome.find_element_by_xpath('//span[text()="发送"]').click()
            # 判断审核成功
            locator = (By.XPATH, '//p[text()="%s"]' % msg1)
            wait.until(ec.presence_of_element_located(locator))
            print('订单审核成功')
        else:  # 驳回流程
            # 点击驳回
            chrome.find_element_by_xpath('//span[text()="审核驳回"]').click()
            locator = (By.XPATH, '//textarea[@placeholder="意见留言"]')
            wait.until(ec.presence_of_element_located(locator))
            sleep(0.5)
            # 填写驳回信息
            chrome.find_element_by_xpath('//textarea[@placeholder="意见留言"]').send_keys('审核驳回')
            sleep(0.5)
            # 点击发送
            chrome.find_element_by_xpath('//span[text()="发送"]').click()
            # 判断驳回成功
            locator = (By.XPATH, '//p[text()="发送成功"]')
            wait.until(ec.presence_of_element_located(locator))
            print('订单驳回成功')
        self.closetab()

    def approve_order(self, type=1):  # 审核/驳回订单：type=1为审核通过，否则为审核驳回
        # 调用query_click进入表单视图
        self.query_click('普通采购订单', orderno1, 'vxe-body--column col_19 col--last col--ellipsis dw-click-cell')
        sleep(1)
        # 调用button_click2审批/驳回
        self.button_click2(type)


class Instore(PurchaseOrder):  # 入库三步曲
    def take_over_goods(self, orderno, type=2):  # 仓库收货
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '仓库收货', orderno,
                                  'vxe-body--column col_19 col--left col--last col--ellipsis dw-click-cell')
        wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="收货"]')))
        sleep(0.5)
        # 点击收货
        chrome.find_element_by_xpath('//span[text()="收货"]').click()
        wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="确定"]')))
        sleep(0.5)
        # 点击确定
        chrome.find_element_by_xpath('//span[text()="确定"]').click()
        wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="提交"]')))
        sleep(0.5)
        # 调用button_click3撤回/提交/保存订单
        self.button_click3(type)

    def button_click3(self, type=2):
        if type == 1:  # 撤回订单
            chrome.find_element_by_xpath('//span[text()="单据撤回"]').click()
        elif type == 2:  # 提交订单
            chrome.find_element_by_xpath('//span[text()="提交"]').click()
            wait.until(ec.presence_of_element_located((By.XPATH, '//p[text()="提交成功"]')))
            print("仓库收货成功")
            PurchaseOrder.closetab(self)
        else:  # 保存订单
            chrome.find_element_by_xpath('//span[text()="保存"]').click()

    def goods_check(self, orderno, type=2):  # 仓库验收
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '仓库验收', orderno,
                                  'vxe-body--column col_19 col--left col--ellipsis dw-click-cell')
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="提交"]')))
        sleep(1.5)
        # 浏览器页面右滑
        chrome.execute_script(
            "document.getElementsByClassName('vxe-table--body-wrapper body--wrapper')[0].scrollLeft = 2000")
        sleep(1)
        # chrome.execute_script("document.getElementsByClassName('vxe-table--footer-wrapper body--wrapper')[0].scrollLeft = 2000")
        # chrome.execute_script("document.getElementsByClassName('vxe-table--body-wrapper body--wrapper')[0].scrollTop = %s*30;" % i)
        # 填写商品信息
        for i in range(1):
            # 填写生产批号
            element = chrome.find_element_by_xpath('(//td[@colid="col_161" and @f="F_PRO_NUM" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys('ZDH' + orderno)
            sleep(1)

            # 填写生产日期
            element = chrome.find_element_by_xpath('(//td[@colid="col_162" and @f="F_PRODUCR_DATE" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(productdate)
            sleep(0.5)
            chrome.find_element_by_xpath('(//td[@colid="col_161" and @f="F_PRO_NUM" and @i=%s])' % i).click()
            sleep(0.5)

            # 填写失效日期
            element = chrome.find_element_by_xpath('(//td[@colid="col_163" and @f="F_PERIOD_DATE" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(expirdate)
            sleep(0.5)
            chrome.find_element_by_xpath('(//td[@colid="col_161" and @f="F_PRO_NUM" and @i=%s])' % i).click()
            sleep(0.5)

            # 填写灭菌批号
            element = chrome.find_element_by_xpath('(//td[@colid="col_165" and @f="F_STE_NUM" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys('ZDH' + orderno)
            sleep(1)

            # 填写灭菌日期
            element = chrome.find_element_by_xpath('(//td[@colid="col_166" and @f="F_STE_DATE" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(productdate)
            sleep(0.5)
            chrome.find_element_by_xpath('(//td[@colid="col_165" and @f="F_STE_NUM" and @i=%s])' % i).click()
            sleep(0.5)

            # 填写灭菌失效日期
            element = chrome.find_element_by_xpath('(//td[@colid="col_167" and @f="F_STE_LOSE_DATE" and @i=%s])' % i)
            chrome.execute_script("arguments[0].click();", element)
            sleep(0.5)
            chrome.find_element_by_class_name('el-input__inner').send_keys(expirdate)
            sleep(0.5)
            chrome.find_element_by_xpath('(//td[@colid="col_165" and @f="F_STE_NUM" and @i=%s])' % i).click()
            sleep(0.5)
        # 调用button_click1保存/提交订单
        PurchaseOrder.button_click1(self, type)
        print('仓库验收成功')

    def goods_grounding(self, orderno, type=2):  # 仓库上架
        # 调用query_click打开表单视图
        PurchaseOrder.query_click(self, '仓库上架', orderno,
                                  'vxe-body--column col_19 col--left col--ellipsis dw-click-cell')
        wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="提交"]')))
        sleep(0.5)
        # 调用button_click1保存/提交订单
        PurchaseOrder.button_click1(self, type, el2='//p[text()="流程结束"]')
        print('仓库上架成功')


###采购退货单页面操作###
class PurchaseReject(PurchaseOrder):
    def relate_order(self, orderno, type=2):  # 关联原单退货
        # 打开采退单
        order_link = chrome.find_element_by_xpath('//span[text()="采购退货单"]')
        chrome.execute_script("arguments[0].click();", order_link)
        chrome.switch_to.frame(chrome.find_element_by_xpath('(//iframe[@class="metro-main-frame"])[2]'))
        sleep(2)
        # 关联原单采退
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="关联原单"]')))
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="关联原单"]').click()
        sleep(3)
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        sleep(0.5)
        # 输入采购单号并查询
        chrome.find_element_by_xpath('//label[text()="原始订单号"]/parent::div/div/div/div/input').send_keys(orderno)
        sleep(1)
        chrome.find_element_by_xpath('//label[text()="原始订单号"]/parent::div/div/div/div/input').send_keys(Keys.ENTER)

        # 选中订单点击采退
        wait.until(ec.visibility_of_element_located((By.XPATH,
                                                     '//th[@class="vxe-header--column col_18 col--center col--checkbox col--ellipsis"]/div/span/span')))
        sleep(1)
        chrome.find_element_by_xpath(
            '//th[@class="vxe-header--column col_18 col--center col--checkbox col--ellipsis"]/div/span/span').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//span[text()="采退"]').click()
        sleep(0.5)
        # chrome.find_element_by_class_name("el-button el-button--primary el-button--mini").click()
        chrome.find_element_by_xpath('//button[@class="el-button el-button--primary el-button--mini"]').click()
        sleep(3)
        # 获取采退单号
        # wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="提交"]')))
        # locator = (By.XPATH,'//iframe[contains(@name,"awsui-dialog-iframe")]')
        # wait.until(ec.frame_to_be_available_and_switch_to_it(locator))
        chrome.switch_to.frame(chrome.find_element_by_xpath('//iframe[contains(@name,"awsui-dialog-iframe")]'))
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="提交"]')))

        global orderno2
        orderno2 = chrome.find_element_by_xpath('//div[text() = "订单号 "]/following-sibling::div/span').text
        # 选择退货原因
        chrome.find_element_by_xpath('//div[text()="其他信息"]').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//label[text() = "退货原因 "]/following-sibling::div//input').click()
        sleep(0.5)
        chrome.find_element_by_xpath('//li[text()="质量召回"]').click()
        sleep(1)

        # 保存/提交订单
        PurchaseOrder.button_click1(self, type=type)
        print("关联原单创建采退单成功，采退单号为:", orderno2)

    def return_rejpchno(self):  # 返回采退单号
        return orderno2

    def commit_order(self, orderno, type=2):  # 保存/提交订单：type=1为保存，否则为提交
        # 查询出订单并点击
        PurchaseOrder.query_click(self, '采购退货单', orderno,
                                  'vxe-body--column col_19 col--last col--ellipsis dw-click-cell', qrcond='采退单号')
        locator = (By.XPATH, '//span[text()="提交"]')
        wait.until(ec.presence_of_element_located(locator))
        sleep(2)
        # 调用button_click1保存/提交订单
        PurchaseOrder.button_click1(self, type)

    def approve_order(self, orderno, type=1):  # 审核/驳回订单：type=1为审核通过，否则为审核驳回
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '采购退货单', orderno,
                                  'vxe-body--column col_19 col--last col--ellipsis dw-click-cell', qrcond='采退单号')
        sleep(1)
        # 调用button_click2审核/驳回订单
        PurchaseOrder.button_click2(self, type, msg1='发送成功')


class Outstore(PurchaseOrder):  # 出库三步曲
    def deliver_goods(self, orderno):  # 仓库发货
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '仓库发货', orderno,
                                  'vxe-body--column col_19 col--last col--ellipsis dw-click-cell')
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="确认发货"]')))
        sleep(1)
        # 确认发货
        chrome.find_element_by_xpath('//span[text()="确认发货"]').click()
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="确定"]')))
        sleep(1)
        # 确定
        chrome.find_element_by_xpath('//span[text()="确定"]').click()
        sleep(3)
        wait.until(ec.visibility_of_element_located((By.XPATH, '//span[text()="确认发货"]')))
        sleep(1)
        # 确认发货
        chrome.find_element_by_xpath('//span[text()="确认发货"]').click()
        sleep(0.5)
        # 判断发货成功
        wait.until(ec.presence_of_element_located((By.XPATH, '//p[text()="办理成功"]')))
        PurchaseOrder.closetab(self)
        print('仓库发货成功')

    def picking_goods(self, orderno, type=2):  # 仓库拣货
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '仓库拣货', orderno,
                                  'vxe-body--column col_19 col--left col--ellipsis dw-click-cell')
        wait.until(ec.presence_of_element_located((By.XPATH, '//span[text()="提交"]')))
        # 调用button_click1保存/提交
        PurchaseOrder.button_click1(self, type)
        print('仓库拣货成功')

    def outbound_review(self, orderno, type=2):  # 出库复核
        # 调用query_click进入表单视图
        PurchaseOrder.query_click(self, '出库复核', orderno,
                                  'vxe-body--column col_19 col--left col--ellipsis dw-click-cell')
        sleep(2)
        wait.until(
            ec.visibility_of_element_located((By.XPATH, '(//td[@colid="col_105" and @f="F_8VDB5FTL" and @i=0])')))
        sleep(0.5)
        # 填写商品信息
        for i in range(1):
            chrome.execute_script(
                "document.getElementsByClassName('vxe-table--footer-wrapper body--wrapper')[0].scrollLeft = 1000")
            sleep(1)
            wait.until(ec.visibility_of_element_located(
                (By.XPATH, '(//td[@colid="col_117" and @f="F_RECHECK_NUM" and @i=%s])' % i)))
            chrome.find_element_by_xpath('(//td[@colid="col_117" and @f="F_RECHECK_NUM" and @i=%s])' % i).click()
            sleep(0.5)
            chrome.find_element_by_class_name('awsui-input-number-right').send_keys('1')
            sleep(1)
        # 调用button_click1保存/提交
        PurchaseOrder.button_click1(self, type, el2='//p[text()="流程结束"]')
        print('出库复核成功')


class ExceptionHandling():
    def handle(self, errmsg, e):
        # 获取项目路径
        pro_path = get_pro_path()
        # 截图
        imgInfo = getErrorPhoto(chrome)
        # 获取信息
        imgName = imgInfo[0]
        image_path = imgInfo[1]
        info = errmsg + repr(e)
        now = get_time()
        # writeFile_append(pro_path + '/logs/logerror%s.txt' % now, info)
        # 发邮件
        send_email1(image_path, info)
        # 写日志
        fmt_str = '%(asctime)s %(thread)d %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
        logging.basicConfig(level=logging.INFO, format=fmt_str, filename=pro_path + '/logs/logerror%s.txt' % now)
        logging.error(errmsg)
        logging.error(traceback.format_exc())


if __name__ == '__main__':
    try:
        step1 = LoginSys()
        step2 = PurchaseOrder()
        step3 = Instore()
        step4 = PurchaseReject()
        step5 = Outstore()
        step1.loginsys(SIT_URL, usr2, pwd2)  # 登录系统
        step2.data_add_purchase()  # 填写表单数据
        step2.button_click1()  # 1为保存，否则为提交
        # step2.commit_order(1)#提交采购单:1为保存，否则为提交
        step2.switch_loggin(usr1, pwd1)  # 切换登录
        step2.approve_order()  # 1为审核通过，否则为审核驳回
        purchord = step2.return_pchno()  # 获取采购订单号
        step3.switch_loggin(usr4, pwd4)
        step3.take_over_goods(purchord)  # 仓库收货：1为收货单据撤回，2为提交，其余为保存
        step3.switch_loggin(usr5, pwd5)
        step3.goods_check(purchord)  # 仓库验收：1为保存，否则为提交
        step3.switch_loggin(usr4, pwd4)
        step3.goods_grounding(purchord)  # 仓库上架：1为保存，否则为提交
        step4.switch_loggin(usr2, pwd2)
        step4.relate_order(purchord)  # 创建采退单1为保存，否则为提交
        rejpurchord = step4.return_rejpchno()  # 获取采退单号
        # step4.commit_order(rejpurchord)#提交采退单：1为保存，否则为提交
        step4.switch_loggin(usr1, pwd1)
        step4.approve_order(rejpurchord)  # 审核/驳回采退单：type=1为审核通过，否则为审核驳回
        step4.switch_loggin(usr4, pwd4)
        step5.deliver_goods(rejpurchord)  # 仓库发货
        step5.picking_goods(rejpurchord)  # 仓库拣货
        step5.switch_loggin(usr5, pwd5)
        step5.outbound_review(rejpurchord)  # 出库复核
        print('流程一：采购_采退结束！！！')
    except Exception as e:
        step6 = ExceptionHandling()
        traceback.print_exc()
        step6.handle(errmsg1, e)

    finally:
        chrome.quit()
