每一天短信发送次数每一分钟发送频率发送限制 hibernate数据库框架

1:代码设计

public static void  sendiscan(){

if (StringUtils.isBlank(mobile)) {

error.code = -1;
error.msg = "手机号码不能为空";


return ;
}

               //根据手机查询 数据是否存在该手机记录
String sql = "select mobile from t_user_send_now  where mobile=? ";
String Smobile = null;


try {
Smobile = t_user_send_now.find(sql, mobile).first();
} catch (Exception e) {
e.printStackTrace();
Logger.info("判断手机是否存在时,根据手机查询数据时:" + e.getMessage());
return ;

}

            //如果存在

if (Smobile != null && mobile.equals(Smobile)) {
Date send_time;
                        Date date=new Date();
Date dateNow=new Date(date.getTime()-60000);
String dql="select send_time from t_user_send_now  where mobile=? ";
try {
send_time=t_user_send_now.find(dql, mobile).first();
} catch (Exception e) {
e.printStackTrace();
Logger.info("查询手机发送短信实际时间时出现错误:" + e.getMessage());
return ;

}

//判断发送间隔是否超过一分钟

if(send_time.getTime()>dateNow.getTime()){

return ;
}else{
int sendCount = 0;
String hql = "select sendCount from t_user_send_now  where mobile=? ";
try {
sendCount = t_user_send_now.find(hql, mobile).first();
} catch (Exception e) {
e.printStackTrace();
Logger.info("判断手机发送短信时,根据手机查询数据时:" + e.getMessage());
return ;

}

//判断当天发送次数是否超过6次

if (sendCount > 6 || sendCount == 6) {
error.code = -4;
error.msg = "该手机发送短信超过6次";
return;
} else {
EntityManager em = JPA.em();
int rows = em.createQuery(
"update t_user_send_now set sendCount=sendCount+1 , send_time=?  where 1=1 and mobile=?")
.setParameter(1, new Date()).setParameter(2, mobile).executeUpdate();
if (rows == 0) {
JPA.setRollbackOnly();

return;
} else {
return ;
}
}

}

//第一次发送

}else{
t_user_send_now sendnow = new t_user_send_now();
sendnow.mobile=mobile;
sendnow.sendCount=1;
sendnow.send_time=new Date();
try {
sendnow.save();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Logger.info("增加发送手机时出现错误 :" + e.getMessage());

return ;
}
return ;

}

}

2:数据库设计


sql 语句mysql

CREATE TABLE `t_user_send_now` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '短信发送号码统计表编号',
  `mobile` varchar(50) DEFAULT NULL COMMENT '发送手机号码',
  `sendCount` int(11) DEFAULT '0' COMMENT '短信发送次数',
  `send_time` datetime DEFAULT NULL COMMENT '上次短信发送时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=78 DEFAULT CHARSET=utf8;


你可能感兴趣的:(每一天短信发送次数每一分钟发送频率发送限制 hibernate数据库框架)