工具类
parent
45b645dea5
commit
767a5759ca
@ -0,0 +1,24 @@
|
||||
package com.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class ApiReturnTools {
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
public static String error(String errorCode,String errorMessage){
|
||||
Map<String,String> returnMap = new HashMap<>();
|
||||
returnMap.put("code",errorCode);
|
||||
returnMap.put("message",errorMessage);
|
||||
return gson.toJson(returnMap);
|
||||
}
|
||||
|
||||
public static String success(Map<String,Object> dataMap){
|
||||
Map<String,Object> returnMap = new HashMap<>();
|
||||
returnMap.put("code","200");
|
||||
returnMap.put("data",dataMap);
|
||||
return gson.toJson(returnMap);
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package com.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Period;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class DateUtil {
|
||||
public static DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
public static DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
public static DateTimeFormatter yyyyMMddHHmm = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
|
||||
public static DateTimeFormatter yyyyMM = DateTimeFormatter.ofPattern("yyyy-MM");
|
||||
|
||||
public static String beforeMonth(String time){
|
||||
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
||||
return localDateTime.minusMonths(1).format(yyyyMM);
|
||||
}
|
||||
public static String lastMonth(String time){
|
||||
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
||||
return localDateTime.plusMonths(1).format(yyyyMM);
|
||||
}
|
||||
public static String nowMonth(String time){
|
||||
LocalDateTime localDateTime = LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
||||
return localDateTime.format(yyyyMM);
|
||||
}
|
||||
|
||||
public static String getCurrentDate(){
|
||||
return LocalDateTime.now().format(yyyyMMdd);
|
||||
}
|
||||
|
||||
public static LocalDateTime getTime(String time){
|
||||
int length = time.length();
|
||||
switch (length){
|
||||
case 7:
|
||||
return LocalDate.parse(time, yyyyMM).atStartOfDay();
|
||||
case 10:
|
||||
return LocalDate.parse(time, yyyyMMdd).atStartOfDay();
|
||||
case 19:
|
||||
return LocalDate.parse(time, yyyyMMddHHmmss).atStartOfDay();
|
||||
case 16:
|
||||
return LocalDate.parse(time, yyyyMMddHHmm).atStartOfDay();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得时间与当前相差的小时
|
||||
* @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static long getBetWeenHours(String startTime){
|
||||
|
||||
Duration duration = Duration.between(LocalDateTime.parse(startTime,yyyyMMddHHmmss),LocalDateTime.now());
|
||||
return duration.toHours();
|
||||
}
|
||||
/**
|
||||
* 获得两个时间相差的年份
|
||||
* @param startTime 开始时间 yyyy-MM-dd
|
||||
* @return
|
||||
*/
|
||||
public static int getBetWeenYears(String startTime){
|
||||
Period duration = Period.between(LocalDate.parse(startTime,yyyyMMdd),LocalDateTime.now().toLocalDate());
|
||||
return duration.getYears();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得两个时间相差的年份
|
||||
* @param startTime 开始时间 yyyy-MM-dd
|
||||
* @return
|
||||
*/
|
||||
public static int getBetWeenDays(String startTime){
|
||||
Period duration = Period.between(LocalDate.parse(startTime,yyyyMMdd),LocalDateTime.now().toLocalDate());
|
||||
return duration.getDays();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得两个时间相差的小时
|
||||
* @param startTime 开始时间 yyyy-MM-dd HH:mm:ss
|
||||
* @param endTime 结束时间 yyyy-MM-dd HH:mm:ss
|
||||
* @return
|
||||
*/
|
||||
public static long getBetWeenHours(String startTime,String endTime){
|
||||
Duration duration = Duration.between(LocalDateTime.parse(startTime,yyyyMMddHHmmss),LocalDateTime.parse(endTime,yyyyMMddHHmmss));
|
||||
return duration.toHours();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
package com.util;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import weaver.conn.RecordSet;
|
||||
import weaver.conn.RecordSetDataSource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DbTools {
|
||||
|
||||
private static ThreadLocal<RecordSet> thread = ThreadLocal.withInitial(()->new RecordSet());
|
||||
|
||||
|
||||
public static List<Map<String,Object>> getSqlToList(String sql,Object...value){
|
||||
RecordSet rs = thread.get();
|
||||
List<Map<String,Object>> result = new ArrayList<>();
|
||||
if (value == null || value.length <=0 || "".equals(value)){
|
||||
rs.executeQuery(sql);
|
||||
}else {
|
||||
rs.executeQuery(sql,value);
|
||||
}
|
||||
while (rs.next()){
|
||||
Map<String,Object> dataMap = new HashMap<>();
|
||||
String[] columns = rs.getColumnName();
|
||||
for (String column:columns){
|
||||
dataMap.put(column,rs.getString(column));
|
||||
}
|
||||
result.add(dataMap);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// public static List<Map<String,Object>> getSqlToList(String sql,String dataSourceName){
|
||||
// RecordSetDataSource rs = threads.get();
|
||||
// if (rs == null){
|
||||
// rs = new RecordSetDataSource(dataSourceName);
|
||||
// threads.set(rs);
|
||||
// }
|
||||
// List<Map<String,Object>> result = new ArrayList<>();
|
||||
// rs.execute(sql);
|
||||
// while (rs.next()){
|
||||
// Map<String,Object> dataMap = new HashMap<>();
|
||||
// String[] columns = rs.getColumnName();
|
||||
// for (String column:columns){
|
||||
// dataMap.put(column,rs.getString(column));
|
||||
// }
|
||||
// result.add(dataMap);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
public static Map<String,Object> getSqlToMap(String sql,Object...value){
|
||||
RecordSet rs = thread.get();
|
||||
Map<String,Object> result = Maps.newHashMap();
|
||||
if (value == null || value.length <=0 || "".equals(value)){
|
||||
rs.executeQuery(sql);
|
||||
}else {
|
||||
rs.executeQuery(sql,value);
|
||||
}
|
||||
if (rs.next()){
|
||||
String[] columns = rs.getColumnName();
|
||||
for (String column:columns){
|
||||
result.put(column.toLowerCase(),rs.getString(column));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static boolean update(String sql,Object...value){
|
||||
RecordSet rs = thread.get();
|
||||
if (value == null || value.length <=0 || "".equals(value)){
|
||||
return rs.executeUpdate(sql);
|
||||
}else {
|
||||
return rs.executeUpdate(sql,value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package com.util;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import weaver.conn.RecordSet;
|
||||
import weaver.general.BaseBean;
|
||||
import weaver.general.Util;
|
||||
|
||||
public class PropBean {
|
||||
|
||||
public static BaseBean bb = new BaseBean();
|
||||
public static String active = Util.null2String(bb.getPropValue("developProp","active")).toUpperCase();
|
||||
|
||||
/***
|
||||
*
|
||||
* @param pkey
|
||||
* @return
|
||||
*/
|
||||
public static String getUfPropValue(String pkey)
|
||||
{
|
||||
BaseBean baseBean = new BaseBean();
|
||||
if(StringUtils.isEmpty(pkey)){
|
||||
return "";
|
||||
}
|
||||
|
||||
if(StringUtils.isEmpty(active)){
|
||||
active = Util.null2String(baseBean.getPropValue("developProp","active")).toUpperCase();
|
||||
}
|
||||
|
||||
//baseBean.writeLog("propbeanactive:"+active+" pkey:"+pkey+" "+active+"VALUE");
|
||||
String pvalue = "";
|
||||
try{
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = " SELECT DEVVALUE,TESTVALUE,PRODVALUE FROM UF_PROP WHERE PKEY = ?";
|
||||
rs.executeQuery(sql,new Object[]{pkey.trim()});
|
||||
|
||||
if(rs.next()){
|
||||
pvalue = Util.null2String(rs.getString(active+"VALUE"));
|
||||
}
|
||||
// baseBean.writeLog("pvalue:"+pvalue);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
bb.writeLog("propbean-e:"+e);
|
||||
}
|
||||
return pvalue;
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
*
|
||||
* @param pkey
|
||||
* @return
|
||||
*/
|
||||
public String getUfPropValueStatic(String pkey)
|
||||
{
|
||||
BaseBean baseBean = new BaseBean();
|
||||
if(StringUtils.isEmpty(pkey)){
|
||||
return "";
|
||||
}
|
||||
String active = Util.null2String(baseBean.getPropValue("developProp","active")).toUpperCase();
|
||||
String pvalue = "";
|
||||
try{
|
||||
RecordSet rs = new RecordSet();
|
||||
String sql = " SELECT DEVVALUE,TESTVALUE,PRODVALUE FROM UF_PROP WHERE PKEY = ?";
|
||||
rs.executeQuery(sql,new Object[]{pkey.trim()});
|
||||
if(rs.next()){
|
||||
pvalue = Util.null2String(rs.getString(active+"VALUE"));
|
||||
}
|
||||
baseBean.writeLog("pvalue:"+pvalue);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
bb.writeLog("propbean-e:"+e);
|
||||
}
|
||||
return pvalue;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.util;
|
||||
|
||||
import com.cloudstore.dev.api.bean.MessageBean;
|
||||
import com.cloudstore.dev.api.bean.MessageType;
|
||||
import com.cloudstore.dev.api.util.Util_Message;
|
||||
import com.engine.dto.BaseResult;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import weaver.general.BaseBean;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 消息中心
|
||||
*/
|
||||
@Slf4j
|
||||
public class SendEmMessage {
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param userIdsSet 接收人id 必填
|
||||
* @param title 标题
|
||||
* @param context 内容
|
||||
* @param pcLinkUrl PC端链接
|
||||
* @param linkMobileUrl 移动端链接
|
||||
*/
|
||||
public static void sendEmMessage(Set<String> userIdsSet,int messageType,int creater, String title,String context,String pcLinkUrl,String linkMobileUrl){
|
||||
MessageType type = MessageType.newInstance(messageType); // 消息来源(见文档第四点补充 必填)
|
||||
|
||||
try {
|
||||
MessageBean messageBean = Util_Message.createMessage(type, userIdsSet, title, context, pcLinkUrl, linkMobileUrl);
|
||||
messageBean.setCreater(creater);// 创建人id
|
||||
|
||||
Util_Message.store(messageBean);
|
||||
} catch (IOException e) {
|
||||
log.error("sendEmMessage error: [{}]",e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static BaseResult sendWxMessage(String chatId,String messageContent){
|
||||
BaseBean baseBean = new BaseBean();
|
||||
//发送消息接口
|
||||
String sendMessageToWxGroupUrl = baseBean.getPropValue("weComConf","sendMessageToWxGroupUrl");
|
||||
Map<String,Object> sendMessageParamMap = Maps.newHashMap();
|
||||
sendMessageParamMap.put("chatid",chatId);
|
||||
sendMessageParamMap.put("msgtype","markdown");
|
||||
Map<String,String> content = Maps.newHashMap();
|
||||
content.put("content",messageContent);
|
||||
sendMessageParamMap.put("markdown",content);
|
||||
Gson gson = new Gson();
|
||||
BaseResult baseResult = gson.fromJson(TokenUtil.post(sendMessageToWxGroupUrl,gson.toJson(sendMessageParamMap)),(Type) BaseResult.class);
|
||||
return baseResult;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.util;
|
||||
|
||||
import com.cloudstore.dev.api.util.Util_DataCache;
|
||||
import com.engine.dto.TokenResult;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import weaver.general.BaseBean;
|
||||
import weaver.general.Util;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class TokenUtil {
|
||||
|
||||
private static final int timeOutSecond = 3600;
|
||||
|
||||
/**
|
||||
* 配置文件名称
|
||||
*/
|
||||
private static final String confFileName = "weComConf";
|
||||
|
||||
private static BaseBean baseBean = new BaseBean();
|
||||
|
||||
private static Gson gson = new Gson();
|
||||
|
||||
public static final String TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
||||
|
||||
private static String corpid = "";
|
||||
|
||||
private static String corpsecret = "";
|
||||
|
||||
static {
|
||||
corpid = baseBean.getPropValue(confFileName,"corpid");
|
||||
corpsecret = baseBean.getPropValue(confFileName,"corpsecret");
|
||||
// Util_DataCache.setObjVal("corpid",corpid);
|
||||
// Util_DataCache.setObjVal("corpsecret",corpsecret);
|
||||
}
|
||||
|
||||
public static String post(String url,String paramJson){
|
||||
String token = Util.null2String(Util_DataCache.getObjVal("wx_access_token"));
|
||||
if ("".equals(token)){
|
||||
token = getAccessToken();
|
||||
Util_DataCache.setObjVal("wx_access_token",token);
|
||||
}
|
||||
url = url + "?access_token="+token;
|
||||
return HttpRequestUtil.doPost(url,paramJson);
|
||||
}
|
||||
|
||||
|
||||
public static String getAccessToken(){
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("corpid",corpid);
|
||||
map.put("corpsecret",corpsecret);
|
||||
// map.put("corpid","wx22cd58d9bee18c0f");
|
||||
// map.put("corpsecret","n4ueR_AnNWXtmYpwr01dRA6i2v9kephxRKbW23n_ZTM");
|
||||
|
||||
String response = HttpRequestUtil.doGet(TOKEN_URL,map);
|
||||
TokenResult tokenResult = gson.fromJson(response,(Type) TokenResult.class);
|
||||
if ("0".equals(tokenResult.getErrcode())){
|
||||
return tokenResult.getAccess_token();
|
||||
}else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.util;
|
||||
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Utils {
|
||||
public static Gson gson = new Gson();
|
||||
public static Type list_map_type = new TypeToken<List<Map<String,String>>>(){}.getType();
|
||||
public static Type map_type = new TypeToken<Map<String,Object>>(){}.getType();
|
||||
|
||||
/**
|
||||
* 解析格式为[{a:b,c:d}]格式的json
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String,String>> resolveList_Map(String json){
|
||||
List<Map<String,String>> list= gson.fromJson(json,list_map_type);
|
||||
return list;
|
||||
}
|
||||
/**
|
||||
* 解析格式为{a:b,c:d}格式的json
|
||||
* @param json
|
||||
* @return
|
||||
*/
|
||||
public static Map<String,Object> resolveMap(String json){
|
||||
Map<String,Object> list= gson.fromJson(json,map_type);
|
||||
return list;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue