You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
12 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="com.wbi.util.ParamUtil" %>
<%@ page import="java.util.Map" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.general.StringUtil" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.time.LocalDateTime" %>
<%@ page import="java.time.format.DateTimeFormatter" %>
<%@ page import="java.time.LocalTime" %>
<%@ page import="com.cloudstore.dev.api.bean.MessageType" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.HashSet" %>
<%@ page import="com.cloudstore.dev.api.bean.MessageBean" %>
<%@ page import="com.cloudstore.dev.api.util.Util_Message" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.time.format.DateTimeParseException" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="org.slf4j.LoggerFactory" %>
<%@ page import="org.slf4j.Logger" %>
<%@ page import="weaver.hrm.company.DepartmentComInfo" %>
<%@ page import="lombok.SneakyThrows" %>
<%@ page contentType="application/json charset=UTF-8" language="java" %>
<%
/****
* 发送消息通用工具类
*
*
* 参数:
* title:标题 支持表单字段 不为空 主表字段
* context:内容 支持表单字段 不为空 主表字段
* --字段为表单字段数据库名称
* 如果为人员浏览按钮或者部门浏览按钮,需要加前缀
* 人员工号 hrmWorkcode_表单字段
* 人员名称 HrmLastName_表单字段
* 部门名称 deptName_表单字段
* 后期可在
*
* linkUrl:PC端链接
* linkMobileUrl:移动端链接
* codeid:消息来源id 不为空
* billid:表单id 不为空时进行表单字段替换,
* 为空是默认跳过标题内容表单字段替换.
* formName:表单名称
* userid: 消息接收人 可以传userid或者userids(,分割)
* 表单字段的话就用 field_字段名称
*
*/
Logger log = LoggerFactory.getLogger("CustomBusiness");
Map<String, Object> param = ParamUtil.request2Map(request);
JSONObject result = new JSONObject();
String ids = Util.null2String(param.get("billids"));
String title = Util.null2String(param.get("title"));
String context = Util.null2String(param.get("context"));
String linkUrl = Util.null2String(param.get("linkUrl"));
String linkMobileUrl = Util.null2String(param.get("linkMobileUrl"));
int codeid = Util.getIntValue(Util.null2String(param.get("codeid")));
String formName = Util.null2String(param.get("formName"));
String userid = Util.null2String(param.get("userid"));
log.info("billids:{}",ids);
int sendNum = 0;
int successNum = 0;
if (StringUtil.isEmpty(ids)){
result.put("code", "E");
result.put("msg", "单据id为空");
out.print(result.toJSONString());
return;
}else {
RecordSet rs = new RecordSet();
rs.executeQuery("select * from "+ formName +" where id in ( " + ids + " )" );
while (rs.next()){
sendNum++;
//调换下表单字段
String contextStr = replaceVariablesByRs(context, rs);
String titleStr = replaceVariablesByRs(title, rs);
String linkUrlStr = replaceVariablesByRs(linkUrl, rs);
String linkMobileUrlStr = replaceVariablesByRs(linkMobileUrl, rs);
// 判断下接收人是否为表单字段
String sender = "";
if (userid.startsWith("field_")){
sender= Util.null2String(rs.getString(userid.split("_")[1]));
}else {
sender = userid;
}
log.info("userid:{}",userid);
log.info("codeid:{}",codeid);
log.info("title:{}",title);
log.info("context:{}",context);
log.info("titleStr:{}",titleStr);
log.info("contextStr:{}",contextStr);
log.info("linkUrl:{}",linkUrl);
log.info("linkMobileUrl:{}",linkMobileUrl);
log.info("linkUrlStr:{}",linkUrlStr);
log.info("linkMobileUrlStr:{}",linkMobileUrlStr);
sendMsg(sender+"",codeid,titleStr,contextStr,linkUrlStr,linkMobileUrlStr);
successNum++;
// }
}
}
result.put("code", "S");
result.put("msg","提醒发送成功");
result.put("sendNum",sendNum);
result.put("successNum",successNum);
out.print(result.toJSONString());
%>
<%!
private static Map<String, String> getScheduleParam(String pointId) throws Exception {
RecordSet rs = new RecordSet();
rs.executeQuery("select id from schedulesetting where POINTID = ?", pointId);
String setid = "";
if (rs.next()) {
setid = rs.getString("id");
}else {
throw new Exception("未查询到对应配置");
}
rs.executeQuery("select * from schedulesettingdetail where SCHEDULEDBID = ? ",setid);
HashMap<String, String> map = new HashMap<>();
while (rs.next()){
map.put(rs.getString("ATTRNAME"),rs.getString("ATTRVALUE"));
}
return map;
}
/**
* 替换字符串中的${变量名}为Map中对应的值
* @param template 字符串模板,包含${变量名}
* @param variables 包含变量名和对应值的Map
* @return 替换后的字符串
*/
public static String replaceVariables(String template, Map<String, String> variables) {
// 定义匹配 ${变量名} 的正则表达式
Pattern pattern = Pattern.compile("\\$\\{([a-zA-Z0-9_]+)\\}");
Matcher matcher = pattern.matcher(template);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
// 获取变量名
String variableName = matcher.group(1);
// 从Map中获取变量的值如果不存在则使用空字符串
String replacement = variables.getOrDefault(variableName, "");
// 替换 ${变量名} 为对应的值
matcher.appendReplacement(result, replacement);
}
// 将剩余的字符串附加到结果中
matcher.appendTail(result);
return result.toString();
}
/**
* 替换字符串中的${变量名}为Map中对应的值
* @param template 字符串模板,包含${变量名}
* @param rs 查询完的数据库对象
* @return 替换后的字符串
*/
public static String replaceVariablesByRs(String template, RecordSet rs) {
// 定义匹配 ${变量名} 的正则表达式
Pattern pattern = Pattern.compile("\\$\\{([a-zA-Z0-9_]+)\\}");
Matcher matcher = pattern.matcher(template);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
// 获取变量名
String variableName = matcher.group(1);
// 从RecordSet中获取变量的值如果不存在则使用空字符串
String replacement = Util.null2String(getShowNameByid(variableName,rs));
// 替换 ${变量名} 为对应的值
matcher.appendReplacement(result, replacement);
}
// 将剩余的字符串附加到结果中
matcher.appendTail(result);
return result.toString();
}
/**
* 获取当前日期,格式为 yyyy-MM-dd
* @return 当前日期的字符串
*/
public static String getCurrentDate() {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 定义日期格式
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 格式化日期
return now.format(dateFormatter);
}
/**
* 获取当前日期的开始时间即00:00
* @return 当前日期的开始时间,格式为 yyyy-MM-dd HH:mm
*/
public static String getStartOfDay() {
// 获取当前日期
LocalDateTime startOfDay = LocalDateTime.now().with(LocalTime.MIN);
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
// 格式化开始时间
return startOfDay.format(formatter);
}
/**
* 获取当前日期的最后一分钟的时间即23:59
* @return 当前日期的最后一分钟时间,格式为 yyyy-MM-dd HH:mm
*/
public static String getEndOfDay() {
// 获取当前日期
LocalDateTime endOfDay = LocalDateTime.now().with(LocalTime.MAX);
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
// 格式化最后一分钟的时间
return endOfDay.format(formatter);
}
/**
* 获取当前日期和时间,格式为 yyyy-MM-dd HH:mm
* @return 当前日期和时间的字符串
*/
public static String getCurrentDateTime() {
// 获取当前日期时间
LocalDateTime now = LocalDateTime.now();
// 定义日期时间格式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
// 格式化日期时间
return now.format(dateTimeFormatter);
}
private void sendMsg(String userid , int codeid , String title , String context , String linkUrl ,String linkMobileUrl){
MessageType messageType = MessageType.newInstance(codeid); // 消息来源(见文档第四点补充 必填)
Set<String> userIdList = new HashSet<>(); // 接收人id 必填
// userIdList.add(userid);
String[] useridArr = userid.split(",");
for (String s : useridArr) {
userIdList.add(s);
}
// String title = "个人周报更新提醒"; // 标题
// String context = "本次周报新增"+(endNum-startNum)+";" +
// "更新前总数"+startNum+";" +
// "更新后总数"+endNum+";"; // 内容
// String linkUrl = ""; // PC端链接
// String linkMobileUrl = ""; // 移动端链接
try {
MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
messageBean.setCreater(1);// 创建人id
//message.setBizState("0");// 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理
// messageBean.setTargetId("121|22"); //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入
Util_Message.store(messageBean);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 将 yyyy-MM-dd HH:mm 格式的字符串转换为 X年X月X日X时 的格式
* @param dateTimeStr 输入的日期时间字符串,格式为 yyyy-MM-dd HH:mm
* @return 转换后的日期时间字符串,格式为 X年X月X日X时
*/
public static String convertToChineseFormat(String dateTimeStr) {
try {
// 定义输入格式
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
// 将字符串解析为 LocalDateTime 对象
LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, inputFormatter);
// 自定义输出格式X年X月X日X时
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy年M月d日H时");
// 返回格式化后的字符串
return dateTime.format(outputFormatter);
} catch (DateTimeParseException e) {
// 如果输入格式错误,捕获异常并返回提示
return dateTimeStr;
}
}
private static String getShowNameByid(String fieldName, RecordSet rs){
if (StringUtil.isEmpty(fieldName) ){
return "";
}
if (fieldName.startsWith("hrmWorkcode_")){
int userid = Util.getIntValue(rs.getString(fieldName.split("_")[1]));
return new User(userid).getLoginid();
}else if(fieldName.startsWith("HrmLastName_")){
int userid = Util.getIntValue(rs.getString(fieldName.split("_")[1]));
return new User(userid).getLastname();
}else if(fieldName.startsWith("deptName_")){
String deptid = Util.null2String(rs.getString(fieldName.split("_")[1]));
return new DepartmentComInfo().getDepartmentmark(deptid);
}else{
return Util.null2String(rs.getString(fieldName));
}
}
%>