async def add_cpcwidget(params_kw={}): ns = params_kw.copy() cpcid = params_kw.get('cpcid') clusterid = params_kw.get('clusterid') type = params_kw.get('type') model = params_kw.get('model') stock = params_kw.get('stock') add_opt = params_kw.get('add_opt', 'no') if (not cpcid) or (not clusterid): return {'status': False,'msg': '无算力中心ID或集群ID'} id = params_kw.id if not id or len(id) > 32: id = uuid() ns['id'] = id db = DBPools() dbname = 'kboss' async with db.sqlorContext(dbname) as sor: ns["update_time"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) kw = ns.copy() # 插入前先判断是否已存在 uniquejson = {'cpcid':cpcid, 'clusterid':clusterid, 'type': type, 'model':model} cpcwidgets = await sor.R('cpcwidget', uniquejson) if len(cpcwidgets) >= 1: debug(f'cpcwidget 记录 {uniquejson} 已存在! 是否增加库存: {add_opt}') if str(add_opt) == 'yes': debug(f'-- 该部件已存在,增加库存{stock}个单位,但若结果为负则设为0') bsql = f'''update cpcwidget set stock = case when cpcid = '{cpcid}' and type = '{type}' and clusterid = '{clusterid}' and model = '{model}' then greatest(stock + {stock}, 0) else stock end''' debug(f'库存增加sql: {bsql}') await sor.sqlExe(bsql, {}) return {'status': True,'msg': f'该部件已存在,新增库存数:{stock}成功','data': uniquejson} else: debug(f'该部件已存在,该模式只可编辑价格!') return {'status': False,'msg': '该部件已存在,请前往编辑价格!'} r = await sor.C('cpcwidget', kw) return {'status': True,'msg': '新增算力部件成功','data': kw} return {'status': False,'msg': '新增算力部件失败'} ret = await add_cpcwidget(params_kw) return ret