diff --git a/b/customer/getNoinvitationcode.dspy b/b/customer/getNoinvitationcode.dspy index ade6145..c354338 100644 --- a/b/customer/getNoinvitationcode.dspy +++ b/b/customer/getNoinvitationcode.dspy @@ -1,21 +1,111 @@ -async def getNoinvitationcode(ns): +async def getNoinvitationcode(ns={}): """展示所有没有销售的客户""" - db = DBPools() - async with db.sqlorContext('kboss') as sor: - orgs = await sor.R('organization', {"org_type": '0'}) - ns['del_flg'] = '0' - ns['sort'] = 'create_at desc' - ns['parentid'] = orgs[0]['id'] - datalist = [] - prc = await sor.R('customer', {'del_flg': '0'}) - for i in prc: - if not i['salemanid']: - customer_id_parentid_li = await sor.R('organization', {'id': i['customerid']}) - customer_id_parentid = customer_id_parentid_li[0]['parentid'] - if customer_id_parentid == ns.get('orgid'): - datalist.append(customer_id_parentid_li[0]) - data = {'rows':datalist} - return {'status': True, 'data': data} + try: + db = DBPools() + async with db.sqlorContext('kboss') as sor: + # 获取组织信息 + orgs = await sor.R('organization', {"org_type": '0'}) + if not orgs: + return {'status': False, 'message': '未找到组织信息'} + + # 获取目标组织ID,优先使用ns中的orgid,否则使用第一个组织的ID + target_org_id = ns.get('orgid') or orgs[0]['id'] + + # 获取分页参数 + page = int(ns.get('page', 1)) # 当前页码,默认为1 + page_size = int(ns.get('size', 10)) # 每页数量,默认为10 + offset = (page - 1) * page_size # 计算偏移量 + + # 获取筛选条件 + orgname = ns.get('orgname', '').strip() # 组织名称 + contactor_phone = ns.get('contactor_phone', '').strip() # 联系人电话 + + # 构建WHERE条件 + where_conditions = [ + "(c.salemanid IS NULL OR c.salemanid = '')", + "o.parentid = '%s'", + "c.del_flg = '0'", + "o.del_flg = '0'" + ] + params = [target_org_id] + + # 添加组织名称模糊查询条件 + if orgname: + where_conditions.append("o.orgname LIKE '%s'") + params.append(f"%%{orgname}%%") + + # 添加联系人电话条件 + if contactor_phone: + where_conditions.append("o.contactor_phone LIKE '%s'") + params.append(f"%%{contactor_phone}%%") + + where_clause = " AND ".join(where_conditions) + + # 构建查询总数的SQL + count_sql = f""" + SELECT COUNT(*) as total + FROM customer c + JOIN organization o ON c.customerid = o.id + WHERE {where_clause} + """ + count_sql = count_sql % tuple(params) + # 执行总数查询 + count_result = await sor.sqlExe(count_sql,{}) + total = count_result[0]['total'] if count_result else 0 + + # 构建查询数据的SQL(添加分页) + data_sql = f""" + SELECT o.* + FROM customer c + JOIN organization o ON c.customerid = o.id + WHERE {where_clause} + ORDER BY o.create_at DESC + LIMIT %s OFFSET %s + """ + + # 添加分页参数 + params.extend([page_size, offset]) + + data_sql = data_sql % tuple(params) + + # 执行数据查询 + result = await sor.sqlExe(data_sql, {}) + + # 返回结果,包含分页信息 + return { + 'status': True, + 'data': { + 'rows': result + }, + 'pagination': { + 'page': page, + 'size': page_size, + 'total': total + } + } + except Exception as e: + # 记录错误日志(可根据实际情况添加日志记录) + return {'status': False, 'message': f'查询失败: {str(e)}'} + + +# async def getNoinvitationcode(ns): +# """展示所有没有销售的客户""" +# db = DBPools() +# async with db.sqlorContext('kboss') as sor: +# orgs = await sor.R('organization', {"org_type": '0'}) +# ns['del_flg'] = '0' +# ns['sort'] = 'create_at desc' +# ns['parentid'] = orgs[0]['id'] +# datalist = [] +# prc = await sor.R('customer', {'del_flg': '0'}) +# for i in prc: +# if not i['salemanid']: +# customer_id_parentid_li = await sor.R('organization', {'id': i['customerid']}) +# customer_id_parentid = customer_id_parentid_li[0]['parentid'] +# if customer_id_parentid == ns.get('orgid'): +# datalist.append(customer_id_parentid_li[0]) +# data = {'rows':datalist} +# return {'status': True, 'data': data} ret = await getNoinvitationcode(params_kw) return ret \ No newline at end of file