派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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;
    }
}