Commit f8d9a91c by lzxry

Merge branch 'bugfix_1608_new'

parents 66eb977c 32c44e53
......@@ -336,6 +336,7 @@ public class ContractController {
@ResponseBody
public ResultModel change(@CurrentAccount User loginAccount,
@RequestBody ContractChange contract, HttpServletRequest request, @PathVariable String platform) {
contract.setPlatform(platform);
Contract contract1 = service.change(loginAccount, contract, null);
NewUserLogThread userlog = new NewUserLogThread(loginAccount.getEmail(), loginAccount.getName(), OperateObjectTypeEnum.CUSTOMER.getKey(), contract1.getContractCode(), "修改套餐", "", contract1.toString(), request, platform);
userlog.start();
......@@ -643,7 +644,7 @@ public class ContractController {
rowBody.createCell(8).setCellValue(df.format(contract.getMoney()));
rowBody.createCell(9).setCellValue(contract.getTrackFlow());
rowBody.createCell(10).setCellValue(contract.getUnitPrice());
rowBody.createCell(11).setCellValue(contract.getClickFlow());
rowBody.createCell(11).setCellValue(contract.getClickFlow()==null?0.0:contract.getClickFlow());
rowBody.createCell(12).setCellValue(df.format(contract.getIntervaIncomeShare() / 100.0));
rowBody.createCell(13).setCellValue(df.format(contract.getAdjustmentFund() / 100.0));
rowBody.createCell(14).setCellValue(df.format(contract.getIncomeShareAll() / 100.0));
......
package common.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author liyin
* @description
* @date
*/
@Entity
public class CalculationFlow {
private Long id;
private String email;
private String contractCode;
private String triggerType;
private Boolean isAll;
private Integer status;
private String createTime;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContractCode() {
return contractCode;
}
public void setContractCode(String contractCode) {
this.contractCode = contractCode;
}
public String getTriggerType() {
return triggerType;
}
public void setTriggerType(String triggerType) {
this.triggerType = triggerType;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Boolean getIsAll() {
return isAll;
}
public void setIsAll(Boolean isAll) {
this.isAll = isAll;
}
public String getCreateTime() {
return createTime;
}
public void setCreateTime(String createTime) {
this.createTime = createTime;
}
}
......@@ -94,7 +94,7 @@ public class Contract {
private String barrioName;//行政区域名称
private String belongGroup;//隶属集团
private Double trackFlow;//流量,tkio的
private Double trackFlow;//流量,tkio的,万单位
private Double unitPrice;//单价,tkio
private Double clickFlow;//区间点击数,tkio
......
package common.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author liyin
* @description
* @date
*/
@Entity
public class TkioFlow {
@Id
@GeneratedValue
private Long id;
private String ds;
private String email;
private String contractCode;
private Long flow;
private Long costFlow;
public String getDs() {
return ds;
}
public void setDs(String ds) {
this.ds = ds;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getContractCode() {
return contractCode;
}
public void setContractCode(String contractCode) {
this.contractCode = contractCode;
}
public Long getFlow() {
return flow;
}
public void setFlow(Long flow) {
this.flow = flow;
}
public Long getCostFlow() {
return costFlow;
}
public void setCostFlow(Long costFlow) {
this.costFlow = costFlow;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
package common.repository;
import common.model.CalculationFlow;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Transactional
public interface CalculationFlowRepository extends JpaRepository<CalculationFlow,Long> {
@Query(value = "select * from calculation_flow where status = ?1",nativeQuery = true)
List<CalculationFlow> findByStatus(int status);
}
......@@ -122,4 +122,10 @@ public interface ContractRepository extends JpaRepository<Contract, Long> {
@Query(value = "select * from contract where contract_code=?1 limit 1 ", nativeQuery = true)
Contract checkByCode(String code);
@Query(value = "select distinct email from contract where platform = ?1", nativeQuery = true)
List<String> findDistinctEmailByPlatform(String platform);
@Query(value = "SELECT * from contract where platform = ?1 and email = ?2", nativeQuery = true)
List<Contract> findByPlatformAndEmail(String platform, String email);
}
package common.repository;
import common.model.TkioFlow;
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 TkioFlowRepository extends JpaRepository<TkioFlow, Long> {
@Query(value = "select sum(flow) from tkio_flow where email = ?1 and contract_code = ?2", nativeQuery = true)
BigDecimal sumFlowByEmailAndContractCode(String email, String contractCode);
@Transactional
@Modifying
@Query(value = "delete from tkio_flow where email = ?1", nativeQuery = true)
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);
}
......@@ -220,6 +220,9 @@ public class ContractServiceImpl implements ContractService {
@Autowired
private BarrioCityRepository barrioCityRepository;
@Autowired
private CalculationFlowRepository calculationFlowRepository;
@Override
public Map<String, Object> checkAccount(String email, String platfrom) {
......@@ -564,11 +567,33 @@ public class ContractServiceImpl implements ContractService {
//this.calculateShareIncome(resource);//判断是否计算调整金
//判断库里是否已有合同的主账号
if("tkio".equals(resource.getPlatform())){
List<Contract> contracts = contractRepository.findByPlatformAndEmail(resource.getPlatform(), resource.getEmail());
CalculationFlow calculationFlow = new CalculationFlow();
calculationFlow.setEmail(resource.getEmail());
calculationFlow.setContractCode(resource.getContractCode());
calculationFlow.setStatus(0);
calculationFlow.setTriggerType("新建");
calculationFlow.setCreateTime(DateUtil.getCurrentDateStr());
calculationFlow.setIsAll(false);
if(contracts==null || contracts.size()>0){
for (Contract contract : contracts) {
if(intersection(contract,resource)){//有交集
calculationFlow.setIsAll(true);
break;
}
}
}
calculationFlowRepository.save(calculationFlow);
}
resource = contractRepository.save(resource);
this.saveContractRelations(resource, resource.getId());
List<String> codes = contractRepository.findContractBodyNames(resource.getCustomerBody(), resource.getContractCode());
if (!codes.isEmpty()) {
// 合同编号已存在
......@@ -866,9 +891,40 @@ public class ContractServiceImpl implements ContractService {
resource.setId(contract.getId());
//判断库里是否已有合同的主账号
if("tkio".equals(resource.getPlatform()) && (contract.getStartDate()!=resource.getStartDate()&&contract.getEndDate()!=resource.getEndDate())){
List<Contract> contracts = contractRepository.findByPlatformAndEmail(resource.getPlatform(), resource.getEmail());
CalculationFlow calculationFlow = new CalculationFlow();
calculationFlow.setEmail(resource.getEmail());
calculationFlow.setContractCode(resource.getContractCode());
calculationFlow.setStatus(0);
calculationFlow.setTriggerType("编辑");
calculationFlow.setCreateTime(DateUtil.getCurrentDateStr(DateUtil.C_TIME_PATTON_DEFAULT));
calculationFlow.setIsAll(false);
if(contracts==null || contracts.size()>0){
for (Contract contract1 : contracts) {
if(contract1.getId()!=contract.getId()){
if(intersection(contract1,resource) || intersection(contract1,contract)){//有交集
calculationFlow.setIsAll(true);
break;
}
}
}
}
calculationFlowRepository.save(calculationFlow);
}
return contractRepository.save(resource);
}
public Boolean intersection(Contract contract1 ,Contract contract2 ){
return (DateUtil.getDate(contract1.getStartDate()).getTime()<=DateUtil.getDate(contract2.getStartDate()).getTime()&&
DateUtil.getDate(contract1.getEndDate()).getTime()>=DateUtil.getDate(contract2.getStartDate()).getTime() )||
(DateUtil.getDate(contract1.getStartDate()).getTime()<=DateUtil.getDate(contract2.getEndDate()).getTime()&&
DateUtil.getDate(contract1.getEndDate()).getTime()>=DateUtil.getDate(contract2.getEndDate()).getTime()) ||
(DateUtil.getDate(contract2.getStartDate()).getTime()<=DateUtil.getDate(contract1.getStartDate()).getTime()&&
DateUtil.getDate(contract2.getEndDate()).getTime()>=DateUtil.getDate(contract1.getEndDate()).getTime());
}
private List<ChangeDelDetail> changeDelInfoForContract(Contract byfind, Contract resource, Long pid, String ip) {
......@@ -1679,6 +1735,28 @@ public class ContractServiceImpl implements ContractService {
resource.setContent(ContractStatusEnum.SUSPEND.getValue());
contract.setStatus(ContractStatusEnum.SUSPEND.getKey());
showTip = true;
//判断库里是否已有合同的主账号
if("tkio".equals(resource.getPlatform())){
List<Contract> contracts = contractRepository.findByPlatformAndEmail(resource.getPlatform(), resource.getEmail());
CalculationFlow calculationFlow = new CalculationFlow();
calculationFlow.setEmail(resource.getEmail());
calculationFlow.setContractCode(resource.getContractCode());
calculationFlow.setStatus(0);
calculationFlow.setTriggerType("中止");
calculationFlow.setCreateTime(DateUtil.getCurrentDateStr());
calculationFlow.setIsAll(false);
if(contracts==null || contracts.size()>0){
for (Contract contract1 : contracts) {
if(contract1.getId()!=contract.getId()){
if(intersection(contract1,contract)){//有交集
calculationFlow.setIsAll(true);
break;
}
}
}
}
calculationFlowRepository.save(calculationFlow);
}
}
}
......@@ -2321,6 +2399,7 @@ public class ContractServiceImpl implements ContractService {
Map<String, String> codeUniqueDic = new HashMap<>();
List<String> accountsEmail = new ArrayList<>();
List<String> moreEmail = new ArrayList<>();
for (int j = 1; j <= rowNumber; j++) {
Row row_data = sheet.getRow(j);
Object[] s_data = new Object[titleKey.size() + extend_size];
......@@ -2434,6 +2513,9 @@ public class ContractServiceImpl implements ContractService {
s_data[w] = dataSTR;
if ("email".equals(sheetTitle) && !StringUtils.isEmpty(dataSTR) && "tkio".equals(platformexcl)) {
if(!accountsEmail.contains(dataSTR.trim())){
moreEmail.add(dataSTR.trim());
}
accountsEmail.add(dataSTR.trim());
}
}
......@@ -2509,7 +2591,17 @@ public class ContractServiceImpl implements ContractService {
// TransactionStatus transactionStatus = transactionUtils.begin();
jdbcTemplate.batchUpdate(sql.toString(), args_data);
// transactionUtils.commit(transactionStatus);
if("tkio".equals(platform)){
for (String email : moreEmail) {
CalculationFlow calculationFlow = new CalculationFlow();
calculationFlow.setEmail(email);
calculationFlow.setStatus(0);
calculationFlow.setTriggerType("导入");
calculationFlow.setCreateTime(DateUtil.getCurrentDateStr());
calculationFlow.setIsAll(true);
calculationFlowRepository.save(calculationFlow);
}
}
return ResultModel.OK();
}
......
......@@ -56,7 +56,9 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
private AccountFlowRestrictService accountFlowRestrictService;
@Autowired
private PackageBaseRepository packageBaseRepository;
@Autowired
private TkioFlowRepository tkioFlowRepository;
@Override
public List<Contract> shareIncomeList(User loginAccount, String startDate, String endDate, String platform, String bodyCode, String serchName) {
......@@ -130,12 +132,13 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
switch (ContractBranchUtil.getValue(platform)){
case "type_one":this.shareIncome4Contract(v, start, end);break;
case "type_two":
if(v.getPriceLevel()==18){
if(v.getPriceLevel()==16){
//TKIO不限量套餐
this.shareIncome4Contract(v, start, end);
}else{
//TKIO普通流量套餐
this.shareIncome4ContractTwo(v, start, end);
//this.shareIncome4ContractTwo(v, start, end);
this.shareIncome4ContractTKIO(v, start, end);
}
break;
default:this.shareIncome4Contract(v, start, end);break;
......@@ -151,6 +154,285 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
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());
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();
contract.setClickFlow(new BigDecimal(clickFlow / 10000.0).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue());
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);
//区间点击数
BigDecimal clickNumReal = tkioFlowRepository.sumFlowByContractCodeAndDs(contract.getContractCode(),selected[0].toString("yyyy-MM-dd"),usePart[1].toString("yyyy-MM-dd"));
int clickNumRealFlow = clickNumReal==null?0:clickNumReal.intValue();
contract.setClickFlow(new BigDecimal(clickNumRealFlow / 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
public void shareIncome4ContractTwo(Contract contract, DateTime start, DateTime end) {
DateTime[] selected = new DateTime[]{
......@@ -471,6 +753,10 @@ public class ShareIncomeServiceImpl implements ShareIncomeService {
//处理精度
BigDecimal dayShareIncome = new BigDecimal(excludTax * 1.0 / contractAllDay);
if("tkio".equals(contract.getPlatform())){//设置tkio无限流量的日单价
contract.setUnitPrice(dayShareIncome.divide(new BigDecimal(100)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue());
}
//作废合同处理
Contract cancleContract = this.cancledShare(contract, contractAllDay, dayShareIncome, contractPart, usePart, selected);
if (cancleContract != null) {
......
package track.task;
import common.model.Account4Web;
import common.model.TrackAccount4Web;
import common.repository.TrackAccount4WebRepository;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import track.model.Account;
import track.repository.TrackAccountRepository;
......
package track.task;
import common.model.CalculationFlow;
import common.model.Contract;
import common.model.TkioFlow;
import common.repository.CalculationFlowRepository;
import common.repository.ContractChangeRepository;
import common.repository.ContractRepository;
import common.repository.TkioFlowRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import tkio.model.Account;
import tkio.repository.AccountRepository;
import tkio.repository.AppRepository;
import tkio.service.AccountFlowRestrictService;
import util.DateUtil;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
/**
* @author liyin
* @description
* @date
*/
public class TrackingFlowTask {
private final Logger logger = LoggerFactory.getLogger(TrackingFlowTask.class);
@Autowired
private ContractRepository contractRepository;
@Autowired
private AccountRepository accountRepository;
@Autowired
private AppRepository appRepository;
@Autowired
private AccountFlowRestrictService accountFlowRestrictService;
@Autowired
private TkioFlowRepository tkioFlowRepository;
@Autowired
private ContractChangeRepository contractChangeRepository;
@Autowired
private CalculationFlowRepository calculationFlowRepository;
public void syncFlow(){
List<CalculationFlow> calculationFlows = calculationFlowRepository.findByStatus(0);
for (CalculationFlow calculationFlow : calculationFlows) {
List<TkioFlow> tkioFlowList = new ArrayList<>();
calculationFlow.setStatus(1);
calculationFlowRepository.save(calculationFlow);
tkioFlowRepository.deleteByEmail(calculationFlow.getEmail());
String email = calculationFlow.getEmail();
//查询用户下所有appkey
try{
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);//查找最早一天的流量
if (ago == null) {
calculationFlow.setStatus(2);
calculationFlowRepository.save(calculationFlow);
continue;
}
int between = 0;
try {
between = DateUtil.daysBetween(ago, DateUtil.getBeforeDays(1)) + 1;
} catch (ParseException e) {
logger.error("强转错误:", e);
}
int startInt = 0;
if (Integer.valueOf(DateUtil.getHH()) > 10) {
startInt = -1;
}
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");
if (clickNum != null && clickNum.longValue() > 0) {
List<Contract> contracts = contractRepository.findByPlatformAndEmail("tkio", email);
if (contracts.size() == 1) {//只有一个合同
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contracts.get(0));
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(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(new DateTime(contractChange.getCreateTime()).toString("yyyy-MM-dd"));
}
}*/
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);
}
}
//多个合同时,进行排序,如果第一个合同流量超出就要看第二个合同,以此往下
if (correlationContract.size() > 1) {
//冒泡
//第一优先级:合同开始日期,第二优先级,合同编号大小
Contract[] contractsArray = new Contract[correlationContract.size()];
contractsArray = correlationContract.toArray(contractsArray);
contractsArray = orderByContract(contractsArray);
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) {//处理成本流量,如果超出了,依次算在下一个合同上
clickNum = BigInteger.valueOf(tkioFlow.getCostFlow());
tkioFlow.setCostFlow(null);
if(tkioFlow.getFlow()>0){
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
} else {
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
break;
}
}
} 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) {
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);
tkioFlowRepository.save(tkioFlow);
}
break;
}
}
} else {
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, correlationContract.get(0));
if (tkioFlow != null) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
}
}
}
}
}
if (tkioFlowList.size() > 0) {
//tkioFlowRepository.save(tkioFlowList);
}
calculationFlow.setStatus(2);
calculationFlowRepository.save(calculationFlow);
}catch(Exception e){
logger.error("CalculationFlow:Id::"+calculationFlow.getId()+":全流量同步失败",e);
calculationFlow.setStatus(3);
calculationFlowRepository.save(calculationFlow);
}
}
}
public void task(){
List<String> emails = contractRepository.findDistinctEmailByPlatform("tkio");
List<TkioFlow> tkioFlowList = new ArrayList<>();
for (String email : emails) {
//查询用户下所有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 yesterday = DateUtil.getBeforeDays(1);//昨日
BigInteger clickNum = accountFlowRestrictService.getTotalNum(yesterday, yesterday, appkeyStr, "account_track_flow_restrict", "click_sum");
if(clickNum!=null&&clickNum.longValue()>0){
List<Contract> contracts = contractRepository.findByPlatformAndEmail("tkio", email);
if(contracts.size()==1){//只有一个合同
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, contracts.get(0));
if(tkioFlow!=null){
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(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(new DateTime(contractChange.getCreateTime()).toString("yyyy-MM-dd"));
}
}*/
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);
}
}
//多个合同时,进行排序,如果第一个合同流量超出就要看第二个合同,以此往下
if(correlationContract.size()>1){
//冒泡
//第一优先级:合同开始日期,第二优先级,合同编号大小
Contract[] contractsArray = new Contract[correlationContract.size()];
contractsArray = correlationContract.toArray(contractsArray);
contractsArray = orderByContract(contractsArray);
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){//处理成本流量,如果超出了,依次算在下一个合同上
clickNum = BigInteger.valueOf(tkioFlow.getCostFlow());
tkioFlow.setCostFlow(null);
if(tkioFlow.getFlow()>0) {
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
}else{
if(tkioFlow!=null){
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
break;
}
}
}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){
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);
tkioFlowRepository.save(tkioFlow);
}
break;
}
}
}else{
TkioFlow tkioFlow = getTkioFlow(clickNum, yesterday, correlationContract.get(0));
if(tkioFlow!=null){
tkioFlowList.add(tkioFlow);
tkioFlowRepository.save(tkioFlow);
}
}
}
}
}
}
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++) {
if(DateUtil.getDate(contractsArray[j].getStartDate()).getTime() > DateUtil.getDate(contractsArray[j + 1].getStartDate()).getTime()){
Contract temp = contractsArray[j];
contractsArray[j] = contractsArray[j+1];
contractsArray[j + 1] = temp;
}else if(DateUtil.getDate(contractsArray[j].getStartDate()).getTime() == DateUtil.getDate(contractsArray[j + 1].getStartDate()).getTime()){
String contractCode = contractsArray[j].getContractCode().replaceAll("BJ-TKIO-", "").replaceAll("-", "");
String contractCodeMin = contractsArray[j + 1].getContractCode().replaceAll("BJ-TKIO-", "").replaceAll("-", "");
if(Long.valueOf(contractCode)>Long.valueOf(contractCodeMin)){
Contract temp = contractsArray[j];
contractsArray[j] = contractsArray[j+1];
contractsArray[j + 1] = temp;
}
}
}
}
return contractsArray;
}
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){//昨日日期早于合同开始日期
//设置为成本流量
tkioFlow.setCostFlow(clickNum.longValue());
}else{
//查看历史总消耗流量是否超出
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){
tkioFlow.setFlow(clickNum.longValue());
}else{
tkioFlow.setFlow(contractTrackFlow.longValue() - totalFlow.longValue());
tkioFlow.setCostFlow(clickNum.longValue()-tkioFlow.getFlow());
}
}
return tkioFlow;
} catch (ParseException 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;
System.out.println(between);
for (int i = between; i > 0; i--) {
System.out.println(DateUtil.getBeforeDays(i));
}
}
}
......@@ -63,4 +63,16 @@
<!--<task:scheduled ref="accountTaskCheck" method="task" cron="0 10 8 * * ?"/>-->
<!--</task:scheduled-tasks>-->
<bean id="syncTrackingFlowTask" class="track.task.TrackingFlowTask"></bean>
<task:scheduled-tasks>
<!--定时同步昨日流量(每天10点执行一次)-->
<task:scheduled ref="syncTrackingFlowTask" method="task" cron="0 0 10 * * ?"/>
</task:scheduled-tasks>
<bean id="syncCalculationFlowTask" class="track.task.TrackingFlowTask"></bean>
<task:scheduled-tasks>
<!--定时同步流量(每7分钟执行一次)-->
<task:scheduled ref="syncCalculationFlowTask" method="syncFlow" cron="0 0/7 * * * ? "/>
</task:scheduled-tasks>
</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