diff --git a/src/com/attendance/engine/util/ApiReturnTools.java b/src/com/attendance/engine/util/ApiReturnTools.java new file mode 100644 index 0000000..b2061cd --- /dev/null +++ b/src/com/attendance/engine/util/ApiReturnTools.java @@ -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 returnMap = new HashMap<>(); + returnMap.put("code",errorCode); + returnMap.put("message",errorMessage); + return gson.toJson(returnMap); + } + + public static String success(Map dataMap){ + Map returnMap = new HashMap<>(); + returnMap.put("code","200"); + returnMap.put("data",dataMap); + return gson.toJson(returnMap); + } +} diff --git a/src/com/attendance/engine/util/DateUtil.java b/src/com/attendance/engine/util/DateUtil.java new file mode 100644 index 0000000..ce7a91d --- /dev/null +++ b/src/com/attendance/engine/util/DateUtil.java @@ -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(); + } + + +} diff --git a/src/com/attendance/engine/util/DbTools.java b/src/com/attendance/engine/util/DbTools.java new file mode 100644 index 0000000..19a3946 --- /dev/null +++ b/src/com/attendance/engine/util/DbTools.java @@ -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 thread = ThreadLocal.withInitial(()->new RecordSet()); + + + public static List> getSqlToList(String sql,Object...value){ + RecordSet rs = thread.get(); + List> result = new ArrayList<>(); + if (value == null || value.length <=0 || "".equals(value)){ + rs.executeQuery(sql); + }else { + rs.executeQuery(sql,value); + } + while (rs.next()){ + Map 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> getSqlToList(String sql,String dataSourceName){ +// RecordSetDataSource rs = threads.get(); +// if (rs == null){ +// rs = new RecordSetDataSource(dataSourceName); +// threads.set(rs); +// } +// List> result = new ArrayList<>(); +// rs.execute(sql); +// while (rs.next()){ +// Map 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 getSqlToMap(String sql,Object...value){ + RecordSet rs = thread.get(); + Map 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); + } + } + +} diff --git a/src/com/attendance/engine/util/HttpRequestUtil.java b/src/com/attendance/engine/util/HttpRequestUtil.java new file mode 100644 index 0000000..c69d14f --- /dev/null +++ b/src/com/attendance/engine/util/HttpRequestUtil.java @@ -0,0 +1,147 @@ +package com.util; + +import com.sun.jersey.core.util.Base64; +import okhttp3.*; +import weaver.general.BaseBean; + +import java.io.IOException; +import java.util.Map; + +public class HttpRequestUtil extends BaseBean{ + private static int retryTimes = 2; + private static BaseBean bb = new BaseBean(); + + /*** + * + * @param dataJson + * @return + */ + public static String doPost(String url,String dataJson){ + + bb.writeLog("url:"+url); + bb.writeLog("dataJson:"+dataJson); + + String msgData = "" ; + + try { + OkHttpClient client = new OkHttpClient().newBuilder() + .addInterceptor(new OkhttpInterceptor(retryTimes)) + .retryOnConnectionFailure(false).build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, dataJson); + Request request = new Request.Builder() + .url(url) + .method("POST", body) + .addHeader("Content-Type", "application/json") + .build(); + + Response response = client.newCall(request).execute(); + + int code = response.code(); + String bodyMsg = response.body().string(); + bb.writeLog("response.code():"+code); + bb.writeLog("response.body():"+bodyMsg); + if(code == 200){ + msgData = bodyMsg; + return msgData; + } + } catch (IOException e) { + e.printStackTrace(); + bb.writeLog("HttpRequestUtil--e:"+e); + } + //bb.writeLog("msgData:"+msgData); + return msgData; + } + + /*** + * + * @param param + * @return + */ + public static String doGet(String url, Map param) { + + if (param.size() > 0) { + url = url + "?"; + for (Map.Entry e : param.entrySet()) { + url = url + e.getKey() + "=" + e.getValue() + "&"; + } + url = url.substring(0, url.length() - 1); + } + + return doGet(url); + } + + + public static String doGet(String url){ + + bb.writeLog("url:"+url); + + String msgData = "" ; + + try { + OkHttpClient client = new OkHttpClient().newBuilder() + .addInterceptor(new OkhttpInterceptor(retryTimes)) + .retryOnConnectionFailure(false).build(); + Request request = new Request.Builder() + .url(url) + .get() + .build(); + + Response response = client.newCall(request).execute(); + + int code = response.code(); + String bodyMsg = response.body().string(); + bb.writeLog("response.code():"+code); + bb.writeLog("response.body():"+bodyMsg); + if(code == 200){ + msgData = bodyMsg; + return msgData; + } + } catch (IOException e) { + e.printStackTrace(); + bb.writeLog("HttpRequestUtil--e:"+e); + } + bb.writeLog("msgData:"+msgData); + return msgData; + } + + + + public static class OkhttpInterceptor implements Interceptor { + // 最大重试次数 + private int maxRentry; + + + public OkhttpInterceptor(int maxRentry) { + this.maxRentry = maxRentry; + + } + + + @Override + public Response intercept(Chain chain) throws IOException { + /* 递归 2次下发请求,如果仍然失败 则返回 null ,但是 intercept must not return null. + * 返回 null 会报 IllegalStateException 异常 + * */ + return retry(chain, 0);//这个递归真的很舒服 + } + + Response retry(Chain chain, int retryCent) { + Request request = chain.request(); + Response response = null; + BaseBean bb = new BaseBean(); + try { +// System.out.println("第" + (retryCent + 1) + "次执行发http请求."); + response = chain.proceed(request); + } catch (Exception e) { + bb.writeLog("OkhttpInterceptor--e:"+e); + if (maxRentry > retryCent) { + return retry(chain, retryCent + 1); + } + } finally { + return response; + } + } + } + +} diff --git a/src/com/attendance/engine/util/PropBean.java b/src/com/attendance/engine/util/PropBean.java new file mode 100644 index 0000000..61e8638 --- /dev/null +++ b/src/com/attendance/engine/util/PropBean.java @@ -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; + } + +} diff --git a/src/com/attendance/engine/util/SendEmMessage.java b/src/com/attendance/engine/util/SendEmMessage.java new file mode 100644 index 0000000..d4d5c04 --- /dev/null +++ b/src/com/attendance/engine/util/SendEmMessage.java @@ -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 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 sendMessageParamMap = Maps.newHashMap(); + sendMessageParamMap.put("chatid",chatId); + sendMessageParamMap.put("msgtype","markdown"); + Map 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; + } +} diff --git a/src/com/attendance/engine/util/TokenUtil.java b/src/com/attendance/engine/util/TokenUtil.java new file mode 100644 index 0000000..3b56deb --- /dev/null +++ b/src/com/attendance/engine/util/TokenUtil.java @@ -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 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 ""; + } + } + +} diff --git a/src/com/attendance/engine/util/Utils.java b/src/com/attendance/engine/util/Utils.java new file mode 100644 index 0000000..9211c8a --- /dev/null +++ b/src/com/attendance/engine/util/Utils.java @@ -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>>(){}.getType(); + public static Type map_type = new TypeToken>(){}.getType(); + + /** + * 解析格式为[{a:b,c:d}]格式的json + * @param json + * @return + */ + public static List> resolveList_Map(String json){ + List> list= gson.fromJson(json,list_map_type); + return list; + } + /** + * 解析格式为{a:b,c:d}格式的json + * @param json + * @return + */ + public static Map resolveMap(String json){ + Map list= gson.fromJson(json,map_type); + return list; + } +}