async def favorite_add(ns={}): """ 添加用户收藏 :param ns: 包含userid, productid, favorite_type等参数 :return: 操作结果 """ ns_dic = { 'id': uuid(), # 生成32位ID 'userid': ns.get('userid'), 'productid': ns.get('productid'), 'favorite_type': ns.get('favorite_type', '1'), # 默认为商品收藏 'tag': ns.get('tag') # 标签 } # 处理userid if ns.get('userid'): ns_dic['userid'] = ns.get('userid') else: ns_dic['userid'] = await get_user() if not ns_dic.get('userid'): server_error(401) db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 检查是否已收藏 处理tag是NULL的情况 conditions = [ "userid = '%s'" % ns_dic['userid'], "productid = '%s'" % ns_dic['productid'], "favorite_type = '%s'" % ns_dic['favorite_type'] if ns_dic['favorite_type'] is not None else "favorite_type IS NULL", "del_flg = '0'", "tag = '%s'" % ns_dic['tag'] if ns_dic['tag'] is not None else "tag IS NULL" ] check_sql = "SELECT id FROM user_favorite WHERE " + " AND ".join(conditions) check_result = await sor.sqlExe(check_sql, {}) if check_result: return { 'status': False, 'msg': '已收藏关注' } # 执行收藏操作 await sor.C('user_favorite', ns_dic) return { 'status': True, 'msg': '关注收藏成功' } except Exception as e: await sor.rollback() return { 'status': False, 'msg': '关注收藏失败, %s' % str(e) } ret = await favorite_add(params_kw) return ret