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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package tkio.service.impl;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tkio.model.*;
import tkio.repository.*;
import tkio.service.AccountFlowRestrictService;
import util.Constant;
import util.DateUtil;
import util.HttpClientUtil;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by song on 2017/10/19.
*/
@Service
public class AccountFlowRestrictServiceImpl implements AccountFlowRestrictService {
@Autowired
private AppRepository appRepository;
@Autowired
private AccountRepository accountRepository;
@Autowired
private PackageTypeRepository packageTypeRepository;
@Autowired
private IsNotifiedRepository isNotifiedRepository;
@Autowired
private SalesManLeaderRepository salesManLeaderRepository;
@Autowired
private IncrementFlowRepository incrementFlowRepository;
@Override
public AccountRestrict4Web findRestrictByAccount(Long accountId) {
Long rootParent = accountRepository.findRootParentByAccountId(accountId).longValue();
AccountRestrict4Web accountRestrict4Web = new AccountRestrict4Web();
Account one = accountRepository.findOne(accountId);
List<App> apps = appRepository.listAppByRootAccount(rootParent);
Account rootAccount = accountRepository.findOne(rootParent);
String companyOrEmail = rootAccount.getCompany() != null ? rootAccount.getCompany() : rootAccount.getEmail();
IsNotified isNotifiedObject = isNotifiedRepository.findIsNotifiedByAccountId(rootParent);
boolean isTrackProduct = true;
boolean isIOProduct = rootAccount.getAllowBehavior();
StringBuffer appsStr = new StringBuffer();
for (App app : apps) {
appsStr = appsStr.append("\'" + app.getAppkey() + "\'" + ",");
}
String appids = "";
if (apps.size() != 0) {
appids = appsStr.substring(0, appsStr.length() - 1);
}
PackageType packageType = packageTypeRepository.findPackageTypeByRootParent(rootParent);
Boolean flowRestrict = rootAccount.getFlowRestrict();
String pastDate = rootAccount.getPastDate();
String trackBeginTime;
Date rootCreateTime = rootAccount.getCreateTime();
String createTime = DateUtil.format(rootCreateTime, "yyyy-MM-dd");
trackBeginTime = (rootAccount.getNewContract() != null && rootAccount.getNewContract()) ? rootAccount.getModifyPricelevelTime() : createTime;
Date currentDate = DateUtil.getCurrentDate();
String currentDateStr = DateUtil.getCurrentDateStr();
int remainingDays = DateUtil.daysBetween(currentDate, DateUtil.parseDate(pastDate)) + 1;
String salesManLeaderEmail = salesManLeaderRepository.findLeaderByBussinessMan(rootAccount.getBussinessman());
ArrayList<String> emailList = new ArrayList<>();
emailList.add(salesManLeaderEmail);
Date lastMonth = getLastMonth(1);
String firstDayOfLastMonth = DateUtil.getFirstDayOfMonth(lastMonth);
String lastDayOfLastMonth = DateUtil.getLastDayOfMonth(lastMonth);
String firstDayOfThisMonth = DateUtil.getFirstDayOfMonth(new Date());
BigInteger clickNum =new BigInteger("0");
BigInteger lastThreeDaysClickNum1=new BigInteger("0");
if(apps.size()>0) {
clickNum = getTotalNum(trackBeginTime, pastDate, appids, "account_track_flow_restrict", "click_sum");
//lastThreeDaysClickNum1 = getTotalNum(trackBeginTime, currentDateStr, appids, "account_track_three_days_flow_restrict", "click_sum");
}
//BigInteger lastThreeDaysClickNum = lastThreeDaysClickNum1.compareTo(new BigInteger("0")) != 0 ? lastThreeDaysClickNum1 : new BigInteger("1");
// int trackRemainDays = (int) Math.floor((packageType.getTrackFlow().doubleValue() - clickNum.doubleValue()) / ((lastThreeDaysClickNum.doubleValue() / 3)));
// double trackRemainPercent = 1.0 - clickNum.doubleValue() / packageType.getTrackFlow().doubleValue();
// DecimalFormat decimalFormat = new DecimalFormat("0%");
//String trackRemainPercent1 = decimalFormat.format(trackRemainPercent);
//
// String IOBeginTime = (isNotifiedObject != null && isNotifiedObject.getThisMonthUpgradeLevel() != null && isNotifiedObject.getThisMonthUpgradeLevel()) ? rootAccount.getModifyPricelevelTime() : firstDayOfThisMonth;
//
//
// BigInteger lastMonthEventNum=new BigInteger("0");
// BigInteger thisMonthEventNum=new BigInteger("0");
// BigInteger lastThreeDaysEventNum=new BigInteger("0");
// if(apps.size()>0){
// lastMonthEventNum = getTotalNum(firstDayOfLastMonth, lastDayOfLastMonth, appids, "account_io_flow_restrict", "event_sum");
// thisMonthEventNum = getTotalNum(IOBeginTime, currentDateStr, appids, "account_io_flow_restrict", "event_sum");
// lastThreeDaysEventNum = getTotalNum(IOBeginTime, currentDateStr, appids, "account_io_three_days_flow_restrict", "event_sum");
// }
//
// BigInteger lastMonthExceedNum = (lastMonthEventNum.subtract(packageType.getIoFlow()).compareTo(new BigInteger("0")) == 1) ? lastMonthEventNum.subtract(packageType.getIoFlow()) : new BigInteger("0");
// if (isNotifiedObject != null && isNotifiedObject.getThisMonthUpgradeLevel() != null && isNotifiedObject.getThisMonthUpgradeLevel()) {
// lastMonthExceedNum = new BigInteger("0");
// }
// double v = packageType.getIoFlow().doubleValue() * 1 - lastMonthExceedNum.doubleValue();
// DecimalFormat decimalFormat1 = new DecimalFormat("0");
// String format = decimalFormat1.format(v);
// BigInteger thisMonthAvailableTotalFlow = new BigInteger(format);
// BigInteger thisMonthRemainingFlow = thisMonthAvailableTotalFlow.subtract(thisMonthEventNum);
// int IORemainDays = -1;
// if (lastThreeDaysEventNum.compareTo(new BigInteger("1")) == 1) {
// IORemainDays = (int) (Math.floor(Double.valueOf(thisMonthRemainingFlow.divide(lastThreeDaysEventNum.divide(new BigInteger("3"))).toString())));
// }
// double ioRemainPercent = thisMonthRemainingFlow.doubleValue() / thisMonthAvailableTotalFlow.doubleValue();
//String ioRemainPercent1 = decimalFormat.format(ioRemainPercent);
BigInteger num = incrementFlowRepository.listByRootAccount(accountId, DateUtil.getBeforeDays(0));
BigInteger trackNum = packageType.getTrackFlow();
if(trackNum.doubleValue() != -1 && null != num){
trackNum = trackNum.add(num);
}
accountRestrict4Web.setPastDate(pastDate);
accountRestrict4Web.setPackageName(packageType.getPackageName());
accountRestrict4Web.setOriginalName(packageType.getOriginalName());
accountRestrict4Web.setIOLimit(packageType.getIoFlow());
accountRestrict4Web.setTrackLimit(trackNum);
accountRestrict4Web.setThisMonthIOFlow(new BigInteger("0"));
accountRestrict4Web.setTrackTotalFlow(clickNum);
accountRestrict4Web.setAccountId(rootParent);
accountRestrict4Web.setPriceLevel(rootAccount.getPricelevel().intValue());
accountRestrict4Web.setLastMonthIOFlow(new BigInteger("0"));
accountRestrict4Web.setIORemainPercent(0d);
//accountRestrict4Web.setTrackRemainPercent(trackRemainPercent);
//accountRestrict4Web.setiOremainingDays(0);
//accountRestrict4Web.setTrackRemainingDays(trackRemainDays);
accountRestrict4Web.setPackLevel(new Long(packageType.getId()));
accountRestrict4Web.setFlowRestrict(flowRestrict);
accountRestrict4Web.setAllowBehavior(isIOProduct);
if (packageType.getTrackFlow().doubleValue() < 0) {
accountRestrict4Web.setPackageName("包年套餐");
}
boolean ioFlag = true;
boolean ioStatus = true;
boolean trackFlag = true;
boolean trackStatus = true;
if (rootParent.equals(accountId)) {
if (!(packageType.getTrackFlow().doubleValue() < 0) && flowRestrict) {
/**
* 不是超级用户,是包流量的用户
*/
if (isTrackProduct) {
boolean tenPercent = (trackNum.doubleValue() * 0.9 <= clickNum.doubleValue()) && (trackNum.doubleValue() * 0.95 > clickNum.doubleValue());
boolean exPercent = trackNum.doubleValue() <= clickNum.doubleValue();
if (exPercent) {
trackStatus = false;
trackFlag = false;
}else if (tenPercent && (isNotifiedObject.getTrackTenPercentNotified() == null || !isNotifiedObject.getTrackTenPercentNotified())) {
trackStatus = false;
}
}
// if (isIOProduct) {
// boolean tenPercent = (thisMonthAvailableTotalFlow.doubleValue() * 0.9 <= thisMonthEventNum.doubleValue()) && (thisMonthAvailableTotalFlow.doubleValue() * 0.95 > thisMonthEventNum.doubleValue());
// boolean exPercent = thisMonthAvailableTotalFlow.doubleValue() * 1 <= thisMonthEventNum.doubleValue();
// if (exPercent) {
// trackFlag = false;
// trackStatus = false;
// }else if (tenPercent && (isNotifiedObject.getIoTenPercentNotified() == null || !isNotifiedObject.getIoTenPercentNotified())) {
// trackStatus = false;
// }
// }
//是否流量用尽
if(trackFlag){
accountRestrict4Web.setIOFlowNotified(false);
} else{
accountRestrict4Web.setIOFlowNotified(true);
}
accountRestrict4Web.setAllowBehavior(false);
accountRestrict4Web.setTrackFlowNotified(trackStatus);
}
}
return accountRestrict4Web;
}
/**
* 查询广告效果监控产品的点击量和行为分析产品的点击量的方法
*/
@Override
public BigInteger getTotalNum(String createTime, String pastDate, String appids, String reportName, String
sumType) {
Long appId = 1218L;
Map<String, String> conditions = new HashMap<String, String>();
conditions.put("appids", appids);
conditions.put("startdate", createTime);
conditions.put("enddate", pastDate);
conditions.put("datatype", "list");
conditions.put("iscache", "0");
String url = Constant.reportUrl + "/api/trackingio/" + reportName + "/" + appId;
String responseJson = HttpClientUtil.doHttpPostRequest(url, "trackingio", conditions);
//System.out.println(reportName + "===========================" + responseJson);
HashMap<String, String> resultValMap = new HashMap<>();
String click_sum = "";
String s = "";
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();
//System.out.println(sumType + "=======================" + s);
} catch (JSONException e) {
e.printStackTrace();
}
return new BigInteger(s);
}
@Override
public Boolean isTrackAndIoBothExhaust(Long accountId) {
Long rootParent = accountRepository.findRootParentByAccountId(accountId).longValue();
List<App> apps = appRepository.listAppByRootAccount(rootParent);
Boolean isBothExhaust = false;
if (apps.size() != 0) {
Account rootAccount = accountRepository.findOne(rootParent);
StringBuffer appsStr = new StringBuffer();
for (App app : apps) {
appsStr = appsStr.append("\'" + app.getAppkey() + "\'" + ",");
}
String appids = appsStr.substring(0, appsStr.length() - 1);
/**
* 1.根据母帐号获取用户等级
*/
PackageType packageType = packageTypeRepository.findPackageTypeByRootParent(rootParent);
Boolean flowRestrict = rootAccount.getFlowRestrict();
String pastDate = rootAccount.getPastDate();
String trackBeginTime;
Date rootCreateTime = rootAccount.getCreateTime();
String createTime = DateUtil.format(rootCreateTime, "yyyy-MM-dd");
trackBeginTime = (rootAccount.getNewContract() != null && rootAccount.getNewContract()) ? rootAccount.getModifyPricelevelTime() : createTime;
String currentDateStr = DateUtil.getCurrentDateStr();
Date lastMonth = getLastMonth(1);
String firstDayOfLastMonth = DateUtil.getFirstDayOfMonth(lastMonth);
String lastDayOfLastMonth = DateUtil.getLastDayOfMonth(lastMonth);
String firstDayOfThisMonth = DateUtil.getFirstDayOfMonth(new Date());
BigInteger clickNum = getTotalNum(trackBeginTime, pastDate, appids, "account_track_flow_restrict", "click_sum");
IsNotified isNotifiedObject = isNotifiedRepository.findIsNotifiedByAccountId(rootParent);
String IOBeginTime = (isNotifiedObject != null && isNotifiedObject.getThisMonthUpgradeLevel() != null && isNotifiedObject.getThisMonthUpgradeLevel()) ? rootAccount.getModifyPricelevelTime() : firstDayOfThisMonth;
BigInteger lastMonthEventNum = getTotalNum(firstDayOfLastMonth, lastDayOfLastMonth, appids, "account_io_flow_restrict", "event_sum");
BigInteger thisMonthEventNum = getTotalNum(IOBeginTime, currentDateStr, appids, "account_io_flow_restrict", "event_sum");
BigInteger lastMonthExceedNum = (lastMonthEventNum.subtract(packageType.getIoFlow()).compareTo(new BigInteger("0")) == 1) ? lastMonthEventNum.subtract(packageType.getIoFlow()) : new BigInteger("0");
if (isNotifiedObject != null && isNotifiedObject.getThisMonthUpgradeLevel() != null && isNotifiedObject.getThisMonthUpgradeLevel()) {
lastMonthExceedNum = new BigInteger("0");
}
double v = packageType.getIoFlow().doubleValue() * 1 - lastMonthExceedNum.doubleValue();
DecimalFormat decimalFormat1 = new DecimalFormat("0");
String format = decimalFormat1.format(v);
BigInteger thisMonthAvailableTotalFlow = new BigInteger(format);
BigInteger thisMonthRemainingFlow = thisMonthAvailableTotalFlow.subtract(thisMonthEventNum);
if (!(packageType.getId() == 7 || packageType.getId() == 8 || packageType.getId() == 5) && flowRestrict) {
if (rootAccount.getAllowBehavior()) {
isBothExhaust = (packageType.getTrackFlow().doubleValue() - clickNum.doubleValue() < 0) && thisMonthRemainingFlow.doubleValue() < 0;
} else {
isBothExhaust = (packageType.getTrackFlow().doubleValue() - clickNum.doubleValue() < 0);
}
}
}
return isBothExhaust;
}
@Override
public Boolean isTrackExhaust(Long accountId) {
Long rootParent = accountRepository.findRootParentByAccountId(accountId).longValue();
List<App> apps = appRepository.listAppByRootAccount(rootParent);
Boolean isBothExhaust = false;
if (apps.size() != 0) {
Account rootAccount = accountRepository.findOne(rootParent);
StringBuffer appsStr = new StringBuffer();
for (App app : apps) {
appsStr = appsStr.append("\'" + app.getAppkey() + "\'" + ",");
}
String appids = appsStr.substring(0, appsStr.length() - 1);
/**
* 1.根据母帐号获取用户等级
*/
PackageType packageType = packageTypeRepository.findPackageTypeByRootParent(rootParent);
Boolean flowRestrict = rootAccount.getFlowRestrict();
String pastDate = rootAccount.getPastDate();
String trackBeginTime;
Date rootCreateTime = rootAccount.getCreateTime();
String createTime = DateUtil.format(rootCreateTime, "yyyy-MM-dd");
trackBeginTime = (rootAccount.getNewContract() != null && rootAccount.getNewContract()) ? rootAccount.getModifyPricelevelTime() : createTime;
BigInteger clickNum = getTotalNum(trackBeginTime, pastDate, appids, "account_track_flow_restrict", "click_sum");
if (!(packageType.getId() == 7 || packageType.getId() == 8 || packageType.getId() == 5) && flowRestrict) {
isBothExhaust = (packageType.getTrackFlow().doubleValue() - clickNum.doubleValue() < 0);
}
}
return isBothExhaust;
}
public Date getLastMonth(int i) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.MONTH, -i);
Date time = calendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String format = sdf.format(time);
calendar.setTime(new Date());
return time;
}
}