kboss/b/shopcart/shoppingCartDelete.dspy
2025-07-16 14:27:17 +08:00

87 lines
2.8 KiB
Plaintext

async def shoppingCartDelete(ns={}):
"""
delete cart *为必填项
`cartid` 购物车id和用户id相同 *
`productid` '产品id' *
`spec_id` '规格id' *
`quantity` '数量' * int
`list_price` '原价' * double
`discount` '折扣' * double
:param ns:
:return:
"""
db = DBPools()
async with db.sqlorContext('kboss') as sor:
# find orgid info from users
org_info = await sor.R('users', {'id': ns.get('cartid'), 'del_flg': '0'})
if org_info:
orgid = org_info[0].get('orgid')
else:
return {
'status': False,
'msg': 'can not find orgid from users'
}
if ns.get('kv'):
if not ns.get('id'):
return {
"status": False,
"msg": "shoppingCart id is empty, please check"
}
ns['del_flg'] = '1'
try:
# delete single goods
await sor.U('cart_goods', ns)
# update customer_cart table
ns_sort = {
'cartid': orgid,
'del_flg': '0'
}
cart_goods = await sor.R('cart_goods', ns_sort)
price_amount = 0
for goods in cart_goods:
price_amount += goods.get('amount')
customer_cart = {
# 购物车id, 用户orgid相同
'id': orgid,
'last_date': time.strftime('%Y-%m-%d'),
'customerid': orgid,
'goods_cnt': len(cart_goods),
'amount': price_amount
}
await sor.U('customer_cart', customer_cart)
return {
"status": True,
"msg": "shoppingCart delete success"
}
except Exception as e:
raise e
# return {
# "status": False,
# "msg": "shoppingCart delete failed"
# }
else:
delete_kv_sql = """update cart_goods set del_flg = 1 where cartid = '%s'""" % orgid
try:
await sor.sqlExe(delete_kv_sql, {})
ns['goods_cnt'] = 0
ns['amount'] = 0
ns['id'] = orgid
ns['last_date'] = time.strftime('%Y-%m-%d')
await sor.U('customer_cart', ns)
return {
"status": True,
"msg": "shoppingCart empty cart success"
}
except Exception as e:
raise e
return {
"status": False,
"msg": "shoppingCart empty cart failed"
}
ret = await shoppingCartDelete(params_kw)
return ret