|
@@ -3,14 +3,18 @@ package cn.com.ctop.toutiao.modules.report.service.impl;
|
|
import cn.com.ctop.common.module.entity.MailLog;
|
|
import cn.com.ctop.common.module.entity.MailLog;
|
|
import cn.com.ctop.common.module.entity.UserAllocation;
|
|
import cn.com.ctop.common.module.entity.UserAllocation;
|
|
import cn.com.ctop.common.module.mapper.MailLogMapper;
|
|
import cn.com.ctop.common.module.mapper.MailLogMapper;
|
|
-import cn.com.ctop.common.module.mapper.UserAllocationMapper;
|
|
|
|
|
|
+import cn.com.ctop.common.module.service.ICorpFeishuUserService;
|
|
|
|
+import cn.com.ctop.common.module.service.IUserAllocationService;
|
|
import cn.com.ctop.common.module.utils.Check;
|
|
import cn.com.ctop.common.module.utils.Check;
|
|
import cn.com.ctop.common.module.utils.CorpWexinUtils;
|
|
import cn.com.ctop.common.module.utils.CorpWexinUtils;
|
|
import cn.com.ctop.common.module.utils.SendMailUtil;
|
|
import cn.com.ctop.common.module.utils.SendMailUtil;
|
|
import cn.com.ctop.toutiao.modules.report.mapper.BytedanceAdvertiserHourlyReportMapper;
|
|
import cn.com.ctop.toutiao.modules.report.mapper.BytedanceAdvertiserHourlyReportMapper;
|
|
import cn.com.ctop.toutiao.modules.report.service.IByteDanceHourlyAccountWarningService;
|
|
import cn.com.ctop.toutiao.modules.report.service.IByteDanceHourlyAccountWarningService;
|
|
|
|
+import cn.com.feishu.provider.MessageProvider;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.context.annotation.Primary;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
@@ -21,13 +25,13 @@ import java.util.List;
|
|
|
|
|
|
@Service
|
|
@Service
|
|
@Primary
|
|
@Primary
|
|
-
|
|
|
|
|
|
+@Slf4j
|
|
public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourlyAccountWarningService {
|
|
public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourlyAccountWarningService {
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private BytedanceAdvertiserHourlyReportMapper bytedanceAdvertiserHourlyReportMapper;
|
|
private BytedanceAdvertiserHourlyReportMapper bytedanceAdvertiserHourlyReportMapper;
|
|
@Autowired
|
|
@Autowired
|
|
- private UserAllocationMapper userAllocationMapper;
|
|
|
|
|
|
+ private IUserAllocationService userAllocationService;
|
|
@Autowired
|
|
@Autowired
|
|
private MailLogMapper mailLogMapper;
|
|
private MailLogMapper mailLogMapper;
|
|
/**
|
|
/**
|
|
@@ -38,17 +42,11 @@ public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourl
|
|
@Override
|
|
@Override
|
|
public void byteDanceAccountWarning(Long accountId, String statDate, String anotherDay) {
|
|
public void byteDanceAccountWarning(Long accountId, String statDate, String anotherDay) {
|
|
try {
|
|
try {
|
|
-
|
|
|
|
Integer maxHour = bytedanceAdvertiserHourlyReportMapper.selectMaxHour(accountId, statDate);
|
|
Integer maxHour = bytedanceAdvertiserHourlyReportMapper.selectMaxHour(accountId, statDate);
|
|
if (Check.isNull(maxHour)) {
|
|
if (Check.isNull(maxHour)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- QueryWrapper<UserAllocation> userAllocationQueryWrapper = new QueryWrapper<>();
|
|
|
|
- userAllocationQueryWrapper.eq("account_id", accountId);
|
|
|
|
- userAllocationQueryWrapper.eq("media_id", 1);
|
|
|
|
- userAllocationQueryWrapper.orderByDesc("create_time");
|
|
|
|
- userAllocationQueryWrapper.last("limit 1");
|
|
|
|
- UserAllocation userAllocation = userAllocationMapper.selectOne(userAllocationQueryWrapper);
|
|
|
|
|
|
+ UserAllocation userAllocation = userAllocationService.getByAccountId(accountId);
|
|
if (Check.isNull(userAllocation)) {
|
|
if (Check.isNull(userAllocation)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -58,20 +56,15 @@ public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourl
|
|
BigDecimal warningAmount = userAllocation.getWarningAmount();
|
|
BigDecimal warningAmount = userAllocation.getWarningAmount();
|
|
BigDecimal warningProportion = userAllocation.getWarningProportion();
|
|
BigDecimal warningProportion = userAllocation.getWarningProportion();
|
|
BigDecimal nowCost = bytedanceAdvertiserHourlyReportMapper.getCost(accountId, maxHour, statDate);
|
|
BigDecimal nowCost = bytedanceAdvertiserHourlyReportMapper.getCost(accountId, maxHour, statDate);
|
|
-
|
|
|
|
BigDecimal cost = nowCost.setScale(2, RoundingMode.HALF_UP);
|
|
BigDecimal cost = nowCost.setScale(2, RoundingMode.HALF_UP);
|
|
-
|
|
|
|
-
|
|
|
|
if (maxHour != 0) {
|
|
if (maxHour != 0) {
|
|
if (!Check.isNull(nowCost) && nowCost.compareTo(new BigDecimal(0)) != 0) {
|
|
if (!Check.isNull(nowCost) && nowCost.compareTo(new BigDecimal(0)) != 0) {
|
|
BigDecimal lastHourCost = bytedanceAdvertiserHourlyReportMapper.getCost(accountId, maxHour - 1, statDate);
|
|
BigDecimal lastHourCost = bytedanceAdvertiserHourlyReportMapper.getCost(accountId, maxHour - 1, statDate);
|
|
if (!Check.isNull(lastHourCost) && lastHourCost.compareTo(new BigDecimal(0)) != 0) {
|
|
if (!Check.isNull(lastHourCost) && lastHourCost.compareTo(new BigDecimal(0)) != 0) {
|
|
- if (!Check.isNull(userAllocation)) {
|
|
|
|
- BigDecimal subtract = nowCost.subtract(lastHourCost);
|
|
|
|
- BigDecimal divide = nowCost.divide(lastHourCost, 2, RoundingMode.HALF_UP);
|
|
|
|
- if (subtract.compareTo(warningAmount) > -1 && divide.compareTo(warningProportion) > -1) {
|
|
|
|
- sendMessage(accountId, userAllocation.getAuthName(), maxHour, cost, userAllocation.getProjectId(), userAllocation.getProjectName());
|
|
|
|
- }
|
|
|
|
|
|
+ BigDecimal subtract = nowCost.subtract(lastHourCost);
|
|
|
|
+ BigDecimal divide = nowCost.divide(lastHourCost, 2, RoundingMode.HALF_UP);
|
|
|
|
+ if (subtract.compareTo(warningAmount) > -1 && divide.compareTo(warningProportion) > -1) {
|
|
|
|
+ sendMessage(accountId, userAllocation.getAuthName(), maxHour, cost, userAllocation.getProjectId(), userAllocation.getProjectName());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -87,11 +80,9 @@ public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourl
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -103,8 +94,6 @@ public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourl
|
|
.append("相较于昨日 " + maxHour + "时").append("<br/>")
|
|
.append("相较于昨日 " + maxHour + "时").append("<br/>")
|
|
.append("出现消耗暴涨,").append("消耗金额为:" + nowCost).append("<br/>")
|
|
.append("出现消耗暴涨,").append("消耗金额为:" + nowCost).append("<br/>")
|
|
.append("请您及时查看!");
|
|
.append("请您及时查看!");
|
|
-
|
|
|
|
-
|
|
|
|
QueryWrapper<MailLog> queryWrapper = new QueryWrapper<>();
|
|
QueryWrapper<MailLog> queryWrapper = new QueryWrapper<>();
|
|
queryWrapper.eq("account_id", accountId);
|
|
queryWrapper.eq("account_id", accountId);
|
|
queryWrapper.eq("hour", maxHour);
|
|
queryWrapper.eq("hour", maxHour);
|
|
@@ -131,11 +120,90 @@ public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourl
|
|
List<String> wexinIds = mailLogMapper.selectWeiXinIdList(projectId);
|
|
List<String> wexinIds = mailLogMapper.selectWeiXinIdList(projectId);
|
|
CorpWexinUtils.sendMessage(wexinIds, text.toString());
|
|
CorpWexinUtils.sendMessage(wexinIds, text.toString());
|
|
}
|
|
}
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ private void sendYesterdayFeishuMessage(Long accountId, String authName, Integer maxHour, BigDecimal nowCost, Long projectId, String projectName) throws Exception {
|
|
|
|
+ StringBuilder text = new StringBuilder();
|
|
|
|
+ text.append("较昨日同时段消耗预警").append("\n\r")
|
|
|
|
+ .append("您的项目:" + projectName + "下的,").append("\n\r")
|
|
|
|
+ .append("账号:").append(accountId + ",").append("授权名称为:" + authName).append("\n\r")
|
|
|
|
+ .append("相较于昨日 " + maxHour + "时").append("\n\r")
|
|
|
|
+ .append("出现消耗暴涨,").append("消耗金额为:" + nowCost).append("\n\r")
|
|
|
|
+ .append("请您及时查看!");
|
|
|
|
+ QueryWrapper<MailLog> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("account_id", accountId);
|
|
|
|
+ queryWrapper.eq("hour", maxHour);
|
|
|
|
+ queryWrapper.eq("mail_type", 3);
|
|
|
|
+ queryWrapper.orderByDesc("create_time");
|
|
|
|
+ queryWrapper.last("limit 1");
|
|
|
|
+ MailLog checkMailLog = mailLogMapper.selectOne(queryWrapper);
|
|
|
|
+ if (Check.isNull(checkMailLog)) {
|
|
|
|
+ List<String> mailList = mailLogMapper.selectMailList(projectId);
|
|
|
|
+ String subject = "头条-较昨日同时段账户消耗预警";
|
|
|
|
+ try {
|
|
|
|
+ SendMailUtil.sendMail(mailList, subject, text.toString());
|
|
|
|
+ MailLog mailLog = new MailLog();
|
|
|
|
+ mailLog.setAccountId(accountId);
|
|
|
|
+ mailLog.setMailType(3);
|
|
|
|
+ mailLog.setHour(maxHour);
|
|
|
|
+ mailLog.setContent(text.toString());
|
|
|
|
+ mailLog.setSendEmil(JSON.toJSONString(mailList));
|
|
|
|
+ mailLog.setSubject(subject);
|
|
|
|
+ mailLogMapper.insert(mailLog);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ JSONArray openIds = feishuUserService.getOpenIdByParams(projectId);
|
|
|
|
+ MessageProvider.batchSend(null,openIds,null,text.toString());
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ private void sendFeishuMessage(Long accountId, String authName, Integer hour, BigDecimal amount, Long projectId, String projectName) throws Exception {
|
|
|
|
+ StringBuilder text = new StringBuilder();
|
|
|
|
+ text.append("较上时段消耗预警").append("\n\r")
|
|
|
|
+ .append("您的项目:" + projectName + " 下的").append("\n\r")
|
|
|
|
+ .append("账号:").append(accountId + ",").append("授权名称为:" + authName).append("\n\r")
|
|
|
|
+ .append(hour + " 时").append("相较于 " + (hour - 1) + " 时").append("\n\r")
|
|
|
|
+ .append("出现消耗暴涨,").append("消耗金额为:" + amount).append("\n\r")
|
|
|
|
+ .append("请您及时查看!");
|
|
|
|
+
|
|
|
|
+ QueryWrapper<MailLog> queryWrapper = new QueryWrapper<>();
|
|
|
|
+ queryWrapper.eq("account_id", accountId);
|
|
|
|
+ queryWrapper.eq("hour", hour);
|
|
|
|
+ queryWrapper.eq("mail_type", 2);
|
|
|
|
+ queryWrapper.orderByDesc("create_time");
|
|
|
|
+ queryWrapper.last("limit 1");
|
|
|
|
+ MailLog checkMailLog = mailLogMapper.selectOne(queryWrapper);
|
|
|
|
+ if (Check.isNull(checkMailLog)) {
|
|
|
|
+ List<String> mailList = mailLogMapper.selectMailList(projectId);
|
|
|
|
+ String subject = "头条-账户消耗预警";
|
|
|
|
+ try {
|
|
|
|
+ SendMailUtil.sendMail(mailList, subject, text.toString());
|
|
|
|
+ MailLog mailLog = new MailLog();
|
|
|
|
+ mailLog.setAccountId(accountId);
|
|
|
|
+ mailLog.setMailType(2);
|
|
|
|
+ mailLog.setHour(hour);
|
|
|
|
+ mailLog.setContent(text.toString());
|
|
|
|
+ mailLog.setSendEmil(JSON.toJSONString(mailList));
|
|
|
|
+ mailLog.setSubject(subject);
|
|
|
|
+ mailLogMapper.insert(mailLog);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ JSONArray openIds = feishuUserService.getOpenIdByParams(projectId);
|
|
|
|
+ if(null == openIds&&openIds.isEmpty()){
|
|
|
|
+ log.error("该项目下运营飞书账号尚未绑定邮箱=>projectId:{}",projectId);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ MessageProvider.batchSend(null,openIds,null, text.toString());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private ICorpFeishuUserService feishuUserService;
|
|
|
|
+
|
|
private void sendMessage(Long accountId, String authName, Integer hour, BigDecimal amount, Long projectId, String projectName) {
|
|
private void sendMessage(Long accountId, String authName, Integer hour, BigDecimal amount, Long projectId, String projectName) {
|
|
|
|
|
|
StringBuilder text = new StringBuilder();
|
|
StringBuilder text = new StringBuilder();
|
|
@@ -172,7 +240,6 @@ public class ByteDanceHourlyAccountWarningServiceImpl implements IByteDanceHourl
|
|
}
|
|
}
|
|
List<String> wexinIds = mailLogMapper.selectWeiXinIdList(projectId);
|
|
List<String> wexinIds = mailLogMapper.selectWeiXinIdList(projectId);
|
|
CorpWexinUtils.sendMessage(wexinIds, text.toString());
|
|
CorpWexinUtils.sendMessage(wexinIds, text.toString());
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|