update
This commit is contained in:
parent
c0a15392c4
commit
210b17a11e
@ -1,54 +1,155 @@
|
|||||||
async def getcustomer_goods(ns={}):
|
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()
|
db = DBPools()
|
||||||
async with db.sqlorContext('kboss') as sor:
|
async with db.sqlorContext('kboss') as sor:
|
||||||
try:
|
try:
|
||||||
|
# 获取用户ID
|
||||||
if ns.get('userid'):
|
if ns.get('userid'):
|
||||||
users_id = ns.get('userid')
|
users_id = ns.get('userid')
|
||||||
else:
|
else:
|
||||||
users_id = await get_user()
|
users_id = await get_user()
|
||||||
|
|
||||||
user = await sor.R('users', {'id': users_id})
|
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}$"""
|
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']})
|
await sor.sqlExe(sql, {'date': current_date, 'customerid': customerid})
|
||||||
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']})
|
product_name_filter = ns.get('productname', '').strip()
|
||||||
# jnorg = await sor.R('organization', {'id': i['providerrid'], 'orgname': '济南超算'})
|
start_date = ns.get('start_date')
|
||||||
# if len(jnorg) >= 1:
|
end_date = ns.get('end_date')
|
||||||
# i['org'] = 'jinan'
|
page = max(1, int(ns.get('page', 1)))
|
||||||
for j in ordergoods:
|
page_size = max(1, min(100, int(ns.get('page_size', 20)))) # 限制每页最多100条
|
||||||
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']})
|
base_conditions = {
|
||||||
customer = await sor.R('customer_goods',
|
'customerid': customerid,
|
||||||
{'customerid': user[0]['orgid'], 'sort': 'create_at desc', 'del_flg': '0'})
|
'del_flg': '0',
|
||||||
for cu in customer:
|
'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'})
|
provider_id_li = await sor.R('product', {'id': cu['productid'], 'del_flg': '0'})
|
||||||
if provider_id_li:
|
if not provider_id_li:
|
||||||
cu['classify'] = provider_id_li[0]['classify']
|
continue
|
||||||
|
|
||||||
# 处理k8s资源
|
cu['classify'] = provider_id_li[0]['classify']
|
||||||
if cu.get('classify') and 'CPCC' in cu['classify']:
|
|
||||||
cu['classify'] = 'CPCC'
|
# 处理k8s资源
|
||||||
prd_name = cu['productname'].split('.')[0]
|
if cu.get('classify') and 'CPCC' in cu['classify']:
|
||||||
if '-' == prd_name or 'STANDARD' == prd_name:
|
cu['classify'] = 'CPCC'
|
||||||
cu['productname'] = 'CPU计算型'
|
prd_name = cu['productname'].split('.')[0]
|
||||||
else:
|
if '-' == prd_name or 'STANDARD' == prd_name:
|
||||||
cu['productname'] = prd_name
|
cu['productname'] = 'CPU计算型'
|
||||||
spec_note_li = await sor.R('specificdata', {'id': cu['specdataid']})
|
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'])
|
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'})
|
external_ip_li = await sor.R('cpcnode', {
|
||||||
cu['instance_info']['source_externalip'] = external_ip_li[0]['external_ip']
|
'cpcid': cu['instance_info']['cpcid'],
|
||||||
|
'clusterid': cu['instance_info']['clusterid'],
|
||||||
provider_id = provider_id_li[0]['providerid']
|
'role': 'master'
|
||||||
jnorg = await sor.R('organization', {'id': provider_id, 'del_flg': '0'})
|
})
|
||||||
|
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']
|
cu['providername'] = jnorg[0]['orgname']
|
||||||
if jnorg[0]['orgname'] == '优刻得科技股份有限公司':
|
if jnorg[0]['orgname'] == '优刻得科技股份有限公司':
|
||||||
cu['region'] = 'cn-wlcb'
|
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:
|
except Exception as e:
|
||||||
raise e
|
return {'status': False, 'msg': '%s' % str(e)}
|
||||||
return {'status': False, 'msg': '错误'}
|
|
||||||
|
|
||||||
ret = await getcustomer_goods(params_kw)
|
ret = await getcustomer_goods(params_kw)
|
||||||
return ret
|
return ret
|
||||||
Loading…
x
Reference in New Issue
Block a user