TkioAccountServiceImpl.java 12.4 KB
Newer Older
manxiaoqiang committed
1 2 3 4
package tkio.service.impl;

import com.amazonaws.services.dynamodbv2.xspec.B;
import common.model.*;
5
import common.repository.*;
manxiaoqiang committed
6 7
import dic.RoleEnum;
import dic.RoleTypeEnum;
8 9 10 11
import org.apache.commons.collections.map.AbstractMapDecorator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
manxiaoqiang committed
12 13
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
14
import org.springframework.util.StringUtils;
manxiaoqiang committed
15 16
import tkio.model.Account;
import tkio.model.SalesManLeader;
17 18
import tkio.repository.*;
import tkio.service.AccountFlowRestrictService;
manxiaoqiang committed
19
import tkio.service.TkioAccountService;
20 21 22
import util.Constant;
import util.DateUtil;
import util.HttpClientUtil;
manxiaoqiang committed
23 24
import util.ValidateUtil;

25 26
import java.math.BigDecimal;
import java.math.BigInteger;
27
import java.util.*;
manxiaoqiang committed
28 29 30 31

/**
 * Created by mxq on 2017/12/26.
 */
32
@Service
manxiaoqiang committed
33 34
public class TkioAccountServiceImpl implements TkioAccountService {

35 36 37
    //查询URI
    private final static String URI_REPORT_BY_SQL = "/api/trackingio/bysql";

manxiaoqiang committed
38 39 40 41 42 43 44 45 46 47
    @Autowired
    Account4WebRepository account4WebRepository;
    @Autowired
    SalesManLeaderRepository salesManLeaderRepository;
    @Autowired
    AccountRepository accountRepository;
    @Autowired
    BackVisitRepository backVisitRepository;
    @Autowired
    PaymentRepository paymentRepository;
48 49
    @Autowired
    ReminderRepository reminderRepository;
50 51 52 53 54 55 56 57 58 59
    @Autowired
    CampaignRepository campaignRepository;
    @Autowired
    ChannelRepository channelRepository;
    @Autowired
    AppRepository appRepository;
    @Autowired
    AccountFlowRestrictService accountFlowRestrictService;
    @Autowired
    UserRepository userRepository;
60

manxiaoqiang committed
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
    @Override
    public List<Account4Web> findAll(User user) {
        List<Account4Web> result = new ArrayList<>();
        List<Account4Web> account4WebList = null;
        if(user.getRole().equals(RoleEnum.MANAGER.getKey())){
            account4WebList = account4WebRepository.findAll();
        } else{
            List<Integer> idList = new ArrayList<>();
            if(user.getRoleType().equals(RoleTypeEnum.MANAGER.getKey())){
                List<SalesManLeader> salesManLeaderList = salesManLeaderRepository.findByEmail(user.getEmail());
                if (ValidateUtil.isValid(salesManLeaderList)){
                    for(SalesManLeader sml : salesManLeaderList){
                        idList.add(sml.getId());
                    }
                }
            }else{
                SalesManLeader salesManLeader = salesManLeaderRepository.findOneByEmail(user.getEmail());
                idList.add(salesManLeader.getId());
            }
            if(ValidateUtil.isValid(idList)){
                List<Account> accountList = accountRepository.findBussnissMan(idList);
                List<String> emails = new ArrayList<>();
                if(ValidateUtil.isValid(accountList)){
                    for(Account ac : accountList){
                        emails.add(ac.getEmail());
                    }
                    account4WebList = account4WebRepository.findByEmails(emails);
                }
            }
        }
91 92 93 94 95 96 97
        List<Reminder> reminderList = reminderRepository.findAllDistinct("tkio");
        Map<Long, Boolean> reMap = new HashMap<>();
        if(ValidateUtil.isValid(reminderList)){
            for(Reminder re : reminderList){
                reMap.put(re.getAccountId(), re.getRemindStatus());
            }
        }
manxiaoqiang committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111
        if(ValidateUtil.isValid(account4WebList)){
            Map<String, Integer> backTimeMap = getBackTime();
            Map<String, Long> payMap = getPayment();
            for(Account4Web aw : account4WebList){
                if(backTimeMap.containsKey(aw.getEmail())){
                    aw.setBackTime(backTimeMap.get(aw.getEmail()));
                } else{
                    aw.setBackTime(0);
                }
                if(payMap.containsKey(aw.getEmail())){
                    aw.setMoney(payMap.get(aw.getEmail()));
                } else{
                    aw.setMoney(0L);
                }
112 113 114 115 116
                if(reMap.containsKey(aw.getAccountId())){
                    aw.setRemStatus(reMap.get(aw.getAccountId()));
                } else{
                    aw.setRemStatus(true);
                }
manxiaoqiang committed
117 118 119 120 121
                result.add(aw);
            }
        }
        return result;
    }
122 123

    @Override
124 125 126 127 128
    public Reminder forbiden(User user, Reminder resource) {
        resource.setUser(user.getId());
        resource.setModifyTime(new Date());
        resource.setPlatform("tkio");
        resource.setRemindStatus(false);
129
        resource.setDs(DateUtil.getBeforeDays(0));
130 131 132 133 134 135 136 137 138
        return reminderRepository.save(resource);
    }

    @Override
    public Reminder enable(User user, Reminder resource) {
        resource.setUser(user.getId());
        resource.setModifyTime(new Date());
        resource.setPlatform("tkio");
        resource.setRemindStatus(true);
139
        resource.setDs(DateUtil.getBeforeDays(0));
140
        return reminderRepository.save(resource);
141 142 143
    }

    @Override
144 145 146
    public Payment pay(User user, Payment resource) {
        resource.setUser(user.getId());
        resource.setPayDate(new Date());
147
        resource.setPlatform("tkio");
148
        resource.setDs(DateUtil.getBeforeDays(0));
manxiaoqiang committed
149 150 151 152 153 154 155 156 157

        Reminder re = new Reminder();
        re.setUser(user.getId());
        re.setModifyTime(new Date());
        re.setPlatform("tkio");
        re.setRemindStatus(true);
        re.setDs(DateUtil.getBeforeDays(0));
        reminderRepository.save(re);

158
        return paymentRepository.save(resource);
159 160
    }

161 162 163 164
    @Override
    public BackVisit visit(User user, BackVisit resource) {
        resource.setUser(user.getId());
        resource.setVisitDate(new Date());
165
        resource.setPlatform("tkio");
166
        resource.setDs(DateUtil.getBeforeDays(0));
167 168 169
        return backVisitRepository.save(resource);
    }

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    @Override
    public Map<String, Integer> findFunTimes(Long account, String startDate, String endDate) {
        Map<String, Integer> result = new HashMap<>();
        List<Account> accountList = accountRepository.findByRootParent(account);
        List<Long> idList = new ArrayList<>();
        for(Account ac : accountList){
            idList.add(ac.getId());
        }
        List<String> appkeys = appRepository.findAppkeys(idList);
        String appkeyStr = String.join("','", appkeys);

        BigInteger numAccount = accountRepository.countByRootParent(account, startDate, endDate);
        BigInteger numApp = appRepository.countByAccount(idList, startDate, endDate);
        BigInteger numCampaign = campaignRepository.countByAccount(idList, startDate, endDate);
        BigInteger numChannel = channelRepository.countByAccount(idList, startDate, endDate);

        BigInteger event_sum = accountFlowRestrictService.getTotalNum(startDate, endDate, "'" + appkeyStr + "'", "account_io_flow_restrict", "event_sum");
        BigInteger click_sum =accountFlowRestrictService.getTotalNum(startDate, endDate, "'" + appkeyStr + "'","account_track_flow_restrict","click_sum");
        result.put("numAccount", numAccount.intValue());
        result.put("numApp", numApp.intValue());
        result.put("numCampaign", numCampaign.intValue());
        result.put("numChannel", numChannel.intValue());
        result.put("event_sum", event_sum.intValue());
        result.put("click_sum", click_sum.intValue());
        return result;
    }

    @Override
    public List<Map<String, Object>> findPv(Long account, String startDate, String endDate) {
        List<Map<String, Object>>  result = new ArrayList<>();
        List<Account> accountList = accountRepository.findByRootParent(account);
        List<Long> idList = new ArrayList<>();
        for(Account ac : accountList){
            idList.add(ac.getId());
        }
        List<String> appkeys = appRepository.findAppkeys(idList);
        String appkeyStr = String.join("','", appkeys);

        String querySql = "select menu, count(*) as pv, count(distinct(xwho)) as uv, count(*)/count(distinct(xwho)) as puv " +
                "from tkio_bigtable_view.event_f0f251af10e66a0c94d2e923d8863105 where user_appkey in ('" + appkeyStr + "') " +
                "AND ds >= '" + startDate + "' AND ds <= '" + endDate + "'  group by menu;";
        String url = Constant.reportUrl + URI_REPORT_BY_SQL;

        Map<String, String> conditions = new HashMap<>();
        conditions.put("sql", querySql);
        conditions.put("dbtype", "presto");
        conditions.put("datatype", "list");
        conditions.put("iscache", "0");

        String responseJson = HttpClientUtil.doHttpPostRequest(url, "trackingio", conditions);
        if(responseJson.contains("val")){
            try {
                JSONObject jsonObject = new JSONObject(responseJson);
                JSONArray valArr = jsonObject.getJSONArray("val");
                if(null != valArr && valArr.length() > 0){
                    for(int i = 0; i < valArr.length(); i++){
                        JSONObject val = valArr.getJSONObject(i);
                        Map<String, Object> map = new HashMap<>();
                        map.put("menu", val.getString("menu"));
                        map.put("pv", val.getInt("pv"));
                        map.put("uv", val.getInt("uv"));
                        map.put("puv", val.getInt("puv"));

                        result.add(map);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    @Override
    public List<Reminder> findRed(Long account, String startDate, String endDate) {
        List<Reminder> result = new ArrayList<>();
        List<User> userList = userRepository.findAll();
        Map<Long, String> userMap = new HashMap<>();
        for(User u : userList){
            userMap.put(u.getId(), u.getName());
        }
        List<Reminder> list = reminderRepository.findAll(account, startDate, endDate, "tkio");
        if (ValidateUtil.isValid(list)) {
            for(Reminder re : list){
                re.setUserName(userMap.get(re.getUser()));
                result.add(re);
            }
        }
        return result;
    }

    @Override
    public List<BackVisit> findVisit(Long account, String startDate, String endDate) {
        List<BackVisit> result = new ArrayList<>();
        List<User> userList = userRepository.findAll();
        Map<Long, String> userMap = new HashMap<>();
        for(User u : userList){
            userMap.put(u.getId(), u.getName());
        }
        List<BackVisit> list = backVisitRepository.findAll(account, startDate, endDate, "tkio");
        if (ValidateUtil.isValid(list)) {
            for(BackVisit re : list){
                re.setUserName(userMap.get(re.getUser()));
                result.add(re);
            }
        }
        return result;
    }

    @Override
    public List<Payment> findPay(Long account, String startDate, String endDate) {
        List<Payment> result = new ArrayList<>();
        List<User> userList = userRepository.findAll();
        Map<Long, String> userMap = new HashMap<>();
        for(User u : userList){
            userMap.put(u.getId(), u.getName());
        }
        List<Payment> list = paymentRepository.findAll(account, startDate, endDate, "tkio");
        if (ValidateUtil.isValid(list)) {
            for(Payment re : list){
                re.setUserName(userMap.get(re.getUser()));
                result.add(re);
            }
        }
        return result;
    }
296 297


manxiaoqiang committed
298 299
    public Map<String, Integer> getBackTime(){
        Map<String, Integer> map = new HashMap<>();
300
        List<BackVisit> all = backVisitRepository.findAllByPlatform("tkio");
manxiaoqiang committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314
        if (ValidateUtil.isValid(all)){
            for(BackVisit bv : all){
                if(map.containsKey(bv.getEmail())){
                    map.put(bv.getEmail(), map.get(bv.getEmail()) + 1);
                } else{
                    map.put(bv.getEmail(), 1);
                }
            }
        }
        return map;
    }

    public Map<String, Long> getPayment(){
        Map<String, Long> map = new HashMap<>();
315
        List<Payment> all = paymentRepository.findAllByPlatform("tkio");
manxiaoqiang committed
316 317 318 319 320 321 322 323 324 325 326 327
        if (ValidateUtil.isValid(all)){
            for(Payment pa : all){
                if(map.containsKey(pa.getEmail())){
                    map.put(pa.getEmail(), map.get(pa.getEmail()) + pa.getMoney());
                } else{
                    map.put(pa.getEmail(), pa.getMoney());
                }
            }
        }
        return map;
    }
}