Commit bc55e647 by kangxiaoshan

流量查询方式更改

parent f5cf2cbd
......@@ -2,16 +2,7 @@ package common.repository;
import common.model.TkioFlowHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
public interface TkioFlowHistoryRepository extends JpaRepository<TkioFlowHistory, Long> {
@Transactional
@Modifying
@Query(value = " insert into tkio_flow_history select * from tkio_flow where email = ?1 and cost_flow > 0 ", nativeQuery = true)
void backDatasByEmail(String email);
}
package tkio.service;
import java.math.BigInteger;
import java.util.List;
public interface FlowService {
BigInteger getFlowByAccount(String yesterday, String yesterday1, List<String> appkeys);
}
package tkio.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import tkio.service.FlowService;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class FlowServiceImpl implements FlowService {
@Autowired
@Qualifier("tikoTemplate")
JdbcTemplate etlJdbcTemplate;
@Override
public BigInteger getFlowByAccount(String startDate, String pastDate, List<String> appkeys) {
NamedParameterJdbcTemplate nameJdbc = new NamedParameterJdbcTemplate(etlJdbcTemplate);
String sql = " SELECT sum(click_num+ IFNULL(num_impression,0)) as click_sum FROM tkio.measures_trackingio_new " +
" WHERE appid in (:appids ) AND ds>= :startdate and ds<= :enddate ";
Map param = new HashMap();
param.put("appids", appkeys);
param.put("startdate", startDate);
param.put("enddate", pastDate);
return nameJdbc.queryForObject(sql, param, BigInteger.class);
}
}
......@@ -11,6 +11,7 @@ import tkio.model.Account;
import tkio.repository.AccountRepository;
import tkio.repository.AppRepository;
import tkio.service.AccountFlowRestrictService;
import tkio.service.FlowService;
import util.Constant;
import util.DateUtil;
import util.StringUtil;
......@@ -36,8 +37,10 @@ public class TrackingFlowTask {
private AccountRepository accountRepository;
@Autowired
private AppRepository appRepository;
/*@Autowired
private AccountFlowRestrictService accountFlowRestrictService;*/
@Autowired
private AccountFlowRestrictService accountFlowRestrictService;
private FlowService flowService;
@Autowired
private TkioFlowRepository tkioFlowRepository;
@Autowired
......@@ -47,19 +50,17 @@ public class TrackingFlowTask {
@Autowired
private TkioFlowHistoryRepository tkioFlowHistoryRepository;
public void syncFlow(){
public void syncFlow() {
List<CalculationFlow> calculationFlows = calculationFlowRepository.findByStatus(0);
for (CalculationFlow calculationFlow : calculationFlows) {
List<TkioFlow> tkioFlowList = new ArrayList<>();
calculationFlow.setStatus(1);
calculationFlowRepository.save(calculationFlow);
//记录历史数据
tkioFlowHistoryRepository.backDatasByEmail(calculationFlow.getEmail());
tkioFlowRepository.deleteByEmail(calculationFlow.getEmail());
String email = calculationFlow.getEmail();
//查询用户下所有appkey
try{
try {
Account account = accountRepository.findByEmail(email);
List<Account> accountList = accountRepository.findByRootParent(account.getRootParent());
List<Long> idList = new ArrayList<>();
......@@ -89,7 +90,8 @@ public class TrackingFlowTask {
}
for (int ii = between; ii > startInt; ii--) {
String yesterday = DateUtil.getBeforeDays(ii);//昨日
BigInteger clickNum = accountFlowRestrictService.getTotalNum(yesterday, yesterday, appkeyStr, "account_track_flow_restrict", "click_sum");
//BigInteger clickNum = accountFlowRestrictService.getTotalNum(yesterday, yesterday, appkeyStr, "account_track_flow_restrict", "click_sum");
BigInteger clickNum = flowService.getFlowByAccount(yesterday,yesterday,appkeys);
if (clickNum != null && clickNum.longValue() > 0) {
List<Contract> contracts = contractRepository.findByPlatformAndEmail("tkio", email);
if (contracts.size() == 1) {//只有一个合同
......@@ -127,10 +129,10 @@ public class TrackingFlowTask {
for (int i = 0; i < contractsArray.length; i++) {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[i]);
if (tkioFlow != null && i < contractsArray.length - 1 && tkioFlow.getCostFlow() !=null && tkioFlow.getCostFlow() > 0L) {//处理成本流量,如果超出了,依次算在下一个合同上
if (tkioFlow != null && i < contractsArray.length - 1 && tkioFlow.getCostFlow() != null && tkioFlow.getCostFlow() > 0L) {//处理成本流量,如果超出了,依次算在下一个合同上
clickNum = BigInteger.valueOf(tkioFlow.getCostFlow());
tkioFlow.setCostFlow(null);
if(tkioFlow.getFlow()>0){
if (tkioFlow.getFlow() > 0) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
......@@ -187,20 +189,20 @@ public class TrackingFlowTask {
}
calculationFlow.setStatus(2);
calculationFlowRepository.save(calculationFlow);
}catch(Exception e){
logger.error("CalculationFlow:Id::"+calculationFlow.getId()+":全流量同步失败",e);
} catch (Exception e) {
logger.error("CalculationFlow:Id::" + calculationFlow.getId() + ":全流量同步失败", e);
calculationFlow.setStatus(3);
calculationFlowRepository.save(calculationFlow);
}
}
}
public void task(){
public void task() {
List<String> emails = contractRepository.findDistinctEmailByPlatform("tkio");
List<TkioFlow> tkioFlowList = new ArrayList<>();
String yesterday = DateUtil.getBeforeDays(1);//昨日
logger.info("昨日流量同步:"+yesterday);
logger.info("昨日流量同步:" + yesterday);
for (String email : emails) {
//查询用户下所有appkey
......@@ -212,21 +214,21 @@ public class TrackingFlowTask {
}
List<String> appkeys = appRepository.findAppkeysNotDebug(idList);
String appkeyStr = String.join("','", appkeys);
appkeyStr= "'"+appkeyStr+"'";
appkeyStr = "'" + appkeyStr + "'";
BigInteger clickNum = accountFlowRestrictService.getTotalNum(yesterday, yesterday, appkeyStr, "account_track_flow_restrict", "click_sum");
if(clickNum!=null&&clickNum.longValue()>0){
//BigInteger clickNum = accountFlowRestrictService.getTotalNum(yesterday, yesterday, appkeyStr, "account_track_flow_restrict", "click_sum");
BigInteger clickNum = flowService.getFlowByAccount(yesterday,yesterday,appkeys);
if (clickNum != null && clickNum.longValue() > 0) {
List<Contract> contracts = contractRepository.findByPlatformAndEmail("tkio", email);
if(contracts.size()==1){//只有一个合同
if (contracts.size() == 1) {//只有一个合同
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contracts.get(0));
if(tkioFlow!=null){
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
}else{//多个合同
} else {//多个合同
//看昨日被哪几个合同包含了
List<Contract> correlationContract = new ArrayList<>();
List<Contract> correlationContract = new ArrayList<>();
for (Contract contract : contracts) {
/*if (ContractStatusEnum.CANCEL.getKey().equals(contract.getStatus()) || ContractStatusEnum.SUSPEND.getKey().equals(contract.getStatus())) {
//中止或作废合同处理结束时间,以方便昨日流量的归属计算
......@@ -237,12 +239,12 @@ public class TrackingFlowTask {
}*/
String startDate = contract.getStartDate();
String endDate = contract.getEndDate();
if(DateUtil.getDate(yesterday).getTime()>=DateUtil.getDate(startDate).getTime() && DateUtil.getDate(yesterday).getTime()<=DateUtil.getDate(endDate).getTime()){
if (DateUtil.getDate(yesterday).getTime() >= DateUtil.getDate(startDate).getTime() && DateUtil.getDate(yesterday).getTime() <= DateUtil.getDate(endDate).getTime()) {
correlationContract.add(contract);
}
}
//多个合同时,进行排序,如果第一个合同流量超出就要看第二个合同,以此往下
if(correlationContract.size()>1){
if (correlationContract.size() > 1) {
//冒泡
//第一优先级:合同开始日期,第二优先级,合同编号大小
Contract[] contractsArray = new Contract[correlationContract.size()];
......@@ -252,15 +254,15 @@ public class TrackingFlowTask {
for (int i = 0; i < contractsArray.length; i++) {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[i]);
if(tkioFlow!=null&& i<contractsArray.length-1 && tkioFlow.getCostFlow()!=null && tkioFlow.getCostFlow()>0L){//处理成本流量,如果超出了,依次算在下一个合同上
if (tkioFlow != null && i < contractsArray.length - 1 && tkioFlow.getCostFlow() != null && tkioFlow.getCostFlow() > 0L) {//处理成本流量,如果超出了,依次算在下一个合同上
clickNum = BigInteger.valueOf(tkioFlow.getCostFlow());
tkioFlow.setCostFlow(null);
if(tkioFlow.getFlow()>0) {
if (tkioFlow.getFlow() > 0) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
}else{
if(tkioFlow!=null){
} else {
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
......@@ -268,27 +270,27 @@ public class TrackingFlowTask {
}
}
}else{
if(correlationContract.size()==0){//昨日不包含在所有合同中
} else {
if (correlationContract.size() == 0) {//昨日不包含在所有合同中
//排序
Contract[] contractsArray = new Contract[contracts.size()];
contractsArray = contracts.toArray(contractsArray);
contractsArray = orderByContract(contractsArray);
//如果昨日日期在第一个合同之前,则归属到第一个合同的成本,其余区间都归属到前一个合同的成本上
for (int i = 0; i < contractsArray.length; i++) {
if(DateUtil.getDate(yesterday).getTime()<=DateUtil.getDate(contractsArray[i].getStartDate()).getTime() || i==contractsArray.length-1){
if (DateUtil.getDate(yesterday).getTime() <= DateUtil.getDate(contractsArray[i].getStartDate()).getTime() || i == contractsArray.length - 1) {
int j = 0;
if(i!=0){
j=i-1;
if (i != 0) {
j = i - 1;
}
if(i==contractsArray.length-1){
if (i == contractsArray.length - 1) {
j = i;
}
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[j]);
if(tkioFlow!=null){
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
......@@ -297,10 +299,9 @@ public class TrackingFlowTask {
}
}else{
} else {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, correlationContract.get(0));
if(tkioFlow!=null){
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
......@@ -311,31 +312,30 @@ public class TrackingFlowTask {
}
}
if(tkioFlowList.size()>0){
if (tkioFlowList.size() > 0) {
//tkioFlowRepository.save(tkioFlowList);
}
}
//排序
public Contract[] orderByContract(Contract[] contractsArray){
for (int i = 0; i < contractsArray.length-1; i++) {
for (int j = 0; j < contractsArray.length-1-i; j++) {
public Contract[] orderByContract(Contract[] contractsArray) {
for (int i = 0; i < contractsArray.length - 1; i++) {
for (int j = 0; j < contractsArray.length - 1 - i; j++) {
String startDate1 = contractsArray[j].getSignedDate() == null ? contractsArray[j].getStartDate() : contractsArray[j].getSignedDate();
String startDate2 = contractsArray[j + 1].getSignedDate() == null ? contractsArray[j + 1].getStartDate() : contractsArray[j + 1].getSignedDate();
if(DateUtil.getDate(startDate1).getTime() > DateUtil.getDate(startDate2).getTime()){
if (DateUtil.getDate(startDate1).getTime() > DateUtil.getDate(startDate2).getTime()) {
Contract temp = contractsArray[j];
contractsArray[j] = contractsArray[j+1];
contractsArray[j] = contractsArray[j + 1];
contractsArray[j + 1] = temp;
}else if(DateUtil.getDate(startDate1).getTime() == DateUtil.getDate(startDate2).getTime()){
} else if (DateUtil.getDate(startDate1).getTime() == DateUtil.getDate(startDate2).getTime()) {
String contractCode = StringUtil.matchNumber(contractsArray[j].getContractCode());
String contractCodeMin = StringUtil.matchNumber(contractsArray[j+ 1].getContractCode());
if(Long.valueOf(contractCode)>Long.valueOf(contractCodeMin)){
String contractCodeMin = StringUtil.matchNumber(contractsArray[j + 1].getContractCode());
if (Long.valueOf(contractCode) > Long.valueOf(contractCodeMin)) {
Contract temp = contractsArray[j];
contractsArray[j] = contractsArray[j+1];
contractsArray[j] = contractsArray[j + 1];
contractsArray[j + 1] = temp;
}
}
......@@ -344,37 +344,37 @@ public class TrackingFlowTask {
return contractsArray;
}
public TkioFlow getTkioFlow(BigInteger clickNum,String yesterday,Contract contract){
public TkioFlow getTkioFlow(BigInteger clickNum, String yesterday, Contract contract) {
TkioFlow tkioFlow = new TkioFlow();
tkioFlow.setDs(yesterday);
tkioFlow.setEmail(contract.getEmail());
tkioFlow.setContractCode(contract.getContractCode());
try {
if(DateUtil.daysBetween(contract.getStartDate(), yesterday)<0 || DateUtil.daysBetween(contract.getEndDate(), yesterday)>0){//昨日日期早于合同开始日期
if (DateUtil.daysBetween(contract.getStartDate(), yesterday) < 0 || DateUtil.daysBetween(contract.getEndDate(), yesterday) > 0) {//昨日日期早于合同开始日期
//设置为成本流量
tkioFlow.setCostFlow(clickNum.longValue());
}else{
} else {
//查看历史总消耗流量是否超出
BigDecimal totalFlow = tkioFlowRepository.sumFlowByEmailAndContractCode(contract.getEmail(),contract.getContractCode());
totalFlow=totalFlow==null?new BigDecimal(0):totalFlow;
BigDecimal totalFlow = tkioFlowRepository.sumFlowByEmailAndContractCode(contract.getEmail(), contract.getContractCode());
totalFlow = totalFlow == null ? new BigDecimal(0) : totalFlow;
Double contractTrackFlow = contract.getTrackFlow() * 10000;
if(contractTrackFlow.longValue()-totalFlow.longValue()-clickNum.longValue()>=0 || contract.getPriceLevel()==Constant.tkioPriceLevelNotLimit){
if (contractTrackFlow.longValue() - totalFlow.longValue() - clickNum.longValue() >= 0 || contract.getPriceLevel() == Constant.tkioPriceLevelNotLimit) {
tkioFlow.setFlow(clickNum.longValue());
}else{
} else {
tkioFlow.setFlow(contractTrackFlow.longValue() - totalFlow.longValue());
tkioFlow.setCostFlow(clickNum.longValue()-tkioFlow.getFlow());
tkioFlow.setCostFlow(clickNum.longValue() - tkioFlow.getFlow());
}
}
return tkioFlow;
} catch (ParseException e) {
logger.error("合同编号-"+contract.getContractCode()+"-同步昨日流量错误:",e);
logger.error("合同编号-" + contract.getContractCode() + "-同步昨日流量错误:", e);
}
return null;
}
public static void main(String[] args) throws ParseException {
String ago = "2020-10-01";
int between = DateUtil.daysBetween(ago, DateUtil.getBeforeDays(1))+1;
int between = DateUtil.daysBetween(ago, DateUtil.getBeforeDays(1)) + 1;
System.out.println(between);
for (int i = between; i > 0; i--) {
System.out.println(DateUtil.getBeforeDays(i));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment