# -*- encoding=utf8 -*-
import random
import sys

from airtest.core.helper import using

from common.db.sql.sql_del_branch_info import delData
from common.db.sql.sql_del_kind_goodsinfo import delKindGoodsinfo
from common.db.sql.sql_del_notices_info import delByNoticeName
from common.fileUtls import FileUtils
from common.timeUtils import timeUtils
import threading

__author__ = "xiaohesheng"

"""
case_tag:api,herp-web,r_e2用户管理_角色管理_角色列表展示,id20250412,id20250412-20250412,sit,on
内网:创建待结算数据低值
"""

from common.common_func import commonFuc
from air_case.b1_herp3_login.登录内网系统.登录内网系统 import login_system
import requests
import uuid
import json

module = "b2_herp3_bs"


def c_pur():
    # 生成随机数
    u_id = str(uuid.uuid4())
    u_random = random.randint(1000, 100000000000000)

    # 内网登录
    username = commonFuc().get_business_data(module, "username411")
    ## print('医院名称',username)
    password = commonFuc().get_business_data(module, "password411")
    # print(username, password,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
    # 获取token和projectCode
    token, projectCode, uxid, corpId, info = login_system(username, password).get_token()
    # print( token,projectCode,uxid,corpId,info)
    headers = commonFuc().get_business_data(module, "json_headers_pur", token)

    # 获取供应商产品信息
    url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "pur_get_goods_url")
    # print(url)
    request_body = commonFuc().get_business_data(module, "payload_pur1")
    """
    场景: 通过手工入库方式创建结算低值数据
    """

    # 发送请求
    result = requests.post(url, json=request_body, headers=headers)
    # 获取接口响应时间
    result = json.loads(result.content)
    # 轮询获取具体产品信息
    for i in result["data"]["data"]:
        pkg_def_id = i["pkgDefVOList"][0]["pkgDefId"]
        # print(i)
        # 设置结算方式为入中心库结算
        i["inSettlement"] = 1

        # 补充批号效期信息
        initStockBatchVOS = [
            {
                "batchCode": u_random,
                "productDate": "2025-04-01",
                "expdtDate": "2029-04-30",
                "stockQty": 1,
                "viewId": u_id,
                "pkgDefId": pkg_def_id,
                "pkgDefName": "大包",
                "pkgDefQty": 20
            }
        ]

        batchVOS = [
            {
                "batchCode": u_random,
                "productDate": "2025-04-01",
                "expdtDate": "2029-04-30",
                "stockQty": 1,
                "viewId": u_id,
                "pkgDefId": pkg_def_id,
                "pkgDefName": "大包",
                "pkgDefQty": 20
            }
        ]

        i["initStockBatchVOS"] = initStockBatchVOS
        i["batchVOS"] = batchVOS
        i["stockQty"] = 1
        # 补充供应商信息
        json1 = {
            "areaCode": "area0379",
            "deptId": "391d5e06d0ba4cb39a3a7ee2e7934ae7",
            "provId": "p00002a0c",
            "purMode": "10",
            "remark": "",
            "sourceType": 3,
            "stockId": "b278f988a25c4678a1448fb08c3b4a41",
            "subProvId": "p00002a0c",
            "attachmentList": []
        }

        json1["detailVOS"] = [i]
        # 手工入库
        url = commonFuc().get_api_url() + commonFuc().get_business_data(module, "pur_submit_url")
        request_body = json1
        # 发送请求
        result = requests.post(url, json=request_body, headers=headers)
        # 获取接口响应时间
        result = json.loads(result.content)


# 创建多线程
threads = []
for _ in range(5):
    t = threading.Thread(target=c_pur)
    threads.append(t)
    t.start()

# 等待所有线程完成
for t in threads:
    t.join()
