100 lines
3.8 KiB
Plaintext
100 lines
3.8 KiB
Plaintext
async def shoppingCartAdd(ns={}):
|
|
"""
|
|
一个用户有一个购物车, 所以购物车ID和用户ID相同
|
|
add new cart *为必填项
|
|
`cartid` 购物车id和用户id相同 *
|
|
`productid` '产品id' *
|
|
`spec_id` '规格id' *
|
|
`quantity` '数量' * int
|
|
`list_price` '原价' * double
|
|
`discount` '折扣' * double
|
|
:param ns:
|
|
:return:
|
|
"""
|
|
if not ns:
|
|
return {
|
|
"status": False,
|
|
"message": "get no params..."
|
|
}
|
|
if 'specific_pattern' in ns.keys():
|
|
ns['spec_pattern'] = ns.pop('specific_pattern')
|
|
# 产品没有配置规格
|
|
if not ns.get('spec_pattern'):
|
|
nss = {}
|
|
nss.update(ns)
|
|
ns['spec_pattern'] = nss
|
|
# 产品没有任何配置 折扣为1
|
|
if not ns.get('discount'):
|
|
ns['discount'] = 1.0
|
|
else:
|
|
if float(ns['discount']) > 1:
|
|
ns['discount'] = round(float(ns['discount']) / 10, 2)
|
|
empty_values = [val for val in ns.values() if not val]
|
|
if len(empty_values) != 0:
|
|
return {
|
|
"status": False,
|
|
"message": "shoppingCart add failed, something words is empty..."
|
|
}
|
|
# 购物车详情页每个ID随机生成
|
|
ns['id'] = uuid()
|
|
|
|
# get spec_id
|
|
_spec_pattern = ns.get('spec_pattern').replace("\'", '"') if isinstance(ns.get('spec_pattern'), str) else \
|
|
json.dumps(ns.get('spec_pattern'))
|
|
spec_cnf = {
|
|
# 配置id和购物车产品id相同
|
|
'id': ns['id'],
|
|
'create_date': time.strftime('%Y-%m-%d'),
|
|
'productid': ns.get('productid'),
|
|
'spec_pattern': _spec_pattern
|
|
}
|
|
discount = float(ns.get('discount')) if ns.get('discount') else 1.00
|
|
ns['price'] = float(ns.get('list_price')) * discount
|
|
ns['amount'] = float(ns.get('quantity')) * float(ns['price'])
|
|
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
try:
|
|
# find orgid info from users
|
|
org_info = await sor.R('users', {'id': await get_user(), 'del_flg': '0'})
|
|
if org_info:
|
|
orgid = org_info[0].get('orgid')
|
|
else:
|
|
return {
|
|
'status': False,
|
|
'msg': 'can not find orgid from users'
|
|
}
|
|
ns['cartid'] = orgid
|
|
specificdata_sql = """INSERT INTO specificdata(id, create_date, productid, spec_data) VALUES ('%s', '%s',
|
|
'%s', '%s')""" % (spec_cnf['id'], spec_cnf['create_date'], spec_cnf['productid'], spec_cnf['spec_pattern'])
|
|
await sor.sqlExe(specificdata_sql, {})
|
|
# await sor.C('specificdata', spec_cnf)
|
|
ns['spec_id'] = ns['id']
|
|
await sor.C('cart_goods', ns)
|
|
# 根据购物车ID 即 用户ID获取对应购买列表
|
|
ns_sort = {
|
|
'cartid': ns.get('cartid'),
|
|
'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')
|
|
replace_sql = """REPLACE INTO customer_cart (id, customerid, last_date, goods_cnt, amount) VALUES
|
|
('%s', '%s', '%s', '%s', '%s');""" % (ns.get('cartid'), ns.get('cartid'), time.strftime('%Y-%m-%d'),
|
|
len(cart_goods), price_amount)
|
|
await sor.sqlExe(replace_sql, {})
|
|
return {
|
|
"status": True,
|
|
"msg": "shoppingCart add success"
|
|
}
|
|
except Exception as e:
|
|
raise e
|
|
return {
|
|
"status": False,
|
|
"message": "shoppingCart add failed"
|
|
}
|
|
|
|
|
|
ret = await shoppingCartAdd(params_kw)
|
|
return ret |