|
|
@@ -5,6 +5,7 @@ import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.ytpm.advertise.enums.AdPlatformTypeEnum;
|
|
|
import com.ytpm.advertise.enums.AdSourceTypeEnum;
|
|
|
import com.ytpm.agent.enums.UserStatusEnum;
|
|
|
@@ -20,7 +21,15 @@ import com.ytpm.general.StatusCode;
|
|
|
import com.ytpm.lemonios.dao.AdRecordMapper;
|
|
|
import com.ytpm.lemonios.dao.AppUserMapper;
|
|
|
import com.ytpm.lemonios.service.AdService;
|
|
|
-import com.ytpm.middle.view.*;
|
|
|
+import com.ytpm.lemonios.service.AppUserService;
|
|
|
+import com.ytpm.middle.view.AppRankingListVO;
|
|
|
+import com.ytpm.middle.view.AppRevenueHourVO;
|
|
|
+import com.ytpm.middle.view.AppUserHourVO;
|
|
|
+import com.ytpm.middle.view.DashboardAppRevenueVO;
|
|
|
+import com.ytpm.middle.view.DashboardRankingListVO;
|
|
|
+import com.ytpm.middle.view.DashboardRevenueVO;
|
|
|
+import com.ytpm.middle.view.DashboardRiskVO;
|
|
|
+import com.ytpm.middle.view.UserRankingListVO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
@@ -29,11 +38,19 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -54,6 +71,9 @@ public class AdServiceImpl implements AdService {
|
|
|
@Value("${yt.ios.appid:}")
|
|
|
private String appId;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private AppUserService appUserService;
|
|
|
+
|
|
|
/**
|
|
|
* 保存广告记录
|
|
|
*/
|
|
|
@@ -61,41 +81,100 @@ public class AdServiceImpl implements AdService {
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Result<?> saveRecord(DyzAdRecordParam param) {
|
|
|
if (StrUtil.isEmpty(param.getUserId())) {
|
|
|
- YtDyzAdRecord adRecord = new YtDyzAdRecord();
|
|
|
- if(Objects.isNull(param.getBeginTime()) || "null".equals(param.getBeginTime())){
|
|
|
- if ("null".equals(param.getBeginTime())) {
|
|
|
- log.warn("param beginTime is null !");
|
|
|
- }
|
|
|
- param.setBeginTime(param.getFinishTime());
|
|
|
+ // 登陆前保存广告(游客登陆)
|
|
|
+ saveRecordAndChangeUser(param, null);
|
|
|
+ return Result.resultOk(RepMessage.SAVE_SUCCESS);
|
|
|
+ }
|
|
|
+ // 登陆后保存广告
|
|
|
+ YtDyzUser user = appUserMapper.selectPrimaryKey(param.getUserId());
|
|
|
+ if (Objects.isNull(user)) {
|
|
|
+ return Result.resultOk(RepMessage.SAVE_SUCCESS);
|
|
|
+ }
|
|
|
+ if (!UserStatusEnum.NORMAL.getCode().equals(user.getUserStatus())) {
|
|
|
+ return new Result<>(StatusCode.ACCESS_ERR, getTipsMsg());
|
|
|
+ }
|
|
|
+ //增加广告记录
|
|
|
+ saveRecordAndChangeUser(param, user);
|
|
|
+ //调用风控广告校验
|
|
|
+ if(AdSourceTypeEnum.rewarded_video.getAdSourceType() == param.getAdSourceType()){
|
|
|
+ appUserService.addPower(user.getUserId());
|
|
|
+ Result<?> result = riskFeign.checkAdRisk(user);
|
|
|
+ if(result.getCode()!=200){
|
|
|
+ return new Result<>(StatusCode.ACCESS_ERR, getTipsMsg());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.resultOk(RepMessage.SAVE_SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统一处理入参时间
|
|
|
+ */
|
|
|
+ private void handleParam(DyzAdRecordParam param){
|
|
|
+ String timeFormat = "yyyy-MM-dd HH:mm:ss";
|
|
|
+ if (param.getBegintimestamp() != null && param.getFinishtimestamp() != null) {
|
|
|
+ // 处理 begintimestamp(兼容秒级和毫秒级)
|
|
|
+ long beginTimestamp = param.getBegintimestamp();
|
|
|
+ if (String.valueOf(beginTimestamp).length() == 10) { // 秒级时间戳(10位)
|
|
|
+ beginTimestamp *= 1000; // 转为毫秒级
|
|
|
+ }
|
|
|
+ param.setBeginTime(DateUtil.format(new Date(beginTimestamp), timeFormat));
|
|
|
+
|
|
|
+ // 处理 finishtimestamp(兼容秒级和毫秒级)
|
|
|
+ long finishTimestamp = param.getFinishtimestamp();
|
|
|
+ if (String.valueOf(finishTimestamp).length() == 10) { // 秒级时间戳(10位)
|
|
|
+ finishTimestamp *= 1000; // 转为毫秒级
|
|
|
}
|
|
|
- BeanUtils.copyProperties(param, adRecord);
|
|
|
- if (param.getBegintimestamp() != null && param.getFinishtimestamp() != null) {
|
|
|
- adRecord.setBeginTime(DateUtil.format(new Date(param.getBegintimestamp()), "yyyy-MM-dd HH:mm:ss"));
|
|
|
- adRecord.setFinishTime(DateUtil.format(new Date(param.getFinishtimestamp()), "yyyy-MM-dd HH:mm:ss"));
|
|
|
+ param.setFinishTime(DateUtil.format(new Date(finishTimestamp), timeFormat));
|
|
|
+ }
|
|
|
+ if (StrUtil.isEmpty(param.getFinishTime())) {
|
|
|
+ param.setFinishTime(DateUtil.format(new Date(), timeFormat));
|
|
|
+ }
|
|
|
+ if(Objects.isNull(param.getBeginTime()) || "null".equals(param.getBeginTime())){
|
|
|
+ if ("null".equals(param.getBeginTime())) {
|
|
|
+ log.warn("param beginTime is null !");
|
|
|
}
|
|
|
- adRecord.setIosId(param.getIosId());
|
|
|
+ param.setBeginTime(param.getFinishTime());
|
|
|
+ }
|
|
|
+ // ecpm 值统一解析json获取
|
|
|
+ if (StrUtil.isNotEmpty(param.getResultJson())) {
|
|
|
+ JSONObject resultJson = JSONObject.parseObject(param.getResultJson());
|
|
|
+ String ecpmStr = resultJson.getString("adsource_price");
|
|
|
+ if (StrUtil.isNotEmpty(ecpmStr)) {
|
|
|
+ param.setEcpm(new BigDecimal(ecpmStr));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存记录
|
|
|
+ * 始终创建新的事务以保障子方法的独立事务
|
|
|
+ */
|
|
|
+// @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
|
|
|
+ public void saveRecordAndChangeUser(DyzAdRecordParam param, YtDyzUser user) {
|
|
|
+ //增加广告记录
|
|
|
+ //修改用户信息, 广告次数+1 总收益 + revenue
|
|
|
+// YtDyzUser dyzUser = new YtDyzUser();
|
|
|
+// dyzUser.setUserId(user.getUserId());
|
|
|
+// dyzUser.setTotalVideo(Objects.isNull(user.getTotalVideo())?1:(user.getTotalVideo()+1));
|
|
|
+// dyzUser.setTotalIncome(user.getTotalIncome().add(param.getRevenue()));
|
|
|
+// appUserMapper.updateUser(dyzUser);
|
|
|
+ YtDyzAdRecord adRecord = new YtDyzAdRecord();
|
|
|
+ handleParam(param);
|
|
|
+ BeanUtils.copyProperties(param, adRecord);
|
|
|
+ adRecord.setRecordId(IdUtil.fastSimpleUUID());
|
|
|
+ adRecord.setIosId(param.getIosId());
|
|
|
+ adRecord.setNetworkName(AdPlatformTypeEnum.getDesc(Integer.parseInt(param.getNetworkFormId())));
|
|
|
+ if (user == null) {
|
|
|
adRecord.setAppId(appId);
|
|
|
- adRecord.setRecordId(IdUtil.fastSimpleUUID());
|
|
|
- adRecord.setNetworkName(AdPlatformTypeEnum.getDesc(Integer.parseInt(param.getNetworkFormId())));
|
|
|
adRecordMapper.addOneVisitor(adRecord);
|
|
|
} else {
|
|
|
- YtDyzUser user = appUserMapper.selectPrimaryKey(param.getUserId());
|
|
|
- if(!UserStatusEnum.NORMAL.getCode().equals(user.getUserStatus())){
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR,getTipsMsg());
|
|
|
- }
|
|
|
- if(Objects.isNull(user)){
|
|
|
- return Result.resultOk(RepMessage.SAVE_SUCCESS);
|
|
|
- }
|
|
|
- saveRecordAndChangeUser(param, user);
|
|
|
- //调用风控广告校验
|
|
|
- if(AdSourceTypeEnum.rewarded_video.getAdSourceType() == param.getAdSourceType()){
|
|
|
- Result<?> result = riskFeign.checkAdRisk(user);
|
|
|
- if(result.getCode()!=200){
|
|
|
- return new Result<>(StatusCode.ACCESS_ERR, getTipsMsg());
|
|
|
- }
|
|
|
+ adRecord.setUserId(user.getUserId());
|
|
|
+ adRecordMapper.addOne(adRecord);
|
|
|
+ if (param.getRevenue() == null) {
|
|
|
+ param.setRevenue(BigDecimal.ZERO);
|
|
|
}
|
|
|
+ appUserMapper.updateTotal(user.getUserId(), 1, param.getRevenue());
|
|
|
}
|
|
|
- return Result.resultOk(RepMessage.SAVE_SUCCESS);
|
|
|
}
|
|
|
|
|
|
private String getTipsMsg(){
|
|
|
@@ -294,32 +373,4 @@ public class AdServiceImpl implements AdService {
|
|
|
return vos;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 保存记录
|
|
|
- * 始终创建新的事务以保障子方法的独立事务
|
|
|
- */
|
|
|
-// @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
|
|
|
- public void saveRecordAndChangeUser(DyzAdRecordParam param,YtDyzUser user) {
|
|
|
- //增加广告记录
|
|
|
- YtDyzAdRecord adRecord = new YtDyzAdRecord();
|
|
|
- if(Objects.isNull(param.getBeginTime())){
|
|
|
- param.setBeginTime(param.getFinishTime());
|
|
|
- }
|
|
|
- BeanUtils.copyProperties(param, adRecord);
|
|
|
- if (param.getBegintimestamp() != null && param.getFinishtimestamp() != null) {
|
|
|
- adRecord.setBeginTime(DateUtil.format(new Date(param.getBegintimestamp()), "yyyy-MM-dd HH:mm:ss"));
|
|
|
- adRecord.setFinishTime(DateUtil.format(new Date(param.getFinishtimestamp()), "yyyy-MM-dd HH:mm:ss"));
|
|
|
- }
|
|
|
- adRecord.setIosId(param.getIosId());
|
|
|
- adRecord.setRecordId(IdUtil.fastSimpleUUID());
|
|
|
- adRecord.setUserId(user.getUserId());
|
|
|
- adRecord.setNetworkName(AdPlatformTypeEnum.getDesc(Integer.parseInt(param.getNetworkFormId())));
|
|
|
- adRecordMapper.addOne(adRecord);
|
|
|
- //修改用户信息, 广告次数+1 总收益 + revenue
|
|
|
- YtDyzUser dyzUser = new YtDyzUser();
|
|
|
- dyzUser.setUserId(user.getUserId());
|
|
|
- dyzUser.setTotalVideo(Objects.isNull(user.getTotalVideo())?1:(user.getTotalVideo()+1));
|
|
|
- dyzUser.setTotalIncome(user.getTotalIncome().add(param.getRevenue()));
|
|
|
- appUserMapper.updateUser(dyzUser);
|
|
|
- }
|
|
|
}
|