async def operator_get_customers(ns={}): """ 运营获取时间范围内客户 :param ns: :return: """ orgid = ns.get('orgid') year = ns.get('year') month = ns.get('month') db = DBPools() async with db.sqlorContext('kboss') as sor: if ns.get('sor'): sor = ns.get('sor') customers_get_sql = """select id, orgname, create_at from organization where parentid = '%s' and org_type in (2, 3) and del_flg = '0';""" % orgid customers_li = await sor.sqlExe(customers_get_sql, {}) # 统计全部 if not (year or month): customers = [(item['id'], item['orgname']) for item in customers_li] # 按年和月统计 elif (year and month): customers = [(item['id'], item['orgname']) for item in customers_li if item['create_at'][:4] == year and int(item['create_at'][5:7]) == int(month)] # 按年统计 else: customers = [(item['id'], item['orgname']) for item in customers_li if item['create_at'][:4] == year] return customers async def reseller_analysis(ns={}): """ 分销商不同时间段销售额 :return: """ orgid = ns.get('reseller_orgid') start_date = time.strptime(ns['start_date'], "%Y-%m-%d") if ns.get('start_date') else None end_date = time.strptime(ns['end_date'], "%Y-%m-%d") if ns.get('end_date') else None # 通过分销商orgid获取所有客户订单(买退整合) customers = await operator_get_customers({'orgid': orgid}) db = DBPools() async with db.sqlorContext('kboss') as sor: if ns.get('sor'): sor = ns.get('sor') try: # 获取客户所有账单 customer_all_bills = [] for person in customers: customer_bills_li = await sor.R('bill', {'customerid': person[0], 'business_op': 'BUY', 'bill_state': '1', 'del_flg': '0'}) customer_all_bills.extend(customer_bills_li) # 筛选时间间隔 customer_bills_filter = [] if start_date and end_date: for item in customer_all_bills: bill_date = time.strptime(item['bill_date'], "%Y-%m-%d") if start_date <= bill_date <= end_date: customer_bills_filter.append(item) else: customer_bills_filter = customer_all_bills # 获取用户 产品名称 等额外信息 用于展示 for item in customer_bills_filter: item['customername'] = (await sor.R('organization', {'id': item['customerid'], 'del_flg': '0'}))[0]['orgname'] productname_li = await sor.R('product', {'id': item['productid']}) item['productname'] = productname_li[0]['name'] if productname_li else item['productid'] # 获取销售额 reseller_sales = sum([float(item['amount']) for item in customer_bills_filter]) return { 'status': True, 'msg': '获取销售额成功', 'data': { 'user_bills': customer_bills_filter, 'amount': '%.3f' % reseller_sales } } except Exception as e: raise e return { 'status': False, 'msg': '获取销售额失败' } ret = await reseller_analysis(params_kw) return ret