Top5FlowUsers.java 5.43 KB
Newer Older
shenggui.li committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
package com.reyun.service.impl;

import com.reyun.model.Account;
import com.reyun.repository.AccountRepository;
import com.reyun.repository.AppRepository;
import com.reyun.util.Constant;
import com.reyun.util.DynamoDBUtil;
import com.reyun.util.HttpClientUtil;
import org.joda.time.DateTime;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class Top5FlowUsers {


    @Autowired
    AccountRepository accountRepository;

    @Autowired
    AppRepository appRepository;


    /**
     * 使用套餐用户流量top5
     */
    @Test
    public void top5userFlow() {

        List<Account> superUers = accountRepository.findShiYongUser("2018-01-01", "2019-12-31");

        List<AppAccount> appAccounts = new ArrayList<>();

        for (Account superUer : superUers) {
            List<String> appkeys = appRepository.findAllAppByAccount(superUer.getId());

            if (!appkeys.isEmpty()) {
                AppAccount appAccount = new AppAccount();
                appAccount.setAccount(superUer);
                appAccount.setAppkeys("'" + appkeys.stream().collect(Collectors.joining("','")) + "'");
                appAccounts.add(appAccount);
            }

        }


        List<AppAccount> flows = new ArrayList<>();

        for (AppAccount appAccount : appAccounts) {
            //获取流量
            Map<String, String> conditions = new HashMap<>();
            conditions.put("appids", appAccount.getAppkeys());
            Account account = appAccount.getAccount();
            conditions.put("startdate", new DateTime(account.getPubDate()).toString("yyyy-MM-dd"));
            conditions.put("enddate", account.getPastDate());
            BigInteger value = this.getTotalNum(conditions, "account_track_flow_restrict", "click_sum");

            if (value.doubleValue() > 0) {
                appAccount.setFlow(value);
                flows.add(appAccount);
            }

        }


        flows = flows.stream().sorted(Comparator.comparing(AppAccount::getFlow).reversed())
                .collect(Collectors.toList());


        StringBuffer text = new StringBuffer();
        for (AppAccount flow : flows) {
            Account account = flow.getAccount();
            text.append(account.getEmail()).append(",")
                    .append(account.getPastDate()).append(",")
                    .append(flow.getFlow().longValue()).append(",")
                    .append(account.getPricelevel()).append("\t\n");

        }

        try {
            Files.write(Paths.get("d:/awsdf.txt"),text.toString().getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(text.toString());

    }


    public BigInteger getTotalNum(Map<String, String> conditions, String reportName, String sumType) {
        Long appId = 1218L;
        conditions.put("datatype", "list");
        conditions.put("iscache", Constant.iscache);
        String url = Constant.reportUrl + "/api/trackingio/" + reportName + "/" + appId;
        String responseJson = HttpClientUtil.doHttpPostRequest(url, "trackingio", conditions);
        HashMap<String, String> resultValMap = new HashMap<>();
        String click_sum = "";
        String s = "0";
        if (responseJson != null) {
            try {
                JSONObject jsonObject = new JSONObject(responseJson);
                Iterator keys = jsonObject.keys();
                while (keys.hasNext()) {
                    String key = (String) keys.next();
                    String value = jsonObject.getString(key);
                    resultValMap.put(key, value);
                }
                String val = resultValMap.get("val");
                JSONArray jsonArray = new JSONArray(val);
                JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                click_sum = jsonObject1.getString(sumType);
                if (StringUtils.isEmpty(click_sum) || "0".equals(click_sum)) {
                    click_sum = "0";
                }
                Double aDouble = Double.valueOf(click_sum);
                BigDecimal bigDecimal = new BigDecimal(aDouble);
                s = bigDecimal.toPlainString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return new BigInteger(s);
    }


    class AppAccount {
        String appkeys;
        Account account;
        BigInteger flow;

        public String getAppkeys() {
            return appkeys;
        }

        public void setAppkeys(String appkeys) {
            this.appkeys = appkeys;
        }

        public Account getAccount() {
            return account;
        }

        public void setAccount(Account account) {
            this.account = account;
        }

        public BigInteger getFlow() {
            return flow;
        }

        public void setFlow(BigInteger flow) {
            this.flow = flow;
        }
    }
}