zcApi.py 14.5 KB
# -*- coding: utf-8 -*-

# @Time    : 2021/7/28 11:14
# @Author  : Ljq
# @File    : zcApi.py
# @Software: PyCharm

"""
整车进门相关接口
1.整车进门基础接口封装
2.整车进门create_jmsf进门单一步创建接口封装
"""

import json,time,random
from commons.scripts import dealContentType as dct
# from commons.MySession import my
from commons.basic import getLookupList as gLL


def get_carType(session=None,host="",**kwargs):
    """车型获取"""
    url = host + "/jmsf-web/api/jmsf/query/listCarType"
    data = {"businessCode":"jmsf"}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="POST", url=url, data=json.dumps(data), headers=headers)
    return res

def get_listCategoryByCondition(session=None,host="",**kwargs):
    """商品获取"""
    url = host + "/assets-service/api/cusCategory/getTree"
    # marketId = session.userInfo()[]
    data = {"marketId":9,"state":1,"keyword":"蔬菜"}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="POST", url=url, data=json.dumps(data), headers=headers)
    return res

def get_listByExample(session=None,host="",**kwargs):
    """获取接车员信息"""
    url = host + "/dili-uap/userApi/listByExample.api"
    data = {"firmCode":"sy","departmentId":58,"keyword":""}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="POST", url=url, data=json.dumps(data), headers=headers)
    return res

def get_proveType(session=None,host="",**kwargs):
    """查询证明类型"""
    url = host + "/jmsf-web/api/jmsf/query/listCodeByPid"
    data = {"pid":"prove_type"}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="get", url=url, params=json.loads(json.dumps(data)), headers=headers)
    return res

def query_transactionType(session=None,host="",**kwargs):
    """查交易类型"""
    url = host + "/assets-service/api/tradeType/query"
    data = {"pageNum":100,"pageSize":100,"marketId":9}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="POST", url=url, data=json.dumps(data), headers=headers)
    return res

def get_goodsTag(session=None,host="",**kwargs):
    """获取货物标签"""
    url = host + "/jmsf-web/api/jmsf/query/listCodeByPid"
    data = {"pid":"goods_tag"}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="get", url=url, params=json.loads(json.dumps(data)), headers=headers)
    return res

def get_address(session=None,host="",**kwargs):
    """产地获取"""
    url = host + "/jmsf-web/api/jmsf/query/address"
    data = {"province":"C","city":None,"area":None}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="POST", url=url, data=json.dumps(data), headers=headers)
    return res

def get_dep(session=None,host="",**kwargs):
    """获取部门信息"""
    url = host + "/jmsf-web/api/jmsf/query/dep"
    data = {"keyword":""}
    headers = dct.jsonCode()
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="get", url=url, params=data, headers=headers)
    return res



def get_duplicateToken(session=None,host=""):
    """进门防重接口"""
    url = host + "/jmsf-web/appCommon/duplicateToken.action"
    headers = dct.jsonCode()
    res = session.useHeadersRequests(method="get", url=url, headers=headers)
    return res


def create_wholeCarWeighSave(session=None,host="",duplicateToken=None,**kwargs):
    """查询证明类型
    :param host:
    :param **kwargs:
    		{
			"carTypeCode": "${carTypeCode}",
			"carTypeId": ${carTypeId},
			"carTypeName": "${carTypeName}",
			"carTypeWeight": ${carTypeWeight},
			"plate": "${plate}",
			"inGreeterId": 0,
			"inGreeterName": null,
			"productId": ${productId},
			"productName": "${productName}",
			"proveTypeCode": ${proveTypeCode},
			"proveTypeName": "${proveTypeName}",
			"weighRemark": "",
			"weighmanRecord": {
				"grossWeight": ${grossWeight},
				"grossWeightDate": "${grossWeightDate}",
				"newWeight":${grossWeight},
				"tareWeight": ${tareWeight},
				"tareWeightDate": null,
				"weighImgs": []
			},
			"productCode": null,
			"trailerNumber": "${plate}",
			"pathAddress": "A4-BB-6D-0A-F4-13",
			"haveTruckWeight": false,
			"source": 4,
			"entryPlateInputType": 1,
			"dep": ${feeDepId},
			"depName": "${feeDepName}",
			"cateId": ${cateId},
			"notice": 0,
			"backSkinTwo": -1,
			"recordOriginId": 0,
			"recordOriginName": null,
			"recordProductId": 0,
			"recordProductName": null,
			"recordWeight": null,
			"codeIc": null,
			"codeId": 0,
			"difference": 0,
			"bindRecordSign": 0,
			"recordDesc": null,
			"hasCodeItems": false,
			"customerId": 0,
			"customerName": "",
			"driverIc": "",
			"driverName": "",
			"totalPrice": null,
			"originId": ${originId},
			"origin": "${origin}",
			"originCode": "${originCode}",
			"tradeType": "${tradeType}",
			"tradeTypeName": "${tradeTypeName}",
			"weight": ${grossWeight},
			"pwd": "",
			"type": 1,
			"ic": "",
			"boothNumber": "210622124244",
			"productState": ${productState},
			"accountId": 0,
			"passCheckId": 0,
			"passCheckName": null,
			"tradeTypeId": ${tradeTypeId},
			"id": 0,
			"protocolId": null,
			"goodsTagId": ${goodsTagId}
		}
    :return:"""
    url = host + "/jmsf-web/api/entrace/wholeCarWeighSave"
    data = {"carTypeCode": "003", "carTypeId": 53, "carTypeName": "板车", "carTypeWeight": 333, "plate": "213123",
            "inGreeterId": 223, "inGreeterName": "测试沈阳", "productId": 14152, "productName": "蔬菜", "proveTypeCode": 665,
            "proveTypeName": "食品安全", "weighRemark": "备注",
            "weighmanRecord": {"grossWeight": 3.0, "grossWeightDate": "2021-07-28 16:28:24", "newWeight": 2.0,
                               "tareWeight": 1.0, "tareWeightDate": None, "weighImgs": []}, "productCode": None,
            "trailerNumber": "", "pathAddress": "A4-BB-6D-0A-F4-13", "haveTruckWeight": False, "source": 4,
            "entryPlateInputType": 1, "dep": 58, "depName": "水果部", "cateId": 14152, "notice": 0, "backSkinTwo": -1,
            "recordOriginId": 0, "recordOriginName": None, "recordProductId": 0, "recordProductName": None,
            "recordWeight": None, "codeIc": None, "codeId": 0, "difference": 0, "bindRecordSign": 0, "recordDesc": None,
            "hasCodeItems": False, "customerId": 0, "customerName": "", "driverIc": None, "driverName": None,
            "totalPrice": None, "originId": 500101, "origin": "万州区", "originCode": "023", "tradeType": "9001",
            "tradeTypeName": "沈阳1号", "weight": 2.0, "pwd": "", "type": 1, "ic": "", "boothNumber": "210728154359",
            "productState": 1, "accountId": 0, "passCheckId": 0, "passCheckName": None, "tradeTypeId": 70, "id": 0,
            "protocolId": None, "goodsTagId": 663}
    headers = dct.jsonCode()
    headers["jmsf_duplicate_commit_token"] = duplicateToken
    data = dict(data, **kwargs)
    res = session.useHeadersRequests(method="POST", url=url, data=json.dumps(data), headers=headers)
    return res

def create_jmsf(session=None,host=None,carTypeName=None,keyword="蔬菜",userName=None,transactionName=None,
                newWeight=99,plate="川A12345",goodsTagName=None,depName=None,proveName=None):
    """

    """
    templateAttr_list = gLL.get_templateAttr(session=session,host=host)
    print(templateAttr_list)
    # 获取车型
    if "车型" in templateAttr_list:
        carType = get_carType(session=session,host=host)
        if carTypeName == None:
            carTypeIndex = 0
        else:
            carTypeIndex = carType.json()["data"].index(
                [i for i in carType.json()["data"] if i["carTypeName"] == carTypeName][0])
        carTypeCode = carType.json()["data"][carTypeIndex]["code"]
        carTypeId = carType.json()["data"][carTypeIndex]["id"]
        carTypeName = carType.json()["data"][carTypeIndex]["carTypeName"]
        carTypeWeight = carType.json()["data"][carTypeIndex]["weight"]
    else:
        carTypeCode = ""
        carTypeId = ""
        carTypeName = ""
        carTypeWeight = ""

    # 获取商品
    if "商品" in templateAttr_list or "商品(末级)" in templateAttr_list:
        categoryByCondition = get_listCategoryByCondition(session=session,host=host, keyword=keyword)
        cateId = categoryByCondition.json()["data"][0]["id"]
        productId = categoryByCondition.json()["data"][0]["id"]
        productName = categoryByCondition.json()["data"][0]["name"]
        # productCode = categoryByCondition.json()["data"][0]["keycode"]
    else:
        cateId = ""
        productId = ""
        productName = ""
        productCode = ""

    # 获取接车员信息
    if "进门接车员" in templateAttr_list:
        firmCode = session.userInfo["data"]["user"]["firmCode"]
        firmId = session.userInfo["data"]["user"]["firmId"]

        try:
            res_config = gLL.get_config(host=host, marketId=firmId)
            departmentId = [i["value"] for i in res_config.json()["rows"] if i["name"] == "DepartmentId"][0]
        except:
            departmentId = None
        # print("==========1============")
        # print(session.userInfo["data"]["user"])
        # print(firmCode)
        # print(departmentId)
        # print("==========1============")
        listByExample = get_listByExample(session=session,host=host,firmCode=firmCode,departmentId=departmentId)
        print("listByExample",listByExample.json())
        if userName == None:
            listByExampleIndex = 0
        else:
            listByExampleIndex = listByExample.json()["data"].index(
                [i for i in listByExample.json()["data"] if i["userName"] == userName][0])
        inGreeterId = listByExample.json()["data"][listByExampleIndex]["id"]
        inGreeterName = listByExample.json()["data"][listByExampleIndex]["realName"]
    else:
        inGreeterId=""
        inGreeterName = ""

    # 获取交易类型
    if "交易类型" in templateAttr_list:
        transactionType = query_transactionType(session=session,host=host)
        if transactionName == None:
            transactionTypeIndex = 0
        else:
            transactionTypeIndex = transactionType.json()["rows"].index(
                [i for i in transactionType.json()["rows"] if i["name"] == transactionName][0])
        tradeType = transactionType.json()["rows"][transactionTypeIndex]["code"]
        tradeTypeId = transactionType.json()["rows"][transactionTypeIndex]["id"]
        tradeTypeName = transactionType.json()["rows"][transactionTypeIndex]["name"]
    else:
        tradeType = ""
        tradeTypeId = ""
        tradeTypeName = ""

    # 获取货物标签
    if "货物标签" in templateAttr_list:
        goodsTag = get_goodsTag(session=session,host=host)
        if goodsTagName == None:
            transactionTypeIndex = 0
        else:
            transactionTypeIndex = goodsTag.json()["data"].index(
                [i for i in goodsTag.json()["data"] if i["name"] == goodsTagName][0])
        goodsTagId = goodsTag.json()["data"][transactionTypeIndex]["id"]
    else:
        goodsTagId = ""

    # 产地信息获取
    if "产地" in templateAttr_list:
        address = get_address(session=session,host=host)
        originId = address.json()["data"][0]["id"]
        origin = address.json()["data"][0]["name"]
        originCode = address.json()["data"][0]["cityCode"]
    else:
        originId = ""
        origin = ""
        originCode = ""

    # 查询部门信息
    if "接车部门" in templateAttr_list or "收费部门" in templateAttr_list:
        depRes = get_dep(session=session,host=host)
        print("depRes",depRes.json())
        if depName == None:
            depResIndex = 0
        else:
            depResIndex = depRes.json()["data"].index(
                [i for i in depRes.json()["data"] if i["name"] == depName][0])
        feeDepId = depRes.json()["data"][depResIndex]["id"]
        feeDepName = depRes.json()["data"][depResIndex]["name"]
    else:
        feeDepId = ""
        feeDepName = ""

    # 获取证明类型
    if "证明类型" in templateAttr_list:
        proveType = get_proveType(session=session,host=host)
        print(proveType.json())
        # print("proveName",proveName)
        if proveName == None:
            proveTypeIndex = 0
        else:
            proveTypeIndex = proveType.json()["data"].index(
                [i for i in proveType.json()["data"] if i["name"] == proveName][0])
        proveTypeCode = proveType.json()["data"][proveTypeIndex]["id"]
        proveTypeName = proveType.json()["data"][proveTypeIndex]["name"]
    else:
        proveTypeCode = ""
        proveTypeName = ""

    # 获取防重token
    duplicateTokenRes = get_duplicateToken(session=session,host=host)
    print(duplicateTokenRes.json())
    duplicateToken = duplicateTokenRes.json()["data"]

    # 创建生成进门单
    plate = plate
    productState = "1"
    trailerNumber = plate

    # 重量信息准备
    newWeight = newWeight
    tareWeight = carTypeWeight
    weighmanRecord = {"grossWeight": None, "grossWeightDate": "", "newWeight": None, "tareWeight": None,
                      "tareWeightDate": None, "weighImgs": []}
    weighmanRecord["grossWeight"] = carTypeWeight+newWeight
    weighmanRecord["tareWeight"] = carTypeWeight
    weighmanRecord["newWeight"] = newWeight
    print("weighmanRecord",weighmanRecord)
    weighmanRecord["grossWeightDate"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    # 创建进门单
    cre_res = create_wholeCarWeighSave(session=session,host=host, duplicateToken=duplicateToken,
                                           carTypeCode=carTypeCode, carTypeId=carTypeId,
                                           carTypeName=carTypeName, carTypeWeight=carTypeWeight, plate=plate,
                                           productId=productId, productName=productName, proveTypeCode=proveTypeCode,
                                           proveTypeName=proveTypeName, trailerNumber=trailerNumber, depName=feeDepName,
                                           cateId=cateId, originId=originId, origin=origin, originCode=originCode,
                                           tradeType=tradeType, tradeTypeName=tradeTypeName, productState=productState,
                                           tradeTypeId=tradeTypeId, goodsTagId=goodsTagId, inGreeterId=inGreeterId,
                                           inGreeterName=inGreeterName, dep=feeDepId, weighmanRecord=weighmanRecord)
    # print(cre_res.json())
    return cre_res