|
@@ -1,9 +1,14 @@
|
|
|
package com.sqx.scheduler.export;
|
|
package com.sqx.scheduler.export;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
-import com.sqx.modules.coupon.service.TbCouponUserService;
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
|
|
|
|
|
+import com.sqx.common.utils.SftpUtil;
|
|
|
|
|
+import com.sqx.modules.app.entity.RechargeRecord;
|
|
|
|
|
+import com.sqx.modules.app.service.RechargeRecordService;
|
|
|
|
|
+import com.sqx.modules.common.service.CommonInfoService;
|
|
|
import com.sqx.modules.exportExecl.mapper.ExportJobMapper;
|
|
import com.sqx.modules.exportExecl.mapper.ExportJobMapper;
|
|
|
import com.sqx.modules.exportExecl.model.ExportJob;
|
|
import com.sqx.modules.exportExecl.model.ExportJob;
|
|
|
|
|
+import com.sqx.modules.exportExecl.service.ExportJobService;
|
|
|
import com.sqx.modules.order.service.AppOrderService;
|
|
import com.sqx.modules.order.service.AppOrderService;
|
|
|
import com.sqx.modules.utils.excel.ExcelData;
|
|
import com.sqx.modules.utils.excel.ExcelData;
|
|
|
import com.sqx.modules.utils.excel.ExportExcelUtils;
|
|
import com.sqx.modules.utils.excel.ExportExcelUtils;
|
|
@@ -13,14 +18,21 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.redisson.api.RLock;
|
|
import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
|
|
+import java.io.InputStream;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
@@ -36,62 +48,100 @@ import java.util.concurrent.TimeUnit;
|
|
|
@RequestMapping("/admin/aaaaaa")
|
|
@RequestMapping("/admin/aaaaaa")
|
|
|
public class ExportScheduler {
|
|
public class ExportScheduler {
|
|
|
|
|
|
|
|
- private final TbCouponUserService tbCouponUserService;
|
|
|
|
|
|
|
+ private final CommonInfoService commonInfoService;
|
|
|
private final RedissonClient redissonClient;
|
|
private final RedissonClient redissonClient;
|
|
|
- @Autowired
|
|
|
|
|
- private ExportJobMapper exportJobMapper;
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private AppOrderService appOrderService;
|
|
|
|
|
|
|
+ private final RedisTemplate redisTemplate;
|
|
|
|
|
+ private final ExportJobService exportJobService;
|
|
|
|
|
+ private final AppOrderService appOrderService;
|
|
|
|
|
+ private final RechargeRecordService rechargeRecordService;
|
|
|
|
|
+ private final SftpUtil sftpUtil;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 将所有超过失效时间的优惠券改为失效状态
|
|
* 将所有超过失效时间的优惠券改为失效状态
|
|
|
* 每分钟运行一次
|
|
* 每分钟运行一次
|
|
|
*/
|
|
*/
|
|
|
@Async
|
|
@Async
|
|
|
- @Scheduled(cron = "0 */1 * * * ?", zone = "Asia/Shanghai")
|
|
|
|
|
- @GetMapping("excelOrder")
|
|
|
|
|
|
|
+ @Scheduled(cron = "18 */1 * * * ?", zone = "Asia/Shanghai")
|
|
|
|
|
+// @GetMapping("excelOrder")
|
|
|
public void orderExport() throws Exception {
|
|
public void orderExport() throws Exception {
|
|
|
- List<ExportJob> exportJobList=exportJobMapper.selectList(new QueryWrapper<ExportJob>()
|
|
|
|
|
- .eq("flag",0).last("limit 2"));
|
|
|
|
|
- log.info("exportJobList==========="+exportJobList);
|
|
|
|
|
|
|
+ List<ExportJob> exportJobList=exportJobService.list(new QueryWrapper<ExportJob>()
|
|
|
|
|
+ .eq("flag",0).last("order by create_time limit 10"));
|
|
|
|
|
+ log.info("本次导出数据exportJobList==========="+exportJobList);
|
|
|
if (null == exportJobList ||exportJobList.isEmpty()){
|
|
if (null == exportJobList ||exportJobList.isEmpty()){
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ int runningCount =exportJobService.count(new QueryWrapper<ExportJob>().eq("flag",-1));
|
|
|
|
|
+ if (runningCount>=4){
|
|
|
|
|
+ //同时执行的任务超过4条,不再新增任务
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
for(ExportJob exportJob:exportJobList){
|
|
for(ExportJob exportJob:exportJobList){
|
|
|
long bgTime = System.currentTimeMillis();
|
|
long bgTime = System.currentTimeMillis();
|
|
|
String jobExecuteKey = SchedulerLock.EXPORT_LOCK + ":" + exportJob.getExportId();
|
|
String jobExecuteKey = SchedulerLock.EXPORT_LOCK + ":" + exportJob.getExportId();
|
|
|
|
|
+ String jobExecuteRedisKey = SchedulerLock.EXPORT_LOCK +":R:" + exportJob.getExportId();
|
|
|
RLock lock = redissonClient.getLock(jobExecuteKey);
|
|
RLock lock = redissonClient.getLock(jobExecuteKey);
|
|
|
- if (lock.tryLock(0, 30, TimeUnit.SECONDS)) {
|
|
|
|
|
|
|
+ if (lock.tryLock(0, 5, TimeUnit.SECONDS)) {
|
|
|
|
|
+ boolean isRedisKey = redisTemplate.hasKey(jobExecuteRedisKey);
|
|
|
|
|
+ //rediskey避免重复导出
|
|
|
|
|
+ if(isRedisKey){
|
|
|
|
|
+ log.info(exportJob.getExportId()+"已执行,跳过");
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ redisTemplate.opsForValue().set(jobExecuteRedisKey, "LOCK", 5, TimeUnit.MINUTES);
|
|
|
|
|
+ //设置为执行中
|
|
|
|
|
+ exportJob.setFlag(-1);
|
|
|
|
|
+ exportJob.setUpdateTime(new Date());
|
|
|
|
|
+ exportJobService.updateById(exportJob);
|
|
|
String fileName=exportJob.getFileName();
|
|
String fileName=exportJob.getFileName();
|
|
|
String fileType=exportJob.getFileType();
|
|
String fileType=exportJob.getFileType();
|
|
|
|
|
+ InputStream inputStream=null;
|
|
|
|
|
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
|
try {
|
|
try {
|
|
|
ExcelData excelData=new ExcelData();
|
|
ExcelData excelData=new ExcelData();
|
|
|
if("order".equals(fileType)){
|
|
if("order".equals(fileType)){
|
|
|
excelData=getOrderExcelData(exportJob);
|
|
excelData=getOrderExcelData(exportJob);
|
|
|
|
|
+ }else if("recharge".equals(fileType)){
|
|
|
|
|
+ excelData=getRechargeOrderExcelData(exportJob);
|
|
|
}
|
|
}
|
|
|
- File file=new File(fileName);
|
|
|
|
|
- ExportExcelUtils.writeExcel2(file,fileName,excelData);
|
|
|
|
|
- double fileSize = file.length();
|
|
|
|
|
|
|
+ String value=commonInfoService.findOne(430).getValue();
|
|
|
|
|
+ ExportExcelUtils.writeExcel2(os,fileName,excelData);
|
|
|
|
|
+ byte[] buffer = os.toByteArray();
|
|
|
|
|
+ inputStream = new ByteArrayInputStream(buffer);
|
|
|
|
|
+ double fileSize = os.size();
|
|
|
double fileSizeKB = new BigDecimal(fileSize/1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //转换单位为KB
|
|
double fileSizeKB = new BigDecimal(fileSize/1024).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //转换单位为KB
|
|
|
exportJob.setFileSize(String.valueOf(fileSizeKB));
|
|
exportJob.setFileSize(String.valueOf(fileSizeKB));
|
|
|
exportJob.setFlag(1);
|
|
exportJob.setFlag(1);
|
|
|
- exportJobMapper.updateById(exportJob);
|
|
|
|
|
|
|
+ exportJob.setUpdateTime(new Date());
|
|
|
|
|
+ sftpUtil.login();
|
|
|
|
|
+ sftpUtil.upload(value,fileName,inputStream);
|
|
|
|
|
+ exportJobService.updateById(exportJob);
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
|
|
+ exportJob.setUpdateTime(new Date());
|
|
|
exportJob.setFlag(2);
|
|
exportJob.setFlag(2);
|
|
|
- exportJobMapper.updateById(exportJob);
|
|
|
|
|
- log.error(e.getMessage());
|
|
|
|
|
|
|
+ exportJobService.updateById(exportJob);
|
|
|
|
|
+ log.error(e.toString());
|
|
|
} finally {
|
|
} finally {
|
|
|
try {
|
|
try {
|
|
|
if (lock.isLocked()) {
|
|
if (lock.isLocked()) {
|
|
|
lock.unlock();
|
|
lock.unlock();
|
|
|
}
|
|
}
|
|
|
|
|
+ if(os!=null){
|
|
|
|
|
+ os.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ if(inputStream!=null){
|
|
|
|
|
+ inputStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ if(sftpUtil.isConnected()){
|
|
|
|
|
+ sftpUtil.logout();
|
|
|
|
|
+ }
|
|
|
} catch (RuntimeException re) {
|
|
} catch (RuntimeException re) {
|
|
|
log.error("报表解锁失败");
|
|
log.error("报表解锁失败");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
long edTime = System.currentTimeMillis();
|
|
long edTime = System.currentTimeMillis();
|
|
|
- log.info("执行:通用数据导出定时任务......执行任务完成,执行时长:{} s", (edTime - bgTime) / 1000);
|
|
|
|
|
|
|
+ log.info("执行:通用数据导出定时任务"+exportJob.getExportId()+"......执行任务完成,执行时长:{} s", (edTime - bgTime) / 1000);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
//订单导出
|
|
//订单导出
|
|
@@ -117,8 +167,53 @@ public class ExportScheduler {
|
|
|
String riderPhone=conditions[8];
|
|
String riderPhone=conditions[8];
|
|
|
String startTime=conditions[9];
|
|
String startTime=conditions[9];
|
|
|
String endTime=conditions[10];
|
|
String endTime=conditions[10];
|
|
|
- ExcelData excelData = appOrderService.excelAllOrderAdmin(status, phone, shopId, userName, orderNumber, orderType, shopName, riderPhone, startTime, endTime);
|
|
|
|
|
|
|
+ String payStartTime=conditions[11];
|
|
|
|
|
+ String payEndTime=conditions[12];
|
|
|
|
|
+ ExcelData excelData = appOrderService.excelAllOrderAdmin(status, phone, shopId, userName, orderNumber, orderType, shopName, riderPhone, startTime, endTime,payStartTime,payEndTime);
|
|
|
|
|
|
|
|
return excelData;
|
|
return excelData;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ //充值记录导出
|
|
|
|
|
+ private ExcelData getRechargeOrderExcelData(ExportJob exportJob){
|
|
|
|
|
+ String[] conditions=exportJob.getConditions().split(",",-1);
|
|
|
|
|
+ String rechargeOrder=conditions[1];
|
|
|
|
|
+ String acount=conditions[2];
|
|
|
|
|
+ QueryWrapper qw=new QueryWrapper<RechargeRecord>();
|
|
|
|
|
+ if(StringUtils.hasText(rechargeOrder)){
|
|
|
|
|
+ qw.eq("order_no",rechargeOrder);
|
|
|
|
|
+ }
|
|
|
|
|
+ if(StringUtils.hasText(acount)){
|
|
|
|
|
+ qw.eq("user_phone",acount);
|
|
|
|
|
+ }
|
|
|
|
|
+ ExcelData data = new ExcelData();
|
|
|
|
|
+ data.setName("订单列表");
|
|
|
|
|
+ List<String> titles = new ArrayList();
|
|
|
|
|
+ titles.add("用户id");titles.add("编号");titles.add("充值金额");titles.add("充值前余额");
|
|
|
|
|
+ titles.add("充值后余额"); titles.add("被充值账号(手机号)");titles.add("被充值名称");titles.add("被充值昵称");
|
|
|
|
|
+ titles.add("被充值头像"); titles.add("更新时间");titles.add("充值时间");titles.add("充值人账号");
|
|
|
|
|
+ titles.add("备注");
|
|
|
|
|
+ data.setTitles(titles);
|
|
|
|
|
+ List<RechargeRecord> recordList=rechargeRecordService.list(qw);
|
|
|
|
|
+ List<List<Object>> rows = new ArrayList();
|
|
|
|
|
+ for (RechargeRecord record:recordList){
|
|
|
|
|
+ List<Object> row = new ArrayList();
|
|
|
|
|
+ row.add(record.getUserId());
|
|
|
|
|
+ row.add(record.getOrderNo());
|
|
|
|
|
+ row.add(record.getAmount());
|
|
|
|
|
+ row.add(record.getOldMoney());
|
|
|
|
|
+ row.add(record.getBalance());
|
|
|
|
|
+ row.add(record.getUserPhone());
|
|
|
|
|
+ row.add(record.getUserName());
|
|
|
|
|
+ row.add(record.getUserNick());
|
|
|
|
|
+ row.add(record.getUserAvatar());
|
|
|
|
|
+ row.add(record.getUpdateTime());
|
|
|
|
|
+ row.add(record.getCreateTime());
|
|
|
|
|
+ row.add(record.getAccount());
|
|
|
|
|
+ row.add(record.getRemark());
|
|
|
|
|
+ rows.add(row);
|
|
|
|
|
+ }
|
|
|
|
|
+ data.setRows(rows);
|
|
|
|
|
+ return data;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|