import json
import requests


def get_dish_type_list():
    data = {}
    send_post_request('/dish/getDishTypeList', data, '获取品类列表', True)
    pass

def add_dish():
    data = {}
    send_post_request('/dish/addOrUpdateDish', data, '添加dish 空数据')
    data['name'] = '新增一个菜'
    send_post_request('/dish/addOrUpdateDish', data, '添加dish 新增一个无类型')
    data['type'] = 1
    data['price'] = 100
    send_post_request('/dish/addOrUpdateDish', data, '添加dish 新增一个无价格')
    data['packageFee'] = 1
    send_post_request('/dish/addOrUpdateDish', data, '添加dish 新增一个无店铺')
    data['shopId'] = 1
    data['id'] = 360
    send_post_request('/dish/addOrUpdateDish', data, '修改dish 修改id=360的数据')
    data['id'] = 101
    data['name']='xiugai'
    data['price'] = 1
    data['nutritionalValue'] = 'lorem ipsum'
    data['packageFee'] = 0
    send_post_request('/dish/addOrUpdateDish', data, '修改dish 修改id=101的数据')
    pass


def del_dish():
    data = {}
    send_post_request('/dish/deleteDish', data, '删除dish 无参数')
    data['dishId'] = 999999
    send_post_request('/dish/deleteDish', data, '删除dish 不存在的id')
    data['dishId'] = 101
    send_post_request('/dish/deleteDish', data, '删除dish id = 101')
    data['dishId'] = 101
    send_post_request('/dish/deleteDish', data, '删除dish 再次删除 id = 101')


def query_list():
    data = {}
    send_post_request('/dish/queryDishList', data, 'query list 无参', True)
    data['available'] = 0
    send_post_request('/dish/queryDishList', data, 'query list available = %d' % data['available'], True)
    data['available'] = 1
    send_post_request('/dish/queryDishList', data, 'query list available = %d' % data['available'], True)
    # data['type'] = 2
    # send_post_request('/dish/queryDishList', data, 'query list type = 2', True)
    # data['dishName'] = '测试'
    # send_post_request('/dish/queryDishList', data, 'query list dishName = 测试', True)
    # data['shopId'] = 3
    # send_post_request('/dish/queryDishList', data, 'query list shopId = 3', True)
    # data['shopId'] = 5
    # send_post_request('/dish/queryDishList', data, 'query list shopId = 5', True)

    pass


def add_dish_type():
    data = {}
    # send_post_request('/dish/addOrUpdateDishType', data, 'add dish type 无参')
    # data['name'] = '炸鸡汉堡'
    # send_post_request('/dish/addOrUpdateDishType', data, 'add dish type name = 炸鸡汉堡')
    # data['name'] = '煲仔饭'
    # send_post_request('/dish/addOrUpdateDishType', data, 'add dish type name = 煲仔饭')
    # data['id'] = 4
    # data['name'] = '煲仔饭'
    # send_post_request('/dish/addOrUpdateDishType', data, 'update dish id = %d type name = %s' %(data['id'], data['name']))
    # data['name'] = '原生态荷叶蒸饭'
    # send_post_request('/dish/addOrUpdateDishType', data, 'update dish id = %d type name = %s' %(data['id'], data['name']))
    data['name'] = '无人机'
    send_post_request('/dish/addOrUpdateDishType', data, 'add dish type name = %s' %(data['name']), True)
    pass


def add_shop():
    data = {}
    # send_post_request('/shop/addOrUpdateShop', data, 'add shop 无参')
    # data['name'] = '元气寿司'
    # send_post_request('/shop/addOrUpdateShop', data, 'add shop name = %s' % data['name'])
    # data['mobile'] = '13312312300'
    # send_post_request('/shop/addOrUpdateShop', data, 'add shop mobile = %s' % data['mobile'])
    # data['deliveryFee'] = 100
    # data['name'] = '锦里川菜'
    # send_post_request('/shop/addOrUpdateShop', data, 'add shop deliveryFee = %d name = %s' % (data['deliveryFee'], data['name']))
    # data['mobile'] = '13312312301'
    # data['name'] = '锦里川菜2'
    # send_post_request('/shop/addOrUpdateShop', data, 'add shop deliveryFee = %d name = %s' % (data['deliveryFee'], data['name']))
    # data['id'] = 1
    # send_post_request('/shop/addOrUpdateShop', data, 'update shop deliveryFee = %d id = %d name = %s' % (data['deliveryFee'], data['id'], data['name']))

    data['mobile'] = '13312312308'
    data['deliveryFee'] = 100000
    data['name'] = 'Levram'
    data['address'] = ''
    data['psw'] = ''
    send_post_request('/shop/addOrUpdateShop', data, 'add shop deliveryFee = %d name = %s' % (data['deliveryFee'], data['name']), True)
    pass

def get_shops():
    data = {}
    send_post_request('/shop/list', data, 'get shops list', True)
    data['shopId'] = 1
    send_post_request('/shop/detail', data, 'get shop detail', True)
    pass


def get_shop_overview():
    data = {}
    send_post_request('/shop/overview', data, 'get shop overview', True)
    pass


def get_delivery_time():
    data = {}
    data['type'] = 1
    send_post_request('/order/deliveryTime', data, 'get delivery time', True)
    pass


def query_address_list():
    data = {}
    send_post_request('/address/queryDeliveryAddressList', data, 'queryDeliveryAddressList no data', True)
    data['addressKw'] = '内科'
    send_post_request('/address/queryDeliveryAddressList', data, 'queryDeliveryAddressList query %s' % data['addressKw'], True)
    data['addressKw'] = '??'
    send_post_request('/address/queryDeliveryAddressList', data, 'queryDeliveryAddressList query %s' % data['addressKw'], True)
    pass


def addOrUpdateDeliveryAddress():
    data = {}
    # Add address
    send_post_request('/address/addOrUpdateDeliveryAddress', data, 'addOrUpdateDeliveryAddress no data')
    data['address'] = '测试地址11'
    send_post_request('/address/addOrUpdateDeliveryAddress', data, 'addOrUpdateDeliveryAddress add duplicate %s' % data['address'])
    data['address'] = '测试地址'
    send_post_request('/address/addOrUpdateDeliveryAddress', data, 'addOrUpdateDeliveryAddress add normal %s' % data['address'])
    # Update address
    data['id'] = 12
    data['address'] = '测试地址'
    send_post_request('/address/addOrUpdateDeliveryAddress', data, 'addOrUpdateDeliveryAddress update %d %s' % (data['id'], data['address']))
    data['address'] = '测试地址new12'
    send_post_request('/address/addOrUpdateDeliveryAddress', data, 'addOrUpdateDeliveryAddress update %d %s' % (data['id'], data['address']))
    data['id'] = 999
    data['address'] = '测试地址999'
    send_post_request('/address/addOrUpdateDeliveryAddress', data, 'addOrUpdateDeliveryAddress update invalid %d %s' % (data['id'], data['address']))
    pass


def validateImportBatchChargeData():
    data = {}
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData no data')
    # data['data'] = '[]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData 空JSON array')
    # data['data'] = '{"序号":1,"姓名":"Alice","充值金额":1,"ae":"   "}'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData JSON object')
    # data['data'] = '[{"序号":1,"充值金额":1,"ae":"   "}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData no name')
    # data['data'] = '[{"序号":1,"姓名":"Alice","充值金额":1,"ae":"   "}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData no mobile')
    # data['data'] = '[{"序号":2,"姓名":"Bob","手机号":"13800138123"}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData no amount')
    # data['data'] = '[{"序号":2,"姓名":"Bob","手机号":"13800138123","充值金额":}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData invalide amount: empty')
    # data['data'] = '[{"序号":2,"姓名":"Bob","手机号":"13800138123","充值金额":"string"}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData invalide amount: string')
    # data['data'] = '[{"序号":1,"姓名":"Alice","充值金额":1,"ae":"   "},{"序号":2,"姓名":"Bob","手机号":"13800138123","充值金额":0.01},{"序号":5,"姓名":"Catalina","手机号":"12345678999","充值金额":100},{"序号":8,"姓名":"Dogue","手机号":"13313313333","充值金额":10},{"序号":99,"姓名":"Essos","手机号":"13800138000","充值金额":-1},{"序号":"   "}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData 非法信息')
    # data['data'] = '[{"序号":1,"姓名":"18867318932","充值金额":1,"ae":"   "},{"序号":2,"姓名":"戴戴戴戴","手机号":"18867318932","充值金额":0.01}]'
    # send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData 部分非法信息')
    data['data'] = '[{"序号":2,"姓名":"戴戴戴戴","手机号":"18867318932","充值金额":0.01},{"序号":2,"姓名":"薛俊友","手机号":"15889967625","充值金额":0.01}]'
    code, result = send_post_request('/balance/validateImportBatchChargeData', data, 'validateImportBatchChargeData 合法信息', True)
    if code == 0:
        bcrNo = result['data']['bcrNo']
        print('bcrNo %s' % bcrNo)
    pass


def confirmBatchCharge():
    data = {}
    send_post_request('/balance/confirmBatchCharge', data, 'confirmBatchCharge no data')
    data['bcrNo'] = '123'
    send_post_request('/balance/confirmBatchCharge', data, 'confirmBatchCharge no operator')
    data['operatorId'] = 1
    data['operatorName'] = '太和'
    send_post_request('/balance/confirmBatchCharge', data, 'confirmBatchCharge invalid bcrNo')
    data['bcrNo'] = 'BCR201907011122115750021'
    send_post_request('/balance/confirmBatchCharge', data, 'confirmBatchCharge normal one')
    send_post_request('/balance/confirmBatchCharge', data, 'confirmBatchCharge redo')
    pass


def queryBCRList():
    data = {}    
    send_post_request('/balance/queryBCRList', data, 'queryBCRList no data', True)
    data['importTimeStart'] = '2019-06-30'
    data['importTimeEnd'] = '2019-06-30'
    send_post_request('/balance/queryBCRList', data, 'queryBCRList %s - %s' %(data['importTimeStart'], data['importTimeEnd']), True)
    data['importTimeStart'] = '2019-06-30'
    data['importTimeEnd'] = '2019-07-01'
    send_post_request('/balance/queryBCRList', data, 'queryBCRList %s - %s' %(data['importTimeStart'], data['importTimeEnd']), True)
    pass


def refundOrder():
    data = {}
    send_post_request('/mealOrder/refundOrder', data, 'refundOrder no data')
    data['mealOrderId'] = 0
    data['remark'] = '退款测试'
    send_post_request('/mealOrder/refundOrder', data, 'refundOrder invalid orderId')
    data['mealOrderId'] = 502
    send_post_request('/mealOrder/refundOrder', data, 'refundOrder normal one')
    send_post_request('/mealOrder/refundOrder', data, 'refundOrder refund again')
    pass


def queryStaffList():
    data = {}
    send_post_request('/staff/queryList', data, 'queryStaffList no data', True)
    data['name'] = '测试'
    send_post_request('/staff/queryList', data, 'queryStaffList with name %s' % str(data), True)
    data['name'] = ''
    data['mobile'] = '13800138000'
    send_post_request('/staff/queryList', data, 'queryStaffList with mobile %s' % str(data), True)
    data['mobile'] = ''
    data['addressId'] = 1
    send_post_request('/staff/queryList', data, 'queryStaffList with addressId %s' % str(data), True)
    pass


def addOrUpdateStaff():
    data = {}
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff no data')
    data['name'] = '测试'
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff no mobile')
    data['mobile'] = '13800138001'
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff no addressId')
    data['addressId'] = 1
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff add %s %s %d' %(data['name'], data['mobile'], data['addressId']))
    data['name'] = '测试副本'
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff add AGAIN %s %s %d' %(data['name'], data['mobile'], data['addressId']))
    data['id'] = 1
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff update %s %s %d' %(data['name'], data['mobile'], data['addressId']))
    data['name'] = '老资'
    data['mobile'] = '13913913939'
    data['addressId'] = 2
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff update %s %s %d' %(data['name'], data['mobile'], data['addressId']))
    data['id'] = 2
    data['mobile'] = '13800138001'
    send_post_request('/staff/addOrUpdateStaff', data, 'addOrUpdateStaff update duplicate mobile %s %s %d' %(data['name'], data['mobile'], data['addressId']))

    pass


def deleteStaff():
    data = {}
    send_post_request('/staff/deleteStaff', data, 'deleteStaff no data')
    data['staffId'] = 999
    send_post_request('/staff/deleteStaff', data, 'deleteStaff invalid staffId')
    data['staffId'] = 3
    send_post_request('/staff/deleteStaff', data, 'deleteStaff ')
    pass


def queryMealOrderList():
    data = {}
    send_post_request('/mealOrder/queryMealOrderList', data, 'queryMealOrderList', True)
    pass


def getStaffInfoByMobile():
    data = {}
    data['mobile'] = '13800138001'
    send_post_request('/staff/getStaffInfoByMobile', data, 'getStaffInfoByMobile', True)
    pass

def stock_test():
    #add_unit()
    #getUnitList()
    # warehousing2()
    # queryStockList()
    #addOrUpdateSupplier()
    #getSupplierDetail()
    #getSupplierList()
    # warehousing()
    #deleteStock()
    # exWarehousing()
    queryStockRecordList()
    # getDiff()
    pass

def add_unit():
    send_post_request('/stock/addOrUpdateUnit', get_empty_data(), 'Add unit no param')
    data1 = get_empty_data()
    data1['name'] = '克'
    send_post_request('/stock/addOrUpdateUnit', data1, 'Add unit %s' % data1['name'])
    send_post_request('/stock/addOrUpdateUnit', data1, 'Add unit %s AGAIN' % data1['name'])
    data2 = get_empty_data()
    data2['name'] = '公斤'
    data2['id'] = 1
    send_post_request('/stock/addOrUpdateUnit', data2, 'Update unit%d to %s ' % (data2['id'], data2['name']))
    pass

def getUnitList():
    send_post_request('/stock/getUnitList', get_empty_data(), 'getUnitList ', True)
    pass

def queryStockList():
    # send_post_request('/stock/queryStockList', get_empty_data(), 'queryStockList no param ', True)
    # data1 = get_empty_data()
    # data1['name'] = 'Lorem'
    # send_post_request('/stock/queryStockList', data1, 'queryStockList with %s' % data1['name'], True)
    data2 = get_empty_data()
    data2['warehousingDateStart'] = '2019-09-02'
    data2['warehousingDateEnd'] = '2019-09-02'
    send_post_request('/stock/queryStockList', data2, 'queryStockList', True, True)

    pass

def addOrUpdateSupplier():
    send_post_request('/stock/addOrUpdateSupplier', get_empty_data(), 'addOrUpdateSupplier no param ')
    data1 = get_empty_data()
    data1['name'] = 'Falanza'
    send_post_request('/stock/addOrUpdateSupplier', data1, 'addOrUpdateSupplier add name %s ' % data1['name'])
    data2 = get_empty_data()
    data2['name'] = 'hehehe'
    data2['id'] = 1
    data2['phone'] = '13800138000'
    send_post_request('/stock/addOrUpdateSupplier', data2, 'addOrUpdateSupplier update %d %s ' % (data2['id'], data2['name']))
    pass

def getSupplierDetail():
    send_post_request('/stock/getSupplierDetail', get_empty_data(), 'getSupplierDetail no param ')
    data1 = get_empty_data()
    data1['id'] = 1
    send_post_request('/stock/getSupplierDetail', data1, 'getSupplierDetail', True)
    pass

def getSupplierList():
    send_post_request('/stock/getSupplierList', get_empty_data(), 'getSupplierList no param ', True)
    pass

def warehousing2():
    data = get_empty_data()
    data['name'] = '花生油'
    data['amount'] = 233.33
    data['unit'] = 'L'
    data['price'] = 123
    data['supplierId'] = 1
    data['operator'] = 'Pikachu'
    data['operatorId'] = 1
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data2 = get_empty_data()
    data2['name'] = 'Donut'
    data2['amount'] = 100
    data2['unit'] = 'Piece'
    data2['price'] = 500
    data2['supplierId'] = 1
    data2['operator'] = 'Pikachu'
    data2['operatorId'] = 1
    send_post_request('/stock/warehousing', data2, 'warehousing', False, True)

def warehousing():
    send_post_request('/stock/warehousing', get_empty_data(), 'warehousing no param ', True)
    data = get_empty_data()
    data['name'] = 'Cupertino'
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['amount'] = 0
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['amount'] = 10
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['unit'] = '公斤'
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['price'] = -1000
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['price'] = 1000
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['supplierId'] = 1
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['operator'] = 'C1C2Graal'
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    data['operatorId'] = 1
    send_post_request('/stock/warehousing', data, 'warehousing', False, True)
    send_post_request('/stock/warehousing', data, 'warehousing AGAIN', False, True)

def deleteStock():
    send_post_request('/stock/deleteStock', get_empty_data(), 'deleteStock', False, True)
    data = get_empty_data()
    data['id'] = 1
    send_post_request('/stock/deleteStock', data, 'deleteStock', False, True)
    data['operator'] = 'C1C2Graal'
    send_post_request('/stock/deleteStock', data, 'deleteStock', False, True)
    data['operatorId'] = 1
    send_post_request('/stock/deleteStock', data, 'deleteStock', False, True)
    send_post_request('/stock/deleteStock', data, 'deleteStock AGAIN', False, True)

def exWarehousing():
    data = get_empty_data()
    # send_post_request('/stock/exWarehousing', get_empty_data(), 'exWarehousing', False, True)
    # data['stockId'] = 1
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    data['operator'] = 'Graal'
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    data['operatorId'] = 1
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    # data['amount'] = -1
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    # data['amount'] = 1
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    # data['stockId'] = 2
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    # data['amount'] = 10000
    # send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)
    data['stockId'] = 3
    data['amount'] = 20
    send_post_request('/stock/exWarehousing', data, 'exWarehousing', False, True)

def queryStockRecordList():
    data = get_empty_data()
    data['operationDateStart'] = '2019-09-01'
    data['operationDateEnd'] = '2019-09-03'
    send_post_request('/stock/queryStockRecordList', data, 'queryStockRecordList', True)

def getDiff():
    send_post_request('/stock/getDiff', get_empty_data(), 'getDiff', True)
    data = get_empty_data()
    data['recordId'] = 5
    send_post_request('/stock/getDiff', data, 'getDiff', True, True)
    data['recordId'] = 10
    send_post_request('/stock/getDiff', data, 'getDiff', True, True)
    data['recordId'] = 15
    send_post_request('/stock/getDiff', data, 'getDiff', True, True)
    data['recordId'] = 16
    send_post_request('/stock/getDiff', data, 'getDiff', True, True)


def get_empty_data():
    data = {}
    return data

def send_post_request(path, json_data, tag, print_resp=False, print_params=False):
    BASE_URL =  'http://192.168.3.80:8089'
    response = requests.post(url=BASE_URL + path, json=json_data)
    assert response.status_code == 200
    result = response.json()
    assert 'code' in result
    code = result['code']
    if code == 0:
        print('%s PASSED' % tag)
    else :
        print('%s Response code %d, msg: %s' %(tag, code, result['info']))
    if print_params:
        print('=====Params:')
        for (key, value) in json_data.items():
            print(key, ':', value)
        print('=====')
    if print_resp:
        print(json.dumps(result, ensure_ascii=False))
    return code, result


if __name__ == '__main__':
    # get_dish_type_list()
    # add_dish()
    # del_dish()
    # query_list()
    # add_dish_type()
    # add_shop()
    # get_shops()
    # get_shop_overview()
    # get_delivery_time()
    # query_address_list()
    # addOrUpdateDeliveryAddress()
    # validateImportBatchChargeData()
    # confirmBatchCharge()
    # queryBCRList()
    # refundOrder()
    # queryStaffList()
    # addOrUpdateStaff()
    # deleteStaff()
    # queryMealOrderList()
    # getStaffInfoByMobile()
    stock_test()
    pass
