# -*- 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用户管理_角色管理_角色列表展示,id20250509,id20250412-id20250509,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 l_gen1():
    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 = "http://spdtest.cmic.com.cn:8080/api/herpService/mcmsInitStock/insertInitStock"
    # # print(url)
    # request_body = {
    #     "stockId": "1d7a43f605574af3bc69c6a24250718e",
    #     "areaCode": "area0389",
    #     "purMode": "60",
    #     "hosId": "h0347",
    #     "branchId": "h0347-2013"
    # }
    # """
    # 场景: 期初导入
    # """
    #
    # # 发送请求
    # result = requests.post(url, json=request_body, headers=headers)
    # # 获取接口响应时间
    # result = json.loads(result.content)
    # # 获取期初任务id
    # qc_id = result["data"]
    # orderNo = qc_id.replace("h0347", "")
    # 获取产品信息
    url = "http://spdtest.cmic.com.cn:8080/api/herpService/mcmsInitStock/getMcmsGoodsInfoVOS"
    # print(url)
    request_body = {
    "queryObject": {
        "id": "QCh034720250511001976",
        "hosId": "h0347",
        "deptId": "177b66929d40480d910ed7a3177dbd6f",
        "stockId": "1d7a43f605574af3bc69c6a24250718e",
        "orderNo": "QC202505111978",
        "purMode": 60,
        "goodsSpec": "Test-001"
    },
    "pageNum": 1,
    "pageSize": 1500
}
    # 发送请求
    result = requests.post(url, json=request_body, headers=headers)
    # 获取接口响应时间
    result = json.loads(result.content)

    # print(result)

    for i in result["data"]["data"]:
        # 获取产品信息
        supplyId = i["supplyId"]
        hosGoodsId = i["hosGoodsId"]
        hosGoodsCode = i["hosGoodsCode"]
        pkgDefId = i["pkgDefId"]

        # 生成期初任务
        url = "http://spdtest.cmic.com.cn:8080/api/herpService/mcmsInitStock/insertInitStock"
        # print(url)
        request_body = {
            "stockId": "1d7a43f605574af3bc69c6a24250718e",
            "areaCode": "area0389",
            "purMode": "60",
            "hosId": "h0347",
            "branchId": "h0347-2013"
        }
        """
        场景: 期初导入
        """

        # 发送请求
        result = requests.post(url, json=request_body, headers=headers)
        # 获取接口响应时间
        result = json.loads(result.content)
        # 获取期初任务id
        qc_id = result["data"]
        orderNo = qc_id.replace("h0347", "")

        # 期初导入
        url = "http://spdtest.cmic.com.cn:8080/api/herpService/mcmsInitStock/generate"
        # print(url)
        request_body = {
            "initStockId": qc_id,
            "orderNo": orderNo,
            "initStockDetailDTOList": [
                {
                    "hosGoodsId": hosGoodsId,
                    "hosGoodsCode": hosGoodsCode,
                    "provId": "p00002a0c",
                    "subProvId": "p00002a0c",
                    "inSettlement": 6,
                    "supplyId": supplyId,
                    "initStockBatchVOS": [
                        {
                            "batchCode": u_random,
                            "productDate": "2025-04-01",
                            "expdtDate": "2029-04-30",
                            "stockQty": 12,
                            "pkgDefId": pkgDefId,
                            "pkgDefName": "小包",
                            "pkgDefQty": 12
                        }
                    ],
                    "price": 180,
                    "codingType": 0
                }
            ],
            "ignoreValidExpDate": True
        }
        # 发送请求
        result = requests.post(url, json=request_body, headers=headers)
        # 获取接口响应时间
        result = json.loads(result.content)
        print(result)


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

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