定時任務建立分表,轉移資料
阿新 • • 發佈:2021-08-20
//定時任務執行
@Scheduled(cron = "0 10 16 5 * ?") public void doReportDateHistory() throws Exception { if (!InitUtil.isVideoShowMain) return; MyUtil mu = MyUtil.getInstance(); Date date = mu.get_N_Month(mu.getCurrenTime(),-1); //往前推一個月 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMM"); Date date2 = mu.get_N_Month(mu.getCurrenTime(),0); String dateStart = formatter.format(date); String dateEnd = formatter.format(date2); String dateString = formatter2.format(date); Map<String,Object> paramMap = new HashMap<>(); paramMap.put("dateStart",dateStart); paramMap.put("dateEnd",dateEnd); paramMap.put("dateString",dateString); reportDataService.doReportDateHistory(paramMap); }
//下面具體的程式碼
如果學武不是為了當武林盟主,那將毫無意義public void doReportDateHistory(Map<String, Object> paramMap) { MyUtil mu = MyUtil.getInstance(); //1.先得到引數 String dateStart = paramMap.get("dateStart").toString(); String dateEnd = paramMap.get("dateEnd").toString(); String dateString = paramMap.get("dateString").toString(); //2.先建立表 String sql = "CREATE TABLE IF NOT EXISTS `bi_appdata`.`t_ad_report_date_history_" + dateString + "` (" + " `fb_ad_network_revenue` decimal(50,6) DEFAULT '0.000000' COMMENT '預計收入'," + " `fb_ad_network_request` int(11) DEFAULT '0' COMMENT '廣告請求數量'," + " `country` varchar(25) NOT NULL COMMENT '國家'," + " `delivery_method` varchar(50) NOT NULL COMMENT '未競價主因數量'," + " `platform` varchar(50) NOT NULL COMMENT '平臺'," + " `property` varchar(50) NOT NULL COMMENT '按資產編號'," + " `placement` varchar(50) NOT NULL COMMENT '版位編號'," + " `placement_name` varchar(250) NOT NULL," + " `ad_space` varchar(50) NOT NULL COMMENT 'ad_space'," + " `ctime` date NOT NULL," + " `business_id` varchar(50) NOT NULL," + " `type` int(5) NOT NULL COMMENT '1,2" + " PRIMARY KEY (`business_id`,`ctime`,`ad_space`,`placement_name`,`placement`,`property`,`platform`,`delivery_method`,`country`)" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='廣告收入表'"; System.out.println(sql); this.getBaseDao().excuteBySql(sql,null); //3.轉移資料 String sql2 = "INSERT INTO `bi_appdata`.`t_ad_report_date_history_" + dateString + "`" + " SELECT * FROM `bi_appdata`.`t_ad_report_date_history` WHERE ctime >= '" + dateStart + "-01' AND ctime < '" + dateEnd + "-01'"; System.out.println(sql2); this.getBaseDao().excuteBySql(sql2,null); //4.刪除原表的資料 String sql3 = "delete from `bi_appdata`.`t_ad_report_date_history` WHERE ctime >= '" + dateStart + "-01' AND ctime < '" + dateEnd + "-01'"; System.out.println(sql3); this.getBaseDao().excuteBySql(sql3,null); }