1、先创建短信日志类:
2、使用定时器,每隔一天,读取两天内未完成的任务信息
然后插入短信日志里
global class RemindSalesInfo implements Schedulable {
global void execute(SchedulableContext ctx) {
//1.根据角色得到所有的销售人员
//2.循环根据销售人员根据id和销售订单的创建时间与现在时间的差查询出出差申请信息
//3.如果查询的记录<=0时提醒,拜访记录如果两天没有四条提醒
//4.用新的list统计出这些人员的名单
//5.循环这些人员,根据角色得到他们的上级领导,
//6.发送短消息
//如果相同就返回,节假日不发送
set<Holiday> holidays = new set<Holiday>([select ActivityDate from Holiday]);
for(Holiday h : holidays){
if(h.ActivityDate == Date.today()){
return;
}
}
Map<Date, specialDate__c> smap= new Map<Date, specialDate__c>();
Set<specialDate__c> specialDates = new Set<specialDate__c>([select WorkDay__c from specialDate__c]);
for(specialDate__c s : specialDates){
smap.put(s.WorkDay__c , s);
}
//自动判断星期六、天不发送信息,上班日期要发送
Datetime dt = DateTime.now();
String dayOfWeek=dt.format('u');
if((dayOfWeek == '7' || dayOfWeek == '6') && !smap.containsKey(Date.today())){
return;
}
List<User> allSales = [SELECT Id, Name, UserRole.ParentRoleId FROM User where Profile.Name like '%大族粤铭销售业务员%'];
List<User> managers = [SELECT Id, Name,phone FROM User where UserRole.DeveloperName = 'ZJL' ];
System.debug('allSales=' + allSales.size());
DateTime now = System.now();
Datetime lastDateTime = now.addDays(-2);
List<User> unSubmitSales = new List<User>();
List<User> unSubmitVisiter = new List<User>();
//得到所有的经理
Map<String, User> leaderMap = new Map<String,User>();
List<User> allLeaders = [SELECT id, Name, UserRole.id, UserRole.DeveloperName,phone from User where UserRole.Name like '%经理%' and (not UserRole.Name like '%总经理%')];
for(User u : allLeaders){
leaderMap.put(u.UserRole.id , u);
}
//---------查询等于两天时间的出差申请--------------------
Map<String, Integer> cMap = new Map<String,Integer>();
List<CustomObject2__c> trips = [SELECT Id, CreatedById FROM CustomObject2__c WHERE CreatedDate >:lastDateTime and CreatedDate <:now ];
for(CustomObject2__c c : trips){
if(cMap.containsKey(c.CreatedById)){
cMap.put(c.CreatedById , cMap.get(c.CreatedById)+1);
}else{
cMap.put(c.CreatedById , 1);
}
}
for(User u : allSales){
String uid = u.id;
//List<CustomObject2__c> trips = [SELECT Id FROM CustomObject2__c WHERE CreatedById =: uid and CreatedDate <:newDateTime];
if(!cMap.containsKey(uid)){
unSubmitSales.add(u);
}
}
for(User u : unSubmitSales){
//获得上一级的领导信息
List<User> leaders = new List<User>();
if(leaderMap.containsKey(u.UserRole.ParentRoleId)){
User leaderUser = leaderMap.get(u.UserRole.ParentRoleId);
leaders.add(leaderUser);
}
leaders.addAll(managers);
System.debug('leaders=' + leaders.size());
//发送未提交出差申请的短信;
String content = u.Name + ' 两天内还未填写出差申请,请注意!';
SMS.doInsert(content, leaders, 'trip');
}
//---------查询等于两天时间的拜访记录--------------------
Map<String, Integer> visitMap = new Map<String,Integer>();
List<CustomObject1__c> visitList = [SELECT Id, CreatedById FROM CustomObject1__c WHERE CreatedDate >:lastDateTime and CreatedDate <:now ];
for(CustomObject1__c c : visitList){
if(visitMap.containsKey(c.CreatedById)){
visitMap.put(c.CreatedById , cMap.get(c.CreatedById)+1);
}else{
visitMap.put(c.CreatedById , 1);
}
}
for(User u : allSales){
String uid = u.id;
if(!visitMap.containsKey(uid)||visitMap.get(uid)<3){
//System.debug(uid);
unSubmitVisiter.add(u);
}
}
System.debug('unSubmitVisiter.size()=' + unSubmitVisiter.size());
for(User u : unSubmitVisiter){
//获得上一级的领导信息
List<User> leaders = new List<User>();
if(leaderMap.containsKey(u.UserRole.ParentRoleId)){
User leaderUser = leaderMap.get(u.UserRole.ParentRoleId);
leaders.add(leaderUser);
}
leaders.addAll(managers);
System.debug('leaders=' + leaders.size());
//发送提交拜访记录小于3篇的短信;
String content = u.Name + ' 两天内填写的拜访记录少于4篇,请注意!';
SMS.doInsert(content, leaders, 'visit');
}
}
}
3、使用定时器,每天的8-18小时发送短信,星期六,七及假日不发送
global class RemindSales implements Schedulable {
global void execute(SchedulableContext ctx) {
//如果相同就返回
set<Holiday> holidays = new set<Holiday>([select ActivityDate from Holiday]);
for(Holiday h : holidays){
if(h.ActivityDate == Date.today()){
return;
}
}
Map<Date, specialDate__c> smap= new Map<Date, specialDate__c>();
Set<specialDate__c> specialDates = new Set<specialDate__c>([select WorkDay__c from specialDate__c]);
for(specialDate__c s : specialDates){
smap.put(s.WorkDay__c , s);
}
//自动判断星期六、天不发送信息,上班日期要发送
Datetime dt = DateTime.now();
String dayOfWeek=dt.format('u');
if((dayOfWeek == '7' || dayOfWeek == '6') && !smap.containsKey(Date.today())){
return;
}
List<SMSInfo__c> sis = [SELECT Id, content__c, phone__c, TimeNum__c FROM SMSInfo__c where Status__c = '2' and (type__c = 'visit' or type__c = 'trip') limit 49];
for(SMSInfo__c s : sis){
//因为里面已经有了更新方法
SMS.sendMethod(s.phone__c , s.content__c, s.TimeNum__c);
//SMS.sendMethod('15019179692' , s.content__c, s.TimeNum__c);
}
}
}
4、发送短信功能
public class SMS {
private String url;
private String url1;
private String url2;
private String result;
public SMS(){
url='';
url1='';
url2='';
result='';
}
//发送一条短信,接收一个电话号码和短信内容,返回一个请求结果状态
public String sendSMS (String content,String phone,String exId) {
url2 = '&ac='+Label.SMSAccount
+'&authkey='+ Label.SMSPassWord
+'&cgid=16&c=' + EncodingUtil.urlEncode(content, 'utf8')
+'&m='+phone;
url1 = 'http://192.168.0.20/OpenPlatform/OpenApi?action=sendOnce';
//构造短信接口url格式
url = url1 + url2;
system.debug('url'+url);
//发送http请求,需要在远程站点设置用设置访问的网址url,否则请求会失败
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
req.setTimeout(12000);
Http http = new Http();
HttpResponse res = http.send(req);
System.debug(res.getStatus()+'==============sms result========' +'|'+res.getStatusCode());
System.debug('==============sms result2========' +'|'+res.getBody());
//测试短信接口返回的状态,100表示已经发送(请求的格式正确,成功连接到短信接口),但是并不代表短信发送到对方手机上
//当手机号码为连续的任何数字都会成功并返回100状态
XmlStreamReader xsr = new XmlStreamReader(res.getBody());
SMSInfo__c info =[SELECT id,Status__c,msgId__c FROM SMSInfo__c WHERE TimeNum__c=:exId limit 1];
xsr.next();
while(xsr.hasNext()){
if(xsr.isStartElement()){
system.debug('name:'+xsr.getLocalName());
if(xsr.getLocalName()=='xml'){
for(integer i=0;i<xsr.getAttributeCount();i++){
if(xsr.getAttributeLocalName(i)=='result'){
info.Status__c = xsr.getAttributeValueAt(i);
}
}
}else if(xsr.getLocalName()=='Item'){
for(integer i=0;i<xsr.getAttributeCount();i++){
if(xsr.getAttributeLocalName(i)=='msgid'){
info.msgId__c = xsr.getAttributeValueAt(i);
}
}
}
}
xsr.next();
}
update info;
return res.getStatus();
}
public static void doFutureSend(String phone,String content){
SMSInfo__c info = new SMSInfo__c();
info.phone__c = phone;
info.content__c = content;
info.Status__c = '2';//未发送
info.TimeNum__c = String.valueOf(DateTime.now().getTime());
insert info;
sendMethod(phone,content,info.TimeNum__c);
}
//出差申请与拜访记录插入数据
public static void doInsert(String content, List<User> leaders, String type){
List<SMSInfo__c> smsList = new List<SMSInfo__c>();
DateTime now = System.now();
Datetime lastDateTime = now.addDays(-2);
List<SMSInfo__c> infoList = [SELECT Id, content__c, phone__c FROM SMSInfo__c where (addDate__c>: lastDateTime and addDate__c<:now) and type__c =: type ];
Map<String, SMSInfo__c> infoMap = new Map<String, SMSInfo__c>();
for(SMSInfo__c info : infoList){
infoMap.put(info.content__c, info);
}
Integer i = 0;
if(!infoMap.containsKey(content)){
for(User leader : leaders){
SMSInfo__c info = new SMSInfo__c();
info.phone__c = leader.phone;
info.content__c = content;
info.Status__c = '2';//未发送
info.type__c = type;
info.addDate__c = now;
info.TimeNum__c = String.valueOf(DateTime.now().getTime() + i++);
smsList.add(info);
}
}
insert smsList;
}
@future(callout=true)
public static void sendMethod(String phoneNum,String content,String exId){
SMS s= new SMS();
String status ='';
status = s.sendSMS(content, phoneNum, exId);
}
//验证电话号码是否正确
public boolean isPhoneNumber(String str) {
boolean flag = false;
Pattern p = Pattern.compile('^[1][3,4,5,8][0-9]{9}$'); // 验证手机号
Matcher m = p.matcher(str);
return m.matches();
}
}
5、定时器
RemindSales r = new RemindSales();
String sch = '0 0 9-18 * * ?';
String jobID = system.schedule('每天9点到18点的每小时0分运行一次', sch, r);
RemindSalesInfo rs2 = new RemindSalesInfo();
String sch = '0 0 13 * * ?';
String jobID = system.schedule('每天13点运行一次插入未提交“出差申请”和“拜访记录”的业务员的信息到短信', sch, rs2);