whycxzp
2025-04-24 0930471152e0f96d26666006a13ef712ef006a63
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
package com.whyc.util;
 
import com.whyc.constant.YamlProperties;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Component;
 
import java.io.File;
 
/**
 * 通用工具列
 */
@Component
public class CommonUtil {
 
 
    public static String classesPath(){
        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
        File jarFile = applicationHome.getDir();
        return jarFile.toString();
    }
 
    /**
     * 获取存放文件的根路径
     * @return 返回存放文件的根路径
     */
    public static String getRootFile(){
        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
        File jarFile = applicationHome.getDir();
        String baseDirPath;
        if(YamlProperties.runModel == 1) {
            //开发路径
            baseDirPath = jarFile.getParentFile().toString()+File.separator+"battery_gwm_file"+File.separator;
        }else {
            //打包路径
            baseDirPath = jarFile.toString()+File.separator+"battery_gwm_file"+File.separator;
        }
        return baseDirPath;
    }
 
    /**
     * 获取项目所在文件夹路径
     * @return 获取项目所在文件夹路径
     */
    public static String getProjectDir(){
        ApplicationHome applicationHome = new ApplicationHome(CommonUtil.class);
        File jarFile = applicationHome.getDir();
        String baseDirPath;
        if(YamlProperties.runModel == 1) {
            //开发路径
            baseDirPath = jarFile.getParentFile().toString();
        }else {
            //打包路径
            baseDirPath = jarFile.toString();
        }
        return baseDirPath;
    }
 
 
    /**
     * 物料编码自动填充补齐
     * @param code
     * @return
     */
    public static String codeAutoFill(String code){
        int length = code.length();
        if (length == 9) {
            code = "0" + code;
        } else if (length < 9) {
            int centerZeroNum = 10 - length - 1;
            StringBuilder centerZeroStr = new StringBuilder();
            for (int j = 0; j < centerZeroNum; j++) {
                centerZeroStr.append("0");
            }
            String strFront = code.substring(0, 3);
            String strBehind = code.substring(3);
            code = "0" + strFront + centerZeroStr + strBehind;
        }
        return code;
    }
 
}