查询数据库所有账户的异步报告并下载

    @Override
    public void insertBatchReportStatus() throws ParseException {

        ReportParam reportParam = new ReportParam();
        reportParam.setProducttype(0);
        Integer day = Integer.parseInt(DateUtils.getDay())-1;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(DateUtils.getDate());
        DateUtils.addDay(startDate,-1)



        reportParam.setStartDate("2019-08-07");
        reportParam.setEndDate("2019-08-07");
        reportParam.setLevelOfDetails(2);
        reportParam.setOrder(true);
        reportParam.setReportType(700);
        reportParam.setStatRange(2);
        reportParam.setUnitOfTime(5);
        reportParam.setBstype(0);

        ArrayList<String> performanceData = new ArrayList<>();
        performanceData.add("cost");
        performanceData.add("impression");
        performanceData.add("click");
        performanceData.add("cpm");
        performanceData.add("ctr");
        performanceData.add("cpc");
        reportParam.setPerformanceData(performanceData);

        List<Account> accountList = accountDao.listAccount();
        List<ReportTask> reportTasks = new ArrayList<>();
        for (Account account : accountList) {
            try {
                Long mccOutId = account.getMccId();
                Long mccLocalId = mccAccountDao.selectLocalMccId(mccOutId);

                String reportFeedIdJson = reportResource.getReportFeedId(mccLocalId, reportParam, account.getName());
                JSONArray jsonArray = JSON.parseArray(reportFeedIdJson);
                String reportId = JSON.parseObject(jsonArray.get(0).toString()).getString("reportId");

                String isGeneratedJson = reportResource.getReportState(mccLocalId, reportId, account.getName());
                JSONArray isGeneratedArray = JSON.parseArray(isGeneratedJson);
                String isGenerated = JSON.parseObject(isGeneratedArray.get(0).toString()).getInteger("isGenerated").toString();
                ReportTask reportTask = new ReportTask();
                Date date = new Date();
                reportTask.setCreateDate(date);
                reportTask.setUpdateDate(date);
                reportTask.setReportId(reportId);
                reportTask.setIsGenerated(isGenerated);
                reportTask.setStatus(false);
                reportTask.setReportType(700);
                reportTask.setDelFlag(false);
                reportTask.setAccountName(account.getName());
                reportTask.setMccId(mccLocalId);

                reportTasks.add(reportTask);
                Thread.sleep(500);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        reportTaskDao.insertBatch(reportTasks);
    }


    //根据url 下载展现、点击、消费等字段
    @Override
    public void insertBatchDayReport() {
        List<ReportTask> reportTasks = reportTaskDao.selectReportsByStatus();
        List<RptDayAccount> rptDayAccounts = new ArrayList<>();

        for (ReportTask reportTask : reportTasks) {

            String reportPathJson = null;
            try {
                 reportPathJson = reportResource.getReportUrl(reportTask.getMccId(), reportTask.getReportId(), reportTask.getAccountName());
            } catch (Exception e) {
                e.printStackTrace();

            }
            JSONArray reportPathArray = JSON.parseArray(reportPathJson);
            String reportPath = JSON.parseObject(reportPathArray.get(0).toString()).getString("reportFilePath");
            URL url = null;
            try {
                url = new URL(reportPath);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            HttpURLConnection conn = null;
            try {
                conn = (HttpURLConnection) url.openConnection();
            } catch (
                    IOException e) {
                e.printStackTrace();
            }
            //设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            String s;
            try {
                InputStream inputStream = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "GBK"));
                int i = 1;
                while ((s = reader.readLine()) != null) {
//                    System.out.println("weifenge"+s);
                    i++;
//                    System.out.println(i);

                    if (i == 2) {
                        String[] desc = s.split(" ");
                        for (String data : desc) {
//                            System.out.println(data);
                        }
                    }
                    if (i == 3) {
                        String number[] = s.split("\\s+");
                        for (String data : number) {
//                            System.out.println(data);
                        }
                        RptDayAccount rptDayAccount = new RptDayAccount();
                        rptDayAccount.setCreateDate(DateUtils.parseDate(number[6]));
                        rptDayAccount.setUpdateDate(DateUtils.parseDate(number[6]));
                        rptDayAccount.setAccountName(number[7]);
                        rptDayAccount.setAccountId(Long.parseLong(number[8]));
                        rptDayAccount.setImpression(Integer.parseInt(number[1]));
                        rptDayAccount.setClick(Integer.parseInt(number[2]));
                        rptDayAccount.setCost(BigDecimal.valueOf(Float.valueOf(number[0])).setScale(2,BigDecimal.ROUND_HALF_UP));
                        rptDayAccount.setCpc(BigDecimal.valueOf(Float.valueOf(number[5])).setScale(2,BigDecimal.ROUND_HALF_UP));
                        rptDayAccount.setCpm(BigDecimal.valueOf(Float.valueOf(number[3])).setScale(2,BigDecimal.ROUND_HALF_UP));

                        Float f = Float.valueOf(number[4].replace("%","")) / 100;
                        BigDecimal ctr =  BigDecimal.valueOf(f).setScale(2, BigDecimal.ROUND_HALF_UP);

                        rptDayAccount.setCtr(ctr);
//                        5 3 4
//                        rptDayAccount.setActiveconversions(Long.);
                        rptDayAccount.setDelFlag(false);
                        rptDayAccounts.add(rptDayAccount);
                    }
                }
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        rptDayAccountDao.insertBatch(rptDayAccounts);
        //修改状态表 就会及时改变状态表里面的 status字段变为1,那么就不需要在下载了
        reportTaskDao.updateBatchStatus(reportTasks);
    }

    @Override
    public void updateBatchDayReport() {

        List<ReportTask> reportTasks = reportTaskDao.selectInCompleteReports();
        List<Long> reportIds = new ArrayList<>();
        //针对未生成的报告再次获取一遍状态,判断状态是否改变
        for (ReportTask reportTask : reportTasks) {
            try {
                String isGeneratedJson = reportResource.getReportState(reportTask.getMccId(),reportTask.getReportId(), reportTask.getAccountName());
                JSONArray isGeneratedArray = JSON.parseArray(isGeneratedJson);
                int isGenerated = JSON.parseObject(isGeneratedArray.get(0).toString()).getInteger("isGenerated");
                if(isGenerated == 3){
                    reportIds.add(reportTask.getId());
                } else {
                    continue;
                }
                Thread.sleep(500);
            }catch (Exception e) {
                logger.error("获取报告url失败!");
            }

        }
        reportTaskDao.updateByIsGenerated(reportIds);
        //然后符合条件的是3,就调用 下载的方法
        insertBatchDayReport();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值