54 lines
2.2 KiB
Plaintext
54 lines
2.2 KiB
Plaintext
async def user_browse_history_add(ns={}):
|
|
# 必要参数校验
|
|
if not ns.get('userid') or not ns.get('productid'):
|
|
return {
|
|
'status': False,
|
|
'msg': 'userid and productid are required'
|
|
}
|
|
|
|
db = DBPools()
|
|
async with db.sqlorContext('kboss') as sor:
|
|
try:
|
|
# 根据timestamp时间字段:browse_time去重 查找10个小时内是否存在
|
|
# browse_time = datetime.datetime.now() - datetime.timedelta(hours=10)
|
|
check_sql = """
|
|
SELECT * FROM user_browse_history
|
|
WHERE userid = '%s' AND productid = '%s' AND del_flg = '0';
|
|
""" % (ns.get('userid'), ns.get('productid'))
|
|
check_result = await sor.sqlExe(check_sql, {})
|
|
if check_result:
|
|
# 数据库更新browse_time字段
|
|
update_sql = """
|
|
UPDATE user_browse_history
|
|
SET browse_time = '%s'
|
|
WHERE id = '%s';
|
|
""" % (datetime.datetime.now(), check_result[0]['id'])
|
|
await sor.sqlExe(update_sql, {})
|
|
return {
|
|
'status': True,
|
|
'msg': 'Browse history recorded successfully',
|
|
'data': {'record_id': check_result[0]['id']}
|
|
}
|
|
|
|
# 生成记录ID
|
|
record_id = uuid()
|
|
insert_sql = """
|
|
INSERT INTO user_browse_history (
|
|
id, userid, productid, ip_address, user_agent, del_flg, tag
|
|
) VALUES ('%s', '%s', '%s', '%s', '%s', '0', '%s');
|
|
""" % (record_id, ns.get('userid'), ns.get('productid'),
|
|
ns.get('ip_address', ''), ns.get('user_agent', ''), ns.get('tag', ''))
|
|
await sor.sqlExe(insert_sql, {})
|
|
return {
|
|
'status': True,
|
|
'msg': 'Browse history recorded successfully',
|
|
'data': {'record_id': record_id}
|
|
}
|
|
except Exception as e:
|
|
return {
|
|
'status': False,
|
|
'msg': 'Failed to record browse history, %s' % str(e)
|
|
}
|
|
|
|
ret = await user_browse_history_add(params_kw)
|
|
return ret |