139 lines
4.8 KiB
Plaintext
139 lines
4.8 KiB
Plaintext
async def check_lease_data(ns={}):
|
|
bill_data = ns.get("bill_data")
|
|
if not bill_data:
|
|
return "bill_data is None"
|
|
|
|
for k, y in ns["bill_data"].items():
|
|
if k.startswith("test_"):
|
|
continue
|
|
if type(y) != int and len(y) == 0:
|
|
return f"the args len is 0, please check key:{k}"
|
|
|
|
try:
|
|
int(ns["bill_data"]["device_num"])
|
|
int(ns["bill_data"]["ip_num"])
|
|
# int(ns["bill_data"]["total_price"])
|
|
except Exception as e:
|
|
return f"args error, please check key:{str(e)}"
|
|
|
|
t = ns["bill_data"].get("test_type", None)
|
|
|
|
# 测试周期
|
|
if t == "0":
|
|
ns["bill_data"]["test_price"] = None
|
|
ns["bill_data"]["test_day"] = None
|
|
elif t == "1":
|
|
try:
|
|
if int(ns["bill_data"].get("test_day", 0)) > 2 or int(ns["bill_data"].get("test_day", 0)) < 1:
|
|
return "test_day error, please check test_day"
|
|
except Exception as e:
|
|
return f"args error, please check key:test_day"
|
|
ns["bill_data"]["test_price"] = None
|
|
elif t == "2":
|
|
try:
|
|
if int(ns["bill_data"].get("test_price", 0)) > 0 and int(ns["bill_data"].get("test_day", 0)) > 0:
|
|
pass
|
|
else:
|
|
return "test_price or test_day error, please check test_price or test_day"
|
|
except Exception as e:
|
|
return f"args error, please check key:test_price"
|
|
else:
|
|
return "test_type error, please check test_type"
|
|
|
|
if len(ns["device_ids"]) == 0:
|
|
return "device_id is None"
|
|
|
|
# 购买时间限制
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
# 获取可售日期
|
|
product_dict = {}
|
|
product_data = await sor.R("lease_product_data", {"del_flg": 0})
|
|
for p in product_data:
|
|
product_dict[p['id']] = [p['lease_start_time'], p['lease_end_time']]
|
|
|
|
product_ids = set([i[0] for i in ns.get("device_ids")])
|
|
for pid in product_ids:
|
|
try:
|
|
start_date, end_date = product_dict.get(pid)
|
|
except Exception as e:
|
|
return "设备租赁入库时间获取失败,无法购买"
|
|
if ns["bill_data"]["start_date"] > start_date and ns["bill_data"]["end_date"] <= end_date:
|
|
pass
|
|
else:
|
|
return f"购买时间区间错误, 请检查产品:{pid}"
|
|
return ""
|
|
|
|
|
|
async def buy(ns={}):
|
|
"""
|
|
购买
|
|
:param ns:
|
|
:return:
|
|
"""
|
|
f = await check_lease_data(ns)
|
|
if f:
|
|
return {"status": False, "msg": f}
|
|
response = {"status": False, "msg": "sql error"}
|
|
bill_data = ns.get("bill_data")
|
|
server_id = [{i[1]} for i in ns.get("device_ids")]
|
|
now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
# 查库存
|
|
sql = "update lease_server_data set status = 1, update_at=${now_time}$ where status = 0 and id in ${server_id}$"
|
|
u_number = await sor.sqlExe(sql, {"server_id": server_id, "now_time": now_time})
|
|
if u_number != int(ns["bill_data"]["device_num"]):
|
|
response = {"status": False, "msg": f"只能购买未被租用的设备,请重新查询设备状态!购买时间:{now_time}", "error": f"update_number:{u_number},device_num:{ns['bill_data']['device_num']}"}
|
|
raise Exception("购买失败,状态回滚")
|
|
else:
|
|
info("购买成功")
|
|
lease_bill = {
|
|
'id': uuid(),
|
|
'buy_time': now_time,
|
|
**bill_data
|
|
}
|
|
|
|
# 账单
|
|
await sor.C("lease_bill", lease_bill)
|
|
info("账单入库成功")
|
|
|
|
# 账单详情
|
|
device_ids = ns.get("device_ids")
|
|
|
|
stock_data = {}
|
|
for d in device_ids:
|
|
# 更改商品库存
|
|
stock_data["id"] = d[0]
|
|
stock_data[d[0]] = stock_data.get(d[0], 0) + 1
|
|
stock_data["stock"] = stock_data[d[0]]
|
|
|
|
device_ids_data = {
|
|
"id": uuid(),
|
|
"bill_id": lease_bill['id'],
|
|
"device_id": d[1],
|
|
}
|
|
|
|
await sor.C("lease_bill_data", device_ids_data)
|
|
info("账单详情入库成功")
|
|
|
|
# 修改服务器状态
|
|
server_status_data = {
|
|
"id": d[1],
|
|
"status": "1",
|
|
"lease_start_time": bill_data['start_date'],
|
|
"lease_end_time": bill_data['end_date'],
|
|
}
|
|
await sor.U("lease_server_data", server_status_data)
|
|
info("服务器状态修改成功")
|
|
# 修改库存
|
|
sql = "update lease_product_data set stock = stock-${stock}$ where id = ${id}$"
|
|
await sor.sqlExe(sql, stock_data)
|
|
return {"status": True, "msg": "save ok", "data": lease_bill["id"]}
|
|
return response
|
|
|
|
|
|
ret = await buy(params_kw)
|
|
return ret
|