diff --git a/b/customer/getcustomer_goods.dspy b/b/customer/getcustomer_goods.dspy index 72c7fab..6047c05 100644 --- a/b/customer/getcustomer_goods.dspy +++ b/b/customer/getcustomer_goods.dspy @@ -1,54 +1,155 @@ async def getcustomer_goods(ns={}): + """ + 获取客户产品列表,支持产品名称筛选、日期范围筛选和分页功能 + + 参数: + ns: 参数字典,包含: + - userid: 用户ID (可选) + - productname: 产品名称筛选关键字 (可选) + - start_date: 开始日期 (格式: YYYY-MM-DD, 可选) + - end_date: 结束日期 (格式: YYYY-MM-DD, 可选) + - page: 页码 (默认1) + - page_size: 每页条数 (默认20) + + 返回: + dict: 包含状态、数据列表、总条数、当前页、总页数的信息 + """ db = DBPools() async with db.sqlorContext('kboss') as sor: try: + # 获取用户ID if ns.get('userid'): users_id = ns.get('userid') else: users_id = await get_user() + user = await sor.R('users', {'id': users_id}) - date = datetime.datetime.now().date() + if not user: + return {'status': False, 'msg': '用户不存在'} + + customerid = user[0]['orgid'] + current_date = datetime.datetime.now().date() + + # 更新过期产品的删除标志 sql = """update customer_goods SET del_flg = '1' where expire_date < ${date}$ and customerid = ${customerid}$""" - await sor.sqlExe(sql, {'date': date, 'customerid': user[0]['orgid']}) - customer_goods = await sor.R('customer_goods', {'customerid': user[0]['orgid'],'sort':'create_at desc','del_flg':'0'}) - for i in customer_goods: - ordergoods = await sor.R('order_goods', {'orderid':i['orderid']}) - # jnorg = await sor.R('organization', {'id': i['providerrid'], 'orgname': '济南超算'}) - # if len(jnorg) >= 1: - # i['org'] = 'jinan' - for j in ordergoods: - if j['end_date']: - await sor.U('customer_goods',{'id':i['id'],'expire_date':j['end_date']}) - await sor.sqlExe(sql, {'date': date, 'customerid': user[0]['orgid']}) - customer = await sor.R('customer_goods', - {'customerid': user[0]['orgid'], 'sort': 'create_at desc', 'del_flg': '0'}) - for cu in customer: + await sor.sqlExe(sql, {'date': current_date, 'customerid': customerid}) + + # 获取筛选参数 + product_name_filter = ns.get('productname', '').strip() + start_date = ns.get('start_date') + end_date = ns.get('end_date') + page = max(1, int(ns.get('page', 1))) + page_size = max(1, min(100, int(ns.get('page_size', 20)))) # 限制每页最多100条 + + # 构建基础查询条件 + base_conditions = { + 'customerid': customerid, + 'del_flg': '0', + 'sort': 'create_at desc' + } + + # 先获取所有有效的产品数据 + all_customer_goods = await sor.R('customer_goods', base_conditions) + + # 处理订单商品数据并更新过期日期 + for item in all_customer_goods: + ordergoods = await sor.R('order_goods', {'orderid': item['orderid']}) + for order_item in ordergoods: + if order_item['end_date']: + await sor.U('customer_goods', {'id': item['id'], 'expire_date': order_item['end_date']}) + + # 重新获取更新后的数据 + all_customer_goods = await sor.R('customer_goods', base_conditions) + + # 处理产品数据并应用筛选 + processed_goods = [] + for cu in all_customer_goods: provider_id_li = await sor.R('product', {'id': cu['productid'], 'del_flg': '0'}) - if provider_id_li: - cu['classify'] = provider_id_li[0]['classify'] - - # 处理k8s资源 - if cu.get('classify') and 'CPCC' in cu['classify']: - cu['classify'] = 'CPCC' - prd_name = cu['productname'].split('.')[0] - if '-' == prd_name or 'STANDARD' == prd_name: - cu['productname'] = 'CPU计算型' - else: - cu['productname'] = prd_name - spec_note_li = await sor.R('specificdata', {'id': cu['specdataid']}) + if not provider_id_li: + continue + + cu['classify'] = provider_id_li[0]['classify'] + + # 处理k8s资源 + if cu.get('classify') and 'CPCC' in cu['classify']: + cu['classify'] = 'CPCC' + prd_name = cu['productname'].split('.')[0] + if '-' == prd_name or 'STANDARD' == prd_name: + cu['productname'] = 'CPU计算型' + else: + cu['productname'] = prd_name + + spec_note_li = await sor.R('specificdata', {'id': cu['specdataid']}) + if spec_note_li: cu['instance_info'] = json.loads(spec_note_li[0]['spec_data']) - external_ip_li = await sor.R('cpcnode', {'cpcid': cu['instance_info']['cpcid'], 'clusterid': cu['instance_info']['clusterid'], 'role': 'master'}) - cu['instance_info']['source_externalip'] = external_ip_li[0]['external_ip'] - - provider_id = provider_id_li[0]['providerid'] - jnorg = await sor.R('organization', {'id': provider_id, 'del_flg': '0'}) + external_ip_li = await sor.R('cpcnode', { + 'cpcid': cu['instance_info']['cpcid'], + 'clusterid': cu['instance_info']['clusterid'], + 'role': 'master' + }) + if external_ip_li: + cu['instance_info']['source_externalip'] = external_ip_li[0]['external_ip'] + + provider_id = provider_id_li[0]['providerid'] + jnorg = await sor.R('organization', {'id': provider_id, 'del_flg': '0'}) + if jnorg: cu['providername'] = jnorg[0]['orgname'] if jnorg[0]['orgname'] == '优刻得科技股份有限公司': cu['region'] = 'cn-wlcb' - return {'status': True, 'data': customer} + + # 应用筛选条件 + # 产品名称筛选 + if product_name_filter and product_name_filter.lower() not in cu.get('productname', '').lower(): + continue + + # 日期范围筛选 + # create_at字段存在且在指定范围内 格式是:"2025-10-05 22:57:05" + if start_date and cu.get('create_at'): + try: + start_dt = datetime.datetime.strptime(start_date, '%Y-%m-%d').date() + create_at_dt = datetime.datetime.strptime(cu['create_at'], '%Y-%m-%d %H:%M:%S') + if create_at_dt.date() < start_dt: + continue + except Exception as e: + print(f"开始日期解析错误: {e}") + + if end_date and cu.get('create_at'): + try: + end_dt = datetime.datetime.strptime(end_date, '%Y-%m-%d').date() + create_at_dt = datetime.datetime.strptime(cu['create_at'], '%Y-%m-%d %H:%M:%S') + if create_at_dt.date() > end_dt: + continue + except Exception as e: + print(f"结束日期解析错误: {e}") + + # 通过specdataid读取规格数据 + cu['spec_data'] = None + if cu.get('specdataid'): + specdata_li = await sor.R('specificdata', {'id': cu['specdataid']}) + if specdata_li: + cu['spec_data'] = json.loads(specdata_li[0]['spec_data']) + + processed_goods.append(cu) + + # 分页处理 + total_count = len(processed_goods) + + # 计算当前页的数据 + start_index = (page - 1) * page_size + end_index = start_index + page_size + paginated_data = processed_goods[start_index:end_index] + + return { + 'status': True, + 'data': paginated_data, + 'pagination': { + 'total': total_count, + 'page': page, + 'page_size': page_size, + } + } except Exception as e: - raise e - return {'status': False, 'msg': '错误'} + return {'status': False, 'msg': '%s' % str(e)} ret = await getcustomer_goods(params_kw) return ret \ No newline at end of file