业务数据统计思想

'''投资次数'''
@restapi.bp.get("/investment_num")
async def touzi_num(request):
    params = request.args
    # print(params)
    data =await get_items(app.pool,"orders_product_view",params)
    memberid = []
    for d in data:
        memberid.append(d.get("memberid"))
    memberid_set = set(memberid)
    investment_num = []
    for item in memberid_set:
        investment_num.append(memberid.count(item))
    #投资次数列表
    investment_num_set = set(investment_num)
    touzi_count = {}
    for item in investment_num_set:
        print("the %d has found %d" % (item,investment_num.count(item)))
        touzi_count[item] =  investment_num.count(item)
    x=0
    for k,v in touzi_count.items():
        if k>=5:
            x+=v;
    touzi_count[0] = x
    print(touzi_count)
    final_count = {}
    final_count["复投"] = touzi_count.get(0)
    final_count["一投"] = touzi_count.get(1)
    final_count["二投"] = touzi_count.get(2)
    final_count["三投"] = touzi_count.get(3)
    final_count["四投"] = touzi_count.get(4)
    return ok(final_count)


'''投资期限'''
@restapi.bp.get("/investment_limit")
async def investment_limit(request):
    params = request.args
    print(params)
    data = await get_items(app.pool,"orders_product_view",params)
    sum  = 0;
    sum1 = 0;
    sum2 = 0;
    sum3 = 0;
    sum4 = 0;
    for d in data:
        sanshi = int(d.get("timelong") == 30)
        if sanshi==1:
           sum+=1;
        sishiwu = int(d.get("timelong") == 45)
        if sishiwu == 1:
            sum1+= 1;
        jiushi = int(d.get("timelong") == 90)
        if jiushi == 1:
            sum2 += 1;
        yibaiba = int(d.get("timelong") == 180)
        if yibaiba == 1:
            sum3 += 1;
        sanbailiushiwu = int(d.get("timelong") == 365)
        if sanbailiushiwu == 1:
            sum4 += 1;
    asset_num = {}
    asset_num['30'] = sum;
    asset_num['45'] = sum1;
    asset_num['90'] = sum2;
    asset_num['180'] = sum3;
    asset_num['365'] = sum4;
    return ok(asset_num)

    '''统计信息'''
    @restapi.bp.get("/statistical_info")
    async  def tongji_info(request):
        params = request.args
        print(params)
        data = await get_items(app.pool, "orders_product_view", params)
        print(params)
        total_money_list = []
        statistical_info = {}
        #统计投资金额
        for d in data:
            total_people_num = len(data)
            total_money_list.append(d.get("amount"))
            total_money = sum(total_money_list)
            statistical_info['total_people_num'] = total_people_num
            statistical_info['total_money'] = total_money
        ###统计回款金额
        receivable_money_list = []
        print(params.get("create_at-range"))
        orderdone_data = await get_items(app.pool, "orders", {"status":"DONE","create_at-range":params.get("create_at-range")})
        if not data:
            statistical_info['receivable_money'] = 0
        else:
            for o in orderdone_data:
                receivable_money_list.append(o.get("amount"))
                total_receivable_money = sum(receivable_money_list)
                statistical_info['receivable_money'] = total_receivable_money
                # 统计存量金额
                statistical_info['stock_money'] = total_money - total_receivable_money

        return ok(statistical_info)


# 业务数据统计
@restapi.bp.get('/business_data')
async def business_data(request):
    req_params = request.args
    data = {}
    params = {"status_code-in": "HOLDING,DONE"}
    current_time = time.strftime("%Y-%m-%d")
    if req_params.get("data-range"):
        start_time, end_time = req_params.get("data-range").split('|')
        if start_time:
            params["create_at-gte"] = f"{start_time} 0:00:00"
            if end_time:
                params["create_at-lte"] = f"{end_time} 23:59:59"
    else:
        tomorrow = (dt.strptime(current_time[0:10], '%Y-%m-%d') + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
        params["create_at-gte"] = current_time
        params["create_at-lt"] = tomorrow
    result = await get_items(app.pool, "orders_product_view", params)
    investment_amount = 0
    receivable_amount = 0
    stock_amount = 0
    threeth_product = 0
    fourthfive_product = 0
    nineth_product = 0
    eighth_product = 0
    three_product = 0
    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    data = {}
    for i in result:
        if str(i.get("timelong")) == "30":
            threeth_product += 1
        elif str(i.get("timelong")) == "45":
            fourthfive_product += 1
        elif str(i.get("timelong")) == "90":
            nineth_product += 1
        elif str(i.get("timelong")) == "180":
            eighth_product += 1
        elif str(i.get("timelong")) == "365":
            three_product += 1
        investment_amount += i.get("amount")
        if i.get("status") == "DONE":
            receivable_amount = i.get("amount") + float(format(i.get("outputmonery"), '0.2f'))
            stock_amount += i.get("amount")
        if data.get(i.get("memberid")):
            data[i.get("memberid")] = data[i.get("memberid")] + 1
        else:
            data[i.get("memberid")] = 1
        if data.get(i.get("memberid")) == 1:
            one += 1
        elif data.get(i.get("memberid")) == 2:
            two += 1
        elif data.get(i.get("memberid")) == 3:
            three += 1
        elif data.get(i.get("memberid")) == 4:
            four += 1
        elif data.get(i.get("memberid")) >= 5:
            five += 1
    order_count = {"统计期限": {"玖富30": threeth_product, "玖富45": fourthfive_product,
                            "玖富90": nineth_product, "玖富180": eighth_product,
                            "玖富365": three_product},
                   "统计次数": {"首投": one, "二投": two, "三投": three, "四投": four, "复投": five},
                   "统计信息": {"人数": len(data),
                            "投资金额": investment_amount,
                            "回款金额": receivable_amount,
                            "存量金额": investment_amount - stock_amount}}
    return ok(order_count)

猜你喜欢

转载自blog.csdn.net/Heyll__/article/details/80045945