浏览代码

消耗预警

yumeng 5 年之前
父节点
当前提交
c5027a1a3d

+ 10 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/controller/CallbackController.java

@@ -35,6 +35,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -152,6 +153,8 @@ public class CallbackController {
                     //创建人
                     String userId = json.getString("userId");
                     Long projectId = json.getLong("projectId");
+                    BigDecimal WarningProportion = json.getBigDecimal("WarningProportion");
+                    BigDecimal WarningAmount = json.getBigDecimal("WarningAmount");
 
                     CtopOauthToken topOauthToken = new CtopOauthToken();
                     topOauthToken.setId(String.valueOf(accountId));
@@ -195,6 +198,9 @@ public class CallbackController {
                     userAllocation.setAccountId(accountId);
                     userAllocation.setProjectId(projectId);
                     userAllocation.setProjectName(projectName);
+                    userAllocation.setWarningProportion(WarningProportion);
+                    userAllocation.setWarningAmount(WarningAmount);
+
                     userAllocation.setCreateTime(new Date());
                     userAllocation.setUpdateTime(new Date());
                     allocationMapper.deleteById(userAllocation.getId());
@@ -281,6 +287,8 @@ public class CallbackController {
             String userName = json.getString("userName");
             Long projectId = json.getLong("projectId");
             String projectName = json.getString("projectName");
+            BigDecimal WarningProportion = json.getBigDecimal("WarningProportion");
+            BigDecimal WarningAmount = json.getBigDecimal("WarningAmount");
 
             JSONObject data = resultObject.getJSONObject("data");
             CtopOauthToken token = new CtopOauthToken(advertiserId, data);
@@ -315,6 +323,8 @@ public class CallbackController {
             userAllocation.setProjectName(projectName);
             userAllocation.setAuthName((String) advertiserDataMap.get("name"));
             userAllocation.setAdvertiserId(advertiserId);
+            userAllocation.setWarningProportion(WarningProportion);
+            userAllocation.setWarningAmount(WarningAmount);
             userAllocation.setCreateTime(new Date());
             userAllocation.setUpdateTime(new Date());
             allocationMapper.deleteById(userAllocation.getId());

+ 59 - 0
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/job/KuaishouHourlyAccountWarningLoadJob.java

@@ -0,0 +1,59 @@
+package org.jeecg.modules.ctop.job;
+
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.utils.CtopAdConstant;
+import cn.com.ctop.kuaishou.modules.batch.service.IAccountWarningService;
+import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
+import org.quartz.Job;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.util.List;
+
+import static org.jeecg.common.util.DateUtils.getAnotherDay;
+
+/**
+ * 获取广告主分时报表
+ *
+ * @author syh
+ */
+@Slf4j
+public class KuaishouHourlyAccountWarningLoadJob implements Job {
+    @Autowired
+    private ICtopOauthTokenService tokenService;
+    @Autowired
+    private IAccountWarningService accountWarningService;
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        Thread thread = new Thread() {
+            @Override
+            public void run() {
+                log.info("开始时段消耗预警定时任务");
+                long l = System.currentTimeMillis();
+                try {
+                    List<CtopOauthToken> tokens = tokenService.getTokenListByType(CtopAdConstant.PLATFORM_TYPE_KUAISHOU);
+                    if (null == tokens || tokens.size() <= 0) {
+                        log.info("定时获取头条数据异常:未获取到可用的token");
+                        return;
+                    }
+                    String statDate = DateUtils.getDate("yyyy-MM-dd");
+                    String anotherDay = getAnotherDay("yyyy-MM-dd", statDate, -1);
+
+                    for (CtopOauthToken token : tokens) {
+                        accountWarningService.accountWarning(token.getAccountId(), statDate, anotherDay);
+                    }
+                    log.info("开始时段消耗预警定时任务执行完毕,耗时:{} ms", System.currentTimeMillis() - l);
+
+                } catch (Exception e) {
+                    e.printStackTrace();
+
+                }
+            }
+        };
+        thread.start();
+    }
+}

+ 3 - 3
jeecg-boot-module-system/src/main/java/org/jeecg/modules/ctop/service/impl/BidWarningServiceImpl.java

@@ -80,16 +80,15 @@ public class BidWarningServiceImpl implements IBidWarningService {
     private void sendMessage(Long accountId, Long projectId, String projectName, String authName, List<String> unitWarning, Long maxBid) {
 
         String createTime = DateUtils.getNowDate("yyyy-MM-dd");
-        Integer count = mailLogMapper.selectCountByAccountIdAndDate(accountId, createTime);
+        Integer count = mailLogMapper.selectCountByAccountIdAndDate(accountId, createTime, 1);
         StringBuilder text = new StringBuilder();
 
-
         String s = "";
         for (int i = 0; i < unitWarning.size(); i++) {
             s += unitWarning.get(i) + "," + "<br/>";
 
         }
-        text.append("您的项目:").append(projectName + ",").append("<br/>")
+        text.append("项目出价预警").append("<br/>").append("您的项目:").append(projectName + ",").append("<br/>")
                 .append("授权名称为:" + authName + " 下的,").append("<br/>").append(s)
                 .append("大于项目设置的最高出价:").append(maxBid / 1000).append(" 元! 请您及时调整。");
         if (count < 3) {
@@ -99,6 +98,7 @@ public class BidWarningServiceImpl implements IBidWarningService {
                 SendMailUtil.sendMail(mailList, subject, text.toString());
                 MailLog mailLog = new MailLog();
                 mailLog.setAccountId(accountId);
+                mailLog.setMailType(1);
                 mailLog.setContent(text.toString());
                 mailLog.setSendEmil(JSON.toJSONString(mailList));
                 mailLog.setSubject(subject);

+ 6 - 17
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -1,12 +1,10 @@
 package org.jeecg;
 
-import cn.com.ctop.common.module.utils.HttpUtils;
 import cn.com.ctop.common.module.utils.HttpUtils2;
-import cn.com.ctop.common.module.utils.PropertiesUtils;
 import cn.com.ctop.crawler.modules.appium.service.IAppiumJobService;
 import cn.com.ctop.crawler.modules.appium.service.IAppiumTaskService;
+import cn.com.ctop.kuaishou.modules.batch.service.IAccountWarningService;
 import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
-import com.alibaba.fastjson.JSONObject;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.http.impl.client.BasicCookieStore;
 import org.jeecg.modules.ctop.service.IBidWarningService;
@@ -25,10 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
@@ -153,19 +148,13 @@ public class SampleTest {
     @Autowired
     private IBidWarningService bidWarningService;
 
+    @Autowired
+    private IAccountWarningService accountWarningService;
+
     @Test
     public void testMa() throws Exception {
-        //String url = PropertiesUtils.getValue("bytedance_config", "bytedance_api_url") + PropertiesUtils.getValue("bytedance_config", "bytedance_v2_advertiser_info");
-        String url = "https://ad.oceanengine.com/open_api/2/advertiser/public_info/";
-        Map<String, String> headers = new HashMap<>();
-        headers.put("Content-Type", "application/json");
-        headers.put("Access-Token", "7827db33fe53348e935706250a991461e7cffda2");
-        TreeMap<String, Object> params = new TreeMap<>();
-        //TODO jsonArray
-        params.put("advertiser_ids", "[" + 111463131228L+ "]");
-        String result = HttpUtils.httpGetRequest(url, headers, params);
-        JSONObject jsonObject = JSONObject.parseObject(result);
-        System.err.println(jsonObject);
+        accountWarningService.accountWarning(23212L);
+
     }
 
 

+ 5 - 0
module-common/src/main/java/cn/com/ctop/common/module/entity/MailLog.java

@@ -56,6 +56,11 @@ public class MailLog {
     @Excel(name = "内容", width = 15)
     @ApiModelProperty(value = "内容")
     private String content;
+
+
+    private Integer mailType;
+    private Integer hour;
+
     /**
      * 创建时间
      */

+ 2 - 1
module-common/src/main/java/cn/com/ctop/common/module/mapper/MailLogMapper.java

@@ -15,9 +15,10 @@ import java.util.List;
  */
 public interface MailLogMapper extends BaseMapper<MailLog> {
 
-    Integer selectCountByAccountIdAndDate(@Param("accountId") Long accountId, @Param("createTime") String createTime);
+    Integer selectCountByAccountIdAndDate(@Param("accountId") Long accountId, @Param("createTime") String createTime, @Param("type") Integer type);
 
     List<String> selectMailList(@Param("projectId") Long projectId);
 
     List<String> selectWeiXinIdList(@Param("projectId") Long projectId);
+
 }

+ 2 - 0
module-common/src/main/java/cn/com/ctop/common/module/mapper/xml/MailLogMapper.xml

@@ -11,6 +11,7 @@
         ctop_mail_log
         where account_id = #{accountId}
         and DATE_FORMAT(create_time,'%Y-%m-%d') = #{createTime}
+        and mail_type = #{type}
 
 
     </select>
@@ -38,4 +39,5 @@
     </select>
 
 
+
 </mapper>

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/IAccountWarningService.java

@@ -1,5 +1,5 @@
 package cn.com.ctop.kuaishou.modules.batch.service;
 
 public interface IAccountWarningService {
-    void accountWarning(Long account);
+    void accountWarning(Long account, String statDate, String anotherDay);
 }

+ 92 - 38
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/AccountWarningServiceImpl.java

@@ -11,7 +11,6 @@ import cn.com.ctop.kuaishou.modules.batch.service.IAccountWarningService;
 import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouReportHourlyAccountMapper;
 import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import org.jeecg.common.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -35,39 +34,55 @@ public class AccountWarningServiceImpl implements IAccountWarningService {
      * @param accountId
      */
     @Override
-    public void accountWarning(Long accountId) {
+    public void accountWarning(Long accountId, String statDate, String anotherDay) {
         try {
-            String statDate = DateUtils.getDate("yyyy-MM-dd");
+
 
             Integer maxHour = hourlyAccountMapper.selectMaxHour(accountId, statDate);
-            if (Check.isNull(maxHour) || maxHour == 0) {
+            if (Check.isNull(maxHour)) {
                 return;
             }
 
-            BigDecimal nowCost = hourlyAccountMapper.getNowCost(accountId, maxHour, statDate);
-            if (!Check.isNull(nowCost) && nowCost.compareTo(new BigDecimal(0)) != 0) {
-                BigDecimal lastHourCost = hourlyAccountMapper.getNowCost(accountId, maxHour - 1, statDate);
-
-                if (!Check.isNull(lastHourCost) && lastHourCost.compareTo(new BigDecimal(0)) != 0) {
-                    QueryWrapper<UserAllocation> userAllocationQueryWrapper = new QueryWrapper<>();
-                    userAllocationQueryWrapper.eq("account_id", accountId);
-                    userAllocationQueryWrapper.orderByDesc("create_time");
-                    userAllocationQueryWrapper.last("limit 1");
-                    UserAllocation userAllocation = userAllocationMapper.selectOne(userAllocationQueryWrapper);
-                    if (!Check.isNull(userAllocation)) {
-                        BigDecimal warningAmount = userAllocation.getWarningAmount();
-                        BigDecimal warningProportion = userAllocation.getWarningProportion();
-
-                        BigDecimal subtract = nowCost.subtract(lastHourCost);
-                        BigDecimal divide = nowCost.divide(lastHourCost, 2, RoundingMode.HALF_UP);
-                        if (subtract.compareTo(warningAmount) > -1 && divide.compareTo(warningProportion) > -1) {
+            QueryWrapper<UserAllocation> userAllocationQueryWrapper = new QueryWrapper<>();
+            userAllocationQueryWrapper.eq("account_id", accountId);
+            userAllocationQueryWrapper.eq("media_id", 2);
+            userAllocationQueryWrapper.orderByDesc("create_time");
+            userAllocationQueryWrapper.last("limit 1");
+            UserAllocation userAllocation = userAllocationMapper.selectOne(userAllocationQueryWrapper);
+            if (Check.isNull(userAllocation)) {
+                return;
 
+            }
+            BigDecimal warningAmount = userAllocation.getWarningAmount();
+            BigDecimal warningProportion = userAllocation.getWarningProportion();
+            BigDecimal nowCost = hourlyAccountMapper.getCost(accountId, maxHour, statDate);
+            if (maxHour != 0) {
+                if (!Check.isNull(nowCost) && nowCost.compareTo(new BigDecimal(0)) != 0) {
+                    BigDecimal lastHourCost = hourlyAccountMapper.getCost(accountId, maxHour - 1, statDate);
+                    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, nowCost, userAllocation.getProjectId());
+                            }
                         }
 
-
                     }
 
                 }
+            }
+
+
+            if (!Check.isNull(nowCost) && nowCost.compareTo(new BigDecimal(0)) != 0) {
+                BigDecimal yesterdayHourCost = hourlyAccountMapper.getCost(accountId, maxHour, anotherDay);
+                if (!Check.isNull(yesterdayHourCost) && yesterdayHourCost.compareTo(new BigDecimal(0)) != 0) {
+                    BigDecimal subtract = nowCost.subtract(yesterdayHourCost);
+                    BigDecimal divide = nowCost.divide(yesterdayHourCost, 2, RoundingMode.HALF_UP);
+                    if (subtract.compareTo(warningAmount) > -1 && divide.compareTo(warningProportion) > -1) {
+                        sendYesterdayMessage(accountId, userAllocation.getAuthName(), maxHour, nowCost, userAllocation.getProjectId());
+                    }
+                }
 
             }
 
@@ -75,32 +90,73 @@ public class AccountWarningServiceImpl implements IAccountWarningService {
             e.printStackTrace();
         }
 
-
     }
 
+    private void sendYesterdayMessage(Long accountId, String authName, Integer maxHour, BigDecimal nowCost, Long projectId) {
 
-    private void sendMessage(Long accountId, Long projectId, String projectName, String authName, List<String> unitWarning, Long maxBid) {
-
-        String createTime = DateUtils.getNowDate("yyyy-MM-dd");
-        Integer count = mailLogMapper.selectCountByAccountIdAndDate(accountId, createTime);
         StringBuilder text = new StringBuilder();
+        text.append("较昨日同时段消耗预警").append("<br/>").append("您的账号:").append(accountId + ",").append("<br/>")
+                .append("授权名称为:" + authName + "相较于昨日").append(maxHour + "时").append("<br/>")
+                .append("出现消耗暴涨,").append("消耗金额为:" + nowCost).append("<br/>")
+                .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();
+            }
+            List<String> wexinIds = mailLogMapper.selectWeiXinIdList(projectId);
+            CorpWexinUtils.sendMessage(wexinIds, text.toString());
+        }
+
 
+    }
 
-        String s = "";
-        for (int i = 0; i < unitWarning.size(); i++) {
-            s += unitWarning.get(i) + "," + "<br/>";
 
-        }
-        text.append("您的项目:").append(projectName + ",").append("<br/>")
-                .append("授权名称为:" + authName + " 下的,").append("<br/>").append(s)
-                .append("大于项目设置的最高出价:").append(maxBid / 1000).append(" 元! 请您及时调整。");
-        if (count < 3) {
+    private void sendMessage(Long accountId, String authName, Integer hour, BigDecimal amount, Long projectId) {
+
+        StringBuilder text = new StringBuilder();
+        text.append("较上时段消耗预警").append("<br/>").append("您的账号:").append(accountId + ",").append("<br/>")
+                .append("授权名称为:" + authName + ",").append(hour + " 时").append("相较于 " + (hour - 1) + " 时").append("<br/>")
+                .append("出现消耗暴涨,").append("消耗金额为:" + amount).append("<br/>")
+                .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 = "广告组出价预警";
+            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);
@@ -109,11 +165,9 @@ public class AccountWarningServiceImpl implements IAccountWarningService {
                 e.printStackTrace();
             }
         }
-
         List<String> wexinIds = mailLogMapper.selectWeiXinIdList(projectId);
         CorpWexinUtils.sendMessage(wexinIds, text.toString());
 
-
     }
 
 

+ 0 - 4
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/batch/service/impl/KuaishouInterfaceServiceImpl.java

@@ -104,9 +104,6 @@ public class KuaishouInterfaceServiceImpl implements IKuaishouInterfaceService {
     @Autowired
     private KuaiShouAdvertiserBaseInfoMapper advertiserBaseInfoMapper;
 
-    @Autowired
-    private IAccountWarningService accountWarningService;
-
     @Override
     public void getAdvertiserReportHourly(CtopOauthToken token, Date startDate, Date endDate) {
         getAdvertiserReportHourlyByPage(token, startDate, endDate, 1);
@@ -151,7 +148,6 @@ public class KuaishouInterfaceServiceImpl implements IKuaishouInterfaceService {
             hourlyAccountService.saveOrUpdate(kuaishouReportHourlyAccount);
         }
 
-        accountWarningService.accountWarning(token.getAccountId());
 
     }
 

+ 1 - 1
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/mapper/KuaishouReportHourlyAccountMapper.java

@@ -9,5 +9,5 @@ import java.math.BigDecimal;
 public interface KuaishouReportHourlyAccountMapper extends BaseMapper<KuaishouReportHourlyAccount> {
     Integer selectMaxHour(@Param("accountId") Long accountId, @Param("statDate") String statDate);
 
-    BigDecimal getNowCost(@Param("accountId") Long accountId, @Param("statHour") Integer statHour, @Param("statDate") String statDate);
+    BigDecimal getCost(@Param("accountId") Long accountId, @Param("statHour") Integer statHour, @Param("statDate") String statDate);
 }

+ 2 - 2
module-kuaishou/src/main/java/cn/com/ctop/kuaishou/modules/report/mapper/xml/KuaishouReportHourlyAccountMapper.xml

@@ -6,12 +6,12 @@
         select max(stat_hour)
         from
         ctop_kuaishou_report_hourly_account
-        where accountId = #{accounId}
+        where account_id = #{accountId}
         and stat_date = #{statDate}
 
     </select>
 
-    <select id="getNowCost" resultType="java.math.BigDecimal">
+    <select id="getCost" resultType="java.math.BigDecimal">
         select charge
         from
         ctop_kuaishou_report_hourly_account