Parcourir la source

添加数据库备份定时任务

syh il y a 5 ans
Parent
commit
6accc9daed

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

@@ -0,0 +1,45 @@
+package org.jeecg.modules.ctop.job;
+
+import cn.com.ctop.common.module.utils.LoadFileUtil;
+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.Value;
+
+import java.util.Date;
+
+@Slf4j
+public class BakDataBaseJob implements Job {
+    @Value("${spring.datasource.master.username}")
+    private String userName;
+    @Value("${spring.datasource.master.password}")
+    private String password;
+    @Value("${jeecg.path.bak-database-file}")
+    private String filePath;
+
+    @Override
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+        String currentFileName = filePath + "db-" + DateUtils.formatDate(new Date()) + ".sql";
+        String historyFileName = filePath + "db-" + DateUtils.formatDate(DateUtils.addDay(new Date(), -4)) + ".sql";
+        String command = "mysqldump jeecg-boot -h 127.0.0.1 -u" + userName + " -p" + password + " --default-character-set=utf8mb4 --result-file=" + currentFileName;
+        //1:删除历史数据
+        LoadFileUtil.delFile(historyFileName);
+        //2:导出当天数据
+        Runtime runtime = Runtime.getRuntime();
+        Process pro = null;
+        try {
+            pro = runtime.exec(command);
+            int status = pro.waitFor();
+            if (status == 0) {
+                log.info("数据备份shell脚本执行成功");
+            } else {
+                log.info("数据备份shell脚本执行失败");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+}

+ 1 - 0
jeecg-boot-module-system/src/main/resources/application-prod.yml

@@ -150,6 +150,7 @@ jeecg:
     image-upload: /mnt/upload/image/
     chrome-driver: /usr/bin/chromedriver
     csv-upload: /mnt/upload/csv/
+    bak-database-file: /mnt/data/bak/
 kuaishou:
   group-control:
     path: http://39.106.184.70

+ 142 - 0
jeecg-boot-module-system/src/test/java/org/jeecg/SampleTest.java

@@ -1,8 +1,19 @@
 package org.jeecg;
 
+import cn.com.ctop.bytedance.entity.BytedancePlanDailyReport;
+import cn.com.ctop.bytedance.service.IBytedancePlanDailyReportService;
+import cn.com.ctop.bytedance.service.IReportService;
+import cn.com.ctop.common.module.entity.CtopOauthToken;
+import cn.com.ctop.common.module.entity.UserAllocation;
+import cn.com.ctop.common.module.service.ICtopOauthTokenService;
+import cn.com.ctop.common.module.service.IUserAllocationService;
+import cn.com.ctop.common.module.utils.CtopAdConstant;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.extern.slf4j.Slf4j;
+import org.jeecg.common.util.DateUtils;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
@@ -10,6 +21,9 @@ import org.springframework.test.context.junit4.SpringRunner;
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.List;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
@@ -105,8 +119,136 @@ public class SampleTest {
             e.printStackTrace();
 
         }
+    }
+
+    @Autowired
+    private ICtopOauthTokenService tokenService;
+
+    @Test
+    public void testBakToken() {
+        Date getDate = DateUtils.addDay(new Date(), -1);
+        QueryWrapper<UserAllocation> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("project_id", 215);
+        List<UserAllocation> allocations = userAllocationService.list(queryWrapper);
+        if (null != allocations && allocations.size() > 0) {
+            for (UserAllocation allocation : allocations) {
+                CtopOauthToken token = tokenService.getTokenByAccountId(allocation.getAccountId());
+                reportService.getAdvertiserPlanReport(token, getDate, getDate, CtopAdConstant.BYTEDANCE_REPORT_TYPE_DAILY);
+            }
+        }
+        formatZybData(215, getDate);
+    }
+
+    @Autowired
+    private IUserAllocationService userAllocationService;
+
+    private void formatZybData(Integer projectId, Date getDate) {
+        //清洗关于作业帮数据
+        //1:查询作业帮相关的用户数据
+        QueryWrapper<UserAllocation> wrapper = new QueryWrapper<>();
+        wrapper.eq("project_id", projectId);
+        List<UserAllocation> allocations = userAllocationService.list(wrapper);
+        if (null != allocations && allocations.size() > 0) {
+            allocations.forEach(allocation -> {
+                //根据accountId和时间,查询相关的日报表信息
+                QueryWrapper<BytedancePlanDailyReport> queryWrapper = new QueryWrapper<>();
+                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
+                String dateString = dateFormat.format(getDate) + " 00:00:00";
+                queryWrapper.eq("advertiser_id", allocation.getAccountId())
+                        .eq("stat_datetime", dateString)
+                ;
+                List<BytedancePlanDailyReport> bytedancePlanDailyReports = planDailyReportService.list(queryWrapper);
+                if (null != bytedancePlanDailyReports && bytedancePlanDailyReports.size() > 0) {
+                    bytedancePlanDailyReports.forEach(report -> {
+                        String adName = report.getAdName();
+                        String[] tags = adName.split("-");
+                        report.setAdsense(tags[1]);
+                        report.setPlanCode(tags[2]);
+                        String materialName = tags[3];
+                        report.setMaterialName(materialName);
+                        if (materialName.contains("其他")) {
+                            report.setMaterialType("其他");
+                        } else if (materialName.contains("图片轮播")) {
+                            report.setMaterialType("图片轮播");
+                        } else if (materialName.contains("大字报")) {
+                            report.setMaterialType("大字报");
+                        } else if (materialName.contains("文字动画")) {
+                            report.setMaterialType("文字动画");
+                        } else if (materialName.contains("采访形式")) {
+                            report.setMaterialType("采访形式");
+                        } else if (materialName.contains("剧情")) {
+                            report.setMaterialType("剧情");
+                        } else if (materialName.contains("口播")) {
+                            report.setMaterialType("口播");
+                        } else {
+                            report.setMaterialType("其他");
+                        }
+                        planDailyReportService.updateById(report);
+                    });
+                }
+            });
+        }
+    }
+
+    @Autowired
+    private IBytedancePlanDailyReportService planDailyReportService;
+    @Autowired
+    private IReportService reportService;
 
+    /**
+     * CSV文件生成方法
+     *
+     * @param head       文件头
+     * @param dataList   数据列表
+     * @param outPutPath 文件输出路径
+     * @param filename   文件名
+     * @return
+     */
+    public static File createCSVFile(String head, List<CtopOauthToken> dataList, String outPutPath, String filename) {
+
+        File csvFile = null;
+        BufferedWriter csvWtriter = null;
+        try {
+            csvFile = new File(outPutPath + File.separator + filename + ".csv");
+            File parent = csvFile.getParentFile();
+            if (parent != null && !parent.exists()) {
+                parent.mkdirs();
+            }
+            csvFile.createNewFile();
+
+            // GB2312使正确读取分隔符","
+            csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
+                    csvFile), "GB2312"), 1024);
+            // 写入文件头部
+            writeRow(head, csvWtriter);
+
+            // 写入文件内容
+            for (CtopOauthToken row : dataList) {
+                writeRow(row.toString(), csvWtriter);
+            }
+            csvWtriter.flush();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            try {
+                csvWtriter.close();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return csvFile;
+    }
 
+    /**
+     * 写一行数据方法
+     *
+     * @param csvWriter
+     * @throws IOException
+     */
+    private static void writeRow(String data, BufferedWriter csvWriter) throws IOException {
+        // 写入文件头部
+        csvWriter.write(data);
+        csvWriter.newLine();
     }
 
     public static byte[] readInputStream(InputStream inputStream) throws IOException {

+ 16 - 0
module-common/src/main/java/cn/com/ctop/common/module/entity/CtopOauthToken.java

@@ -12,6 +12,7 @@ import lombok.experimental.Accessors;
 import org.jeecgframework.poi.excel.annotation.Excel;
 import org.springframework.format.annotation.DateTimeFormat;
 
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -142,4 +143,19 @@ public class CtopOauthToken {
         calendar.add(Calendar.SECOND, seconds);
         return calendar.getTime();
     }
+
+    @Override
+    public String toString() {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return id +
+                "," + mediaId +
+                "," + advertiserId +
+                "," + accountId +
+                "," + accessToken +
+                "," + sdf.format(accessTokenExpiresIn) +
+                "," + refreshToken +
+                "," + sdf.format(refreshTokenExpiresIn) +
+                "," + sdf.format(createTime) +
+                "," + sdf.format(updateTime);
+    }
 }