diff --git a/fw-hestia-common/pom.xml b/fw-hestia-common/pom.xml index 4c82c02..af0c587 100644 --- a/fw-hestia-common/pom.xml +++ b/fw-hestia-common/pom.xml @@ -14,6 +14,13 @@ jar fw-hestia-common + + + com.alibaba + fastjson + + + diff --git a/fw-hestia-common/src/main/java/cn/fw/hestia/common/constant/MessageStr.java b/fw-hestia-common/src/main/java/cn/fw/hestia/common/constant/MessageStr.java new file mode 100644 index 0000000..3e43254 --- /dev/null +++ b/fw-hestia-common/src/main/java/cn/fw/hestia/common/constant/MessageStr.java @@ -0,0 +1,12 @@ +package cn.fw.hestia.common.constant; + +/** + * @author : kurisu + * @className : MessageStr + * @description : 返回的信息 + * @date: 2020-08-13 10:09 + */ +public interface MessageStr { + String QUERY_FAILURE = "查询失败"; + String SAVE_FAILURE = "操作失败"; +} diff --git a/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/CoordinateTransformUtil.java b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/CoordinateTransformUtil.java new file mode 100644 index 0000000..e8388b5 --- /dev/null +++ b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/CoordinateTransformUtil.java @@ -0,0 +1,175 @@ +package cn.fw.hestia.common.utils; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * 百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换的工具 + *

+ * create at 2018-10-09 + * + * @author kurisu + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class CoordinateTransformUtil { + + // π + private static double PI = Math.PI; + private static double X_PI = PI * 3000.0 / 180.0; + // 长半轴 + private static double A = 6378245.0; + // 扁率 + private static double EE = 0.00669342162296594323; + + /** + * 百度坐标系(BD-09)转WGS坐标 + * + * @param lng 百度坐标纬度 + * @param lat 百度坐标经度 + * @return WGS84坐标数组 + */ + public static double[] bd09toWgs84(double lng, double lat) { + double[] gcj = bd09toGcj02(lng, lat); + return gcj02toWgs84(gcj[0], gcj[1]); + } + + /** + * WGS坐标转百度坐标系(BD-09) + * + * @param lng WGS84坐标系的经度 + * @param lat WGS84坐标系的纬度 + * @return 百度坐标数组 + */ + public static double[] wgs84toBd09(double lng, double lat) { + double[] gcj = wgs84toGcj02(lng, lat); + return gcj02toBd09(gcj[0], gcj[1]); + } + + /** + * 火星坐标系(GCJ-02)转百度坐标系(BD-09) + * 谷歌、高德——>百度 + * + * @param lng 火星坐标经度 + * @param lat 火星坐标纬度 + * @return 百度坐标数组 + */ + public static double[] gcj02toBd09(double lng, double lat) { + double z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI); + double theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI); + double bd_lng = z * Math.cos(theta) + 0.0065; + double bd_lat = z * Math.sin(theta) + 0.006; + return new double[]{bd_lng, bd_lat}; + } + + /** + * 百度坐标系(BD-09)转火星坐标系(GCJ-02) + * 百度——>谷歌、高德 + * + * @param bd_lon 百度坐标纬度 + * @param bd_lat 百度坐标经度 + * @return 火星坐标数组 + */ + public static double[] bd09toGcj02(double bd_lon, double bd_lat) { + double x = bd_lon - 0.0065; + double y = bd_lat - 0.006; + double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI); + double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI); + double gg_lng = z * Math.cos(theta); + double gg_lat = z * Math.sin(theta); + return new double[]{gg_lng, gg_lat}; + } + + /** + * WGS84转GCJ02(火星坐标系) + * + * @param lng WGS84坐标系的经度 + * @param lat WGS84坐标系的纬度 + * @return 火星坐标数组 + */ + public static double[] wgs84toGcj02(double lng, double lat) { + if (out_of_china(lng, lat)) { + return new double[]{lng, lat}; + } + double dlat = transformLat(lng - 105.0, lat - 35.0); + double dlng = transformLng(lng - 105.0, lat - 35.0); + double radlat = lat / 180.0 * PI; + double magic = Math.sin(radlat); + magic = 1 - EE * magic * magic; + double sqrtmagic = Math.sqrt(magic); + dlat = (dlat * 180.0) / ((A * (1 - EE)) / (magic * sqrtmagic) * PI); + dlng = (dlng * 180.0) / (A / sqrtmagic * Math.cos(radlat) * PI); + double mglat = lat + dlat; + double mglng = lng + dlng; + return new double[]{mglng, mglat}; + } + + /** + * GCJ02(火星坐标系)转GPS84 + * + * @param lng 火星坐标系的经度 + * @param lat 火星坐标系纬度 + * @return WGS84坐标数组 + */ + public static double[] gcj02toWgs84(double lng, double lat) { + if (out_of_china(lng, lat)) { + return new double[]{lng, lat}; + } + double dlat = transformLat(lng - 105.0, lat - 35.0); + double dlng = transformLng(lng - 105.0, lat - 35.0); + double radlat = lat / 180.0 * PI; + double magic = Math.sin(radlat); + magic = 1 - EE * magic * magic; + double sqrtmagic = Math.sqrt(magic); + dlat = (dlat * 180.0) / ((A * (1 - EE)) / (magic * sqrtmagic) * PI); + dlng = (dlng * 180.0) / (A / sqrtmagic * Math.cos(radlat) * PI); + double mglat = lat + dlat; + double mglng = lng + dlng; + return new double[]{lng * 2 - mglng, lat * 2 - mglat}; + } + + /** + * 纬度转换 + * + * @param lng 经度 + * @param lat 纬度 + * @return 纬度 + */ + public static double transformLat(double lng, double lat) { + double ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng)); + ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0; + ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0; + ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0; + return ret; + } + + /** + * 经度转换 + * + * @param lng 经度 + * @param lat 纬度 + * @return 经度 + */ + public static double transformLng(double lng, double lat) { + double ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng)); + ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0; + ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0; + ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0; + return ret; + } + + /** + * 判断是否在国内,不在国内不做偏移 + * + * @param lng 经度 + * @param lat 纬度 + * @return 是否 + */ + public static boolean out_of_china(double lng, double lat) { + if (lng < 72.004 || lng > 137.8347) { + return true; + } else { + return lat < 0.8293 || lat > 55.8271; + } + } + +} diff --git a/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/DateUtil.java b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/DateUtil.java new file mode 100644 index 0000000..42fb875 --- /dev/null +++ b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/DateUtil.java @@ -0,0 +1,578 @@ +package cn.fw.hestia.common.utils; + +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.*; +import java.time.temporal.TemporalAdjusters; +import java.util.Calendar; +import java.util.Date; +import java.util.Objects; + +/** + * 日期处理工具 + *

+ * + * @author kurisu + */ +public final class DateUtil { + + static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + + /** + * 默认构造器 + */ + private DateUtil() { + throw new UnsupportedOperationException(); + } + + + public static Date parse(String date) { + try { + return sdf.parse(date); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 开始日期处理 + */ + public static Date startDate(final Date date) { + if (date == null) { + return null; + } + return localDateTime2Date(date2LocalDate(date).atStartOfDay()); + } + + /** + * 最结束日期处理 + */ + public static Date endDate(final Date date) { + if (date == null) { + return null; + } + return localDateTime2Date(date2LocalDate(date).plusDays(1).atStartOfDay()); + } + + /** + * 处理日期,保留年月日 + */ + public static LocalDate date2LocalDate(final Date date) { + if (date == null) { + return null; + } + return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate(); + } + + public static LocalTime date2LocalTime(final Date date) { + if (date == null) { + return null; + } + return LocalTime.of(date.getHours(), date.getMinutes(), date.getSeconds()); + } + + public static LocalDateTime date2LocalDateTime(final Date date) { + Instant instant = date.toInstant(); + ZoneId zoneId = ZoneId.systemDefault(); + return instant.atZone(zoneId).toLocalDateTime(); + } + + /** + * convert LocalDateTime to Date + */ + public static Date localDateTime2Date(final LocalDateTime dateTime) { + return Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant()); + } + + /** + * convert LocalDate to Date + */ + public static Date localDate2Date(final LocalDate localDate) { + if (localDate == null) { + return null; + } + return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); + } + + public static Date getBeginInTime(Date inTime) { + if (inTime == null) { + return null; + } + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + c.set(Calendar.HOUR_OF_DAY, 0); + c.set(Calendar.MINUTE, 0); + c.set(Calendar.SECOND, 0); + return c.getTime(); + } + + public static Date getEndInTime(Date inTime) { + if (inTime == null) { + return null; + } + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + c.set(Calendar.HOUR_OF_DAY, 23); + c.set(Calendar.MINUTE, 59); + c.set(Calendar.SECOND, 59); + return c.getTime(); + } + + /** + * 得到本周周一 + * + * @return + */ + public static Date getMondayOfThisWeek(Date inTime) { + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1; + if (day_of_week == 0) { + day_of_week = 7; + } + c.add(Calendar.DATE, -day_of_week + 1); + return c.getTime(); + } + + /** + * 得到本周周日 + * + * @return + */ + public static Date getSundayOfThisWeek(Date inTime) { + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1; + if (day_of_week == 0) { + day_of_week = 7; + } + c.add(Calendar.DATE, -day_of_week + 7); + return c.getTime(); + } + + /** + * 得到月初 + * + * @return + */ + public static Date getMonthFirstDay(Date inTime) { + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + c.add(Calendar.MONTH, 0); + c.set(Calendar.DAY_OF_MONTH, 1); + return c.getTime(); + } + + /** + * 获取当月月初时间 + */ + public static Date getMonthFirstDay() { + return DateUtil.localDate2Date(LocalDateTime.now() + .with(TemporalAdjusters.firstDayOfMonth()).toLocalDate()); + } + + /** + * 得到月末 + * + * @return + */ + public static Date getMonthEndDay(Date inTime) { + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + c.add(Calendar.MONTH, 1); + c.set(Calendar.DAY_OF_MONTH, 0); + return c.getTime(); + } + + /** + * 得到月末 + * + * @return + */ + public static Date getMonthEndDay() { + return DateUtil.localDate2Date(LocalDateTime.now() + .with(TemporalAdjusters.lastDayOfMonth()).toLocalDate()); + } + + /** + * 得到年初 + * + * @return + */ + public static Date getYearFirstDay(Date inTime) { + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + int x = c.get(Calendar.YEAR); + try { + return sdf.parse(x + "-01" + "-01"); + } catch (Exception e) { + + } + return null; + } + + /** + * 得到年末 + * + * @return + */ + public static Date getYearEndDay(Date inTime) { + Calendar c = Calendar.getInstance(); + c.setTime(inTime); + int x = c.get(Calendar.YEAR); + try { + return sdf.parse(x + "-12" + "-31"); + } catch (Exception e) { + + } + return null; + } + + public static Date getThisHourStartTime(Date time) { + final LocalDateTime thisHourStart = LocalDateTime.of(DateUtil.date2LocalDateTime(time).getYear(), + DateUtil.date2LocalDateTime(time).getMonth(), + DateUtil.date2LocalDateTime(time).getDayOfMonth(), + DateUtil.date2LocalDateTime(time).getHour(), + 0, 0); + return DateUtil.localDateTime2Date(thisHourStart); + } + + public static Date getThisHourEndTime(Date time) { + final LocalDateTime thisHourEnd = LocalDateTime.of(DateUtil.date2LocalDateTime(time).getYear(), + DateUtil.date2LocalDateTime(time).getMonth(), + DateUtil.date2LocalDateTime(time).getDayOfMonth(), + DateUtil.date2LocalDateTime(time).getHour(), + 59, 59); + return DateUtil.localDateTime2Date(thisHourEnd); + } + + /** + * 获取当天起始时间 + * + * @return 当天起始时间 + */ + public static Date getCurrentDayStartTime() { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime start = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 0, 0, 0); + ZoneId zone = ZoneId.systemDefault(); + Instant instant = start.atZone(zone).toInstant(); + return Date.from(instant); + } + + /** + * 获取当天结束时间 + * + * @return 当天结束时间 + */ + public static Date getCurrentDayEndTime() { + LocalDateTime now = LocalDateTime.now(); + LocalDateTime end = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), 23, 59, 59); + ZoneId zone = ZoneId.systemDefault(); + Instant instant = end.atZone(zone).toInstant(); + return Date.from(instant); + } + + + /** + * 获取短时间字符串 + * + * @param date 需要转换格式的日期 + * @return 返回短时间字符串格式 yyyy-MM-dd + */ + public static String getStringDateShort(Date date) { + if (date == null) { + return null; + } + String dateString = sdf.format(date); + return dateString; + } + + /** + * 获取时间字符串 + * + * @param date 需要转换格式的日期 + * @return 返回短时间字符串格式 yyyy-MM-dd HH:mm:ss + */ + public static String getFullDateString(Date date) { + if (date == null) { + return ""; + } + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + return format.format(date); + } + + /** + * 获取时间字符串 + * + * @param date 需要转换格式的日期 + * @return 返回短时间字符串格式 yyyy-MM + */ + public static String getMonthString(Date date) { + if (date == null) { + return ""; + } + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM"); + return format.format(date); + } + + /** + * 获取时间字符串 + * + * @param date 需要转换格式的日期 + * @param pattern 时间字符串格式 + * @return 返回时间字符串 + */ + public static String getFormatString(Date date, String pattern) { + if (date == null) { + return ""; + } + SimpleDateFormat format = new SimpleDateFormat(pattern); + return format.format(date); + } + + /** + * 获取下月月初时间 + */ + public static Date getNextMonthStartTime() { + return DateUtil.localDate2Date(LocalDateTime.now() + .with(TemporalAdjusters.lastDayOfMonth()).plusDays(1L).toLocalDate()); + } + + /** + * @param offsetYear + * @return 当前时间 + offsetYear + */ + public static Timestamp getNowExpiredYear(int offsetYear) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.YEAR, offsetYear); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offsetMonth + */ + public static Timestamp getNowExpiredMonth(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.MONTH, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offsetDay + */ + public static Timestamp getNowExpiredDay(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.DATE, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offsetDay + */ + public static Timestamp getNowExpiredHour(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.HOUR, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offsetSecond + * @return 当前时间 + offsetSecond + */ + public static Timestamp getNowExpiredSecond(int offsetSecond) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.SECOND, offsetSecond); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 当前时间 + offset + */ + public static Timestamp getNowExpiredMinute(int offset) { + Calendar now = Calendar.getInstance(); + now.add(Calendar.MINUTE, offset); + return new Timestamp(now.getTime().getTime()); + } + + /** + * @param offset + * @return 指定时间 + offsetDay + */ + public static Timestamp getExpiredDay(Date givenDate, int offset) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.DATE, offset); + return new Timestamp(date.getTime().getTime()); + } + + /** + * 实现ORACLE中ADD_MONTHS函数功能 + * + * @param offset + * @return 指定时间 + offsetMonth + */ + public static Timestamp getExpiredMonth(Date givenDate, int offset) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.MONTH, offset); + return new Timestamp(date.getTime().getTime()); + } + + /** + * @param + * @return 指定时间 + offsetYear + */ + public static Timestamp getExpiredYear(Date givenDate, int offsetYear) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.YEAR, offsetYear); + return new Timestamp(date.getTime().getTime()); + } + + /** + * @param second + * @return 指定时间 + offsetSecond + */ + public static Timestamp getExpiredSecond(Date givenDate, int second) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(Calendar.SECOND, second); + return new Timestamp(date.getTime().getTime()); + } + + /** + * 计算时间差 + * + * @param givenDate 日期 + * @param offset 2 + * @param type 日期字段类型 + * @return + */ + public static Timestamp getExpired(Date givenDate, int offset, Integer type) { + Calendar date = Calendar.getInstance(); + date.setTime(givenDate); + date.add(type, offset); + return new Timestamp(date.getTime().getTime()); + } + + /** + * 计算日期差 + * + * @param d1 开始日期 + * @param d2 截止日期 + * @param type 类型 + * @return 计算结果 + */ + public static Integer sub(Date d1, Date d2, String type) { + if (d1 == null || d2 == null) { + return null; + } + long result = 0L; + long DAY = 24 * 60 * 60 * 1000; + long sub = d1.getTime() - d2.getTime(); + long daysub = (sub / DAY); + long y1 = d1.getYear(), y2 = d2.getYear(); + long m1 = d1.getMonth(), m2 = d2.getMonth(); + long monthsub = (y1 - y2) * 12 + (m1 - m2); + //月 + if ("m".equals(type)) { + result = monthsub; + //年 + } else if ("y".equals(type)) { + result = monthsub / 12; + //天 + } else if ("d".equals(type)) { + result = daysub; + } + return (int) result; + } + + /** + * 判断是否是凌晨 + * + * @param date + * @return + */ + public static boolean isBeforeDawn(Date date) { + if (Objects.isNull(date)) { + return false; + } + LocalDateTime dateTime = date2LocalDateTime(date); + SimpleDateFormat df = new SimpleDateFormat("HH"); + String str = df.format(localDateTime2Date(dateTime)); + int a = Integer.parseInt(str); + return a >= 0 && a <= 6; + } + + /** + * 判断是否是上午 + * + * @param date + * @return + */ + public static boolean isMorning(Date date) { + if (Objects.isNull(date)) { + return false; + } + LocalDateTime dateTime = date2LocalDateTime(date); + SimpleDateFormat df = new SimpleDateFormat("HH"); + String str = df.format(localDateTime2Date(dateTime)); + int a = Integer.parseInt(str); + return a > 6 && a <= 12; + } + + /** + * 判断是否是下午 + * + * @param date + * @return + */ + public static boolean isAfternoon(Date date) { + if (Objects.isNull(date)) { + return false; + } + LocalDateTime dateTime = date2LocalDateTime(date); + SimpleDateFormat df = new SimpleDateFormat("HH"); + String str = df.format(localDateTime2Date(dateTime)); + int a = Integer.parseInt(str); + return a > 12 && a <= 18; + } + + /** + * 判断是否是晚上 + * + * @param date + * @return + */ + public static boolean isEvening(Date date) { + if (Objects.isNull(date)) { + return false; + } + LocalDateTime dateTime = date2LocalDateTime(date); + SimpleDateFormat df = new SimpleDateFormat("HH"); + String str = df.format(localDateTime2Date(dateTime)); + int a = Integer.parseInt(str); + return a > 18 && a <= 23; + } + + public static void main(String[] args) throws ParseException { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = format.parse("2020-09-19 15:45:30"); + + Timestamp expired = DateUtil.getExpired(date, 3, Calendar.DATE); + if (!isBeforeDawn(expired)) { + expired = getExpired(startDate(expired), 1, Calendar.DATE); + System.out.println(expired); + } + System.out.println(getEndInTime(getMonthEndDay())); + System.out.println(expired); + System.out.println(startDate(DateUtil.getMonthFirstDay(date))); + ; + } +} diff --git a/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/MessageFormatUtil.java b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/MessageFormatUtil.java new file mode 100644 index 0000000..f99c0d0 --- /dev/null +++ b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/MessageFormatUtil.java @@ -0,0 +1,31 @@ +package cn.fw.hestia.common.utils; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +/** + * @author kurisu + * @description 占位符转换工具 + * @date 2019/5/30. + */ +public class MessageFormatUtil { + + public static String MessageFormatTransfer(String pattern, Object... arguments) { + List param = new ArrayList<>(); + for (int i = 0; i < arguments.length; i++) { + if (arguments[i] instanceof Number) { + param.add(String.valueOf(arguments[i])); + }else { + param.add(arguments[i]); + } + } + return MessageFormat.format(pattern, param.toArray()); + } + + public static void main(String[] args) { + System.out.println("示例:" + MessageFormatTransfer("crm:valhalla:save:lock:key:{0}:{1}", 11111L, new Date())); + } + +} diff --git a/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/StringUtils.java b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/StringUtils.java new file mode 100644 index 0000000..1b2e755 --- /dev/null +++ b/fw-hestia-common/src/main/java/cn/fw/hestia/common/utils/StringUtils.java @@ -0,0 +1,545 @@ +package cn.fw.hestia.common.utils; + + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * 字符串工具 + * + * @author: kurisu + * @version: 1.0 + */ +public final class StringUtils { + + /** + * 空字符串 + */ + public static final String EMPTY = ""; + /** + * 特殊字符正则表达式 + */ + public static final String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; + + public static final String regNo = "^[-\\+]?[\\d]*$"; + + private StringUtils() { + } + + /** + * 首字母小写 + * + * @param s String + * @return String + */ + public static String firstCharLowerCase(String s) { + if (isValid(s)) { + return s.substring(0, 1).toLowerCase() + s.substring(1); + } + return s; + } + + /** + * 删除前缀 + * + * @param s + * @param prefix + * @return + */ + public static String removePrefix(String s, String prefix) { + int index = s.indexOf(prefix); + return index == 0 ? s.substring(prefix.length()) : s; + } + + /** + * 删除后缀 + * + * @param s + * @param suffix + * @return + */ + public static String removeSuffix(String s, String suffix) { + return s.endsWith(suffix) ? s.substring(0, s.length() - suffix.length()) : s; + } + + /** + * 首字母大写 + * + * @param s String + * @return String + */ + public static String firstCharUpperCase(String s) { + if (isValid(s)) { + return s.substring(0, 1).toUpperCase() + s.substring(1); + } + return s; + } + + /** + * 检查对象是否有效 obj != null && obj.toString().length() > 0 + * + * @param obj + * @return boolean + */ + public static boolean isValid(Object obj) { + return obj != null && obj.toString().length() > 0; + } + + /** + * 是否是空的 + * + * @param obj + * @return + */ + public static boolean isEmpty(Object obj) { + return obj == null || obj.toString().length() == 0; + } + + /** + * 是否是数字 + * + * @param obj + * @return + */ + public static boolean isNumber(Object obj) { + if (isEmpty(obj)) { + return false; + } + Pattern compile = Pattern.compile(regNo); + return compile.matcher(obj.toString()).matches(); + } + + /** + * 转化为String对象 + * + * @param obj + * @return boolean + */ + public static String asString(Object obj) { + return obj != null ? obj.toString() : ""; + } + + /** + * 返回其中一个有效的对象 value != null && value.toString().length() > 0 + * + * @param values + */ + public static String tryThese(Object... values) { + for (int i = 0; i < values.length; i++) { + String value = StringUtils.asString(values[i]); + if (!value.isEmpty()) { + return value; + } + } + return ""; + } + + /** + * EL表达式提供的定义方法 + * + * @param v1 + * @param v2 + * @return + */ + public static String tryThese(String v1, String v2) { + return tryThese(new Object[]{v1, v2}); + } + + /** + * 连接字符串 + * + * @param list + * @param split + * @return 字符串 + */ + public static String join(T[] list, String split) { + return join(list, split, ""); + } + + /** + * 连接字符串 + * + * @param list + * @param split + * @return 字符串 + */ + public static String join(T[] list, String split, String wrap) { + if (list == null) { + return null; + } + StringBuilder s = new StringBuilder(128); + for (int i = 0; i < list.length; i++) { + if (i > 0) { + s.append(split); + } + s.append(wrap + list[i] + wrap); + } + return s.toString(); + } + + /** + * 连接 + * + * @param list + * @param split + * @param wrap + * @return + */ + public static String join(List list, String split, String wrap) { + return join(list.toArray(), split, wrap); + } + + /** + * 连接字符串 + * + * @param list + * @param split + * @return 字符串 + */ + public static String join(List list, String split) { + return join(list.toArray(), split); + } + + /** + * 包裹字符串 id:12, {, } 输出 {id:12} + * + * @param input 输入串 + * @param begin { + * @param end } + * @return String + */ + public static String wrap(String begin, String input, String end) { + if (!input.startsWith(begin)) { + input = begin + input; + } + if (!input.endsWith(end)) { + input = input + end; + } + return input; + } + + /** + * 取得匹配的字符串 + * + * @param input + * @param regex + * @return + */ + public static List matchs(String input, String regex) { + return matchs(input, regex, 0); + } + + /** + * 取得匹配的字符串 + * + * @param input + * @param regex + * @return + */ + public static List matchs(String input, String regex, int group) { + Pattern pattern = Pattern.compile(regex); + Matcher match = pattern.matcher(input); + List matches = new ArrayList(); + while (match.find()) { + matches.add(match.group(group)); + } + return matches; + } + + /** + * 找到匹配的第一个字符串 + * + * @param input + * @param regex + * @param group + * @return + */ + public static String matchFirst(String input, String regex, int group) { + List matches = matchs(input, regex, group); + return matches.isEmpty() ? null : matches.get(0); + } + + /** + * 截取指定长度字符串 + * + * @return + */ + public static String getShorterString(String str, int maxLength) { + return getShorterString(str, "...", maxLength); + } + + /** + * 截取指定长度字符串 + * + * @param input + * @param tail + * @param length + * @return + */ + public static String getShorterString(String input, String tail, int length) { + tail = isValid(tail) ? tail : ""; + StringBuffer buffer = new StringBuffer(512); + try { + int len = input.getBytes("GBK").length; + if (len > length) { + int ln = 0; + for (int i = 0; ln < length; i++) { + String temp = input.substring(i, i + 1); + if (temp.getBytes("GBK").length == 2) { + ln += 2; + } else { + ln++; + } + + if (ln <= length) { + buffer.append(temp); + } + } + } else { + return input; + } + buffer.append(tail); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return buffer.toString(); + } + + /** + * 取得GBK编码 + * + * @return + */ + public static String getBytesString(String input, String code) { + try { + byte[] b = input.getBytes(code); + return Arrays.toString(b); + } catch (UnsupportedEncodingException e) { + return String.valueOf(code.hashCode()); + } + } + + /** + * 转换格式 CUST_INFO_ID - > custInfoId + * + * @param input + * @return + */ + public static String getFieldString(String input) { + if (input == null) { + return null; + } + String field = input.toLowerCase(); + String[] values = field.split("\\_"); + StringBuffer b = new StringBuffer(input.length()); + for (int i = 0; i < values.length; i++) { + if (i == 0) { + b.append(values[i]); + } else { + b.append(firstCharUpperCase(values[i])); + } + } + return b.toString(); + } + + /** + * 转换格式 CUST_INFO_ID - > custInfoId + * + * @param columnName + * @return + */ + public static String toFieldName(String columnName) { + return getFieldString(columnName); + } + + /** + * 转换格式 custInfoId - > CUST_INFO_ID + * + * @param field + * @return + */ + public static String toColumnName(String field) { + if (field == null) { + return null; + } + StringBuffer b = new StringBuffer(field.length() + 3); + for (int i = 0; i < field.length(); i++) { + Character char1 = field.charAt(i); + if (Character.isUpperCase(char1) && i != 0) { + b.append("_"); + } + b.append(char1); + } + return b.toString(); + } + + /** + * 转化为JSON值 + * + * @param value + * @return + * @throws IOException + */ + public static String toJsonValue(Object value) throws IOException { + if (value instanceof Number) { + return value.toString(); + } else { + return "'" + value.toString() + "'"; + } + } + + /** + * 字符串转化为UUID + * + * @param value + * @return + */ + public static String toUUID(String value) { + if (value == null) { + throw new RuntimeException("value is null!"); + } + return UUID.nameUUIDFromBytes(value.getBytes()).toString(); + } + + /** + * 获取Style样式中样式的值 + * + * @param styleString + * @param styleName + * @return 相应的值 + */ + public static String getStyleValue(String styleString, String styleName) { + String[] styles = styleString.split(";"); + for (int i = 0; i < styles.length; i++) { + String tempValue = styles[i].trim(); + if (tempValue.startsWith(styleName)) { + String[] style = tempValue.split(":"); + return style[1]; + } + } + return ""; + } + + /** + * 生成重复次字符 + * + * @param charactor + * @param repeat + * @return + */ + public static String getRepeat(String charactor, int repeat) { + return repeat(charactor, repeat, ""); + } + + /** + * 生成重复次字符 + * + * @param charactor + * @param repeat + * @return + */ + public static String repeat(String charactor, int repeat, String split) { + StringBuilder s = new StringBuilder(charactor.length() * repeat); + for (int i = 0; i < repeat; i++) { + if (i != 0) { + s.append(split != null ? split : ""); + } + s.append(charactor); + } + return s.toString(); + } + + /** + * 取得长度 + * + * @param text + * @return + */ + public static int length(String text) { + int len = text.length(); + try { + len = text.getBytes("GBK").length;//SQLServer数据库用的GBK编码 + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + return len; + } + + /** + * 字符串替换函数 + * + * @param data 字符串 + * @param data from 旧值 + * @param to from 新值 + */ + public static String replaceString(String data, String from, String to) { + StringBuffer buf = new StringBuffer(data.length()); + int pos = -1; + int i = 0; + while ((pos = data.indexOf(from, i)) != -1) { + buf.append(data.substring(i, pos)).append(to); + i = pos + from.length(); + } + buf.append(data.substring(i)); + return buf.toString(); + } + + /** + * 转义特殊字符 + * + * @param s + * @return + */ + public static String escapeQueryChars(String s) { + if (StringUtils.isEmpty(s)) { + return s; + } + StringBuilder sb = new StringBuilder(); + //查询字符串一般不会太长,挨个遍历也花费不了多少时间 + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' + || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"' + || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' + || c == '|' || c == '&' || c == ';' || c == '/' || c == '.' + || c == '$' || c == '%' || c == '<' || c == '>' || Character.isWhitespace(c)) { + sb.append('\\'); + } + sb.append(c); + } + return sb.toString(); + } + + + /** + * 获取默认值 + * @param str + * @param defaultStr + * @return + */ + public static String getStrWithDefault(CharSequence str, String defaultStr) { + if (isEmpty(defaultStr)) { + defaultStr = ""; + } + if (isEmpty(str)) { + return defaultStr; + } + return str.toString(); + } + + public static void main(String[] args) { + System.out.println(toUUID("1")); + System.out.println(removePrefix("abcd123", "ab")); + System.out.println(removeSuffix("abcd123", "123")); + System.out.println(toColumnName("usernameId")); + System.out.println(getFieldString("user_name_id")); + System.out.println(repeat("?", 10, ",")); + length("AAA中国()111222bb"); + } +} \ No newline at end of file diff --git a/fw-hestia-dao/src/main/java/cn/fw/hestia/dao/DemoDao.java b/fw-hestia-dao/src/main/java/cn/fw/hestia/dao/DemoDao.java new file mode 100644 index 0000000..708a299 --- /dev/null +++ b/fw-hestia-dao/src/main/java/cn/fw/hestia/dao/DemoDao.java @@ -0,0 +1,10 @@ +package cn.fw.hestia.dao; + +/** + * @author : kurisu + * @className : DemoDao + * @description : + * @date: 2021-09-23 15:23 + */ +public interface DemoDao { +} diff --git a/fw-hestia-dao/src/main/resources/mapper/DemoMapper.xml b/fw-hestia-dao/src/main/resources/mapper/DemoMapper.xml new file mode 100644 index 0000000..9ee9077 --- /dev/null +++ b/fw-hestia-dao/src/main/resources/mapper/DemoMapper.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/fw-hestia-domain/src/main/java/Demo.java b/fw-hestia-domain/src/main/java/cn/fw/hestia/domain/db/Demo.java index c51dc5f..1409c05 100644 --- a/fw-hestia-domain/src/main/java/Demo.java +++ b/fw-hestia-domain/src/main/java/cn/fw/hestia/domain/db/Demo.java @@ -1,8 +1,10 @@ +package cn.fw.hestia.domain.db; + /** * @author : kurisu * @className : Demo * @description : - * @date: 2021-09-23 14:45 + * @date: 2021-09-23 15:26 */ public class Demo { } diff --git a/fw-hestia-sdk/src/main/java/Demo.java b/fw-hestia-domain/src/main/java/cn/fw/hestia/domain/vo/Demo.java index 2322119..e3b8f1b 100644 --- a/fw-hestia-sdk/src/main/java/Demo.java +++ b/fw-hestia-domain/src/main/java/cn/fw/hestia/domain/vo/Demo.java @@ -1,8 +1,10 @@ +package cn.fw.hestia.domain.vo; + /** * @author : kurisu * @className : Demo * @description : - * @date: 2021-09-23 14:59 + * @date: 2021-09-23 15:26 */ public class Demo { } diff --git a/fw-hestia-rpc/pom.xml b/fw-hestia-rpc/pom.xml new file mode 100644 index 0000000..7c871d4 --- /dev/null +++ b/fw-hestia-rpc/pom.xml @@ -0,0 +1,39 @@ + + + + fw-hestia + cn.fw + 1.0.0 + ../pom.xml + + 4.0.0 + + fw-hestia-rpc + jar + fw-hestia-rpc + + + + org.springframework + spring-context + + + cn.fw + fw-hestia-common + + + + cn.fw + fw-common-core + true + + + org.springframework.data + spring-data-redis + true + + + + \ No newline at end of file diff --git a/fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/AbsBaseRpcService.java b/fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/AbsBaseRpcService.java new file mode 100644 index 0000000..2ea8972 --- /dev/null +++ b/fw-hestia-rpc/src/main/java/cn/fw/hestia/rpc/AbsBaseRpcService.java @@ -0,0 +1,116 @@ +package cn.fw.hestia.rpc; + +import cn.fw.hestia.common.utils.StringUtils; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.BoundListOperations; +import org.springframework.data.redis.core.BoundValueOperations; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.lang.NonNull; +import org.springframework.lang.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +/** + * @author : kurisu + * @className : AbsBaseRpcService + * @description : 公共方法 + * @date: 2020-12-17 14:13 + */ +@Slf4j +public abstract class AbsBaseRpcService { + /** + * Redis工具 + */ + @Autowired + protected StringRedisTemplate redisTemplate; + + /** + * 缓存KEY前缀 + * + * @return + */ + protected abstract String getKeyPrefix(); + + /** + * 从缓存获取对象 + * + * @param key + * @param clazz + * @param 获取的对象 + * @return + */ + @Nullable + protected E getFromCache(@NonNull final String key, Class clazz) { + String cache = getFromCache(key); + if (StringUtils.isEmpty(cache)) { + return null; + } + return JSON.parseObject(cache, clazz); + } + + protected String getFromCache(@NonNull final String key) { + String json = null; + try { + BoundValueOperations ops = redisTemplate.boundValueOps(key); + json = ops.get(); + } catch (Exception e) { + log.error("从缓存获信息失败[{}]", key, e); + } + return json; + } + + protected void setToCache(@NonNull final String key, @NonNull final String value) { + setToCache(key, value, 30); + } + + /** + * 缓存信息 + * @param key + * @param value + * @param timeout 缓存时间(秒) + */ + protected void setToCache(@NonNull final String key, @NonNull final String value, long timeout) { + try { + redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); + } catch (Exception e) { + log.error("缓存信息失败[{}][{}]", key, value, e); + } + } + + protected List getListFromCache(final String key, Class clazz) { + try { + BoundListOperations queue = getQueue(key); + final long size = Optional.ofNullable(queue.size()).orElse(0L); + if (size > 0) { + final List dtos = new ArrayList<>(); + for (long i = 0; i < size; i++) { + final String json = Objects.requireNonNull(queue.index(i)); + dtos.add(JSONObject.parseObject(json, clazz)); + } + return dtos; + } + } catch (Exception e) { + log.error("从缓存获取信息失败[{}]", key, e); + } + return null; + } + + protected void setListToCache(@NonNull final String key, @NonNull final String value) { + try { + getQueue(key).rightPush(value); + } catch (Exception e) { + log.error("缓存息失败[{}][{}]", key, value, e); + } + } + + protected BoundListOperations getQueue(@NonNull final String key) { + return redisTemplate.boundListOps(key); + } +} diff --git a/fw-hestia-server/src/main/java/Demo.java b/fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/api/Demo.java index 18eeb05..c3f83a2 100644 --- a/fw-hestia-server/src/main/java/Demo.java +++ b/fw-hestia-sdk/src/main/java/cn/fw/hestia/sdk/api/Demo.java @@ -1,8 +1,10 @@ +package cn.fw.hestia.sdk.api; + /** * @author : kurisu * @className : Demo * @description : - * @date: 2021-09-23 14:47 + * @date: 2021-09-23 15:27 */ public class Demo { } diff --git a/fw-hestia-server/pom.xml b/fw-hestia-server/pom.xml index 3acddc8..985843f 100644 --- a/fw-hestia-server/pom.xml +++ b/fw-hestia-server/pom.xml @@ -67,7 +67,7 @@ cn.fw - fw-pstn-service + fw-hestia-service @@ -108,15 +108,11 @@ io.micrometer micrometer-registry-prometheus - - cn.hutool - hutool-all - - fw-pstn-server + fw-hestia-server src/main/resources diff --git a/fw-hestia-server/src/main/java/cn/fw/hestia/server/Application.java b/fw-hestia-server/src/main/java/cn/fw/hestia/server/Application.java new file mode 100644 index 0000000..375315d --- /dev/null +++ b/fw-hestia-server/src/main/java/cn/fw/hestia/server/Application.java @@ -0,0 +1,34 @@ +package cn.fw.hestia.server; + +import cn.fw.security.auth.client.EnableAuthClient; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.scheduling.annotation.EnableScheduling; + +/** + * 启动类 + * + * @author kurisu + */ +@EnableCaching +@EnableAuthClient +@EnableScheduling +@EnableDiscoveryClient +@EnableAutoConfiguration +@Configuration +@EnableRedisRepositories +@MapperScan("cn.fw.**.mapper") +@ComponentScan({"cn.fw.hestia.*"}) +@EnableFeignClients({"cn.fw.**.sdk"}) +public class Application { + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/fw-hestia-common/src/main/java/Demo.java b/fw-hestia-server/src/main/java/cn/fw/hestia/server/controller/api/Demo.java index a3ac47f..0de82a7 100644 --- a/fw-hestia-common/src/main/java/Demo.java +++ b/fw-hestia-server/src/main/java/cn/fw/hestia/server/controller/api/Demo.java @@ -1,8 +1,10 @@ +package cn.fw.hestia.server.controller.api; + /** * @author : kurisu * @className : Demo - * @description : demo - * @date: 2021-09-23 14:29 + * @description : + * @date: 2021-09-23 15:39 */ public class Demo { } diff --git a/fw-hestia-server/src/main/resources/application.yml b/fw-hestia-server/src/main/resources/application.yml index 0cb2a7e..7b2426f 100644 --- a/fw-hestia-server/src/main/resources/application.yml +++ b/fw-hestia-server/src/main/resources/application.yml @@ -54,7 +54,7 @@ auth: fastdfs: charset: UTF-8 connectTimeout: 2 - group: pstndfs + group: hestiadfs networkTimeout: 30 port: 8080 secretKey: FastDFS1234567890 diff --git a/fw-hestia-server/src/main/resources/logfile.xml b/fw-hestia-server/src/main/resources/logfile.xml index e0b534c..ce176d7 100644 --- a/fw-hestia-server/src/main/resources/logfile.xml +++ b/fw-hestia-server/src/main/resources/logfile.xml @@ -1,6 +1,6 @@ - + diff --git a/fw-hestia-service/pom.xml b/fw-hestia-service/pom.xml index 29c845b..6e7f650 100644 --- a/fw-hestia-service/pom.xml +++ b/fw-hestia-service/pom.xml @@ -10,6 +10,8 @@ 4.0.0 fw-hestia-service + jar + fw-hestia-service @@ -48,7 +50,7 @@ cn.fw - fw-pstn-dao + fw-hestia-dao org.slf4j @@ -68,7 +70,7 @@ cn.fw - fw-pstn-sdk + fw-hestia-sdk org.apache.rocketmq @@ -88,10 +90,6 @@ org.redisson redisson - - com.google.zxing - javase - diff --git a/fw-hestia-service/src/main/java/cn/fw/hestia/service/Demo.java b/fw-hestia-service/src/main/java/cn/fw/hestia/service/Demo.java new file mode 100644 index 0000000..798b9cc --- /dev/null +++ b/fw-hestia-service/src/main/java/cn/fw/hestia/service/Demo.java @@ -0,0 +1,10 @@ +package cn.fw.hestia.service; + +/** + * @author : kurisu + * @className : Demo + * @description : + * @date: 2021-09-23 15:40 + */ +public interface Demo { +} diff --git a/fw-hestia-dao/src/main/java/Demo.java b/fw-hestia-service/src/main/java/cn/fw/hestia/service/impl/Demo.java index d4520ea..3380611 100644 --- a/fw-hestia-dao/src/main/java/Demo.java +++ b/fw-hestia-service/src/main/java/cn/fw/hestia/service/impl/Demo.java @@ -1,8 +1,10 @@ +package cn.fw.hestia.service.impl; + /** * @author : kurisu * @className : Demo - * @description : demo - * @date: 2021-09-23 14:34 + * @description : + * @date: 2021-09-23 15:41 */ public class Demo { } diff --git a/pom.xml b/pom.xml index 897d7f1..2f6663f 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,8 @@ fw-hestia-dao fw-hestia-domain fw-hestia-sdk + fw-hestia-rpc + fw-hestia-rpc @@ -34,8 +36,7 @@ 1.0.0 2.1.0 1.0 - 3.0.0 - 5.2.5 + 1.2.51 @@ -100,14 +101,9 @@ ${redis.spring.boot.starter} - com.google.zxing - javase - ${javase} - - - cn.hutool - hutool-all - ${hutool.all} + com.alibaba + fastjson + ${fastjson}