Helius
2021-08-05 fdb91cc72f7cbe8c095a1ce6442c9259ff01ff06
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.xzx.gc.common.utils;
 
import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.emoji.EmojiUtil;
import cn.hutool.json.JSONUtil;
import com.xzx.gc.common.constant.CommonEnum;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.QueueEnum;
import com.xzx.gc.common.constant.RedisKeyConstant;
import com.xzx.gc.common.dto.log.ConsoleContentDto;
import com.xzx.gc.common.dto.log.ConsoleDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
 
@Component
@Slf4j
public class BusinessUtil {
 
    @Autowired
    private RedisUtil redisUtil;
 
    private static final int REPEAT_COUNT=30;
 
    /**
     * 保存登录信息到redis
     * @param ip
     * @param userId
     */
    public void saveLoginInfoToRedis(String ip,String userId){
        //记录最后1次登录时间
        String format = DateUtil.format(new Date(), DateUtils.DATE_FORMAT_YMDHMS);
        Dict set = Dict.create().set("time", format).set("ip", ip);
        String s = JSONUtil.toJsonStr(set);
        if(!redisUtil.hexists(RedisKeyConstant.USER_LOGIN_INFO,userId)) {
            redisUtil.hset(RedisKeyConstant.USER_LOGIN_INFO, userId, s);
        }
    }
 
 
    /**
     * 手机号脱敏
     * @param mobile
     * @return
     */
    public String changeMobile(String mobile){
        if(StrUtil.isNotBlank(mobile)&&mobile.length()==11){
            String replace = StrUtil.replace(mobile, 3, 7, Convert.toChar("*"));
            return replace;
        }
        return mobile;
    }
 
 
    /**
     * 获取真实版本号
     * @param version
     * @return
     */
    public Double getVersion(String version){
        if(StrUtil.isNotBlank(version)) {
            return Convert.toDouble(StrUtil.removePrefix(version, "v"));
        }
        return null;
    }
    /**
     * 是否要加密
     * @param version 版本号
     * @return
     */
    public boolean isAuth(String version){
//        if(StrUtil.isNotBlank(version)&&version.startsWith("v")) {
//            double versionDouble = 0;
//            try {
//                versionDouble = Convert.toDouble(StrUtil.removePrefix(version,"v"));
//            } catch (Exception e) {
//               return false;
//            }
//            if (NumberUtil.compare(versionDouble, Convert.toDouble("1.2")) > 0) {
//                return true;
//            }
//        }
//        return false;
        return true;
    }
 
 
    /**
     * 是否验证的url地址
     * @param url
     * @return
     */
    public boolean isAuthUrl(String url){
        List<String> noAuthUrls = Constants.NO_AUTH_URLS;
        for (String noAuthUrl : noAuthUrls) {
            if(url.contains(noAuthUrl)){
                return false;
            }
        }
        return true;
    }
 
    /**
     * 是否验证的方法名称
     * @param url
     * @return
     */
    public boolean isAuthMethod(String url){
        List<String> noAuthUrls = Constants.NO_AUTH_METHODS;
        for (String noAuthUrl : noAuthUrls) {
            if(url.equals(noAuthUrl)){
                return false;
            }
        }
        return true;
    }
 
    /**
     * 格式化输入请求参数
     * @param getArgs
     * @return
     */
    public String prettyInParam( Object[] getArgs){
        List<Object> objects = new ArrayList<>(Arrays.asList(getArgs));
        String param="{}";
        if(CollUtil.isNotEmpty(objects)) {
            objects.removeIf(next -> next instanceof HttpServletRequest);
            if(objects.size()==0){
                return param;
            }else if(objects.size()==1){
                if(objects.get(0)==null){
                    return param;
                }else {
                    if (objects.get(0).toString().startsWith("<xml>") && (objects.get(0).toString().endsWith("</xml>"))) {
                        return objects.get(0).toString();
                    } else {
                        return JSONS.format(objects.get(0), -1, -1);
                    }
                }
            }else{
                return JSONS.format(objects,-1,-1);
            }
        }
        return  param;
 
    }
 
    /**
     * 格式化输出输出参数
     * @param result
     * @return
     */
    public String prettyOut(  Object result){
        if(result!=null) {
            try {
                return JSONS.format(result,-1,-1);
            } catch (Exception e) {
                return result.toString();
            }
        }
        return "";
    }
 
 
    public String changeArea(String townName){
        if("宁乡县".equals(townName)){
            return "宁乡市";
        }
        return townName;
    }
 
    /**
     * 格式化头部
     * @return
     */
    public String prettyHeader( Map<String, String> headerMaps){
        Dict dict=Dict.create();
        //键全变成了小写
        if(MapUtil.isNotEmpty(headerMaps)){
//            dict.set("token",headerMaps.get("token")).set("timestamp",headerMaps.get("timestamp")).set("nonce",headerMaps.get("nonce")).set("sign",headerMaps.get("sign"))
//                  .set("userId",headerMaps.get("userId")).set("authKey",headerMaps.get("authkey")).set("version",headerMaps.get("version"));
            dict.set("userId",headerMaps.get("userId")).set("version",headerMaps.get("version"));
        }
        return JSONS.format(dict,-1,-1);
    }
 
 
    /**
     * 获取访问路径
     * @return
     */
    public String getViewUrl(){
        String urlPrefix=null;
        if(SpringUtil.isProdOrCloud()){
            urlPrefix=Constants.IMG_VIEW_PATH;
        }else if(SpringUtil.isTest()){
            urlPrefix="http://192.168.0.100/resource/static/upload";
        }else if(SpringUtil.isCheck()){
            urlPrefix="https://wxtest.cnxzx.com/resource/static/upload";
        } else if (SpringUtil.isXc()) {
            urlPrefix="https://xcfiletest.cnxzx.com/static/upload";
        }else{
//            urlPrefix="http://192.168.0.18/static/upload";
            urlPrefix="http://120.27.238.55:8002/static/upload";
        }
        return urlPrefix;
    }
 
 
    /**
     * 处理金额 保留2位小数
     * @param s
     * @return
     */
    public String changeMoney(String s){
        if(StrUtil.isBlank(s)){
            s=Convert.toStr(Constants.MONEY_INIT);
        }
        return NumberUtil.roundStr(s, 2, RoundingMode.DOWN);
    }
 
    public String changeMoney(BigDecimal s){
        if(s==null){
            s= Constants.MONEY_INIT;
        }
        String money=s.toString();
        return NumberUtil.roundStr(money, 2, RoundingMode.DOWN);
    }
 
    /**
     * 处理重量 保留3位小数
     * @param s
     * @return
     */
    public String changeWeight(String s){
        if(StrUtil.isBlank(s)){
            s=Convert.toStr(Constants.WEIGHT_INIT);
        }
        return NumberUtil.roundStr(s, 3, RoundingMode.DOWN);
    }
 
 
 
 
    /**
     * 打印
     * @param
     * @return
     */
    public  String console(ConsoleDto consoleDto){
        String laughing = EmojiUtil.toUnicode(":"+consoleDto.getEmjio()+":");
        StrBuilder strBuilder = StrBuilder.create("   "+consoleDto.getHeader()+":\n\n");
        strBuilder.append("┏"+StrUtil.repeat("━",REPEAT_COUNT)+" "+laughing+consoleDto.getTitle()+"请求开始"+laughing+" "+StrUtil.repeat("━",REPEAT_COUNT)+"\n");
        for (ConsoleContentDto consoleContentDto : consoleDto.getList()) {
            strBuilder.append("┣ "+consoleContentDto.getName()+":   "+consoleContentDto.getValue()+" \n");
        }
        strBuilder.append("┗"+StrUtil.repeat("━",REPEAT_COUNT)+" "+laughing+consoleDto.getTitle()+"请求结束"+laughing+" "+StrUtil.repeat("━",REPEAT_COUNT)+"\n");
        return strBuilder.toString();
    }
 
    public String checkProject(String url){
        String s = StrUtil.removePrefix(url, "/");
        String s1 = StrUtil.removePrefix(s, "/");
        if(s1.startsWith("gc-user")){
            return "用户服务";
        }else if(s1.startsWith("gc-pay")){
            return "支付服务";
        }else if(s1.startsWith("gc-order")){
            return "订单服务";
        }else if(s1.startsWith("gc-sys")){
            return "消息服务";
        }else if(s1.startsWith("gc-role")){
            return "权限服务";
        }else {
            return "服务";
        }
    }
 
 
 
 
    /**
     * 获取设备类型名称
     * @param clientType
     * @return
     */
    public  String getClientTypeName(String clientType){
        if(StringUtils.isNotBlank(clientType)){
            if("1".equals(clientType)){
                clientType="苹果";
            }else if("2".equals(clientType)) {
                clientType = "安卓";
            } else if("3".equals(clientType)){
                clientType="WEB";
            }
        }else{
            clientType="未知";
        }
        return clientType;
    }
 
    /**
     * 是否是web请求
     * @param clientType  客户端类型
     * @return
     */
    public   boolean isWeb(String clientType){
        if(CommonEnum.WEB.getValue().equals(clientType)){
            return true;
        }else {
            return false;
        }
    }
 
    /**
     * 是否是App请求
     * @param clientType  客户端类型
     * @return
     */
    public   boolean isApp(String clientType){
        if(CommonEnum.安卓.getValue().equals(clientType)||CommonEnum.苹果.getValue().equals(clientType)){
            return true;
        }else {
            return false;
        }
    }
 
    /**
     * 输出
     * @param title
     * @return
     */
    public String repeatService(String title){
        return StrUtil.repeat("*",30)+title+StrUtil.repeat("*",30);
    }
 
 
    /**
     * 队列输出
     * @param queueName
     * @param uuid
     * @param errMsg
     */
    public void queueConsole(String queueName,String uuid,String errMsg){
        ConsoleDto consoleDto=new ConsoleDto();
        consoleDto.setEmjio("snake");
        consoleDto.setTitle("队列发送");
        consoleDto.setHeader("您有新的队列");
        List<ConsoleContentDto> list=CollUtil.newArrayList();
 
        ConsoleContentDto consoleContentDto=new ConsoleContentDto();
        consoleContentDto.setName("队列标识");
        consoleContentDto.setValue(uuid);
        list.add(consoleContentDto);
 
        consoleContentDto=new ConsoleContentDto();
        consoleContentDto.setName("队列名称");
        consoleContentDto.setValue(queueName);
        list.add(consoleContentDto);
 
        consoleContentDto=new ConsoleContentDto();
        consoleContentDto.setName("队列描述");
        consoleContentDto.setValue(QueueEnum.getDescByName(queueName));
        list.add(consoleContentDto);
 
        if(StrUtil.isNotBlank(errMsg)) {
            consoleContentDto = new ConsoleContentDto();
            consoleContentDto.setName("异常信息");
            consoleContentDto.setValue(errMsg);
            list.add(consoleContentDto);
        }
 
        consoleContentDto = new ConsoleContentDto();
        consoleContentDto.setName("发送状态");
        if(StrUtil.isBlank(errMsg)){
            consoleContentDto.setValue("发送成功!");
        }else{
            consoleContentDto.setValue("发送失败!");
        }
        list.add(consoleContentDto);
 
        consoleDto.setList(list);
//        String console = console(consoleDto);
//        log.debug(console);
    }
 
 
 
    public BigDecimal changeMul(BigDecimal stepMulForReceiver){
        if(stepMulForReceiver==null){
            stepMulForReceiver=BigDecimal.ZERO;
        }else{
            stepMulForReceiver=Convert.toBigDecimal(changeMoney(Convert.toStr(stepMulForReceiver,"0")));
        }
        return stepMulForReceiver;
    }
 
    public BigDecimal changeMul(String stepMulForReceiver){
            BigDecimal mul=StrUtil.isNotBlank(stepMulForReceiver)?Convert.toBigDecimal(stepMulForReceiver):Constants.MONEY_INIT;
            return NumberUtil.round(mul, 2, RoundingMode.DOWN);
    }
 
 
    /**
     * 是否是后台请求
     * @param url
     * @return
     */
    public boolean isAdmin(String url){
        return url.contains("/admin");
    }
 
 
    /**
     * 计算日环比
     * @return
     */
    public String getRatio(BigDecimal yesterday,BigDecimal today){
        //回收日环比计算方法: (本日回收金额/昨日回收金额 — 1 )× 100%  正值为增长率  负值为下降率
        String recyleRatio="";
        if (yesterday.compareTo(BigDecimal.ZERO) != 0) {
            BigDecimal mul = NumberUtil.mul(NumberUtil.sub(NumberUtil.div(today, yesterday), 1), 100);
            if(mul.compareTo(BigDecimal.ZERO)>=0){
                recyleRatio = recyleRatio+"+";
            }
            recyleRatio =recyleRatio+ changeMoney(mul) + "%";
        } else {
            recyleRatio = "+100.00%";
        }
        return recyleRatio;
    }
 
 
 
    /**
     * 是否是特别的区域 主要指1个区多合伙人
     * @param townShipId
     * @return
     */
    public boolean isSpecialArea(String townShipId){
        if(Constants.YUELU_CODE.equals(townShipId)){
            return true;
        }else if(Constants.YUHUA_CODE.equals(townShipId)){
            return true;
        }
        return false;
    }
 
    /**
     * 否是特别的区域 根据adoce查找。主要指1个区多合伙人
     * @param adcode
     * @return
     */
    public boolean isSpecialAreaByAdcode(String adcode){
        if(Constants.YUELU_ADCODE.equals(adcode)){
            return true;
        }else if(Constants.YUHUA_ADCODE.equals(adcode)){
            return true;
        }
        return false;
    }
 
    /**
     * 检查验证码
     * @param code
     * @param phone
     * @return
     */
    public boolean checkSmsCode(String code,String phone){
        String s = redisUtil.get(RedisKeyConstant.SMS_CODE + phone);
        if(StrUtil.isNotBlank(s)&&StrUtil.isNotBlank(code)&&s.equals(code)){
            return true;
        }
        return false;
    }
 
 
    public static void main(String[] args) {
    }
 
}