Commit 7be2b9ca by lzxry

tkio流量计算分摊报表

parent f106616f
...@@ -336,6 +336,7 @@ public class ContractController { ...@@ -336,6 +336,7 @@ public class ContractController {
@ResponseBody @ResponseBody
public ResultModel change(@CurrentAccount User loginAccount, public ResultModel change(@CurrentAccount User loginAccount,
@RequestBody ContractChange contract, HttpServletRequest request, @PathVariable String platform) { @RequestBody ContractChange contract, HttpServletRequest request, @PathVariable String platform) {
contract.setPlatform(platform);
Contract contract1 = service.change(loginAccount, contract, null); Contract contract1 = service.change(loginAccount, contract, null);
NewUserLogThread userlog = new NewUserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), contract1.getContractCode(), "修改套餐", "", contract1.toString(), request, platform); NewUserLogThread userlog = new NewUserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), contract1.getContractCode(), "修改套餐", "", contract1.toString(), request, platform);
userlog.start(); userlog.start();
......
...@@ -6,6 +6,9 @@ import org.springframework.data.jpa.repository.Modifying; ...@@ -6,6 +6,9 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.BigInteger;
public interface TkioFlowRepository extends JpaRepository<TkioFlow, Long> { public interface TkioFlowRepository extends JpaRepository<TkioFlow, Long> {
@Query(value = "select sum(flow) from tkio_flow where email = ?1 and contract_code = ?2", nativeQuery = true) @Query(value = "select sum(flow) from tkio_flow where email = ?1 and contract_code = ?2", nativeQuery = true)
...@@ -15,4 +18,7 @@ public interface TkioFlowRepository extends JpaRepository<TkioFlow, Long> { ...@@ -15,4 +18,7 @@ public interface TkioFlowRepository extends JpaRepository<TkioFlow, Long> {
@Modifying @Modifying
@Query(value = "delete from tkio_flow where email = ?1", nativeQuery = true) @Query(value = "delete from tkio_flow where email = ?1", nativeQuery = true)
void deleteByEmail(String email); void deleteByEmail(String email);
@Query(value = "select sum(flow) from tkio_flow where contract_code = ?1 and ds >= ?2 and ds <= ?3", nativeQuery = true)
BigDecimal sumFlowByContractCodeAndDs(String code, String startDate, String endDate);
} }
...@@ -1742,7 +1742,7 @@ public class ContractServiceImpl implements ContractService { ...@@ -1742,7 +1742,7 @@ public class ContractServiceImpl implements ContractService {
calculationFlow.setEmail(resource.getEmail()); calculationFlow.setEmail(resource.getEmail());
calculationFlow.setContractCode(resource.getContractCode()); calculationFlow.setContractCode(resource.getContractCode());
calculationFlow.setStatus(0); calculationFlow.setStatus(0);
calculationFlow.setTriggerType("止"); calculationFlow.setTriggerType("止");
calculationFlow.setCreateTime(DateUtil.getCurrentDateStr()); calculationFlow.setCreateTime(DateUtil.getCurrentDateStr());
calculationFlow.setIsAll(false); calculationFlow.setIsAll(false);
if(contracts==null || contracts.size()>0){ if(contracts==null || contracts.size()>0){
......
...@@ -54,6 +54,8 @@ public class ShareIncomeServiceImpl implements ShareIncomeService { ...@@ -54,6 +54,8 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
private AccountRepository accountRepository; private AccountRepository accountRepository;
@Autowired @Autowired
private AccountFlowRestrictService accountFlowRestrictService; private AccountFlowRestrictService accountFlowRestrictService;
@Autowired
private TkioFlowRepository tkioFlowRepository;
@Override @Override
public List<Contract> shareIncomeList(User loginAccount, String startDate, String endDate, String platform, String bodyCode, String serchName) { public List<Contract> shareIncomeList(User loginAccount, String startDate, String endDate, String platform, String bodyCode, String serchName) {
...@@ -131,7 +133,8 @@ public class ShareIncomeServiceImpl implements ShareIncomeService { ...@@ -131,7 +133,8 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
this.shareIncome4Contract(v, start, end); this.shareIncome4Contract(v, start, end);
}else{ }else{
//TKIO普通流量套餐 //TKIO普通流量套餐
this.shareIncome4ContractTwo(v, start, end); //this.shareIncome4ContractTwo(v, start, end);
this.shareIncome4ContractTKIO(v, start, end);
} }
break; break;
default:this.shareIncome4Contract(v, start, end);break; default:this.shareIncome4Contract(v, start, end);break;
...@@ -145,6 +148,281 @@ public class ShareIncomeServiceImpl implements ShareIncomeService { ...@@ -145,6 +148,281 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
return contracts; return contracts;
} }
private void shareIncome4ContractTKIO(Contract contract, DateTime start, DateTime end) {
DateTime[] selected = new DateTime[]{
start,
end};//用户筛选开始/结束日期
DateTime[] contractPart = new DateTime[]{
new DateTime(contract.getStartDate()),
new DateTime(contract.getEndDate())};//合同开始结束 时间
DateTime[] usePart = new DateTime[]{
selected[0].compareTo(contractPart[0]) <= 0 ? contractPart[0] : selected[0],
selected[1].compareTo(contractPart[1]) >= 0 ? contractPart[1] : selected[1]
};
usePart[0] = usePart[0].compareTo(usePart[1]) >= 0 ? usePart[1] : usePart[0];
Long excludTax = new BigDecimal(contract.getMoney() / 1.06)
.setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100L)).longValue();//不含税收入*100
contract.setIncomeExcludingTax(excludTax);
//计算单价,精准保留16位数
BigDecimal unitPriceAccurate = new BigDecimal(contract.getMoney()/1.06/contract.getTrackFlow())
.setScale(16, BigDecimal.ROUND_HALF_UP);
//四舍五入单价
double unitPrice = unitPriceAccurate.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//不限量套餐会在方法里面重新赋值单价
contract.setUnitPrice(unitPrice);
//作废合同 累计收入= 0
//中止合同 累计收入= 如果中止日期在选择日期之内,【合同开始日期-中止日期】,否则【合同开始日期-选择结束日期】,不需要考虑调整金
//晚录合同 累计收入= 同下
//正常合同 累计收入= 如果合同结束日期在选择日期之内,则直接取 [合同金额/1.06]得到总金额 ,否则 【合同开始日期-选择结束日期】
//作废合同处理
Contract cancleContract = this.cancledShareFlow(contract, unitPriceAccurate, usePart);
if (cancleContract != null) {
return;
}
//中止合同处理
Contract suspendContract = this.suspendShareFlow(contract, unitPriceAccurate,contractPart, usePart, selected);
if (suspendContract != null) {
return;
}
//晚录合同处理//正常合同处理
DateTime create = new DateTime(new DateTime(contract.getCreateTime()).toString("yyyy-MM-dd")); //录入时间点
DateTime[] creatPoints = new DateTime[]{
create, //录入日
create.dayOfMonth().withMinimumValue() //录入月1日
};
this.afterContractFlow(contract,unitPriceAccurate, contractPart, usePart, selected, creatPoints);
}
private void afterContractFlow(Contract contract, BigDecimal unitPriceAccurate, DateTime[] contractPart, DateTime[] usePart, DateTime[] selected, DateTime[] creatPoints) {
Long adjustmentFund = 0L;//调整金
contract.setClickFlow(0.0);
boolean isLateContract = false; // 是否为晚录合同(为了兼容历史数据 此处做冗余判断)
if (ContractStatusEnum.LATE.getKey().equals(contract.getStatus())) {
isLateContract = true;
} else if (checkLateContract(contractPart[0], creatPoints[0])) {
isLateContract = true;
contract.setStatus(ContractStatusEnum.LATE.getKey());
}
boolean isLater = true;
if (!isLateContract) {
//非合同晚录
BigDecimal clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),usePart[0].toString("yyyy-MM-dd"),usePart[1].toString("yyyy-MM-dd"));
int clickFlow = clickNum==null?0:clickNum.intValue();
BigDecimal clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
//区间点击数
contract.setClickFlow(new BigDecimal(clickFlow / 10000.0).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
}
//区间分摊收入
contract.setIntervaIncomeShare(unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
contract.setAdjustmentFund(0L);
contract.setIncomeShareAll(contract.getIntervaIncomeShare());
if(checkTwoTime(selected[0],contractPart[1]) && checkTwoTime(contractPart[1],selected[1])){
contract.setIncomeGross(contract.getIncomeExcludingTax());
}else{
//累计总收入
clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),contractPart[0].toString("yyyy-MM-dd"),selected[1].toString("yyyy-MM-dd"));
clickFlow = clickNum==null?0:clickNum.intValue();
contract.setIncomeGross(unitPriceAccurate.multiply(new BigDecimal(clickFlow / 10000.0 * 100)).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
}
isLater = false;
} else if (selected[1].isBefore(creatPoints[1])) {
//录入月1号之前 调整金为 0 分摊为 0
contract.setIntervaIncomeShare(0L);
contract.setAdjustmentFund(0L);
contract.setIncomeShareAll(contract.getIntervaIncomeShare());
contract.setClickFlow(0.0);
contract.setIncomeGross(0L);
contract.setStatus(ContractStatusEnum.LATE.getKey());
} else {
contract.setStatus(ContractStatusEnum.LATE.getKey());
//合同晚录
//所选时间范围内的分摊收入(录入月1号 即creatPoints[1] 开始计算)
DateTime useStart = creatPoints[1].compareTo(selected[0]) >= 0 ? creatPoints[1] : selected[0];
BigDecimal clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),useStart.toString("yyyy-MM-dd"),usePart[1].toString("yyyy-MM-dd"));
int clickFlow = clickNum==null?0:clickNum.intValue();
BigDecimal clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
//区间点击数
contract.setClickFlow(new BigDecimal(clickFlow / 10000.0).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
}
//区间分摊收入
contract.setIntervaIncomeShare(unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
if (checkTwoTime(selected[0], creatPoints[1]) && checkTwoTime(creatPoints[1], selected[1])) {
//所选时间范围包含 录入月 1 号 显示统计的调整金
//合同开始日期,合同录入日期的一号,-1就是上个月最后一天
//contractPart[0], creatPoints[1].plusDays(-1)
clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),contractPart[0].toString("yyyy-MM-dd"),creatPoints[1].plusDays(-1).toString("yyyy-MM-dd"));
clickFlow = clickNum==null?0:clickNum.intValue();
clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
//区间点击数
}
contract.setAdjustmentFund(unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
} else {
contract.setAdjustmentFund(0L);
}
if(checkTwoTime(selected[0],contractPart[1]) && checkTwoTime(contractPart[1],selected[1])){
contract.setIncomeGross(contract.getIncomeExcludingTax());
}else{
//累计总收入
clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),contractPart[0].toString("yyyy-MM-dd"),selected[1].plusDays(-1).toString("yyyy-MM-dd"));
clickFlow = clickNum==null?0:clickNum.intValue();
contract.setIncomeGross(unitPriceAccurate.multiply(new BigDecimal(clickFlow / 10000.0 * 100)).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
}
}
if (checkTwoTime(contractPart[1], selected[1])) {
//最后一日分摊收入计算处理(合同的最后一天,不管流量剩余多少,都算作做最后一天的收入)
Long lastDay;
DateTime lastDate[];
if (isLater) {
lastDate = new DateTime[]{creatPoints[1],contractPart[1].plusDays(-1)};
} else {
lastDate = new DateTime[]{contractPart[0],contractPart[1].plusDays(-1)};
}
//合同除去最后一天的流量
BigDecimal clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),lastDate[0].toString("yyyy-MM-dd"),lastDate[1].plusDays(-1).toString("yyyy-MM-dd"));
int clickFlow = clickNum==null?0:clickNum.intValue();
BigDecimal clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
}
long beforeTheMoney = unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
//最后一天的流量
clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),contractPart[1].toString("yyyy-MM-dd"),contractPart[1].plusDays(-1).toString("yyyy-MM-dd"));
clickFlow = clickNum==null?0:clickNum.intValue();
clickTenThousand = new BigDecimal(0);
if(clickFlow!=0)clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
long lastDayTheMoney = unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
//合同的税后总金额减去最后合同一日之前的钱和调整金
lastDay = contract.getIncomeExcludingTax() - contract.getAdjustmentFund() - beforeTheMoney;
//最后一日 或 包含最后一日 时
contract.setIntervaIncomeShare(contract.getIntervaIncomeShare() - lastDayTheMoney + lastDay);
}
contract.setIncomeShareAll(contract.getIntervaIncomeShare() + contract.getAdjustmentFund());
}
private Contract suspendShareFlow(Contract contract, BigDecimal unitPriceAccurate, DateTime[] contractPart, DateTime[] usePart, DateTime[] selected) {
if (!ContractStatusEnum.SUSPEND.getKey().equals(contract.getStatus())) {
return null;
}
ContractChange contractChange = contractChangeRepository.findByContentCode(ContractStatusEnum.SUSPEND.getValue(),
contract.getContractCode());
if (contractChange == null) {
contract.setAdjustmentFund(0L);
contract.setIntervaIncomeShare(0L);
contract.setIncomeShareAll(0L);
contract.setClickFlow(0.0);
return contract;
}
// 合同中止日
DateTime cancelDate = new DateTime(contractChange.getDs());
//中止操作入库时间
DateTime suspendDate = new DateTime(new DateTime(contractChange.getCreateTime()).toString("yyyy-MM-dd"));
contract.setClickFlow(0.0);
//调整金
Long adjustmentFund = 0L;
if (checkTwoTime(suspendDate, cancelDate)) {
adjustmentFund = 0L;
} else {
//查询调整金的点击,日期开始结束时间注意不能颠倒
BigDecimal clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),cancelDate.toString("yyyy-MM-dd"),suspendDate.toString("yyyy-MM-dd"));
int clickFlow = clickNum==null?0:clickNum.intValue();
BigDecimal clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
}
adjustmentFund = unitPriceAccurate.multiply(clickTenThousand.multiply(new BigDecimal(-1))).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
}
//调整金
contract.setAdjustmentFund(adjustmentFund);
DateTime usedEnd = usePart[1].compareTo(cancelDate) <= 0 ? usePart[1] : cancelDate;
//查询开始->中止日期的点击(有收入的点击),不包括调整金
BigDecimal clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),usePart[0].toString("yyyy-MM-dd"),usedEnd.toString("yyyy-MM-dd"));
int clickFlow = clickNum==null?0:clickNum.intValue();
BigDecimal clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
//区间点击数
contract.setClickFlow(new BigDecimal(clickFlow / 10000.0).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
}
//区间分摊收入,乘以100,前端除以100
long share = unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
contract.setIntervaIncomeShare(share);
contract.setIncomeShareAll(share+adjustmentFund);
if(checkTwoTime(selected[0],contractPart[1]) && checkTwoTime(contractPart[1],selected[1])){
contract.setIncomeGross(contract.getIncomeExcludingTax());
}else{
//累计总收入
clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),contractPart[0].toString("yyyy-MM-dd"),selected[1].toString("yyyy-MM-dd"));
clickFlow = clickNum==null?0:clickNum.intValue();
contract.setIncomeGross(unitPriceAccurate.multiply(new BigDecimal(clickFlow / 10000.0 * 100)).setScale(0, BigDecimal.ROUND_HALF_UP).longValue());
}
return contract;
}
private Contract cancledShareFlow(Contract contract, BigDecimal unitPriceAccurate, DateTime[] usePart) {
if (!ContractStatusEnum.CANCEL.getKey().equals(contract.getStatus())) {
return null;
}
ContractChange contractChange = contractChangeRepository.findByContentCode(ContractStatusEnum.CANCEL.getValue(), contract.getContractCode());
if (contractChange == null) {
contract.setAdjustmentFund(0L);
contract.setIntervaIncomeShare(0L);
contract.setIncomeShareAll(0L);
contract.setClickFlow(0.0);
return contract;
}
DateTime cancelDate = new DateTime(contractChange.getDs());//合同作废日
DateTime usedEnd = usePart[1].compareTo(cancelDate) <= 0 ? usePart[1] : cancelDate;
BigDecimal clickNum = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),usePart[0].toString("yyyy-MM-dd"),usedEnd.toString("yyyy-MM-dd"));
int clickFlow = clickNum==null?0:clickNum.intValue();
//区间点击数
contract.setClickFlow(0.0);
BigDecimal clickTenThousand = new BigDecimal(0);
if(clickFlow!=0){
clickTenThousand = new BigDecimal(clickFlow / 10000.0 * 100);
//区间点击数
contract.setClickFlow(new BigDecimal(clickFlow / 10000.0).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
}
//区间分摊收入,乘以100,前端除以100
long share = unitPriceAccurate.multiply(clickTenThousand).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
contract.setIntervaIncomeShare(share);
//调整金
contract.setAdjustmentFund(share*-1L);
//分摊总收入
contract.setIncomeShareAll(0L);
contract.setIncomeGross(0L);
return contract;
}
//需求禅道 1608 //需求禅道 1608
public void shareIncome4ContractTwo(Contract contract, DateTime start, DateTime end) { public void shareIncome4ContractTwo(Contract contract, DateTime start, DateTime end) {
DateTime[] selected = new DateTime[]{ DateTime[] selected = new DateTime[]{
......
...@@ -9,6 +9,7 @@ import common.repository.ContractChangeRepository; ...@@ -9,6 +9,7 @@ import common.repository.ContractChangeRepository;
import common.repository.ContractRepository; import common.repository.ContractRepository;
import common.repository.TkioFlowRepository; import common.repository.TkioFlowRepository;
import dic.ContractStatusEnum; import dic.ContractStatusEnum;
import org.joda.time.DateTime;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -57,126 +58,132 @@ public class TrackingFlowTask { ...@@ -57,126 +58,132 @@ public class TrackingFlowTask {
tkioFlowRepository.deleteByEmail(calculationFlow.getEmail()); tkioFlowRepository.deleteByEmail(calculationFlow.getEmail());
String email = calculationFlow.getEmail(); String email = calculationFlow.getEmail();
//查询用户下所有appkey //查询用户下所有appkey
Account account = accountRepository.findByEmail(email);
List<Account> accountList = accountRepository.findByRootParent(account.getRootParent());
List<Long> idList = new ArrayList<>();
for (Account ac : accountList) {
idList.add(ac.getId());
}
List<String> appkeys = appRepository.findAppkeysNotDebug(idList);
String appkeyStr = String.join("','", appkeys);
appkeyStr= "'"+appkeyStr+"'";
String ago = DateUtil.format(account.getCreateTime(),DateUtil.C_DATE_PATTON_DEFAULT);//查找最早一天的流量 try{
if(ago==null){ Account account = accountRepository.findByEmail(email);
calculationFlow.setStatus(2); List<Account> accountList = accountRepository.findByRootParent(account.getRootParent());
calculationFlowRepository.save(calculationFlow); List<Long> idList = new ArrayList<>();
continue; for (Account ac : accountList) {
} idList.add(ac.getId());
}
List<String> appkeys = appRepository.findAppkeysNotDebug(idList);
String appkeyStr = String.join("','", appkeys);
appkeyStr = "'" + appkeyStr + "'";
String ago = DateUtil.format(account.getCreateTime(), DateUtil.C_DATE_PATTON_DEFAULT);//查找最早一天的流量
if (ago == null) {
calculationFlow.setStatus(2);
calculationFlowRepository.save(calculationFlow);
continue;
}
int between = 0; int between = 0;
try { try {
between = DateUtil.daysBetween(ago, DateUtil.getBeforeDays(1))+1; between = DateUtil.daysBetween(ago, DateUtil.getBeforeDays(1)) + 1;
} catch (ParseException e) { } catch (ParseException e) {
logger.error("强转错误:",e); logger.error("强转错误:", e);
} }
int startInt = 0; int startInt = 0;
if(Integer.valueOf(DateUtil.getHH())>10){ if (Integer.valueOf(DateUtil.getHH()) > 10) {
startInt = -1; startInt = -1;
} }
for (int ii = between; ii > startInt; ii--) { for (int ii = between; ii > startInt; ii--) {
String yesterday = DateUtil.getBeforeDays(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");
if(clickNum!=null&&clickNum.longValue()>0){ if (clickNum != null && clickNum.longValue() > 0) {
List<Contract> contracts = contractRepository.findByPlatformAndEmail("tkio", email); List<Contract> contracts = contractRepository.findByPlatformAndEmail("tkio", email);
if(contracts.size()==1){//只有一个合同 if (contracts.size() == 1) {//只有一个合同
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contracts.get(0)); TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contracts.get(0));
if(tkioFlow!=null){ if (tkioFlow != null) {
tkioFlowList.add(tkioFlow); tkioFlowList.add(tkioFlow);
}
}else{
//多个合同
//看昨日被哪几个合同包含了
List<Contract> correlationContract = new ArrayList<>();
for (Contract contract : contracts) {
if (ContractStatusEnum.CANCEL.getKey().equals(contract.getStatus()) || ContractStatusEnum.SUSPEND.getKey().equals(contract.getStatus())) {
//中止或作废合同处理结束时间,以方便昨日流量的归属计算
ContractChange contractChange = contractChangeRepository.findByContentCode(ContractStatusEnum.CANCEL.getValue(), contract.getContractCode());
if(contractChange!=null){
contract.setEndDate(contractChange.getDs());
}
}
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()){
correlationContract.add(contract);
} }
} } else {
//多个合同时,进行排序,如果第一个合同流量超出就要看第二个合同,以此往下 //多个合同
if(correlationContract.size()>1){ //看昨日被哪几个合同包含了
//冒泡 List<Contract> correlationContract = new ArrayList<>();
//第一优先级:合同开始日期,第二优先级,合同编号大小 for (Contract contract : contracts) {
Contract[] contractsArray = new Contract[correlationContract.size()]; /*if (ContractStatusEnum.CANCEL.getKey().equals(contract.getStatus()) || ContractStatusEnum.SUSPEND.getKey().equals(contract.getStatus())) {
contractsArray = correlationContract.toArray(contractsArray); //中止或作废合同处理结束时间,以方便昨日流量的归属计算
ContractChange contractChange = contractChangeRepository.findByContentCode(ContractStatusEnum.CANCEL.getValue(), contract.getContractCode());
contractsArray = orderByContract(contractsArray); if (contractChange != null) {
contract.setEndDate(new DateTime(contractChange.getCreateTime()).toString("yyyy-MM-dd"));
for (int i = 0; i < contractsArray.length; i++) {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[i]);
if(tkioFlow!=null&& i<contractsArray.length-1 && tkioFlow.getCostFlow()>0L){//处理成本流量,如果超出了,依次算在下一个合同上
clickNum = BigInteger.valueOf(tkioFlow.getCostFlow());
tkioFlow.setCostFlow(null);
tkioFlowList.add(tkioFlow);
}else{
if(tkioFlow!=null){
tkioFlowList.add(tkioFlow);
} }
break; }*/
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()) {
correlationContract.add(contract);
} }
} }
}else{ //多个合同时,进行排序,如果第一个合同流量超出就要看第二个合同,以此往下
if(correlationContract.size()==0){//昨日不包含在所有合同中 if (correlationContract.size() > 1) {
//排序 //冒泡
Contract[] contractsArray = new Contract[contracts.size()]; //第一优先级:合同开始日期,第二优先级,合同编号大小
contractsArray = contracts.toArray(contractsArray); Contract[] contractsArray = new Contract[correlationContract.size()];
contractsArray = orderByContract(contractsArray); contractsArray = correlationContract.toArray(contractsArray);
//如果昨日日期在第一个合同之前,则归属到第一个合同的成本,其余区间都归属到前一个合同的成本上
for (int i = 0; i < contractsArray.length; i++) {
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==contractsArray.length-1){ contractsArray = orderByContract(contractsArray);
j = i;
}
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[j]); for (int i = 0; i < contractsArray.length; i++) {
if(tkioFlow!=null){ TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[i]);
if (tkioFlow != null && i < contractsArray.length - 1 && tkioFlow.getCostFlow() > 0L) {//处理成本流量,如果超出了,依次算在下一个合同上
clickNum = BigInteger.valueOf(tkioFlow.getCostFlow());
tkioFlow.setCostFlow(null);
tkioFlowList.add(tkioFlow);
} else {
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow); tkioFlowList.add(tkioFlow);
} }
break; break;
} }
} }
}else{ } else {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, correlationContract.get(0)); if (correlationContract.size() == 0) {//昨日不包含在所有合同中
if(tkioFlow!=null){ //排序
tkioFlowList.add(tkioFlow); 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) {
int j = 0;
if (i != 0) {
j = i - 1;
}
if (i == contractsArray.length - 1) {
j = i;
}
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contractsArray[j]);
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
}
break;
}
}
} else {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, correlationContract.get(0));
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
}
} }
}
}
} }
} }
} }
if (tkioFlowList.size() > 0) {
tkioFlowRepository.save(tkioFlowList);
}
calculationFlow.setStatus(2);
calculationFlowRepository.save(calculationFlow);
}catch(Exception e){
calculationFlow.setStatus(3);
calculationFlowRepository.save(calculationFlow);
} }
if(tkioFlowList.size()>0){
tkioFlowRepository.save(tkioFlowList);
}
calculationFlow.setStatus(2);
calculationFlowRepository.save(calculationFlow);
} }
} }
...@@ -212,13 +219,13 @@ public class TrackingFlowTask { ...@@ -212,13 +219,13 @@ public class TrackingFlowTask {
//看昨日被哪几个合同包含了 //看昨日被哪几个合同包含了
List<Contract> correlationContract = new ArrayList<>(); List<Contract> correlationContract = new ArrayList<>();
for (Contract contract : contracts) { for (Contract contract : contracts) {
if (ContractStatusEnum.CANCEL.getKey().equals(contract.getStatus()) || ContractStatusEnum.SUSPEND.getKey().equals(contract.getStatus())) { /*if (ContractStatusEnum.CANCEL.getKey().equals(contract.getStatus()) || ContractStatusEnum.SUSPEND.getKey().equals(contract.getStatus())) {
//中止或作废合同处理结束时间,以方便昨日流量的归属计算 //中止或作废合同处理结束时间,以方便昨日流量的归属计算
ContractChange contractChange = contractChangeRepository.findByContentCode(ContractStatusEnum.CANCEL.getValue(), contract.getContractCode()); ContractChange contractChange = contractChangeRepository.findByContentCode(ContractStatusEnum.CANCEL.getValue(), contract.getContractCode());
if(contractChange!=null){ if(contractChange!=null){
contract.setEndDate(contractChange.getDs()); contract.setEndDate(new DateTime(contractChange.getCreateTime()).toString("yyyy-MM-dd"));
} }
} }*/
String startDate = contract.getStartDate(); String startDate = contract.getStartDate();
String endDate = contract.getEndDate(); 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()){
...@@ -325,7 +332,7 @@ public class TrackingFlowTask { ...@@ -325,7 +332,7 @@ public class TrackingFlowTask {
tkioFlow.setEmail(contract.getEmail()); tkioFlow.setEmail(contract.getEmail());
tkioFlow.setContractCode(contract.getContractCode()); tkioFlow.setContractCode(contract.getContractCode());
try { try {
if(DateUtil.daysBetween(contract.getStartDate(), yesterday)<0){//昨日日期早于合同开始日期 if(DateUtil.daysBetween(contract.getStartDate(), yesterday)<0 || DateUtil.daysBetween(contract.getEndDate(), yesterday)>0){//昨日日期早于合同开始日期
//设置为成本流量 //设置为成本流量
tkioFlow.setCostFlow(clickNum.longValue()); tkioFlow.setCostFlow(clickNum.longValue());
}else{ }else{
......
...@@ -66,13 +66,13 @@ ...@@ -66,13 +66,13 @@
<bean id="syncTrackingFlowTask" class="track.task.TrackingFlowTask"></bean> <bean id="syncTrackingFlowTask" class="track.task.TrackingFlowTask"></bean>
<task:scheduled-tasks> <task:scheduled-tasks>
<!--定时同步昨日流量(每天10点执行一次)--> <!--定时同步昨日流量(每天10点执行一次)-->
<task:scheduled ref="syncTrackingFlowTask" method="task" cron="* * 10 * * ?"/> <task:scheduled ref="syncTrackingFlowTask" method="task" cron="0 0 10 * * ?"/>
</task:scheduled-tasks> </task:scheduled-tasks>
<bean id="syncCalculationFlowTask" class="track.task.TrackingFlowTask"></bean> <bean id="syncCalculationFlowTask" class="track.task.TrackingFlowTask"></bean>
<task:scheduled-tasks> <task:scheduled-tasks>
<!--定时同步流量(每7分钟执行一次)--> <!--定时同步流量(每7分钟执行一次)-->
<task:scheduled ref="syncCalculationFlowTask" method="syncFlow" cron="* 0/7 * * * ? "/> <task:scheduled ref="syncCalculationFlowTask" method="syncFlow" cron="0 0/7 * * * ? "/>
</task:scheduled-tasks> </task:scheduled-tasks>
</beans> </beans>
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