自动验收上架coding

import time
from dingtalkchatbot.chatbot import DingtalkChatbot
import requests

webhook ='https://oapi.dingtalk.com/robot/send?access_token=b7527b37d578d1e244b9b08609e61d209b4170a7acc1d95f2da75e22c62728e9'
bot = DingtalkChatbot(webhook)

session_wms = requests.Session()

这个url是登陆入口

login_url = "https://apollo-api.xtw-tech.com/v1/auth/token"

请求头文件Request Headers

headers = {
# 'accept': 'application/json, text/plain, /',
# 'accept-encoding': 'gzip, deflate, br',
# 'accept-language': 'zh-CN,zh;q=0.9',
# 'content-length': '46',
# 'content-type': 'application/json',
'grpc-metadata-application-code': 'VENUS',
# 'grpc-metadata-user-auth-token': 'null',
# 'origin': 'https://mercury.xtw-tech.com',
# 'referer': 'https://mercury.xtw-tech.com/',
# 'sec-fetch-mode': 'cors',
# 'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36'
}

登陆wms

data = {

}

登录wms

response_wms = session_wms.post(url=login_url, headers=headers, json=data)
tenant_id= '3'
for asn_id in ['743021']:
asn_url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}?tenant_id={tenant_id}'
headers = {
'authority': 'titan-api.xtw-tech.com',
'method': 'GET',
'path': f'/v1/asn/by_id/{asn_id}?tenant_id={tenant_id}',
'scheme': 'https',
'accept': 'application/json, text/plain, /',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'zh-CN,zh;q=0.9',
'grpc-metadata-application-code': 'MERCURY',
'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
'origin': 'https://mercury.xtw-tech.com',
'referer': 'https://mercury.xtw-tech.com/',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': "macOS",
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36',
}
response_asn = session_wms.get(headers=headers, url=asn_url)
respon = response_asn.json()['asnWithDetails']

url_start =f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/inspection/start'
header = {
    'authority': 'titan-api.xtw-tech.com',
    'method': 'POST',
    'path': f'/v1/asn/by_id/{asn_id}/inspection/start',
    'scheme': 'https',
    'accept': 'application/json, text/plain, */*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-length': '35',
    'content-type': 'application/json',
    'grpc-metadata-application-code': 'MERCURY',
    'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
    'origin': 'https://mercury.xtw-tech.com',
    'referer': 'https://mercury.xtw-tech.com/',
    'sec-ch-ua-platform': "Windows",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
}
data ={'asnId': f"{asn_id}", 'tenantId': f"{tenant_id}"}
re_start = session_wms.post(headers=header,url=url_start,json=data)
k = re_start.json()['asnWithDetails']['inventories']
n =0
dic = {}
for i in k:
    expectedQuantity = i['expectedQuantity']
    inventoryRelationId = i['reportItem'][0]['inventoryRelationId']
    try:
        productionTime = i['reportItem'][0]['lot']['productionTime']
        lotId = i['reportItem'][0]['lot']['id']
        status= i['reportItem'][0]['lot']["status"]
        # LOT_STATUS_CLOSE_TO_EXPIRY(临期)
        # LOT_STATUS_EXPIRED(过期)
    except:
        productionTime = ''
        lotId =''
        status =''
    sp = i['standardProductTenantMapping']['standardProduct']['code']
    name= i['standardProductTenantMapping']['standardProduct']['name']

    # print(sp,name,expectedQuantity,inventoryRelationId,productionTime,lotId)

    # 开始进行确认验收
    report_url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/inspection/report/temp_item_record/create_or_update'
    header ={
        'authority': 'titan-api.xtw-tech.com',
        'method': 'POST',
        'path': f'/v1/asn/by_id/{asn_id}/inspection/report/temp_item_record/create_or_update',
        'scheme': 'https',
        'accept': 'application/json, text/plain, */*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        'content-length': '177',
        'content-type': 'application/json',
        'grpc-metadata-application-code': 'MERCURY',
        'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
        'origin': 'https://mercury.xtw-tech.com',
        'referer': 'https://mercury.xtw-tech.com/',
        'sec-ch-ua-platform': "Windows",
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
    }
    if lotId !="":
        data = {'inventoryRelationId': f"{inventoryRelationId}",'tenantId': f"{tenant_id}",'reportItem':[{'actualQuantity': f"{expectedQuantity}",'inventoryRelationId': f"{inventoryRelationId}",'lotId': f"{lotId}",'productionTime': f"{productionTime}"}]}
    else:
        data = {'inventoryRelationId': f"{inventoryRelationId}",'tenantId': f"{tenant_id}",'reportItem':[{'actualQuantity': f"{expectedQuantity}",'inventoryRelationId': f"{inventoryRelationId}"}]}

    #记录不同效期 上架库位的信息
        # 上架到过期库位上
    if status =='LOT_STATUS_EXPIRED':
        dic[f"{inventoryRelationId}"]={'inventoryRelationId': f"{inventoryRelationId}",'putawayQuantity':f"{expectedQuantity}", 'locationId': "205548",'lotId': f"{lotId}"}
        # 上架到临期库位上
    elif status =='LOT_STATUS_CLOSE_TO_EXPIRY':
        dic[f"{inventoryRelationId}"]= {'inventoryRelationId': f"{inventoryRelationId}", 'putawayQuantity': f"{expectedQuantity}",
           'locationId': "2434", 'lotId': f"{lotId}"}
        # 上架到正常库位上
    else:
        dic[f"{inventoryRelationId}"]={'inventoryRelationId': f"{inventoryRelationId}", 'putawayQuantity': f"{expectedQuantity}",
               'locationId': "260835", 'lotId': f"{lotId}"}
    kk = session_wms.post(headers=header,url=report_url,json=data)
    print(n, kk.status_code, asn_id, sp, status)
    n +=1

    time.sleep(0.1)

try:
    # 确认收货
    url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/inspection/report'
    header = {
        'authority': 'titan-api.xtw-tech.com',
        'method': 'POST',
        'path': f'/v1/asn/by_id/{asn_id}/inspection/report',
        'scheme': 'https',
        'accept': 'application/json, text/plain, */*',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9',
        'content-length': '177',
        'content-type': 'application/json',
        'grpc-metadata-application-code': 'MERCURY',
        'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
        'origin': 'https://mercury.xtw-tech.com',
        'referer': 'https://mercury.xtw-tech.com/',
        'sec-ch-ua-platform': "Windows",
        'sec-fetch-dest': 'empty',
        'sec-fetch-mode': 'cors',
        'sec-fetch-site': 'same-site',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
    }
    data ={
        'actualArrivalTime': "2022-11-01T23:58:16+08:00",
        'asnId': f"{asn_id}",
        'pictures': [],
        'tenantId': f"{tenant_id}",
    }
    status = session_wms.post(headers=header,url=url,json=data)
    if status.status_code==400:
        str = status.json()['message']
        f =bot.send_markdown(title=f'验收上架异常:',
                          text=f"### {str}&{asn_id}",
                          at_mobiles=['18620332090'])
        print('检测到异常了')
        break
    else:
        pass

except:
    bot.send_markdown(title=f'验收上架异常:',
                          text=f"### 验收,请及时核查。。",
                          at_mobiles=['18620332090'])


# 还需要获取

putaway_url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/putaway/start'
header ={
    'authority': 'titan-api.xtw-tech.com',
    'method': 'POST',
    'path': f'/v1/asn/by_id/{asn_id}/putaway/start',
    'scheme': 'https',
    'accept': 'application/json, text/plain, */*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-length': '35',
    'content-type': 'application/json',
    'grpc-metadata-application-code': 'MERCURY',
    'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
    'origin': 'https://mercury.xtw-tech.com',
    'referer': 'https://mercury.xtw-tech.com/',
    'sec-ch-ua-platform': "Windows",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
}
data = {'asnId': f"{asn_id}", 'tenantId': f"{tenant_id}"}
session_wms.post(url=putaway_url,headers=header,json=data)




location_url = f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/putaway/location'
header ={
    'authority': 'titan-api.xtw-tech.com',
    'method': 'POST',
    'path': f'/v1/asn/by_id/{asn_id}/putaway/location',
    'scheme': 'https',
    'accept': 'application/json, text/plain, */*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-length': '35',
    'content-type': 'application/json',
    'grpc-metadata-application-code': 'MERCURY',
    'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
    'origin': 'https://mercury.xtw-tech.com',
    'referer': 'https://mercury.xtw-tech.com/',
    'sec-ch-ua-platform': "Windows",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
}


for key,value in dic.items():
    try:
        if value['lotId'] =='':
            data ={'tenantId': f"{tenant_id}",'inventoryPutawayLocationRelation': [{'inventoryRelationId': f"{key}", 'putawayQuantity': f"{value['putawayQuantity']}", 'locationId': f"{value['locationId']}"}]}
            s1 = session_wms.post(url=location_url,headers=header,json=data)


        else:
            data ={'tenantId': f"{tenant_id}",'inventoryPutawayLocationRelation': [{'inventoryRelationId': f"{key}", 'putawayQuantity': f"{value['putawayQuantity']}", 'locationId': f"{value['locationId']}",'lotId': f"{value['lotId']}"}]}
            s2 = session_wms.post(url=location_url,headers=header,json=data)


    except:
        print('出现异常了')

finish =f'https://titan-api.xtw-tech.com/v1/asn/by_id/{asn_id}/putaway/finish'
header ={
    'authority': 'titan-api.xtw-tech.com',
    'method': 'POST',
    'path': f'/v1/asn/by_id/{asn_id}/putaway/finish',
    'scheme': 'https',
    'accept': 'application/json, text/plain, */*',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'content-length': '35',
    'content-type': 'application/json',
    'grpc-metadata-application-code': 'MERCURY',
    'grpc-metadata-user-auth-token': f'{response_wms.cookies.values()[0]}',
    'origin': 'https://mercury.xtw-tech.com',
    'referer': 'https://mercury.xtw-tech.com/',
    'sec-ch-ua-platform': "Windows",
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36',
}
data = {'tenantId': f"{tenant_id}"}
code = session_wms.post(url=finish,headers=header,json=data)
if code.status_code == 200:
    t = f'https://mercury.xtw-tech.com/asn/{asn_id}/?tenant_id=3'
    bot.send_markdown(title=f'完成上架:',
                  text=f"### [深圳中心仓{n}个sp已完成验收上架,点我跳转至ASN页面]({t})",
                  at_mobiles=['18620332090'])
else:
    pass
posted @ 2022-11-03 11:50  烦恼1234  阅读(24)  评论(0编辑  收藏  举报