|
|
|
@ -1,8 +1,10 @@ |
|
|
|
package com.bnyer.img.service.impl; |
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.bnyer.common.core.exception.ServiceException; |
|
|
|
import com.bnyer.common.core.utils.StringUtils; |
|
|
|
import com.bnyer.common.redis.service.RedisService; |
|
|
|
import com.bnyer.common.redis.service.RedissonService; |
|
|
|
import com.bnyer.img.constants.ProfitOrderConstant; |
|
|
|
import com.bnyer.img.constants.RedisKeyConstant; |
|
|
|
import com.bnyer.img.domain.Creator; |
|
|
|
@ -12,13 +14,13 @@ import com.bnyer.img.dto.ProfitVerifyOrderDto; |
|
|
|
import com.bnyer.img.dto.ProfitVerifyOrderInsertDto; |
|
|
|
import com.bnyer.img.dto.ProfitVerifyOrderPageDto; |
|
|
|
import com.bnyer.img.dto.ProfitVerifyOrderUpdateDto; |
|
|
|
import com.bnyer.img.mapper.CreatorMapper; |
|
|
|
import com.bnyer.img.mapper.ProfitVerifyOrderMapper; |
|
|
|
import com.bnyer.img.service.CreatorProfitService; |
|
|
|
import com.bnyer.img.service.CreatorService; |
|
|
|
import com.bnyer.img.service.ProfitVerifyOrderService; |
|
|
|
import com.bnyer.img.vo.ConfirmProfitVo; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.redisson.api.RLock; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
@ -28,6 +30,7 @@ import java.text.SimpleDateFormat; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
@Service |
|
|
|
@Slf4j |
|
|
|
@ -46,7 +49,7 @@ public class ProfitVerifyOrderServiceImpl implements ProfitVerifyOrderService { |
|
|
|
private CreatorService creatorService; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private CreatorMapper creatorMapper; |
|
|
|
private RedissonService redissonService; |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
@ -104,59 +107,76 @@ public class ProfitVerifyOrderServiceImpl implements ProfitVerifyOrderService { |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public boolean verify(ProfitVerifyOrderDto params) { |
|
|
|
if(params.getVerifyStatus().equals(ProfitOrderConstant.PASS)){ |
|
|
|
//审核通过计算收益
|
|
|
|
//昨日总收益的80%作为广告收益
|
|
|
|
BigDecimal yesterdayTotalAdProfit = params.getAmt().multiply(BigDecimal.valueOf(0.8)); |
|
|
|
//昨日总收益的10%作为邀请收益
|
|
|
|
BigDecimal yesterdayTotalInviteProfit = params.getAmt().multiply(BigDecimal.valueOf(0.1)); |
|
|
|
//获取昨日日期
|
|
|
|
Date yesterday = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24); |
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
String date = simpleDateFormat.format(yesterday); |
|
|
|
String tiktokKey = RedisKeyConstant.TIKTOK_IMG_TOTAL_DOWNLOAD_NUM_KEY + date; |
|
|
|
String hashKey = params.getAppType()+":"+ params.getPlatform(); |
|
|
|
if(redisService.hasHashKey(tiktokKey, hashKey)){ |
|
|
|
//存在昨日平台下载数量
|
|
|
|
Integer platformDownloadNum = redisService.getCacheMapValue(tiktokKey, hashKey); |
|
|
|
//广告单价
|
|
|
|
BigDecimal adPrice = yesterdayTotalAdProfit.divide(BigDecimal.valueOf(platformDownloadNum), 3, BigDecimal.ROUND_DOWN); |
|
|
|
//邀请单价
|
|
|
|
BigDecimal invitePrice = yesterdayTotalInviteProfit.divide(BigDecimal.valueOf(platformDownloadNum), 3, BigDecimal.ROUND_DOWN); |
|
|
|
//查出昨日对应平台的所有广告待入账信息
|
|
|
|
List<CreatorProfit> adProfits = creatorProfitService.queryPreProfit(params.getPlatform(), params.getAppType(), "0",date); |
|
|
|
if(adProfits.size() > 0){ |
|
|
|
for (CreatorProfit adProfit : adProfits) { |
|
|
|
//设置广告收益
|
|
|
|
adProfit.setProfit(BigDecimal.valueOf(adProfit.getDownloadNum()).multiply(adPrice)); |
|
|
|
adProfit.setConfirmStatus("0"); |
|
|
|
adProfit.setIncomeTime(new Date()); |
|
|
|
adProfit.setStatus("1"); |
|
|
|
creatorProfitService.update(adProfit); |
|
|
|
//获取分布式锁
|
|
|
|
RLock lock = redissonService.getRLock(RedisKeyConstant.VERIFY_PROFIT_LOCK_KEY + params.getId()); |
|
|
|
try{ |
|
|
|
if(lock.tryLock(0L, 30L, TimeUnit.SECONDS)){ |
|
|
|
log.info("锁获取成功,开始执行审核收益操作"); |
|
|
|
//审核通过计算收益
|
|
|
|
//昨日总收益的80%作为广告收益
|
|
|
|
BigDecimal yesterdayTotalAdProfit = params.getAmt().multiply(BigDecimal.valueOf(0.8)); |
|
|
|
//昨日总收益的10%作为邀请收益
|
|
|
|
BigDecimal yesterdayTotalInviteProfit = params.getAmt().multiply(BigDecimal.valueOf(0.1)); |
|
|
|
//获取昨日日期
|
|
|
|
Date yesterday = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24); |
|
|
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); |
|
|
|
String date = simpleDateFormat.format(yesterday); |
|
|
|
String tiktokKey = RedisKeyConstant.TIKTOK_IMG_TOTAL_DOWNLOAD_NUM_KEY + date; |
|
|
|
String hashKey = params.getAppType()+":"+ params.getPlatform(); |
|
|
|
if(redisService.hasHashKey(tiktokKey, hashKey)){ |
|
|
|
//存在昨日平台下载数量
|
|
|
|
Integer platformDownloadNum = redisService.getCacheMapValue(tiktokKey, hashKey); |
|
|
|
//广告单价
|
|
|
|
BigDecimal adPrice = yesterdayTotalAdProfit.divide(BigDecimal.valueOf(platformDownloadNum), 3, BigDecimal.ROUND_DOWN); |
|
|
|
//邀请单价
|
|
|
|
BigDecimal invitePrice = yesterdayTotalInviteProfit.divide(BigDecimal.valueOf(platformDownloadNum), 3, BigDecimal.ROUND_DOWN); |
|
|
|
//查出昨日对应平台的所有广告待入账信息
|
|
|
|
List<CreatorProfit> adProfits = creatorProfitService.queryPreProfit(params.getPlatform(), params.getAppType(), "0",date); |
|
|
|
if(adProfits.size() > 0){ |
|
|
|
for (CreatorProfit adProfit : adProfits) { |
|
|
|
//设置广告收益
|
|
|
|
adProfit.setProfit(BigDecimal.valueOf(adProfit.getDownloadNum()).multiply(adPrice)); |
|
|
|
adProfit.setConfirmStatus("0"); |
|
|
|
adProfit.setIncomeTime(new Date()); |
|
|
|
adProfit.setStatus("1"); |
|
|
|
creatorProfitService.update(adProfit); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//查出昨日对应平台的所有邀请待入账信息
|
|
|
|
List<CreatorProfit> inviteProfits = creatorProfitService.queryPreProfit(params.getPlatform(), params.getAppType(), "1",date); |
|
|
|
if(inviteProfits.size() > 0){ |
|
|
|
//设置邀请收益
|
|
|
|
for (CreatorProfit inviteProfit : inviteProfits) { |
|
|
|
inviteProfit.setProfit(BigDecimal.valueOf(inviteProfit.getDownloadNum()).multiply(invitePrice)); |
|
|
|
inviteProfit.setConfirmStatus("0"); |
|
|
|
inviteProfit.setIncomeTime(new Date()); |
|
|
|
inviteProfit.setStatus("1"); |
|
|
|
creatorProfitService.update(inviteProfit); |
|
|
|
//查出昨日对应平台的所有邀请待入账信息
|
|
|
|
List<CreatorProfit> inviteProfits = creatorProfitService.queryPreProfit(params.getPlatform(), params.getAppType(), "1",date); |
|
|
|
if(inviteProfits.size() > 0){ |
|
|
|
//设置邀请收益
|
|
|
|
for (CreatorProfit inviteProfit : inviteProfits) { |
|
|
|
inviteProfit.setProfit(BigDecimal.valueOf(inviteProfit.getDownloadNum()).multiply(invitePrice)); |
|
|
|
inviteProfit.setConfirmStatus("0"); |
|
|
|
inviteProfit.setIncomeTime(new Date()); |
|
|
|
inviteProfit.setStatus("1"); |
|
|
|
creatorProfitService.update(inviteProfit); |
|
|
|
} |
|
|
|
} |
|
|
|
//设置完结算收益,删除数量缓存
|
|
|
|
redisService.deleteHashKey(tiktokKey,hashKey); |
|
|
|
//更新审核单信息
|
|
|
|
ProfitVerifyOrder order = new ProfitVerifyOrder(); |
|
|
|
order.setId(params.getId()); |
|
|
|
order.setReason(params.getReason()); |
|
|
|
order.setVerifyAdminId(params.getVerifyAdminId()); |
|
|
|
order.setVerifyStatus(params.getVerifyStatus()); |
|
|
|
order.setVerifyTime(new Date()); |
|
|
|
profitVerifyOrderMapper.updateById(order); |
|
|
|
return true; |
|
|
|
} |
|
|
|
//设置完结算收益,删除数量缓存
|
|
|
|
redisService.deleteHashKey(tiktokKey,hashKey); |
|
|
|
//更新审核单信息
|
|
|
|
ProfitVerifyOrder order = new ProfitVerifyOrder(); |
|
|
|
order.setId(params.getId()); |
|
|
|
order.setReason(params.getReason()); |
|
|
|
order.setVerifyAdminId(params.getVerifyAdminId()); |
|
|
|
order.setVerifyStatus(params.getVerifyStatus()); |
|
|
|
order.setVerifyTime(new Date()); |
|
|
|
profitVerifyOrderMapper.updateById(order); |
|
|
|
return true; |
|
|
|
}else{ |
|
|
|
throw new ServiceException("系统繁忙,请稍候重试!"); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
log.error("审核收益单失败!错误原因为【{}】",e.getMessage()); |
|
|
|
}finally { |
|
|
|
//释放锁
|
|
|
|
if(lock.isHeldByCurrentThread()){ |
|
|
|
lock.unlock(); |
|
|
|
log.info("审核收益操作执行完毕,释放锁成功!"); |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
}else{ |
|
|
|
|