Commit 7fc3376e authored by shiyu's avatar shiyu

定时作业

parent 80825c2f
package com.wwdz.ch.core.util;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StringArrayExcelReadListener extends AnalysisEventListener<Map<String, String>> {
/**
* 存储读取到的表头
*/
private List<String> head = new ArrayList<>();
/**
* 存储读取到的 Excel 数据
*/
private List<List<String>> data = new ArrayList<>();
/**
* 每解析一行都会回调invoke()方法
* @param item 读取后的数据对象
* @param context 内容
*/
@Override
public void invoke(Map<String, String> item, AnalysisContext context) {
if(item != null && !item.isEmpty()) {
List<String> info = item.entrySet().stream().map(e -> e.getValue()).collect(Collectors.toList());
data.add(info);
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
}
/**
* 处理读取到的表头数据
* @param headMap
* @param context
*/
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
if(headMap != null && !headMap.isEmpty()) {
head = headMap.entrySet().stream().map(e -> e.getValue()).collect(Collectors.toList());
}
}
/**
* 获取表头数据信息
* @return
*/
public List<String> getHead() {
return this.head;
}
/**
* 获取读取到的 Excel 数据
* @return
*/
public List<List<String>> getData() {
return this.data;
}
}
package com.wwdz.ch.wx.impl.distribution;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.github.pagehelper.PageInfo;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.refund.model.Status;
......@@ -24,6 +28,7 @@ import com.wwdz.ch.wx.service.FollowFansRecordService;
import com.wwdz.ch.wx.service.distribution.DistributorProfitService;
import com.wwdz.ch.wx.service.distribution.GroupPurchaseService;
import com.wwdz.ch.wx.service.distribution.UserAccountDetailService;
import com.xxdxxs.utils.DateUtils;
import com.xxdxxs.utils.StringUtils;
import org.redisson.api.RLock;
import org.slf4j.Logger;
......@@ -33,6 +38,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;
......@@ -80,6 +86,9 @@ public class GroupPurchaseServiceImpl implements GroupPurchaseService {
@Autowired
FollowFansRecordService followFansRecordService;
@Autowired
RedisUtils redisUtils;
@Override
public Result findList(GroupPurchaseConfigRequestDto dto) {
try {
......@@ -360,4 +369,40 @@ public class GroupPurchaseServiceImpl implements GroupPurchaseService {
}
return Result.failed();
}
@Override
public Result sendMsgFromExcel(GroupPurchaseRecordRequestDto dto) {
try {
String key = "sendMsgFromExcel_key";
Date now = new Date();
Date time = DateUtils.parseString("2024-08-08 20:00:00");
if (now.before(time)) {
return Result.failed("未到指定的时间");
}
if (redisUtils.hasKey(key)) {
return Result.failed("已经执行过了");
}
String fileName = "喜马拉雅品类用户手机号.xlsx";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
StringArrayExcelReadListener listener = new StringArrayExcelReadListener();
ExcelReader reader = EasyExcelFactory.read(inputStream, listener).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
reader.read(readSheet);
reader.finish();
List<String> head = listener.getHead();
logger.info("excel title = {}", head);
List<List<String>> dataList = listener.getData();
logger.info(">>>>>>>>>> 喜马拉雅类目 excel 读取数据 : {} 条", dataList.size());
dataList.stream().forEach(row -> {
String phone = row.get(0);
logger.info("手机号: {}", phone);
});
redisUtils.set(key, now, 60);
return Result.success();
} catch (Exception e) {
logger.error("系统增加团购记录 error : {}", e);
}
return Result.failed();
}
}
package com.wwdz.ch.wx.job;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.wwdz.ch.core.api.WxAppletApi;
import com.wwdz.ch.core.consts.CommConsts;
import com.wwdz.ch.core.notify.AliSmsSender;
import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.RedisUtils;
import com.wwdz.ch.core.util.StringArrayExcelReadListener;
import com.xxdxxs.utils.DateUtils;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
@Component
public class SendMsgFromExcelJob {
private static final Logger logger = LoggerFactory.getLogger(SendMsgFromExcelJob.class);
private static final String SENDMSGFROMEXCEL_KEY = "SENDMSGFROMEXCEL_KEY";
@Autowired
RedisUtils redisUtils;
@Autowired
AliSmsSender aliSmsSender;
@Autowired
WxAppletApi wxAppletApi;
@Autowired
RedissonClient redissonClient;
// 每年8月8日晚上8点执行一次
// @Scheduled(cron = "0 0 20 8 8 ?")
@Scheduled(cron = "0 20 15 8 8 ?")
public void executeTask() {
RLock lock = redissonClient.getLock(SENDMSGFROMEXCEL_KEY);
if (!lock.tryLock()) {
logger.warn("给指定类目用户发送通知任务,当前服务实例获取锁成功: {} 获取锁失败,锁被占用不执行", Thread.currentThread().getId());
return;
}
logger.info(">>>>>>>>>>>> sendMsgFromExcel 开始执行 <<<<<<<<<<<<");
try {
String key = "xmly:sendMsgFromExcel";
Date now = new Date();
if (redisUtils.hasKey(key)) {
String msg = "给指定类目用户发送通知任务正在重复执行";
aliSmsSender.sendSms("15757185512", msg);
logger.info(">>>>>>>>>>>> sendMsgFromExcel 已经执行过了 <<<<<<<<<<<<");
return;
}
String fileName = "喜马拉雅品类用户手机号.xlsx";
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(fileName);
StringArrayExcelReadListener listener = new StringArrayExcelReadListener();
ExcelReader reader = EasyExcelFactory.read(inputStream, listener).build();
ReadSheet readSheet = EasyExcel.readSheet(0).build();
reader.read(readSheet);
reader.finish();
List<String> head = listener.getHead();
logger.info("excel title = {}", head);
List<List<String>> dataList = listener.getData();
logger.info(">>>>>>>>>> 喜马拉雅类目 excel 读取数据 : {} 条", dataList.size());
dataList.stream().forEach(row -> {
String phone = row.get(0);
logger.info("手机号: {}, 喜马拉雅类目, 发送短信成功", phone);
});
/* String url = wxAppletApi.getUrlLink(CommConsts.ITEM_OF_GROUPPURCHASE_URL,"itemId=" + 32894809);
//发短信
String msg = "藏传手绘黄财神小唐卡开启众筹啦,请前往"+ url + "查看";
aliSmsSender.sendSms("15757185512", msg);*/
redisUtils.set(key, now, 3600 * 24);
logger.info(">>>>>>>>>>>> sendMsgFromExcel 发送通知给指定用户定时任务执行完成 <<<<<<<<<<<<");
} catch (Exception e) {
logger.error("sendMsgFromExcel error : {}", e);
} finally {
if (lock != null && lock.isHeldByCurrentThread()) {
lock.unlock();
logger.info("======================== 线程id: {} ,给指定类目用户发送通知任务结束, 释放锁成功 ========================", Thread.currentThread().getId());
}
}
}
}
......@@ -46,4 +46,11 @@ public interface GroupPurchaseService {
*/
Result systemAddRecord(GroupPurchaseRecordRequestDto dto);
/**
* 读取excel手机号发送短信
* @return
*/
Result sendMsgFromExcel(GroupPurchaseRecordRequestDto dto);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment