python 调用接口造数据

发布时间:2019-08-24 09:21:16编辑:auto阅读(2205)

    #!/usr/bin/python
    #!-*-coding:utf-8 -*-
    import MySQLdb
    import urllib2
    import random,string
    import datetime
    from datetime import timedelta
    import json
    import sys
    import time
    from time import sleep


    reload(sys)
    sys.setdefaultencoding('utf8')

    so_num=input("Please enter the SOorder number:")
    so_goods_num=input("please enter the SOgoods number:")
    po_num=input("Please enter the POorder number:")
    #so_num=2
    #so_goods_num=2
    #po_num=1
    #po_goods_num=input("please enter the goods number:")
    po_goods_num=so_num*so_goods_num

    conn=MySQLdb.connect(
                host = "192.168.60.48",
                port = 5201,
                user = "root",
                passwd = "root123",
                db = "lsh_wms",
                charset='utf8'
                         )
    cur = conn.cursor()
    post_url="http://api.wms.lsh123.com/"

    print '---------------Start of program-----'
    warehouse_id=1
    owner_uid=1
    order_type=1
    print '---------------SO-------------------'
    for n in range(0,so_num):
        print '---SO:NUM ',n+1


        def getRandom(length):
            slcNum = [random.choice(string.digits) for i in range(length)]
            random.shuffle(slcNum)
            random_num = "1"+ ''.join([i for i in slcNum])
            return random_num

        def getDeleaveryCode(num):
            deleaveryCOde=random.randint(1,num)
            return  str(deleaveryCOde)

        def getNowTime():
            #print 'time-----:',time.strftime("%Y-%m-%d",time.localtime(time.time()))
            return time.strftime("%Y-%m-%d",time.localtime(time.time()))

        order_time=getNowTime()
        def getDayTime(n):
            now = datetime.datetime.now()
            if(n>0):
                n_days_before = now + timedelta(days=n)
                return datetime.datetime(n_days_before.year,n_days_before.month,n_days_before.day,n_days_before.hour,n_days_before.minute,n_days_before.second)
            else:
                return datetime.datetime(now.year,now.month,now.day,now.hour,now.minute,now.second)

        dateTime=getDayTime(13)
        trans_time=str(dateTime)[0:2]
        deleavery_code=getDeleaveryCode(10000)
        select_sql="SELECT bi.sku_name,bi.unit_name,bi.sku_code,bi.pack_unit,bi.pack_name from baseinfo_item bi where rand() limit "+str(so_goods_num)
        cur.execute(select_sql)
        select_fet=cur.fetchall()
        items_before="["
        items_end="]"
        items=""
        for i in select_fet:
            sku_name=i[0]
            unit_name=i[1]
            sku_code=i[2]
            pack_unit=i[3]
            pack_name=i[4]
            #lot_code=i[5]
            #qty=i[6]
            #cost=i[7]
            #orderQty=int(pack_unit)*2
            orderQty=2
            qty=orderQty * pack_unit
            lot_code=1
            cost=1
            detail_other_id=getRandom(8)
            order_other_id = getRandom(6)
            print order_other_id, detail_other_id, sku_name
            item={"detailOtherId":str(detail_other_id),"orderQty":str(orderQty),"packName":str(pack_name),"packUnit":str(pack_unit),"price":str(cost),"skuCode":str(sku_code),"skuName":str(sku_name),"unitName":str(unit_name),"unitQty":str(qty)}
            if so_goods_num > 1:
                items=items+str(item)+","
            else :
                items=items+str(item)
            print items
            items_str=eval(str(items_before)+str(items)+str(items_end))
            deleavery_code=6487
            parameter={"orderOtherId": str(order_other_id),"ownerUid": "1","orderType": "4","transTime": str(trans_time),"deliveryAddrs": "北京市海淀区苏州街1","orderUser":"测试的超市","waveId":"0","orderUserCode":deleavery_code,"deliveryName": "test","deliveryCode": deleavery_code,"items":items_str}
            data=json.dumps(parameter)
            print data
            headers={'content-type':'application/json'}
            url=post_url+"api/wms/java/v1/order/so/insert"
            req=urllib2.Request(url,data,headers)
            response=urllib2.urlopen(req)
            content=response.read()
            print content




    print '---------------PO-------------------'
    conn1=MySQLdb.connect(
                host = "192.168.60.48",
                port = 5201,
                user = "root",
                passwd = "root123",
                db = "lsh_wms",
                charset='utf8'
                         )
    cur1 = conn1.cursor()
    endDeliveryDate=str(dateTime)[0:10]
    for i in range(0,po_goods_num):
        items_before="["
        items_end="]"
        items=""
        select_st="SELECT od.id,od.sku_id,od.sku_code,od.sku_name,od.price,od.order_id,od.unit_name,od.detail_other_id from obd_detail od ORDER BY id DESC LIMIT "+str(po_goods_num)
        cur1.execute(select_st)
        select_fet=cur1.fetchall()
        for i in select_fet:
            id=i[0]
            sku_id=i[1]
            sku_code=i[2]
            sku_name=i[3]
            price=i[4]
            so_order_id=i[5]
            unit_name=i[6]
            print "unit_name:",unit_name
            detail_other_id=getRandom(8)
            
            select_sql="SELECT bi.code,bi.pack_unit,bi.produce_place,bi.pack_name,bi.owner_id from baseinfo_item bi where sku_id= "+str(sku_id)
            cur1.execute(select_sql)
            select_fet=cur1.fetchall()

            for i in select_fet:
                code=i[0]
                pack_unit=i[1]
                produce_place=i[2]
                pack_name=i[3]
                owner_id=i[4]
                print owner_id
                orderQty=int(pack_unit) * 2
            
                item={"packName":str(pack_name),"detailOtherId":str(detail_other_id),"skuCode":str(sku_code),"skuName":str(sku_name),"barCode":str(code),"orderQty":str(orderQty),"packUnit":str(pack_unit),"price":str(price),"madein":str(produce_place),"unitQty":str(pack_unit),"unitName":unit_name}
                items=items+","+str(item)
                items_str=eval(str(items_before)+str(items)[1:]+str(items_end))
                
        supplier_id="SELECT supplier_code,supplier_name from csi_supplier where owner_id="+str(owner_id)
        cur1.execute(supplier_id)
        supplierID=cur1.fetchall()
        for id in supplierID:
            supplierCode=id[0]
            supplierName =id[1]
        order_other_id=getRandom(6)
        
        parameter={"orderOtherId":str(order_other_id),"ownerUid":str(owner_id),"orderType":4,"supplierName":str(supplierName),"supplierCode":str(supplierCode),"orderTime":str(order_time),"deliveryDate":str(order_time),"endDeliveryDate":str(endDeliveryDate),"items":items_str}
        data=json.dumps(parameter)

        headers={'content-type':'application/json'}
        url=post_url+"api/wms/java/v1/order/po/insert"
        req=urllib2.Request(url,data,headers)
        response=urllib2.urlopen(req)
        content=response.read()
    conn1.close()


    conn2=MySQLdb.connect(
                host = "192.168.60.48",
                port = 5201,
                user = "root",
                passwd = "root123",
                db = "lsh_wms",
                charset='utf8'
                         )
    cur2 = conn2.cursor()


    #select_po="SELECT ibd_header.order_other_id,ibd_detail.detail_other_id
    # from ibd_header LEFT JOIN ibd_detail on ibd_header.order_id = ibd_detail.order_id ORDER BY ibd_header.id desc LIMIT "+str(po_goods_num * po_num)
    select_po="SELECT ibd_header.order_other_id,ibd_detail.detail_other_id from ibd_header,ibd_detail where ibd_header.order_id = ibd_detail.order_id ORDER BY ibd_detail.id desc LIMIT "+str(po_goods_num * po_num)
    cur2.execute(select_po)
    select_fet=cur2.fetchall()

    for i in select_fet:
        #print 'poNum:',i
        po_header=i[0]
        po_detail=i[1]
        print po_header,po_detail
        select_so="SELECT obd_header.order_other_id,obd_detail.detail_other_id from obd_header,obd_detail where obd_detail.order_id = obd_header.order_id ORDER BY obd_header.id desc LIMIT  "+str(so_goods_num * so_num)
        cur2.execute(select_so)
        select_fet=cur2.fetchall()
        for i in select_fet:
          so_header=i[0]
          so_detail=i[1]
          insert_po_so="INSERT INTO ibd_obd_relation(ibd_other_id,ibd_detail_id,obd_other_id,obd_detail_id) VALUES(%s,%s,%s,%s)" %(po_header,po_detail,so_header,so_detail)
          cur2.execute(insert_po_so)
          print insert_po_so

    conn2.commit()
    cur2.close()
    conn2.close()



    print '------------------End of program----'

关键字