GsonUtils
public class GsonUtil {
private static Gson gson;
private GsonUtil() {
}
static {
gson = new GsonBuilder().enableComplexMapKeySerialization().serializeNulls()
.setDateFormat("yyyy-MM-dd HH:mm:ss").create();
}
public static String toJson(Object obj) {
return gson.toJson(obj);
}
public static <T> T fromJson(String json, Class<T> classOfT) {
return gson.fromJson(json, classOfT);
}
}
redis 的基本用法
/**
* 〈一句話功能簡述〉 redis工具類
* 〈功能詳細描述〉
*
* @author 17040683
* @see [相關類/方法](可選)
* @since [產品/模塊版本] (可選)
* @Date 2017年5月23日 下午4:56:36
*/
public class RedisTool {
protected ShardedJedisClientImpl shardedClient;
public RedisTool() {
shardedClient = new ShardedJedisClientImpl("redis.conf");
}
/**
*
* get操作 <br>
*/
public String get(final String key) {
String value = shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
return shardedJedis.get(key);
}
});
return value;
}
/**
*
* set操作 <br>
*/
public String set(final String key, final String val) {
return shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
return shardedJedis.set(key, val);
}
});
}
/**
*
* expire操作 <br>
*/
public Long expire(final String key, final int seconds) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.expire(key, seconds);
}
});
}
/**
*
* setex操作 <br>
*/
public String setex(final String key, final int seconds, final String value) {
return shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
return shardedJedis.setex(key, seconds, value);
}
});
}
/**
*
* del操作 <br>
*/
public Long delKey(final String key) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.del(key);
}
});
}
/**
*
* 獲取管道操作 <br>
*/
public ShardedJedisPipeline pipeline() {
return shardedClient.execute(new ShardedJedisAction<ShardedJedisPipeline>() {
@Override
public ShardedJedisPipeline doAction(ShardedJedis shardedJedis) {
return shardedJedis.pipelined();
}
});
}
/**
*
* hset操作 <br>
*
*/
public Long hset(final String key, final String field, final String value) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.hset(key, field, value);
}
});
}
/**
*
* hget操作<br>
*
*/
public String hget(final String key, final String field) {
return shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
return shardedJedis.hget(key, field);
}
});
}
/**
*
* hdel操作 <br>
*
*/
public Long hdel(final String key, final String field) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.hdel(key, field);
}
});
}
/**
*
* hkeys操作 <br>
*
*/
public Set<String> hkeys(final String key) {
return shardedClient.execute(new ShardedJedisAction<Set<String>>() {
@Override
public Set<String> doAction(ShardedJedis shardedJedis) {
return shardedJedis.hkeys(key);
}
});
}
/**
*
* ltrim操作 <br>
*
* @param key
* @param field
*/
public String ltrim(final String key, final long start, final long end) {
return shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
return shardedJedis.ltrim(key, start, end);
}
});
}
/**
*
* lpush操作 <br>
*
*/
public Long lpush(final String key, final String... strings) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.lpush(key, strings);
}
});
}
/**
*
* rpush操作 <br>
*
*/
public Long rpush(final String key, final String... strings) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.rpush(key, strings);
}
});
}
/**
*
* lrem操作 :根據參數 count 的值,移除列表中與參數 value 相等的元素,返回被移除元素的數量<br>
*
*/
public Long lrem(final String key, final long count, final String value) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.lrem(key, count, value);
}
});
}
/**
*
* lrange操作 <br>
*
*/
public List<String> lrange(final String key, final long start, final long end) {
return shardedClient.execute(new ShardedJedisAction<List<String>>() {
@Override
public List<String> doAction(ShardedJedis shardedJedis) {
return shardedJedis.lrange(key, start, end);
}
});
}
/**
*
* llen操作 <br>
*
*/
public Long llen(final String key) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.llen(key);
}
});
}
/**
*
* type操作,獲取key是哪一種數據存儲類型 <br>
*
* @param key
*/
public String type(final String key) {
return shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
return shardedJedis.type(key);
}
});
}
/**
*
* smembers操作 <br>
*
* @param key
*/
public Set<String> smembers(final String key) {
return shardedClient.execute(new ShardedJedisAction<Set<String>>() {
@Override
public Set<String> doAction(ShardedJedis shardedJedis) {
return shardedJedis.smembers(key);
}
});
}
/**
*
* lrange操作 <br>
*/
public List<String> lrange(final String key) {
return shardedClient.execute(new ShardedJedisAction<List<String>>() {
@Override
public List<String> doAction(ShardedJedis shardedJedis) {
return shardedJedis.lrange(key, 0L, shardedJedis.llen(key) - 1L);
}
});
}
/**
*
* ttl操作 <br>
*
*/
public Long ttl(final String key) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
return shardedJedis.ttl(key);
}
});
}
/**
*
* 應用在分佈式鎖場景中<br>
*
* @param key 需要setnx的key
* @param value key對應的值
*/
public long setnx(final String key, final String value) {
return shardedClient.execute(new ShardedJedisAction<Long>() {
@Override
public Long doAction(ShardedJedis shardedJedis) {
long tmpVal = shardedJedis.setnx(key, value);
return tmpVal;
}
});
}
/**
*
* 應用在分佈式鎖場景中<br>
*
* @param key getSet
* @param value key對應的值
*/
public String getSet(final String key, final String value) {
String retValue = shardedClient.execute(new ShardedJedisAction<String>() {
@Override
public String doAction(ShardedJedis shardedJedis) {
String tmpVal = shardedJedis.getSet(key, value);
return tmpVal;
}
});
return retValue;
}
/**
*
* 獲取所有的key <br>
*
* @param key
* @return
*/
public Set<String> keys(final String key) {
return shardedClient.execute(new ShardedJedisAction<Set<String>>() {
@Override
public Set<String> doAction(ShardedJedis shardedJedis) {
Set <String> keySet = new HashSet<String>();
Collection<Jedis> jedisList = shardedJedis.getAllShards();
for (Jedis jedis : jedisList) {
Set<String> keys = jedis.keys(key);
keySet.addAll(keys);
}
return keySet;
}
});
}
}
bigDecimal的用法:
/**
* SUNING APPLIANCE CHAINS.
* Copyright (c) 2014-2014 All Rights Reserved.
*/
package com.suning.sdipospc.util;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
/**
* 〈一句話功能簡述〉 BigDecimal類型數據處理的常用工具類
* 〈功能詳細描述〉
*
* @author 17040683
* @see [相關類/方法](可選)
* @since [產品/模塊版本] (可選)
* @Date 2017年6月3日 上午9:09:10
*/
public class BigDecimalUtils {
/**
* BigDecimal精確一個小數
*/
public static final int ONE_DECIMAL_PLACES = 1;
/**
* BigDecimal精確兩個小數
*/
public static final int TWO_DECIMAL_PLACES = 2;
/**
* BigDecimal精確三個小數
*/
public static final int THREE_DECIMAL_PLACES = 3;
/**
* POS默認最小金額(門店只能收到1角錢)
*/
public static final BigDecimal POS_DEFAULT_BIGDECIMAL = new BigDecimal("0.1");
/**
* POS默認最小金額(發票最少1元錢)
*/
public static final BigDecimal POS_DEFAULT_INVOCIE_BIGDECIMAL = new BigDecimal("1");
/**
* POS默認最小金額(門店只能收到1角錢)
*/
public static final String POS_DEFAULT_STRING = "0.1";
/**
* 默認的保留兩位小數的bigdecimal金額
*/
public static final BigDecimal ZERO_BIGDECIMAL = new BigDecimal("0.00");
/**
* 默認的保留三位小數的bigdecimal金額
*/
public static final BigDecimal ZERO_BIGDECIMAL_THREE = new BigDecimal("0.000");
/**
* 默認的保留一位小數的bigdecimal金額
*/
public static final BigDecimal ZERO_BIGDECIMAL_1 = new BigDecimal("0.0");
/**
* 0.1
*/
public static final BigDecimal ZERO_POINT_ONE = new BigDecimal("0.1");
/**
* 默認的不帶小數的bigdecimal金額0
*/
public static final BigDecimal ZERO_BIGDECIMAL_0 = new BigDecimal("0");
/**
* 默認的不帶小數的bigdecimal金額1
*/
public static final BigDecimal ONE_BIGDECIMAL_1 = new BigDecimal("1");
/**
* 默認的不帶小數的bigdecimal金額2
*/
public static final BigDecimal TWO_BIGDECIMAL_2 = new BigDecimal("2");
/**
* 默認的不帶小數的bigdecimal金額3
*/
public static final BigDecimal THREE_BIGDECIMAL_3 = new BigDecimal("3");
/**
* 默認不帶小數的String金額
*/
public static final String ZERO_STRING_0 = "0";
/**
* 默認的保留1位小數的String金額
*/
public static final String ZERO_STRING_1 = "0.0";
/**
* 默認的保留兩位小數的String金額
*/
public static final String ZERO_STRING_2 = "0.00";
/**
* pos默認發票的String金額
*/
public static final String POS_DEFAULT_INVOICE_STRING = "1";
/**
* 默認的無小數的金額
*/
public static final int ZERO_INT = 0;
/**
* 功能描述: <br>
* 〈將BigDecimal值保留一位小數,四捨五入〉
*
* @param num
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static BigDecimal HalfUpOneDecimal(BigDecimal num) {
return num.setScale(ONE_DECIMAL_PLACES, RoundingMode.HALF_UP);
}
/**
* 功能描述: <br>
* 〈將BigDecimal值保留兩位小數,四捨五入〉
*
* @param num
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static BigDecimal HalfUpTwoDecimal(BigDecimal num) {
return num.setScale(TWO_DECIMAL_PLACES, RoundingMode.HALF_UP);
}
/**
* 功能描述: <br>
* 〈將BigDecimal值保留三位小數,四捨五入〉
*
* @param num
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static BigDecimal HalfUpThreeDecimal(BigDecimal num) {
return num.setScale(THREE_DECIMAL_PLACES, RoundingMode.HALF_UP);
}
/**
* 功能描述:當字符金額精度大於制定的精度時,按照舍入的規則保留指定標度
*
* @param scale
* @param bigDecimalString 金額字符創
* 返回值: 類型 <說明>
* @return 返回值
*/
public static String convertBdStringPrecision(int scale, String bigDecimalString) {
if (StringUtils.isBlank(bigDecimalString)) {
return bigDecimalString;
} else {
BigDecimal bd = new BigDecimal(bigDecimalString);
//取標度
int bdscale = bd.scale();
if (bdscale > scale) {
return bd.setScale(scale, BigDecimal.ROUND_DOWN).toString();
} else {
return bigDecimalString;
}
}
}
/**
* 功能描述: 將BigDecimal金額按照四捨五入的規則轉化爲自定義精度的string類型金額
*
* @param num
* @param bd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String bdConvertString(int num, BigDecimal bd) {
if (bd == null) {
if (0 == num) {
return ZERO_STRING_0;
} else if (1 == num) {
return ZERO_STRING_1;
} else {
return ZERO_STRING_2;
}
}
return bd.setScale(num, BigDecimal.ROUND_HALF_UP).toString();
}
/**
* 功能描述:將BigDecimal金額按照佘位的規則轉化爲自定義精度的string類型金額
* 輸入參數:<按照參數定義順序>
*
* @param num
* @param bd
* @return 返回值
*/
public static String bdRoundDownToString(int num, BigDecimal bd) {
if (bd == null) {
if (0 == num) {
return ZERO_STRING_0;
} else if (1 == num) {
return ZERO_STRING_1;
} else {
return ZERO_STRING_2;
}
}
return bd.setScale(num, BigDecimal.ROUND_DOWN).toString();
}
/**
* 功能描述:將Object金額按照佘位規則轉化爲自定義精度的BigDecimal類型金額 默認精度爲兩位小數
*
* @param obj 必須又阿拉伯數字或小數點組成
* @return BigDecimal
* @throw NumberFormatException if {@code val} is not a valid
* representation of a {@code BigDecimal}
*/
public static BigDecimal objConvertBd(Object obj) {
if (null == obj) {
return ZERO_BIGDECIMAL;
}
if (obj instanceof String) {
if (StringUtils.isBlank((CharSequence) obj)) {
return ZERO_BIGDECIMAL;
} else if (StringUtils.equals((CharSequence) obj, "null")) {
return ZERO_BIGDECIMAL;
}
}
if (obj instanceof BigDecimal) {
return ((BigDecimal) obj).setScale(TWO_DECIMAL_PLACES, BigDecimal.ROUND_DOWN);
}
BigDecimal bd = new BigDecimal(String.valueOf(obj));
return bd.setScale(TWO_DECIMAL_PLACES, BigDecimal.ROUND_DOWN);
}
/**
* 功能描述:將Object金額按照佘位規則轉化爲自定義精度的BigDecimal類型金額 若入參爲空默認精度爲兩位小數 不可空 則轉化爲定義的精度
*
* @param num 精度位數
* @param obj 必須又阿拉伯數字或小數點組成
* @return BigDecimal
* @throw NumberFormatException if {@code val} is not a valid
* representation of a {@code BigDecimal}
*/
public static BigDecimal objConvertBd(int num, Object obj) {
if (null == obj) {
return ZERO_BIGDECIMAL;
}
if (obj instanceof String) {
if (StringUtils.isBlank((CharSequence) obj)) {
return ZERO_BIGDECIMAL;
} else if (StringUtils.equals((CharSequence) obj, "null")) {
return ZERO_BIGDECIMAL;
}
}
if (obj instanceof BigDecimal) {
return ((BigDecimal) obj).setScale(num, BigDecimal.ROUND_DOWN);
}
BigDecimal bd = new BigDecimal(String.valueOf(obj));
return bd.setScale(num, BigDecimal.ROUND_DOWN);
}
/**
* 比較大小,若orginalBd大於targtBd則返回true,否則返回false 功能描述: <br>
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isMoreBd(BigDecimal orginalBd, int targtBd) {
return isMoreBd(orginalBd, new BigDecimal(targtBd));
}
/**
* 功能描述: 比較大小,若orginalBd大於targtBd則返回true,否則返回false
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isMoreBd(BigDecimal orginalBd, BigDecimal targtBd) {
if (null == orginalBd) {
orginalBd = ZERO_BIGDECIMAL;
}
if (null == targtBd) {
targtBd = ZERO_BIGDECIMAL;
}
return orginalBd.compareTo(targtBd) == 1 ? true : false;
}
/**
* 功能描述: 比較大小,若orginalBd大於等於targtBd則返回true,否則返回false
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isMoreOrEqualBd(BigDecimal orginalBd, BigDecimal targtBd) {
if (null == orginalBd) {
orginalBd = ZERO_BIGDECIMAL;
}
if (null == targtBd) {
targtBd = ZERO_BIGDECIMAL;
}
return orginalBd.compareTo(targtBd) >= 0 ? true : false;
}
/**
* 功能描述:判斷是否大於0
*
* @param orginalBd
* @return true or false
* @throw 異常描述
*/
public static boolean isMoreZero(BigDecimal orginalBd) {
if (null != orginalBd) {
return orginalBd.compareTo(ZERO_BIGDECIMAL) == 1 ? true : false;
} else {
return false;
}
}
/**
* 功能描述:判斷是否大於0.1
*
* @param orginalBd
* @return true or false
* @throw 異常描述
*/
public static boolean isMoreZeroPointOne(BigDecimal orginalBd) {
if (null != orginalBd) {
return formatDecimal2CurrencyNumber(orginalBd).compareTo(ZERO_POINT_ONE) > 0;
} else {
return false;
}
}
/**
* 功能描述:判斷是否小於0
*
* @param orginalBd
* @return true or false
* @throw 異常描述
*/
public static boolean isLessZero(BigDecimal orginalBd) {
if (null != orginalBd) {
return orginalBd.compareTo(ZERO_BIGDECIMAL) == -1 ? true : false;
} else {
return false;
}
}
/**
* 功能描述:判斷是否等於0
*
* @param orginalBd
* @return true or false
* @throw 異常描述
*/
public static boolean isEqualZero(BigDecimal orginalBd) {
if (null != orginalBd) {
return orginalBd.compareTo(ZERO_BIGDECIMAL) == 0 ? true : false;
} else {
return false;
}
}
/**
* 功能描述:判斷是否小於等於0
*
* @param orginalBd
* @return true or false
* @throw 異常描述
*/
public static boolean isLessOrEqualZero(BigDecimal orginalBd) {
return !isMoreZero(orginalBd);
}
/**
* 功能描述: 比較大小,若orginalBd小於等於targtBd則返回true,否則返回false
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isLessOrEqualBd(BigDecimal orginalBd, BigDecimal targtBd) {
if (null == orginalBd) {
orginalBd = ZERO_BIGDECIMAL;
}
if (null == targtBd) {
targtBd = ZERO_BIGDECIMAL;
}
return orginalBd.compareTo(targtBd) <= 0 ? true : false;
}
/**
* 功能描述: 比較大小,若orginalBd小於targtBd則返回true,否則返回false
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isLessBd(BigDecimal orginalBd, BigDecimal targtBd) {
if (null == orginalBd) {
orginalBd = ZERO_BIGDECIMAL;
}
if (null == targtBd) {
targtBd = ZERO_BIGDECIMAL;
}
return orginalBd.compareTo(targtBd) < 0 ? true : false;
}
/**
* 比較大小,若orginalBd等於targtBd則返回true,否則返回false 功能描述: <br>
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isEqualsBd(BigDecimal orginalBd, int targtBd) {
if (null == orginalBd) {
orginalBd = ZERO_BIGDECIMAL;
}
return orginalBd.compareTo(new BigDecimal(targtBd)) == 0 ? true : false;
}
/**
* 比較大小,若orginalBd等於targtBd則返回true,否則返回false 功能描述: <br>
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isEquals(BigDecimal orginalBd, BigDecimal targtBd) {
if (null == orginalBd) {
orginalBd = ZERO_BIGDECIMAL;
}
if (null == targtBd){
targtBd = ZERO_BIGDECIMAL;
}
return orginalBd.compareTo(targtBd) == 0 ? true : false;
}
/**
* 比較大小,若orginalBd不等於targtBd則返回true,否則返回false 功能描述: <br>
* 〈功能詳細描述〉
*
* @param orginalBd
* @param targtBd
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static boolean isNotEquals(BigDecimal orginalBd, BigDecimal targtBd) {
return !isEquals(orginalBd, targtBd);
}
/**
* 功能描述:如果orginalBd等於0則返回空,否則返回根據四捨五入規則截取的保留0位小數的string字符串
*
* @param orginalBd BigDecimal
* @return String
* @throw 異常描述
*/
public static String defaultIfZero(BigDecimal orginalBd) {
return isEqualsBd(orginalBd, 0) == true ? "" : bdConvertString(0, orginalBd);
}
/**
* 功能描述:將double類型按照佘位規則轉化爲bigdecimal類型
*
* @param num
* @param db
* @return 返回值
*/
public static BigDecimal dbConvertBd(int num, Double db) {
if (db == null) {
return ZERO_BIGDECIMAL;
}
return new BigDecimal(db).setScale(num, BigDecimal.ROUND_DOWN);
}
/**
* 功能描述:轉換成POS的到角的價格精度(ps:POS大陸店價格只可到角 暫不考慮香港店到元的情況) 不支持處理爲負的金額
* 若金額精度爲分 ps: 0.01-0.09 則進一位到角 返回0.1 若金額精度有角有分 ps:0.12 則捨去一位返回 0.1
*
* @param bd
* @return 返回值
*/
public static String convertPosJiaoScale(BigDecimal bd) {
if (bd == null) {
return ZERO_STRING_1;
}
int bdScale = bd.scale();
//如果精度小於等1則表示進度到角,無需處理直接返回
if (bdScale <= 1) {
return bd.toString();
} else {
// 如果大於0
if (isMoreZero(bd)) {
//小於等於0.1的 則視爲0.1
if (isLessOrEqualBd(bd, POS_DEFAULT_BIGDECIMAL)) {
return POS_DEFAULT_STRING;
} else {
//大於0.1 則 捨去小數點1位以後的位數
return bd.setScale(1, BigDecimal.ROUND_DOWN).toString();
}
} else {
return bd.toString();
}
}
}
/**
* 功能描述:轉換成POS的到元的價格精度(ps:線下記收入發票金額至少1元) 不支持處理爲負的金額
* 若金額精度爲分 ps: 0.0001...-0.9999... 則進一位到元 返回1 若金額精度有角有分 ps:1.12 則捨去一位返回 1.1
*
* @param bd
* @return 返回值
*/
public static String convertPosYuanScale(BigDecimal bd) {
if (bd == null) {
return ZERO_STRING_0;
}
int bdScale = bd.scale();
//如果精度小於1則表示進度到元,無需處理直接返回
if (bdScale < 1) {
return bd.toString();
} else {
// 精度如果大於1
// 大於0
if (isMoreZero(bd)) {
//小於等於1的 則視爲1
if (isLessOrEqualBd(bd, POS_DEFAULT_INVOCIE_BIGDECIMAL)) {
return POS_DEFAULT_INVOICE_STRING;
} else {
//大於1 則 捨去小數點1位以後的位數
return bd.setScale(1, BigDecimal.ROUND_DOWN).toString();
}
} else {
return bd.toString();
}
}
}
/**
* 用於計算用券金額時,保留一位小數,舍入模式RoundingMode.DOWN<br>
* 10.999 = 10.9;10.921 = 10.9
* 功能描述: <br>
* 〈功能詳細描述〉
*
* @param bd1
* @param bd2
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static BigDecimal couponMultiply(BigDecimal bd1, BigDecimal bd2) {
BigDecimal rtn = null;
if (bd1 == null || bd2 == null) {
rtn = ZERO_BIGDECIMAL_1;
} else {
rtn = bd1.multiply(bd2).setScale(1, RoundingMode.DOWN);
}
return rtn;
}
public static BigDecimal multiply(BigDecimal bd1, BigDecimal bd2) {
BigDecimal rtn = null;
if (bd1 == null || bd2 == null) {
rtn = ZERO_BIGDECIMAL;
} else {
rtn = bd1.multiply(bd2).setScale(2, RoundingMode.DOWN);
}
return rtn;
}
/**
* 用於計算商品價格--精度1,舍入模式RoundingMode.DOWN<br>
* 1/3 = 0.3; 2/3 = 0.6 <br>
* 功能描述: <br>
* 〈功能詳細描述〉
*
* @param bd1 商品銷售金額
* @param divisor
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static BigDecimal productDivide(BigDecimal bd1, BigDecimal divisor) {
BigDecimal rtn = null;
if (bd1 == null || divisor == null || isEquals(divisor, BigDecimal.ZERO)) {
rtn = ZERO_BIGDECIMAL_1;
} else {
rtn = bd1.divide(divisor, 1, RoundingMode.DOWN);
}
return rtn;
}
/**
* 用於計算商品價格--精度1,舍入模式RoundingMode.UP<br>
* 1/3 = 0.3; 2/3 = 0.6 <br>
* 功能描述: <br>
* 〈功能詳細描述〉
*
* @param bd1 商品銷售金額
* @param divisor
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static BigDecimal productDivideUpmodel(BigDecimal bd1, BigDecimal divisor) {
BigDecimal rtn = null;
if (bd1 == null || divisor == null || isEquals(divisor, BigDecimal.ZERO)) {
rtn = ZERO_BIGDECIMAL_1;
} else {
rtn = bd1.divide(divisor, 1, RoundingMode.UP);
}
return rtn;
}
/**
* 格式化金額爲兩位小數
*
* @param input BigDecimal 類型
* @return
*/
public static BigDecimal formatDecimal2CurrencyNumber(BigDecimal input) {
DecimalFormat format = new DecimalFormat(ZERO_STRING_2);
return new BigDecimal(format.format(input));
}
/**
* 格式化金額爲兩位小數
*
* @param input 整形類型
* @return
*/
public static BigDecimal formatInteger2CurrencyNumber(Integer input) {
DecimalFormat format = new DecimalFormat(ZERO_STRING_2);
return new BigDecimal(format.format(input));
}
/**
* 兩個bigdecimal類型數據相加
* @param orginalBd
* @param targetBd
* @return BigDecimal
*/
public static BigDecimal add(BigDecimal orginalBd, BigDecimal targetBd) {
if(null != orginalBd && null != targetBd){
return orginalBd.add(targetBd);
}else{
return objConvertBd(orginalBd).add(objConvertBd(targetBd));
}
}
}
編碼方式 工具類
/**
* 封裝各種格式的編碼解碼工具類.
* <p/>
* 1.Commons-Codec的 hex/base64 編碼 2.自制的base62 編碼 3.Commons-Lang的xml/html escape 4.JDK提供的URLEncoder
*
* @author 袁兵 15041220
*/
public class EncodeUtils {
private EncodeUtils() {
}
private static final String DEFAULT_URL_ENCODING = "UTF-8";
private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
/**
* Hex編碼.
*/
public static String encodeHex(byte[] input) {
return Hex.encodeHexString(input);
}
/**
* Hex解碼.
*/
public static byte[] decodeHex(String input) {
try {
return Hex.decodeHex(input.toCharArray());
} catch (DecoderException e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
* Base64編碼.
*/
public static String encodeBase64(byte[] input) {
return Base64.encodeBase64String(input);
}
/**
* Base64編碼, URL安全(將Base64中的URL非法字符'+'和'/'轉爲'-'和'_', 見RFC3548).
*/
public static String encodeUrlSafeBase64(byte[] input) {
return Base64.encodeBase64URLSafeString(input);
}
/**
* Base64解碼.
*/
public static byte[] decodeBase64(String input) {
return Base64.decodeBase64(input);
}
/**
* Base62編碼。
*/
public static String encodeBase62(byte[] input) {
char[] chars = new char[input.length];
for (int i = 0; i < input.length; i++) {
chars[i] = BASE62[(input[i] & 0xFF) % BASE62.length];
}
return new String(chars);
}
/**
* Html 轉碼.
*/
public static String escapeHtml(String html) {
return StringEscapeUtils.escapeHtml4(html);
}
/**
* Html 解碼.
*/
public static String unescapeHtml(String htmlEscaped) {
return StringEscapeUtils.unescapeHtml4(htmlEscaped);
}
/**
* Xml 轉碼.
*/
public static String escapeXml(String xml) {
return StringEscapeUtils.escapeXml(xml);
}
/**
* Xml 解碼.
*/
public static String unescapeXml(String xmlEscaped) {
return StringEscapeUtils.unescapeXml(xmlEscaped);
}
/**
* URL 編碼, Encode默認爲UTF-8.
*/
public static String urlEncode(String part) {
try {
return URLEncoder.encode(part, DEFAULT_URL_ENCODING);
} catch (UnsupportedEncodingException e) {
throw ExceptionUtils.unchecked(e);
}
}
/**
* URL 解碼, Encode默認爲UTF-8.
*/
public static String urlDecode(String part) {
try {
return URLDecoder.decode(part, DEFAULT_URL_ENCODING);
} catch (UnsupportedEncodingException e) {
throw ExceptionUtils.unchecked(e);
}
}
}
登陸使用工具類
package com.suning.sdipospc.util;
import goja.QRCode;
import goja.QRCodeFormat;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.util.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* 登錄工具類
*
* @author 17040365
*
*/
public class LoginUtils {
/** 日誌對象 **/
private static final Logger logger = LoggerFactory
.getLogger(LoginUtils.class);
private static final int QRCODESIZE = 160;
private LoginUtils() {
}
/**
* 生成二維碼
*
* @param macAddress
* @return
* @throws IOException
*/
public static String getQrCodeImage(String macAddress) throws IOException {
QRCodeFormat codeFormat = QRCodeFormat.NEW();
codeFormat.setSize(QRCODESIZE);
if (StringUtils.isNotBlank(macAddress)) {
BufferedImage bi = QRCode.toQRCode(macAddress, codeFormat);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
return Base64.encodeBase64String(bytes).trim();
} else {
return null;
}
}
/**
* 獲取遠程MAC地址
*
* @return
*/
public static String getRemoteMac() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
return getRemoteMac(request);
}
/**
* 獲取操作系統名稱
*/
public static String getOsName() {
return System.getProperty("os.name");
}
/**
* 獲取遠程MAC地址
*
* @param request
* @return
*/
public static String getRemoteMac(HttpServletRequest request) {
String ip = getIpAddress(request);
logger.info("============remote ip=======" + ip);
String os = System.getProperty("os.name");
String mac = "";
if (os.startsWith("Windows")) {
mac = getMACAddressWindows(ip);
} else if (os.startsWith("Linux")) {
try {
mac = getMACAddressLinux(ip);
} catch (Exception e) {
logger.error("linux os get mac address fail", e);
}
}
return mac;
}
/**
* 獲取IP地址
*
* @param request
* @return
*/
public static String getIpAddress(HttpServletRequest request) {
String ip = request.getHeader("X-Forwarded-For");
if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
// 多次反向代理後會有多個ip值,第一個ip纔是真實ip
int index = ip.indexOf(',');
if (index != -1) {
return ip.substring(0, index);
} else {
return ip;
}
}
ip = request.getHeader("X-Real-IP");
if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
return ip;
}
return request.getRemoteAddr();
}
/**
* 通過遠程IP獲取MAC地址(windows)
*
* @param ip
* 遠程IP地址
* @return
*/
public static String getMACAddressWindows(String ip) {
String str = "";
String macAddress = "";
try {
Process p = Runtime.getRuntime().exec("nbtstat -a " + ip);
InputStreamReader ir = new InputStreamReader(p.getInputStream(),
"GBK");
LineNumberReader input = new LineNumberReader(ir);
logger.info("nbtstat -a " + ip + "\n mac info:" + str);
while ((str = input.readLine()) != null) {
if (str.indexOf("MAC 地址", 1) > -1) {
// 客戶端使用的是中文版操作系統
macAddress = str.substring(str.indexOf("MAC 地址") + 9,
str.length());
break;
} else if (str.indexOf("MAC Address", 1) > -1) {
// 客戶端使用的是英文版操作系統
macAddress = str.substring(str.indexOf("MAC Address") + 14,
str.length());
break;
}
}
} catch (IOException e) {
logger.error("=========獲取遠程mac地址異常=======IOException:", e);
}
logger.info("客戶端MAC地址:" + macAddress);
return macAddress;
}
/**
* 執行單條指令
*
* @param cmd
* 命令
* @return 執行結果
* @throws Exception
*/
public static String command(String cmd) throws Exception {
Process process = Runtime.getRuntime().exec(cmd);
process.waitFor();
InputStream in = process.getInputStream();
StringBuilder result = new StringBuilder();
byte[] data = new byte[256];
while (in.read(data) != -1) {
String encoding = System.getProperty("sun.jnu.encoding");
logger.info("===========system encodeing========="+ encoding);
result.append(new String(data, encoding));
}
return result.toString();
}
/**
* 獲取mac地址(linux)
*
* @param ip
* @return
* @throws Exception
*/
public static String getMACAddressLinux(String ip) throws Exception {
String result = command("ping " + ip + " -n 2");
if (result.contains("TTL")) {
result = command("arp -a " + ip);
}
String regExp = "([0-9A-Fa-f]{2})([-:][0-9A-Fa-f]{2}){5}";
Pattern pattern = Pattern.compile(regExp);
Matcher matcher = pattern.matcher(result);
StringBuilder mac = new StringBuilder();
while (matcher.find()) {
String temp = matcher.group();
mac.append(temp);
}
return mac.toString();
}
}
NSFDateUtil
/**
*
* 日期公用類
*/
public class NSFDateUtils {
/**
* 日誌
*/
private static final Logger LOGGER = LoggerFactory.getLogger(NSFDateUtils.class);
/** yyyy-MM-dd時間格式 */
public static final String FORMAT_10 = "yyyy-MM-dd";
/** yyyy/MM/dd時間格式 */
public static final String FORMAT2_10 = "yyyy/MM/dd";
/** yyyyMMdd時間格式 */
public static final String FORMAT_8 = "yyyyMMdd";
/** yyyyMMddHHmmss時間格式 */
public static final String FORMAT_14 = "yyyyMMddHHmmss";
/** yyyy-MM-dd HH:mm:ss時間格式 */
public static final String FORMAT_19 = "yyyy-MM-dd HH:mm:ss";
/** yyyy-MM-dd HH:mm時間格式 */
public static final String FORMAT_16 = "yyyy-MM-dd HH:mm";
/** yyyy-MM-dd hh:mm:ss.SSS 毫秒 */
public static final String format22 = "yyyy-MM-dd HH:mm:ss.SSS";
/**
* pos上午時間段
*/
private static final long POS_AM_TIME = 90000;
/**
* pos中午時間段
*/
private static final long POS_NOON_TIME = 120000;
/**
* pos下午時間段
*/
private static final long POS_PM_TIME = 180000;
/**
* 09:00—12:00
*/
public static final String AM_CHINESE = "上午";
/**
* 12:00-18:00
*/
public static final String PM_CHINESE = "下午";
public static final String ALL_DAY_CHINESE = "全天";
/**
* 週末
*/
public static final String[] WEEKENDS = { "週日", "週六" };
public static String getChar8() {
return DateFormatUtils.format(new Date(), FORMAT_8);
}
public static String getChar8(Timestamp time) {
return DateFormatUtils.format(time.getTime(), FORMAT_8);
}
public static String getChar14() {
return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss");
}
public static String getChar19() {
return DateFormatUtils.format(new Date(), FORMAT_19);
}
public static String toChar(String datestr) {
return datestr.replaceAll("-", "").replaceAll(":", "");
}
/**
* 將長時間格式字符串轉換爲時間 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 將長時間格式字符串轉換爲時間 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 將長時間格式時間轉換爲字符串
*
* @param dateDate
* @return
*/
public static String dateToStrLong(java.util.Date dateDate, String format) {
SimpleDateFormat formatter = new SimpleDateFormat(format);
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 格式化字符串日期由YYYY-MM-DD 轉爲 YYYYMMDD
*
* @param dateDate
* @return
*/
public static String convertDateString(String date) {
return StringUtils.replace(date, "-", "");
}
/**
* 取當前時間的字符串,精確到分鐘
*
* @return 取當前時間字符串,格式爲“yyyyMMddHHmm”
*/
public static String getChar12() {
return DateFormatUtils.format(new Date(), "yyyyMMddHHmm");
}
public static String getChar6() {
return DateFormatUtils.format(new Date(), "HHmmss");
}
public static String formatChar14(String char14) {
if (char14 == null || char14.length() == 0) {
return char14;
}
return char14.substring(0, 4) + "-" + char14.substring(4, 6) + "-" + char14.substring(6, 8) + " "
+ char14.substring(8, 10) + ":" + char14.substring(10, 12) + ":" + char14.substring(12, 14) + " ";
}
public static String formatChar12(String char14) {
if (char14 == null || char14.length() == 0) {
return char14;
}
return char14.substring(0, 4) + "-" + char14.substring(4, 6) + "-" + char14.substring(6, 8) + " "
+ char14.substring(8, 10) + ":" + char14.substring(10, 12);
}
/**
* 判斷是否超過指定的結束時間
*
* @param srcBeginDate String 開始時間(yyy-MM-dd)
* @param srcEndDate String 結束時間(有兩種可能性:1:相對開始時間後的天數或者月數,2:絕對的結束時間)開始時間(yyy-MM-dd)
* @param relatetivelyFlag int 相對標誌(1:天數,2:月數)
* @return boolean
*/
public static boolean judgeIfExceedEndDate(String srcBeginDate, String srcEndDate, int relatetivelyFlag) {
if (srcEndDate.trim().length() != 8) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(srcBeginDate.substring(0, 4)), Integer.parseInt(srcBeginDate.substring(5, 7)),
Integer.parseInt(srcBeginDate.substring(8, 10)));
if (relatetivelyFlag == 1) {
cal.roll(Calendar.DAY_OF_YEAR, Integer.parseInt(srcEndDate));
cal.roll(Calendar.YEAR, Integer.parseInt(srcEndDate) / 365);
} else if (relatetivelyFlag == 2) {
cal.roll(Calendar.MONTH, Integer.parseInt(srcEndDate));
cal.roll(Calendar.YEAR, Integer.parseInt(srcEndDate) / 12);
}
srcEndDate = formatToChar8(cal);
}
if (Long.parseLong(getChar8()) >= Long.parseLong(srcEndDate)) {
return true;
}
return false;
}
/**
* 將當前日期向後滾動n個月
*
* @param srcDate String 當前日期
* @param rollMonth String 待滾動的月數
* @return String
*/
public static String rollMonth(String srcDate, String rollMonth) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(srcDate.substring(0, 4)), Integer.parseInt(srcDate.substring(4, 6)) - 1,
Integer.parseInt(srcDate.substring(6, 8)));
cal.roll(Calendar.MONTH, Integer.parseInt(rollMonth));
cal.roll(Calendar.YEAR, Integer.parseInt(rollMonth) / 12);
return formatToChar8(cal);
}
/**
* 將當前日期向前滾動n個月
*
* @param srcDate String 當前日期
* @param rollMonth String 待滾動的月數
* @return String
*/
public static String preMonth(String srcDate, String rollMonth) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(srcDate.substring(0, 4)), Integer.parseInt(srcDate.substring(4, 6)) - 1,
Integer.parseInt(srcDate.substring(6, 8)));
cal.add(Calendar.MONTH, Integer.parseInt(rollMonth));
cal.add(Calendar.YEAR, Integer.parseInt(rollMonth) / 12);
return formatToChar8(cal);
}
public static String formatToChar8(Calendar tmpcal) {
String tmpYear = Integer.toString(tmpcal.get(Calendar.YEAR));
String tmpMonth = Integer.toString(tmpcal.get(Calendar.MONTH) + 1);
String tmpDay = Integer.toString(tmpcal.get(Calendar.DAY_OF_MONTH));
String tmpDate = tmpYear + (tmpMonth.length() == 1 ? "0" + tmpMonth : tmpMonth)
+ (tmpDay.length() == 1 ? "0" + tmpDay : tmpDay);
return tmpDate;
}
public static String formatChar8(String char8) {
if (char8 == null || char8.length() == 0) {
return char8;
}
return char8.substring(0, 4) + "-" + char8.substring(4, 6) + "-" + char8.substring(6, 8) + " ";
}
public static String formatCha6(String char6) {
if (char6 == null || char6.length() == 0) {
return char6;
}
return char6.substring(0, 2) + ":" + char6.substring(2, 4) + ":" + char6.substring(4, 6);
}
/**
* 獲取指定日期 向前或向後滾動特定小時數後的日期
*
* @param nowDate String 當前日期
* @param rollDate String 待滾動的月數
* @return String 指定日期 +/- 特定天數 後的日期(格式 CCYYMMDD)
*/
public static String rollHour(String nowDate, int rollDate) {
String dateReturn = "";
if (nowDate == null || nowDate.trim().length() < 14) {
return dateReturn;
}
String dateNow = nowDate.trim();
Calendar cal = Calendar.getInstance();
int nYear = Integer.parseInt(dateNow.substring(0, 4));
int nMonth = Integer.parseInt(dateNow.substring(4, 6));
int nDate = Integer.parseInt(dateNow.substring(6, 8));
int nHour = Integer.parseInt(dateNow.substring(8, 10));
int nMinute = Integer.parseInt(dateNow.substring(10, 12));
int nSecond = Integer.parseInt(dateNow.substring(12, 14));
cal.set(nYear, nMonth - 1, nDate, nHour, nMinute, nSecond);
cal.add(Calendar.HOUR_OF_DAY, rollDate);
String strYear = String.valueOf(cal.get(Calendar.YEAR));
String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1);
String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
String strHour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
String strMinute = String.valueOf(cal.get(Calendar.MINUTE));
String strSecond = String.valueOf(cal.get(Calendar.SECOND));
strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth;
strDay = (strDay.length() == 1) ? "0" + strDay : strDay;
strHour = (strHour.length() == 1) ? "0" + strHour : strHour;
strMinute = (strMinute.length() == 1) ? "0" + strMinute : strMinute;
strSecond = (strSecond.length() == 1) ? "0" + strSecond : strSecond;
dateReturn = strYear + strMonth + strDay + strHour + strMinute + strSecond;
return dateReturn;
}
/**
* 獲取指定日期 向前或向後滾動特定天數後的日期
*
* @param nowDate String 當前日期
* @param rollDate String 待滾動的天數
* @return String 指定日期 +/- 特定天數 後的日期(格式 YYMMDD)
*/
public static String rollDate(String nowDate, int rollDate) {
String dateReturn = "";
if (nowDate == null || nowDate.trim().length() < 8) {
return dateReturn;
}
String dateNow = nowDate.trim();
Calendar cal = Calendar.getInstance();
int nYear = Integer.parseInt(dateNow.substring(0, 4));
int nMonth = Integer.parseInt(dateNow.substring(4, 6));
int nDate = Integer.parseInt(dateNow.substring(6, 8));
cal.set(nYear, nMonth - 1, nDate);
cal.add(Calendar.DATE, rollDate);
String strYear = String.valueOf(cal.get(Calendar.YEAR));
String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1);
String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth;
strDay = (strDay.length() == 1) ? "0" + strDay : strDay;
dateReturn = strYear + strMonth + strDay;
return dateReturn;
}
/**
* 獲取指定日期 向前或向後滾動特定天數後的日期
*
* @param nowDate String 當前日期
* @param rollDate String 待滾動的天數
* @return String 指定日期 +/- 特定天數 後的日期(格式 YYYY-MM-DD)
*/
public static String rollDate2(String nowDate, int rollDate) {
String dateReturn = "";
if (nowDate == null || nowDate.trim().length() < 8) {
return dateReturn;
}
String dateNow = nowDate.trim();
Calendar cal = Calendar.getInstance();
int nYear = Integer.parseInt(dateNow.substring(0, 4));
int nMonth = Integer.parseInt(dateNow.substring(5, 7));
int nDate = Integer.parseInt(dateNow.substring(8, 10));
cal.set(nYear, nMonth - 1, nDate);
cal.add(Calendar.DATE, rollDate);
String strYear = String.valueOf(cal.get(Calendar.YEAR));
String strMonth = String.valueOf(cal.get(Calendar.MONTH) + 1);
String strDay = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
strMonth = (strMonth.length() == 1) ? "0" + strMonth : strMonth;
strDay = (strDay.length() == 1) ? "0" + strDay : strDay;
dateReturn = strYear + "-" + strMonth + "-" + strDay;
return dateReturn;
}
/**
* 將完整的時間格式格式化成char14位的
*
* @param fullTime String
* @return String
*/
public static String formatFullTimeToChar14(String fullTime) {
return StringUtils.replace(
StringUtils.replace(StringUtils.replace(StringUtils.replace(fullTime, "-", ""), ":", ""), " ", ""), ".",
"");
}
/******
* 返回當天所在的年份
*
* @return yyyy
*/
public static String getYearByCurrentDate() {
return getChar8().substring(0, 4);
}
/******
* 返回當天所在的月份
*
* @return mm
*/
public static String getMonthByCurrentDate() {
return getChar8().substring(4, 6);
}
public static String formatDate(String date, String formatter) {
SimpleDateFormat myFormatter = null;
Date da = null;
if (date.length() < 15) {
myFormatter = new SimpleDateFormat("yyyyMMddHHmmss");
} else
myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
da = myFormatter.parse(date);
} catch (Exception e) {
LOGGER.error("formatDate error", e);
}
return DateFormatUtils.format(da, formatter);
}
/**
* 轉化日期時間(yyyyMMddHHmmss)的格式
*
* @param date String 日期時間
* @param informatter String 輸入的格式
* @param outformatter String 輸出的格式
* @return String
*/
public static String formatDate(String date, String informatter, String outformatter) {
Date da = null;
try {
SimpleDateFormat myFormatter = new SimpleDateFormat(informatter);
da = myFormatter.parse(date);
} catch (Exception e) {
LOGGER.error("formatDate error", e);
}
return DateFormatUtils.format(da, outformatter);
}
private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
* 根據對象轉換成日期
*
* @param obj
*/
public static final Date getDate(Object obj) {
return getDate(obj, format, null);
}
public static final Date getDate(Object obj, Date defValue) {
return getDate(obj, format, defValue);
}
public static final Date getDate(Object obj, DateFormat format) {
return getDate(obj, format, null);
}
public static final Date getDate(Object obj, DateFormat format, Date defValue) {
if (obj instanceof Date) {
return (Date) obj;
} else {
if (obj instanceof Timestamp) {
Timestamp timestamp = (Timestamp) obj;
return new Date(timestamp.getTime() + timestamp.getNanos() / 1000000);
} else if (obj instanceof Long) {
return new Date(((Long) obj).longValue());
} else if (obj instanceof String) {
synchronized (format) {
try {
return format.parse((String) obj);
} catch (Exception e) {
LOGGER.error(e.getMessage());
return defValue;
}
}
}
return defValue;
}
}
public static final String getDateBeforeMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(Calendar.MONTH, -1);// 減一個月
lastDate.set(Calendar.DATE, 1);// 把日期設置爲當月第一天
lastDate.roll(Calendar.DATE, -1);// 日期回滾一天,也就是本月最後一天
str = sdf.format(lastDate.getTime());
return str;
}
/**
*
* 功能描述:判斷格式爲yyyy-MM-dd的日期是否爲本月今天到上月今天之內的日期 (如:今天爲2012-5-18日,2012-4-19到2012-5-18之間的日期返回1)輸入參數:<按照參數定義順序>
*
* @param param yyyy-MM-dd類型的日期 返回值: 類型 <說明>
* @return 1-代表在此一個月內,0-不在此一個月內,-3-時間轉換報錯
*/
public static final int compareWithInOneMonth(String param) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar afterCalendar = Calendar.getInstance();
Calendar beforeCalendar = Calendar.getInstance();
beforeCalendar.add(Calendar.MONTH, -1);
Date afterDate = afterCalendar.getTime();
Date beforeDate = beforeCalendar.getTime();
try {
Date date = format.parse(param);
if (date.before(afterDate) && date.after(beforeDate)) {
return 1;
} else {
return 0;
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
return -3;
}
}
/**
*
* 功能描述:判定yyyy-MM-dd類型的兩個日期的時間段跨度是否在一個月內(如:2012-4-18到2012-05-18是一個月內) 輸入參數:<按照參數定義順序>
*
* @param arg2 後一個日期
* @param arg1 前一個日期 返回值: 類型 <說明>
* @return 0-時間段不在一個月內 ,1-在一個月內,-3-輸入日期參數有問題
*/
public static final int monthWidthWithInOne(String arg1, String arg2) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
Date before = format.parse(arg1);
Date after = format.parse(arg2);
Calendar calendar = Calendar.getInstance();
calendar.setTime(after);
calendar.add(Calendar.MONTH, -1);
Date afterMulOneMonth = calendar.getTime();
if (before.before(afterMulOneMonth) || before.after(after)) {
return 0;
} else {
return 1;
}
} catch (Exception e) {
LOGGER.error(e.getMessage());
return -3;
}
}
/**
* 轉換pos的YYYYMMDD格式的日期到Timestamp格式 (YYYY-MM-DD HH:MM:SS)
*
* @param strDate
* @return Timestamp
*/
public static Timestamp convertToTimestamp17Byte(String strDate) {
Timestamp dateTime = null;
String formatDate = "";
formatDate = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8)
+ " 00:00:00";
dateTime = Timestamp.valueOf(formatDate);
return dateTime;
}
/**
* 轉換pos的YYYYMMDDHHMMSS格式的日期到Timestamp格式 (YYYY-MM-DD HH:MM:SS)
*
* @param strDate
* @return Timestamp
*/
public static Timestamp convertToTimestamp(String strDate) {
Timestamp dateTime = null;
String formatDate = "";
formatDate = StringUtils.substring(strDate, 0, 4) + "-" + StringUtils.substring(strDate, 4, 6) + "-"
+ StringUtils.substring(strDate, 6, 8) + " " + StringUtils.substring(strDate, 8, 10) + ":"
+ StringUtils.substring(strDate, 10, 12) + ":" + StringUtils.substring(strDate, 12, 14);
dateTime = Timestamp.valueOf(formatDate);
return dateTime;
}
/**
* 轉換pos的YYYYMMDD格式的日期到Date格式(YYYY-MM-DD)
*
* @param strDate
* @return Date
*/
public static java.sql.Date convertToDate10Byte(String strDate) {
java.sql.Date dateTime = null;
String formatDate = "";
formatDate = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8);
dateTime = java.sql.Date.valueOf(formatDate);
return dateTime;
}
/**
* 轉換pos的YYYYMMDD格式的日期到格式(YYYY-MM-DD)
*
* @param strDate
* @return Date
*/
public static String convertDate(String strDate) {
String formatDate = "";
if (StringUtils.length(strDate) < 8) {
formatDate = getNowDate2();
} else {
formatDate = strDate.substring(0, 4) + "-" + strDate.substring(4, 6) + "-" + strDate.substring(6, 8);
}
return formatDate;
}
/**
*
* 功能描述: TimeStamp轉化爲String<br>
*
* @param tp
* @param sdf
* @return
*/
public static final String timeStamp2String(Timestamp tp, SimpleDateFormat sdf) {
Date d = new Date(tp.getTime());
return sdf.format(d);
}
public static String getChar14(Timestamp time) {
return DateFormatUtils.format(time.getTime(), "yyyyMMddHHmmss");
}
public static String dateToString(java.util.Date paramDate, String paramString) {
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
localSimpleDateFormat.setLenient(false);
return localSimpleDateFormat.format(paramDate);
}
/**
*
* 功能描述:獲取系統當前日期(YYYY-MM-DD)
*
* @param 參數說明
* @return 返回值
* @throw 異常描述
* @see 需要參見的其它內容
*/
public static String getNowDate2() {
return DateFormatUtils.format(new Date(), FORMAT_10);
}
/**
*
* 功能描述: 獲取當前日期時間(yyyy-MM-dd hh:mm:ss.SSS) 〈功能詳細描述〉
*
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getNowDate22() {
return DateFormatUtils.format(new Date(), format22);
}
/**
* 獲取系統當前日期(yyyyMMdd) 功能描述: <br>
* 〈功能詳細描述〉
*
* @return
* @throws ParseException
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getNowDate() {
return DateFormatUtils.format(new Date(), FORMAT_8);
}
public static String getNowTime() {
return DateFormatUtils.format(new Date(), "HHmmss");
}
public static java.util.Date stringToDate(String paramString1, String paramString) {
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat(paramString);
localSimpleDateFormat.setLenient(false);
try {
return localSimpleDateFormat.parse(paramString1);
} catch (ParseException e) {
LOGGER.error("stringToDate error", e);
}
return null;
}
/**
* 日期相減
*
* @param qsDate 起始日期
* @param jzDate 截止日期
* @return 返回截止日期減起始日期的天數
* @throws ParseException
* @throws Exception
*/
public static int diffDate(String qsDate, String jzDate) {
try {
return (int) ((getMillis(jzDate) - getMillis(qsDate)) / (24 * 3600 * 1000));
} catch (ParseException e) {
LOGGER.error("diffDate error", e);
}
return 0;
}
public static long getMillis(String date) throws ParseException {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(stringToDate(date, FORMAT_8));
return c.getTimeInMillis();
}
public static long getMillis(String date, String formate) {
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTime(stringToDate(date, formate));
return c.getTimeInMillis();
}
/**
*
* 功能描述: 根據傳入時間獲取所屬時間段的中文描述 日期格式爲hhmmss 或 hh:mm:ss 〈功能詳細描述〉 默認全天
*
* @return 上午、下午、全天
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getChinesTime(String time) {
String realTime = StringUtils.replaceChars(time, ":", "");
if (6 == StringUtils.length(realTime)) {
long timeNumber = Long.parseLong(realTime);
if (timeNumber >= POS_AM_TIME && timeNumber < POS_NOON_TIME) {
return AM_CHINESE;
} else if (timeNumber >= POS_NOON_TIME && timeNumber <= POS_PM_TIME) {
return PM_CHINESE;
} else {
return ALL_DAY_CHINESE;
}
} else {
return ALL_DAY_CHINESE;
}
}
/**
*
* 功能描述:獲取指定格式時間
*
* @param pattern 格式
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static Timestamp getTimeStamp(String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
Timestamp ts = Timestamp.valueOf(df.format(new Date()));
return ts;
}
/**
*
* 功能描述:獲取指定格式時間字符串
*
* @param pattern 格式
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getDateString(String pattern) {
DateFormat df = new SimpleDateFormat(pattern);
return df.format(new Date());
}
/**
* 功能描述: <br>
* 計算出endDate-strDate相差的天數-注意:較大的天數作爲第二個入參<br>
* 傳入參數爲空值時,返回結果爲-1
*
* @param strDate
* @param endDate
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static int diffDate(Date strDate, Date endDate) {
int result = -1;
if (null != strDate && null != endDate) {
result = (int) ((endDate.getTime() - strDate.getTime()) / (24 * 3600 * 1000));
}
return result;
}
/**
* 獲取當前日期是周幾
*
* @param dt
* @return 當前日期是周幾
*/
public static String getWeekOfDate(String date) {
String[] weekDays = { "週日", "週一", "週二", "週三", "週四", "週五", "週六" };
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
try {
c.setTime(format.parse(date));
} catch (ParseException e) {
LOGGER.error("getWeekOfDate error", e);
}
int dayForWeek = 0;
dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
return weekDays[dayForWeek];
}
/**
*
* 功能描述:把字符串日期後延多少天
*
* @param addDays 多少天
* @param dateStr 如20150909
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String strDateAddDay(String dateStr, int addDays) {
SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_8);
ParsePosition pos = new ParsePosition(0);
Date dt = sdf.parse(dateStr, pos);
Calendar rightNow = Calendar.getInstance();
rightNow.setTime(dt);
rightNow.add(Calendar.DAY_OF_YEAR, addDays);// 日期加天
return sdf.format(rightNow.getTime());
}
/**
*
* 功能描述: 字符串日期轉換成long
*
* @param strDate 待轉換的日期字符串
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static long dateStrToLong(String dateStr) {
SimpleDateFormat formatter = new SimpleDateFormat(FORMAT_8);
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(dateStr, pos);
return strtodate.getTime();
}
}
IntegerUtil:
public class NSFIntegerUtils {
private NSFIntegerUtils(){}
public static final Integer NUM_MINUS_ONE = -1;
public static final Integer NUM_ZERO = 0;
public static final Integer NUM_ONE = 1;
public static final Integer NUM_TWO = 2;
public static final Integer NUM_THREE = 3;
public static final Integer NUM_FOUR = 4;
public static final Integer NUM_FIVE = 5;
public static final Integer NUM_SIX = 6;
public static final Integer NUM_SEVEN = 7;
public static final Integer NUM_EIGHT = 8;
public static final Integer NUM_NINE = 9;
public static final Integer NUM_TEN = 10;
public static final Integer NUM_ELEVEN = 11;
public static final Integer NUM_THIRTEEN = 13;
public static final Integer NUM_FOURTEEN = 14;
public static final Integer NUM_FIFTEEN = 15;
public static final Integer NUM_SIXTEEN = 16;
/**
*
* 功能描述: 錯誤返回0,慎用<br>
* 〈功能詳細描述〉
*
* @param str
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static int parseFromString(String str){
int temp=0;
try {
temp=Integer.parseInt(str);
} catch (Exception e) {
temp=0;
}
return temp;
}
}
PaymentUtil.java
/**
* 支付Util類
*
* @author 14062651
* @see [相關類/方法](可選)
* @since [產品/模塊版本] (可選)
*/
public class PaymentUtils {
private PaymentUtils() {
// empty -init()
}
/**
* 標記:0 成功
*/
public static final String SUCCESS_FLAG = "0";
/**
* 標記: 1 失敗
*/
public static final String FAILURE_FLAG = "1";
/**
* 支付標記 <<成功前>> 0’成功前檢查,'1'失敗,'2'撤銷檢查(是否展示撤銷還是退貨)
*/
public static final String PAY_TYPE_SUCCESS = "0";
/**
* 支付標記 <<失敗>> 0’成功前檢查,'1'失敗,'2'撤銷檢查(是否展示撤銷還是退貨)
*/
public static final String PAY_TYPE_FAILURE = "1";
/**
* 支付標記<<撤銷檢查>> 0’成功前檢查,'1'失敗,'2'撤銷檢查(是否展示撤銷還是退貨)
*/
public static final String PAY_TYPE_CANCEL = "2";
/*
* 不合法的入參
*/
public static final String ERRCODE_ARGS_NOT_VALID = "E000";
/*
* 當前狀態不可用
*/
public static final String ERRCODE_PAY_STATUS_NOT_VALID = "E001";
/*
* 付款金額大於剩餘應付金額
*/
public static final String ERRCODE_AMMOUNT_NOT_VALID = "E002";
protected static Validator validator = null;
static {
validator = new Validator();
}
/**
* mis 退款標記
*/
public interface MisRefundFlag {
/**
* 撤銷
*/
String REVOKE = "1";
/**
* 退款
*/
String REFUND = "2";
}
public static void processPaymentError(PaymentErrorEnum error, NsfBaseResult result) {
result.setFlag(FAILURE_FLAG);
result.setErrCode(error.getErrorCode());
result.setErrMsg(error.getErrorMsg());
}
/**
* 根據系統來源 判斷 當前系統的數據是否遷移到了Mysql數據庫上
*
* @param systemSource 系統來源
* @return true:已經是遷移到了Mysql數據庫的系統, false:還未遷移到Mysql數據庫上
*/
public static boolean isMysqlDBEnable(String systemSource) {
return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource);
}
/**
* 根據系統來源判斷是否RMPOS
*
* @param systemSource 系統來源
* @return
*/
public static boolean isRmPos(String systemSource) {
// RMPOS特殊的掃碼付
return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource);
}
/**
* 根據系統來源判斷是否CCT
*
* @param systemSource 系統來源
* @return
*/
public static boolean isCct(String systemSource) {
// 收銀臺
return StringUtils.equals(SystemSourceEnum.CCT.name(), systemSource);
}
/**
* 功能描述: <br>
* 校驗 消息體
*
* @param target
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static List<ConstraintViolation> validateBean(Object target) {
return validator.validate(target);
}
/**
* 將IvOrdDetailMisBean轉換成CctMisDetailDto類型的bean
*
* @param misList
* @param results
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static void wrapDetailMisInfo(List<CctMisDetailDto> misList, List<IvOrdDetailMisBean> results) {
if (CollectionUtils.isNotEmpty(results)) {
CctMisDetailDto tmpMisDetailDto;
for (IvOrdDetailMisBean misBean : results) {
tmpMisDetailDto = new CctMisDetailDto();
// 結算銀行-收單銀行
tmpMisDetailDto.setAccquirer(misBean.getJsbank());
// 銀行日期
tmpMisDetailDto.setAcctDate(misBean.getYhrq());
// 應用標示符-暫不入
tmpMisDetailDto.setaID(StringUtils.EMPTY);
// 應用標籤
tmpMisDetailDto.setAppLable(StringUtils.EMPTY);
// 授權號
tmpMisDetailDto.setAuthorNo(misBean.getSqh());
// 銀行交易日期
tmpMisDetailDto.setBankTransDate(misBean.getYhrq());
// 銀行交易時間
tmpMisDetailDto.setBankTransTime(misBean.getYhsj());
// 批次號
tmpMisDetailDto.setBatchNo(misBean.getBatchnum());
// 單據狀態
tmpMisDetailDto.setBillState(misBean.getDjState());
// 單據類型
tmpMisDetailDto.setBillType(misBean.getDjlx());
// 卡名稱
tmpMisDetailDto.setCardName(misBean.getCardname());
// 卡類型
tmpMisDetailDto.setCardType(misBean.getCardtype());
// 收銀員號碼
tmpMisDetailDto.setCashierNo(misBean.getSyyh());
// 標記位
tmpMisDetailDto.setFlag(misBean.getFlg());
// 備註信息
tmpMisDetailDto.setMemo(misBean.getRspmsg());
// 商戶號
tmpMisDetailDto.setMerchantNo(misBean.getShnum());
// 商戶名稱
tmpMisDetailDto.setMerchantName(misBean.getShname());
// 原批次號
tmpMisDetailDto.setOldBatchNo(misBean.getOldbatchnum());
// 原流水號
tmpMisDetailDto.setOldPosTrace(misBean.getOldpostracenum());
// 原交易參考號
tmpMisDetailDto.setOldTrasRefNo(misBean.getStr10());
// 訂單號
tmpMisDetailDto.setOrderNo(misBean.getDh());
// 支付方式
tmpMisDetailDto.setPaymc(misBean.getStr2());
// 流水號
tmpMisDetailDto.setPosTrace(misBean.getPostracenum());
// 門店編碼
tmpMisDetailDto.setStoreId(misBean.getStr());
// tc
tmpMisDetailDto.settC(StringUtils.EMPTY);
// 卡-有效期
tmpMisDetailDto.setValidityDate(misBean.getYxq());
// 交易類型
tmpMisDetailDto.setTransType(misBean.getTranstype());
// 交易參考號
tmpMisDetailDto.setTransRefNo(misBean.getStr4());
// 卡號
tmpMisDetailDto.setTransCardNo(misBean.getTranscard());
//讓利金額
BigDecimal couponAmtAndAutoDisctAmt = BigDecimalUtils.add(BigDecimalUtils.dbConvertBd(2, misBean.getNum5()), BigDecimalUtils.dbConvertBd(2, misBean.getNum6()));
// 付款金額
tmpMisDetailDto.setTransAmount(BigDecimalUtils.add(misBean.getJyje(),couponAmtAndAutoDisctAmt));
// 終端號
tmpMisDetailDto.setTerminalNo(misBean.getZdnum());
// 已退款金額
// tmpMisDetailDto.setRefundMoney(misBean.getNum2());
//實付金額
tmpMisDetailDto.setRealPayAmt(misBean.getJyje());
//優惠券優惠金額
tmpMisDetailDto.setCouponAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum5()));
//自動折扣立減金額
tmpMisDetailDto.setAutoDisctAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum6()));
misList.add(tmpMisDetailDto);
}
}
}
public static MisTransInfoDto wrapMisTrans(DetailMisVo transDBModel) {
MisTransInfoDto misTrans = new MisTransInfoDto();
String transCard = transDBModel.getTranscard();
// 支付名稱 = 銀行卡名稱 + 卡號後4位
misTrans.setPaymc(transDBModel.getCardname()
+ StringUtils.right(transCard, 4));
if (transDBModel.getJyje() != null) {
BigDecimal jyje = transDBModel.getJyje();
misTrans.setPaymoney(jyje.setScale(2, BigDecimal.ROUND_UP).toString());
}
misTrans.setBatchnum(transDBModel.getBatchnum());
misTrans.setTranscard(transCard);
misTrans.setPostracenum(transDBModel.getPostracenum());
misTrans.setZdnum(transDBModel.getZdnum());
misTrans.setDjlx(transDBModel.getDjlx());
misTrans.setOldpostracenum(transDBModel.getOldpostracenum());
// 處理時間
if (null != transDBModel.getYhrq()) {
misTrans.setTransDate(transDBModel.getYhrq());
}
if (null != transDBModel.getYhsj()) {
misTrans.setTransTime(transDBModel.getYhsj());
}
misTrans.setShnum(transDBModel.getShnum());
misTrans.setStr6(transDBModel.getStr6());
return misTrans;
}
}
UUID 生成:
/**
* 支付Util類
*
* @author 14062651
* @see [相關類/方法](可選)
* @since [產品/模塊版本] (可選)
*/
public class PaymentUtils {
private PaymentUtils() {
// empty -init()
}
/**
* 標記:0 成功
*/
public static final String SUCCESS_FLAG = "0";
/**
* 標記: 1 失敗
*/
public static final String FAILURE_FLAG = "1";
/**
* 支付標記 <<成功前>> 0’成功前檢查,'1'失敗,'2'撤銷檢查(是否展示撤銷還是退貨)
*/
public static final String PAY_TYPE_SUCCESS = "0";
/**
* 支付標記 <<失敗>> 0’成功前檢查,'1'失敗,'2'撤銷檢查(是否展示撤銷還是退貨)
*/
public static final String PAY_TYPE_FAILURE = "1";
/**
* 支付標記<<撤銷檢查>> 0’成功前檢查,'1'失敗,'2'撤銷檢查(是否展示撤銷還是退貨)
*/
public static final String PAY_TYPE_CANCEL = "2";
/*
* 不合法的入參
*/
public static final String ERRCODE_ARGS_NOT_VALID = "E000";
/*
* 當前狀態不可用
*/
public static final String ERRCODE_PAY_STATUS_NOT_VALID = "E001";
/*
* 付款金額大於剩餘應付金額
*/
public static final String ERRCODE_AMMOUNT_NOT_VALID = "E002";
protected static Validator validator = null;
static {
validator = new Validator();
}
/**
* mis 退款標記
*/
public interface MisRefundFlag {
/**
* 撤銷
*/
String REVOKE = "1";
/**
* 退款
*/
String REFUND = "2";
}
public static void processPaymentError(PaymentErrorEnum error, NsfBaseResult result) {
result.setFlag(FAILURE_FLAG);
result.setErrCode(error.getErrorCode());
result.setErrMsg(error.getErrorMsg());
}
/**
* 根據系統來源 判斷 當前系統的數據是否遷移到了Mysql數據庫上
*
* @param systemSource 系統來源
* @return true:已經是遷移到了Mysql數據庫的系統, false:還未遷移到Mysql數據庫上
*/
public static boolean isMysqlDBEnable(String systemSource) {
return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource);
}
/**
* 根據系統來源判斷是否RMPOS
*
* @param systemSource 系統來源
* @return
*/
public static boolean isRmPos(String systemSource) {
// RMPOS特殊的掃碼付
return StringUtils.equals(SystemSourceEnum.RMPOS.name(), systemSource);
}
/**
* 根據系統來源判斷是否CCT
*
* @param systemSource 系統來源
* @return
*/
public static boolean isCct(String systemSource) {
// 收銀臺
return StringUtils.equals(SystemSourceEnum.CCT.name(), systemSource);
}
/**
* 功能描述: <br>
* 校驗 消息體
*
* @param target
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static List<ConstraintViolation> validateBean(Object target) {
return validator.validate(target);
}
/**
* 將IvOrdDetailMisBean轉換成CctMisDetailDto類型的bean
*
* @param misList
* @param results
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static void wrapDetailMisInfo(List<CctMisDetailDto> misList, List<IvOrdDetailMisBean> results) {
if (CollectionUtils.isNotEmpty(results)) {
CctMisDetailDto tmpMisDetailDto;
for (IvOrdDetailMisBean misBean : results) {
tmpMisDetailDto = new CctMisDetailDto();
// 結算銀行-收單銀行
tmpMisDetailDto.setAccquirer(misBean.getJsbank());
// 銀行日期
tmpMisDetailDto.setAcctDate(misBean.getYhrq());
// 應用標示符-暫不入
tmpMisDetailDto.setaID(StringUtils.EMPTY);
// 應用標籤
tmpMisDetailDto.setAppLable(StringUtils.EMPTY);
// 授權號
tmpMisDetailDto.setAuthorNo(misBean.getSqh());
// 銀行交易日期
tmpMisDetailDto.setBankTransDate(misBean.getYhrq());
// 銀行交易時間
tmpMisDetailDto.setBankTransTime(misBean.getYhsj());
// 批次號
tmpMisDetailDto.setBatchNo(misBean.getBatchnum());
// 單據狀態
tmpMisDetailDto.setBillState(misBean.getDjState());
// 單據類型
tmpMisDetailDto.setBillType(misBean.getDjlx());
// 卡名稱
tmpMisDetailDto.setCardName(misBean.getCardname());
// 卡類型
tmpMisDetailDto.setCardType(misBean.getCardtype());
// 收銀員號碼
tmpMisDetailDto.setCashierNo(misBean.getSyyh());
// 標記位
tmpMisDetailDto.setFlag(misBean.getFlg());
// 備註信息
tmpMisDetailDto.setMemo(misBean.getRspmsg());
// 商戶號
tmpMisDetailDto.setMerchantNo(misBean.getShnum());
// 商戶名稱
tmpMisDetailDto.setMerchantName(misBean.getShname());
// 原批次號
tmpMisDetailDto.setOldBatchNo(misBean.getOldbatchnum());
// 原流水號
tmpMisDetailDto.setOldPosTrace(misBean.getOldpostracenum());
// 原交易參考號
tmpMisDetailDto.setOldTrasRefNo(misBean.getStr10());
// 訂單號
tmpMisDetailDto.setOrderNo(misBean.getDh());
// 支付方式
tmpMisDetailDto.setPaymc(misBean.getStr2());
// 流水號
tmpMisDetailDto.setPosTrace(misBean.getPostracenum());
// 門店編碼
tmpMisDetailDto.setStoreId(misBean.getStr());
// tc
tmpMisDetailDto.settC(StringUtils.EMPTY);
// 卡-有效期
tmpMisDetailDto.setValidityDate(misBean.getYxq());
// 交易類型
tmpMisDetailDto.setTransType(misBean.getTranstype());
// 交易參考號
tmpMisDetailDto.setTransRefNo(misBean.getStr4());
// 卡號
tmpMisDetailDto.setTransCardNo(misBean.getTranscard());
//讓利金額
BigDecimal couponAmtAndAutoDisctAmt = BigDecimalUtils.add(BigDecimalUtils.dbConvertBd(2, misBean.getNum5()), BigDecimalUtils.dbConvertBd(2, misBean.getNum6()));
// 付款金額
tmpMisDetailDto.setTransAmount(BigDecimalUtils.add(misBean.getJyje(),couponAmtAndAutoDisctAmt));
// 終端號
tmpMisDetailDto.setTerminalNo(misBean.getZdnum());
// 已退款金額
// tmpMisDetailDto.setRefundMoney(misBean.getNum2());
//實付金額
tmpMisDetailDto.setRealPayAmt(misBean.getJyje());
//優惠券優惠金額
tmpMisDetailDto.setCouponAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum5()));
//自動折扣立減金額
tmpMisDetailDto.setAutoDisctAmt(BigDecimalUtils.dbConvertBd(2, misBean.getNum6()));
misList.add(tmpMisDetailDto);
}
}
}
public static MisTransInfoDto wrapMisTrans(DetailMisVo transDBModel) {
MisTransInfoDto misTrans = new MisTransInfoDto();
String transCard = transDBModel.getTranscard();
// 支付名稱 = 銀行卡名稱 + 卡號後4位
misTrans.setPaymc(transDBModel.getCardname()
+ StringUtils.right(transCard, 4));
if (transDBModel.getJyje() != null) {
BigDecimal jyje = transDBModel.getJyje();
misTrans.setPaymoney(jyje.setScale(2, BigDecimal.ROUND_UP).toString());
}
misTrans.setBatchnum(transDBModel.getBatchnum());
misTrans.setTranscard(transCard);
misTrans.setPostracenum(transDBModel.getPostracenum());
misTrans.setZdnum(transDBModel.getZdnum());
misTrans.setDjlx(transDBModel.getDjlx());
misTrans.setOldpostracenum(transDBModel.getOldpostracenum());
// 處理時間
if (null != transDBModel.getYhrq()) {
misTrans.setTransDate(transDBModel.getYhrq());
}
if (null != transDBModel.getYhsj()) {
misTrans.setTransTime(transDBModel.getYhsj());
}
misTrans.setShnum(transDBModel.getShnum());
misTrans.setStr6(transDBModel.getStr6());
return misTrans;
}
}
HTTPClient.java
@SuppressWarnings("deprecation")
public class HttpClientUtil {
private static final Logger LOGGER = Logger.getLogger(HttpClientUtil.class);
protected HttpClientUtil() {
}
public static String post(String url, Map<String, String> params, boolean isIgnoreCredential) {
DefaultHttpClient httpclient;
if (isIgnoreCredential) {
httpclient = (DefaultHttpClient) HttpsClient.newHttpsClient();
} else {
httpclient = new DefaultHttpClient();
}
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 900000000);
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 900000000);
httpclient.getParams().setParameter(CoreConnectionPNames.MAX_LINE_LENGTH, 0);
httpclient.getParams().setParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 81920);
httpclient.getParams().setParameter(CoreConnectionPNames.MAX_HEADER_COUNT, 0);
LOGGER.info("create httppost:" + url);
HttpPost post = postForm(url, params);
String body = invoke(httpclient, post);
httpclient.getConnectionManager().shutdown();
return body;
}
public static String get(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient();
LOGGER.info("create httpget:" + url);
HttpGet get = new HttpGet(url);
String body = invoke(httpclient, get);
httpclient.getConnectionManager().shutdown();
return body;
}
private static String invoke(DefaultHttpClient httpclient,
HttpUriRequest httpost) {
HttpResponse response = sendRequest(httpclient, httpost);
String body = paseResponse(response);
return body;
}
private static String paseResponse(HttpResponse response) {
LOGGER.info("Get response from http server...");
HttpEntity entity = response.getEntity();
LOGGER.info("response status: " + response.getStatusLine());
String charset = EntityUtils.getContentCharSet(entity);
LOGGER.info(charset);
String body = null;
try {
body = EntityUtils.toString(entity);
LOGGER.info(body);
} catch (ParseException | IOException e) {
LOGGER.error(e.getMessage(), e);
}
return body;
}
private static HttpResponse sendRequest(DefaultHttpClient httpclient,
HttpUriRequest httpost) {
LOGGER.info("execute post...");
HttpResponse response = null;
try {
response = httpclient.execute(httpost);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
return response;
}
private static HttpPost postForm(String url, Map<String, String> params) {
HttpPost httpost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Iterator<Entry<String, String>> it = params.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
try {
LOGGER.info("set utf-8 form entity to httppost");
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
}
return httpost;
}
}
public class CommonUtils {
/**
* 1:零售商商品編碼
*/
public static final int MER_CMMCODE_TYPE = 1;
/**
* 2:標品庫SKU編碼
*/
public static final int STAND_SKU_CMMCODE_TYPE = 2;
/**
* 3:標品庫SPU編碼
*/
public static final int STAND_SPU_CMMCODE_TYPE = 3;
/**
* 主站
*/
public static final int MAIN_SITE = 1;
/**
* 自有系統
*/
public static final int SELF_SITE = 2;
private CommonUtils() {
}
/**
* 功能描述: 生成商品編碼<br>
* 〈功能詳細描述〉 生成商品編碼
*
* @param codeType 生成編碼類型(1:零售商商品編碼 2:標品庫SKU編碼 3:標品庫SPU編碼)
* @return 商品編碼
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getCmmdtyCode(int codeType) {
String cmmdtyCode = "";
Long currentTime = Calendar.getInstance().getTime().getTime();
switch (codeType) {
case MER_CMMCODE_TYPE:
cmmdtyCode = "MER" + getRandom(0, 99999) + currentTime;
break;
case STAND_SKU_CMMCODE_TYPE:
cmmdtyCode = "SKU" + getRandom(0, 99999) + currentTime;
break;
case STAND_SPU_CMMCODE_TYPE:
cmmdtyCode = "SPU" + getRandom(0, 99999) + currentTime;
break;
default:
break;
}
return cmmdtyCode;
}
/**
* 功能描述: 獲取圖片路徑<br>
* 〈功能詳細描述〉 獲取圖片路徑
*
* @param source 圖片來源
* @return 圖片路徑
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getImageFolder(int source) {
String folder = StringUtils.EMPTY;
switch (source) {
case MAIN_SITE:
folder = File.separator + "b2c" + File.separator + "newcatentries" + File.separator;
break;
case SELF_SITE:
folder = File.separator + "sdipos" + File.separator + "catentries" + File.separator;
break;
default:
break;
}
return folder;
}
/**
* 功能描述: 獲取圖片URL路徑<br>
* 〈功能詳細描述〉 獲取圖片URL路徑
*
* @param source 圖片來源
* @return 圖片路徑
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getImageUrlFolder(int source) {
String folder = StringUtils.EMPTY;
switch (source) {
case MAIN_SITE:
folder = "/b2c/newcatentries/";
break;
case SELF_SITE:
folder = "/sdipos/catentries/";
break;
default:
break;
}
return folder;
}
/**
* 功能描述: 生成給定區間的隨機數<br>
* 〈功能詳細描述〉 生成給定區間的隨機數
*
* @param min 生成隨機數最小
* @param max 生成隨機數最大
* @return 隨機數
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static String getRandom(int min, int max) {
int size = Integer.toString(max).length();
int random = new Random().nextInt(max) % (max - min + 1) + min;
int zorecount = size - Integer.toString(random).length();
StringBuilder sb = new StringBuilder(StringUtils.EMPTY);
for (int i = 0; i < zorecount; i++) {
sb.append("0");
}
return sb.toString() + random;
}
/**
*
* 隨機獲取圖片服務器<br>
*
* @return
*/
public static String getRandomUimgServer() {
String uimgUrl = StaticVars.getInstance().getPropsValDefaultString("uimgUrl", "http://image.suning.cn");
String[] urlArr = uimgUrl.split(",");
int len = urlArr.length;
if (len > 1) {
int number = new Random().nextInt(len);
return urlArr[number] + "/uimg";
} else {
return uimgUrl + "/uimg";
}
}
}
MapUtils
public class MapUtils {
/**
* JavaBean 轉 Map
*/
public static Map<String, Object> beanToMap(Object object) throws IllegalAccessException {
Class cls = object.getClass();
Field[] fields = cls.getDeclaredFields();
Map<String, Object> map = new HashMap<String, Object>();
for (Field field : fields) {
field.setAccessible(true);
map.put(field.getName(), field.get(object));
}
return map;
}
/**
* Map 轉 JavaBean
*/
public static Object mapToBean(Map<String, Object> map, Class cls)
throws InstantiationException, IllegalAccessException, NoSuchFieldException {
Object object = cls.newInstance();
Iterator<Entry<String, Object>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Object> entry = it.next();
Field temFiels = cls.getDeclaredField(entry.getKey());
temFiels.setAccessible(true);
temFiels.set(object, entry.getValue());
}
return object;
}
}
解析文件攻擊類
public class ParseFileUtil {
/**
*日誌
*/
private static final Logger LOGGER = LoggerFactory.getLogger(ParseFileUtil.class);
/**
* 〈功能描述〉 解析易付寶賬務彙總查詢,返回list結果集
* 〈功能詳細描述〉
*
* @param array
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static List<EppStatementSummaryDTO> parseEppStatementSummaryFile(String result){
LOGGER.info("Enter parseEppStatementSummaryFile", result);
//返回結果列表
List<EppStatementSummaryDTO> dtos = new ArrayList<EppStatementSummaryDTO>();
String[] array = result.split("\n");
//循環讀取數組數據
for(int i=5;i<array.length-3;i++){
String str = array[i];
//將一行數據進行分割
String[] temp = str.split(",");
//將獲取到的信息,保存到對應的屬性中
EppStatementSummaryDTO dto = new EppStatementSummaryDTO();
//類型
dto.setType(temp[0].trim().replace("\t", ""));
//收入筆數
dto.setIncomeSum(Integer.parseInt(temp[1].trim().replace("\t", "")));
//收入金額(+元)
dto.setIncomeAmount(Double.parseDouble(temp[2].trim().replace("\t", "")));
//支出筆數
dto.setPaySum(Integer.parseInt(temp[3].trim().replace("\t", "")));
//支出金額(-元)
dto.setPayAmount(Double.parseDouble(temp[4].trim().replace("\t", "")));
//總金額(元)
dto.setTotalAmount(Double.parseDouble(temp[5].trim().replace("\t", "")));
//保存到list結果集中
dtos.add(dto);
}
LOGGER.info("Exit parseEppStatementSummaryFile", dtos);
return dtos;
}
/**
* 〈功能描述〉 解析易付寶賬務明細查詢,返回list結果集
* 〈功能詳細描述〉
*
* @param result
* @return
* @see [相關類/方法](可選)
* @since [產品/模塊版本](可選)
*/
public static List<EppStatementDetailDTO> parseEppStatementDetaiFile(String result){
LOGGER.info("Enter parseEppStatementDetaiFile", result);
//返回結果列表
List<EppStatementDetailDTO> dtos = new ArrayList<EppStatementDetailDTO>();
String[] array = result.split("\n");
//循環讀取數組數據
for(int i=5;i<array.length-3;i++){
String str = array[i];
//將一行數據進行分割
String[] temp = str.split(",");
//將獲取到的信息,保存到對應的屬性中
EppStatementDetailDTO dto = new EppStatementDetailDTO();
//類型
dto.setType(temp[0].trim().replace("\t", ""));
//賬務流水號
dto.setStatementNumber(temp[1].trim().replace("\t", ""));
//交易流水號
dto.setTransactionNumber(temp[2].trim().replace("\t", ""));
//商品訂單號
dto.setGoodsOrderNumber(temp[3].trim().replace("\t", ""));
//訂單創建時間
dto.setOrderCreateTime(temp[4].trim().replace("\t", ""));
//對方
dto.setOpposite(temp[5].trim().replace("\t", ""));
//訂單號
dto.setOrderNumber(temp[6].trim().replace("\t", ""));
//訂單名稱
dto.setOrderName(temp[7].trim().replace("\t", ""));
//收支時間
dto.setPaymentTime(temp[8].trim().replace("\t", ""));
//收入金額(+)元
dto.setIncomeAmount(Double.parseDouble(temp[9].trim().replace("\t", "")));
//支出金額(-)元
dto.setPayAmount(Double.parseDouble(temp[10].trim().replace("\t", "")));
//賬戶餘額(元)
dto.setAccountBalance(Double.parseDouble(temp[11].trim().replace("\t", "")));
//支付渠道
dto.setPaymentChannel(temp[12].trim().replace("\t", ""));
//備註
dto.setRemarks(temp[13].trim().replace("\t", ""));
//商戶訂單ID
dto.setMerchantOrderID(temp[14].trim().replace("\t", ""));
//保存到list結果集中
dtos.add(dto);
}
LOGGER.info("Exit parseEppStatementDetaiFile", dtos);
return dtos;
}
}
RSA.java
public class RSA {
private static final Logger LOGGER = LoggerFactory.getLogger(RSA.class);
public static void main(String[] args) throws IOException {
try {
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
gen.initialize(2048);
KeyPair pair = gen.generateKeyPair();
// rsa生成一對公私鑰
PublicKey publicKey = pair.getPublic();
PrivateKey privateKey = pair.getPrivate();
// SHA1withRSA算法進行簽名
Signature sign = Signature.getInstance("SHA1withRSA");
sign.initSign(privateKey);
byte[] data = "sss".getBytes();
// 更新用於簽名的數據
sign.update(data);
byte[] signature = sign.sign();
Signature verifySign = Signature.getInstance("SHA1withRSA");
verifySign.initVerify(publicKey);
// 用於驗籤的數據
verifySign.update(data);
boolean flag = verifySign.verify(signature);
System.out.println(flag);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
}
RSAUtils.java
public class RSAUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(RSAUtils.class);
public static final String MAP_KEY_PUBLIC_KEY = "publicKey";
public static final String MAP_KEY_PRIVATE_KEY = "privateKey";
public static Map<String, String> generateRSAKeyPlain() {
Map<String, String> map = new HashMap<String, String>();
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyPairGenerator.generateKeyPair();
map.put(MAP_KEY_PUBLIC_KEY, getPublicKeyPlain(keyPair.getPublic()));
map.put(MAP_KEY_PRIVATE_KEY, getPrivateKeyPlain(keyPair.getPrivate()));
} catch (NoSuchAlgorithmException e) {
LOGGER.error("無此算法", e);
}
return map;
}
public static byte[] encrypt(PublicKey publicKey, byte[] data)
throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, BadPaddingException {
return encrypt(publicKey, "RSA/ECB/PKCS1Padding", data);
}
public static byte[] encrypt(PublicKey publicKey, String transformation, byte[] data)
throws InvalidKeyException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, BadPaddingException {
Cipher cipher;
try {
cipher = Cipher.getInstance(transformation);
cipher.init(1, publicKey);
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(data.length);
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1 : data.length / blockSize;
int i = 0;
byte[] raw = new byte[outputSize * blocksSize];
while (data.length - i * blockSize > 0) {
if (data.length - i * blockSize > blockSize) {
cipher.doFinal(data, i * blockSize, blockSize, raw, i * outputSize);
} else {
cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * outputSize);
}
i++;
}
return raw;
} catch (IllegalBlockSizeException e) {
LOGGER.error(e.getMessage(), e);
throw new IllegalBlockSizeException(e.getMessage());
} catch (NoSuchAlgorithmException e) {
LOGGER.error(e.getMessage(), e);
throw new NoSuchAlgorithmException(e.getMessage());
} catch (NoSuchPaddingException e) {
LOGGER.error(e.getMessage(), e);
throw new NoSuchPaddingException(e.getMessage());
} catch (ShortBufferException e) {
LOGGER.error(e.getMessage(), e);
throw new ShortBufferException(e.getMessage());
} catch (BadPaddingException e) {
LOGGER.error(e.getMessage(), e);
throw new BadPaddingException(e.getMessage());
} catch (InvalidKeyException e) {
LOGGER.error(e.getMessage(), e);
throw new InvalidKeyException(e.getMessage());
}
}
public static boolean verify(PublicKey publicKey, String algorithm, byte[] data, byte[] signData)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
Signature signature = Signature.getInstance(algorithm);
signature.initVerify(publicKey);
signature.update(data);
return signature.verify(signData);
}
public static byte[] sign(PrivateKey privateKey, byte[] data, String algorithm)
throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
Signature signature = Signature.getInstance(algorithm);
signature.initSign(privateKey);
signature.update(data);
return signature.sign();
}
public static String getPublicKeyPlain(PublicKey publicKey) {
byte[] pbk = publicKey.getEncoded();
return Base64.encodeToString(pbk, 0);
}
public static String getPrivateKeyPlain(PrivateKey privateKey) {
byte[] prk = privateKey.getEncoded();
return Base64.encodeToString(prk, 0);
}
public static PublicKey loadPublicKey(String publicKeyStr) {
PublicKey publicKey = null;
try {
byte[] buffer = Base64.decode(publicKeyStr, 0);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
publicKey = keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
LOGGER.error("無此算法", e);
} catch (InvalidKeySpecException e) {
LOGGER.error("非法公鑰", e);
}
return publicKey;
}
public static PrivateKey loadPrivateKey(String privateKeyStr) {
PrivateKey privateKey = null;
try {
byte[] buffer = Base64.decode(privateKeyStr, 0);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
privateKey = keyFactory.generatePrivate(keySpec);
} catch (NoSuchAlgorithmException e) {
LOGGER.error("無此算法", e);
} catch (InvalidKeySpecException e) {
LOGGER.error("非法私鑰", e);
}
return privateKey;
}
}
SuningPayUtils
public class SuningPaySignUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(SuningPaySignUtil.class);
private static Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
private SuningPaySignUtil() {
}
public static Map<String, String> initParams(Object obj, Date date, String version, String notifyUrl) {
Map<String, String> params = Maps.newHashMap();
try {
Field[] fields = obj.getClass().getSuperclass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (!"serialVersionUID".equals(fields[i].getName())) {
fields[i].setAccessible(true);
if (null != fields[i].get(obj)) {
if (fields[i].get(obj) instanceof List) {
params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), List.class));
} else if (fields[i].getType().getName().startsWith("com.suning")) {
params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), fields[i].getType()));
} else {
params.put(fields[i].getName(), fields[i].get(obj).toString());
}
}
}
}
fields = obj.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (!"serialVersionUID".equals(fields[i].getName())) {
fields[i].setAccessible(true);
if (null != fields[i].get(obj)) {
if (fields[i].get(obj) instanceof List) {
params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), List.class));
} else if (fields[i].getType().getName().startsWith("com.suning")) {
params.put(fields[i].getName(), gson.toJson(fields[i].get(obj), fields[i].getType()));
} else {
if ("storeCode".equals(fields[i].getName())) {
continue;
}
params.put(fields[i].getName(), fields[i].get(obj).toString());
}
}
}
}
params.put("publicKeyIndex", "0001"); // 公鑰索引
params.put("inputCharset", "UTF-8"); // 編碼類型
params.put("merchantNo", "70056371"); // 系統接入方
params.put("submitTime", DateUtil.format(date, "yyyyMMddHHmmss")); // 提交時間
if (StringUtils.isNotBlank(version)) {
params.put("version", version);
}
if (StringUtils.isNotBlank(notifyUrl)) {
params.put("notifyUrl", notifyUrl);
}
initSigns(params);
} catch (IllegalArgumentException e) {
LOGGER.error("IllegalArgumentException", e);
} catch (IllegalAccessException e) {
LOGGER.error("IllegalAccessException", e);
} catch (SecurityException e) {
LOGGER.error("SecurityException", e);
}
return params;
}
private static void initSigns(Map<String, String> map) {
List<Map.Entry<String, String>> list = new ArrayList();
list.addAll(map.entrySet());
KeyComparator kc = new KeyComparator();
Collections.sort(list, kc);
StringBuilder sb = new StringBuilder();
for (Iterator<Map.Entry<String, String>> it = list.iterator(); it.hasNext();) {
Map.Entry<String, String> mapEntity = it.next();
sb.append(mapEntity.getKey()).append("=").append(mapEntity.getValue()).append("&");
}
String str = sb.toString();
String waitSignStr = str.endsWith("&") ? str.substring(0, str.length() - 1) : str;
String sign = null;
String signAlgorithm = null;
try {
sign = SignatureUtil.sign((MD5Util.Md5(waitSignStr)).toUpperCase(), ScmConfUtil.getInstance().getString("PAY_PRIVATEKEY", ""));
signAlgorithm = "RSA";
} catch (Exception e) {
LOGGER.error("Init signs error.", e);
}
map.put("signature", sign); // 簽名
map.put("signAlgorithm", signAlgorithm); // 簽名算法
}
static class KeyComparator implements Comparator<Map.Entry<String, String>> {
@Override
public int compare(Map.Entry<String, String> m, Map.Entry<String, String> n) {
return m.getKey().compareTo(n.getKey());
}
}
}
銀聯支付:
public class UnionPaySignUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(UnionPaySignUtil.class);
private UnionPaySignUtil() {
}
public static String signData(String privateKeyStr, String content) throws UnsupportedEncodingException {
PrivateKey privateKey = RSAUtils.loadPrivateKey(privateKeyStr);
byte[] data = new byte[0];
try {
data = content.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
}
String signature = null;
try {
byte[] signData = RSAUtils.sign(privateKey, data, "SHA1withRSA");
signature = Base64.encodeToString(signData, 6);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return signature;
}
public static boolean verifySignData(String publicKeyStr, String content, String signData) {
PublicKey publicKey = RSAUtils.loadPublicKey(publicKeyStr);
byte[] data = new byte[0];
try {
data = content.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
}
boolean valid = false;
try {
byte[] sign = Base64.decode(signData, 4);
valid = RSAUtils.verify(publicKey, "SHA1withRSA", data, sign);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
}
return valid;
}
}