package com.qxueyou.scc.base.util;
|
|
import java.math.BigDecimal;
|
import java.math.BigInteger;
|
import java.math.MathContext;
|
import java.text.DecimalFormat;
|
import java.util.Random;
|
import java.util.regex.Pattern;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.web.servlet.ModelAndView;
|
|
import com.qxueyou.scc.exercise.model.ExerciseItemAnswerU;
|
|
public class CommonUtils {
|
/** 一位小数 */
|
public static final String PARTEN_ONE = "#.#";
|
/** 二位小数 */
|
public static final String PARTEN_TWO = "#.##";
|
|
//地球半径
|
private static final double EARTH_RADIUS = 6378.137;
|
|
/**
|
* 答案显示图形颜色 自定义
|
* @return
|
*/
|
public static String[] getColors(){
|
String[] colors = new String[]{"#4572a7","#aa4643","#89a54e","#80699b","#3d96ae","#a56f8f","#9f7961","#6f83a5"};
|
|
return colors;
|
}
|
|
/**
|
* 课程表显示图形颜色 自定义
|
* @return
|
*/
|
public static String[] getRGBColors(){
|
String[] colors = new String[]{"21,199,165","130,191,173","155,238,222","178,235,202","210,210,174","205,186,117",
|
"220,205,166","233,174,160","240,94,104","169,169,169"};
|
|
return colors;
|
}
|
|
/**
|
* 换算为2位小数
|
* @return
|
*/
|
public static String formatDecimalFormat(Object obj, String parten){
|
DecimalFormat decimal = new DecimalFormat(parten);
|
return decimal.format(obj);
|
}
|
|
/**
|
* 删除字符最后的符号
|
* @return
|
*/
|
public static String removeStrLastComma(String string){
|
String str = string;
|
if(StringUtils.isBlank(str)){
|
return str;
|
}
|
String lastStr = str.substring(str.length()-1);
|
|
if(lastStr.indexOf(',') > -1){
|
str = str.substring(0, str.length()-1);
|
}
|
|
return str;
|
}
|
|
/**
|
* 转换答案值 0,1,2
|
* @param correct
|
* @return
|
*/
|
public static byte parseInteractCorrectValue(String correct){
|
byte value;
|
|
if("true".equals(correct)){
|
value=ExerciseItemAnswerU.CORRECT_RIGHT;//正确
|
}else if("false".equals(correct)){
|
value=ExerciseItemAnswerU.CORRECT_ERROR;//错误
|
}else{
|
value=7;//未知
|
}
|
|
return value;
|
}
|
|
/**
|
* // 进行加法运算
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double add(double d1, double d2){
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.add(b2).doubleValue();
|
}
|
|
/**
|
* // 进行减法运算
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double subtract(double d1, double d2){
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.subtract(b2).doubleValue();
|
}
|
|
/**
|
* // 进行乘法运算
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double multiply(double d1, double d2){
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.multiply(b2).doubleValue();
|
}
|
|
/**
|
* // 进行除法运算 四舍五入
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double divide(double d1,double d2,int len) {// 进行除法运算
|
BigDecimal b1 = new BigDecimal(String.valueOf(d1));
|
BigDecimal b2 = new BigDecimal(String.valueOf(d2));
|
|
return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
|
}
|
|
/**
|
* // 进行除法运算 四舍五入
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static double divide(String d1,String d2,int len) {// 进行除法运算
|
BigDecimal b1 = new BigDecimal(d1);
|
BigDecimal b2 = new BigDecimal(d2);
|
|
return b1.divide(b2,len,BigDecimal.ROUND_HALF_UP).doubleValue();
|
}
|
|
|
/**
|
* // 进行除法运算 四舍五入
|
* @param d1
|
* @param d2
|
* @return
|
*/
|
public static BigDecimal divide(BigDecimal b1,BigDecimal b2,int len) {// 进行除法运算
|
|
return b1.divide(b2, new MathContext(len));
|
}
|
public CommonUtils() {
|
// TODO Auto-generated constructor stub
|
}
|
/*
|
*
|
* 按指定长度截取源字符串,若源字符串不大于指定长度,则原样返回
|
*/
|
public static String cutString(String res, Integer length, String suffix) {
|
if (StringUtils.isEmpty(res)) {
|
return "";
|
}
|
if (res.length() <= length) {
|
return res;
|
}
|
return res.substring(0, length) + (suffix == null ? "" : suffix);
|
}
|
|
/**
|
* 生成流水号 : module+时间戳 + 5位随机数,长度20位
|
*/
|
public static String generateNo(String module){
|
|
Random rand = new Random();
|
String random = String.valueOf(rand.nextInt(100000));
|
int index = 0;
|
while( random.length() < 5 && index < 5 ){
|
index++;
|
random = "0".concat(random);
|
}
|
|
return module + System.currentTimeMillis() + random ;
|
}
|
|
/**
|
* 计算单位数字
|
*
|
* @param number
|
* @return
|
*/
|
public static String getNumberByUnit(BigInteger number){
|
|
if(number == null){
|
return "0";
|
}
|
|
if(number.compareTo(new BigInteger("10000")) < 0){// 小于1万
|
return String.valueOf(number);
|
}
|
|
if(number.compareTo(new BigInteger("10000")) >= 0
|
&& number.compareTo(new BigInteger("100000000")) < 0){// 大于1万 小于一亿 前进1位为1.2万
|
|
return Math.ceil(number.doubleValue()/1000)/10 + "万";
|
|
}
|
|
if(number.compareTo(new BigInteger("100000000")) >= 0){// 大于一亿 前进1位为1.2亿
|
|
return Math.ceil(number.doubleValue()/1000000)/10 + "亿";
|
}
|
|
return "0";
|
}
|
|
/**
|
* 格式化文本
|
* @param str
|
* @return
|
*/
|
public static String formatText(String str){
|
String text = str;
|
if (StringUtils.isNotBlank(str)) {
|
text = text.replace("\n", "<br/>");
|
}
|
return text;
|
}
|
|
//
|
private static double rad(double d){
|
return d * Math.PI / 180.0;
|
}
|
|
//获取两个坐标距离
|
public static double GetDistance(double long1, double lat1, double long2, double lat2) {
|
double a, b, d, sa2, sb2;
|
double latA = rad(lat1);
|
double latB = rad(lat2);
|
a = latA - latB;
|
b = rad(long1 - long2);
|
|
sa2 = Math.sin(a / 2.0);
|
sb2 = Math.sin(b / 2.0);
|
d = 2 * EARTH_RADIUS
|
* Math.asin(Math.sqrt(sa2 * sa2 + Math.cos(latA)
|
* Math.cos(latB) * sb2 * sb2));
|
return d;
|
}
|
|
/**
|
* 银行卡验证
|
*
|
* @param cardNo
|
* @return
|
*/
|
public static boolean isBankCard(String cardNo){
|
return Pattern.matches("^\\d{8,27}$", cardNo);
|
}
|
|
/**
|
* ftl-返回错误信息页面
|
* @param mv
|
* @param msg
|
* @return
|
*/
|
public static ModelAndView resultDeletePage(ModelAndView mv,String msg){
|
mv = new ModelAndView("/deletePage");
|
mv.addObject("content", msg);
|
return mv;
|
}
|
|
/** 得到classId*/
|
public static String getClassId(String classId){
|
return StringUtils.isEmpty(classId)?(StringUtils.isEmpty(ClientUtils.getClassId())?StringUtils.EMPTY:ClientUtils.getClassId()):classId;
|
}
|
}
|