UnitCodeUtil.java 6.69 KB
Newer Older
hkl's avatar
hkl committed
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
package org.jeecg.modules.utils;

import cn.hutool.extra.pinyin.PinyinUtil;
import org.jeecg.modules.deviceAsset.entity.CurveManagement;
import org.jeecg.modules.deviceAsset.entity.RailManagement;
import org.jeecg.modules.deviceAsset.entity.SwitchManagement;
import org.jeecg.modules.deviceAsset.entity.VerticalCurveManagement;
import org.jeecg.modules.deviceAsset.service.ICurveManagementService;
import org.jeecg.modules.deviceAsset.service.IRailManagementService;
import org.jeecg.modules.deviceAsset.service.ISwitchManagementService;
import org.jeecg.modules.deviceAsset.service.IVerticalCurveManagementService;
import org.jeecg.modules.subwayNetwork.entity.LineAlias;
import org.jeecg.modules.subwayNetwork.service.ILineAliasService;

/**
 * @describe:单元编码生成工具
 */
public class UnitCodeUtil {

    private static ILineAliasService lineAliasService;

    private static ISwitchManagementService switchManagementService;

    private static ICurveManagementService curveManagementService;

    private static IVerticalCurveManagementService verticalCurveManagementService;

    private static IRailManagementService railManagementService;

    public static void setService(ILineAliasService lineAliasService,
                                  ISwitchManagementService switchManagementService,
                                  ICurveManagementService curveManagementService,
                                  IVerticalCurveManagementService verticalCurveManagementService,
                                  IRailManagementService railManagementService) {
        UnitCodeUtil.lineAliasService = lineAliasService;
        UnitCodeUtil.switchManagementService = switchManagementService;
        UnitCodeUtil.curveManagementService = curveManagementService;
        UnitCodeUtil.verticalCurveManagementService = verticalCurveManagementService;
        UnitCodeUtil.railManagementService = railManagementService;
    }

    /**
     * 钢轨单元编码:线别码+GG+流水号(5位流水号)
hkl's avatar
hkl committed
44 45 46
     * 实例:SXXGG00001
     * GG - 钢轨编码
     * 0001-流水号
hkl's avatar
hkl committed
47 48 49 50 51 52
     *
     * @param lineId 线别ID
     */
    public static String railUnitCodeUtil(String lineId) {
        // 获取线别拼音
        LineAlias lineAlias = lineAliasService.getById(lineId);
hkl's avatar
hkl committed
53
        String lineAliasPinyin = PinyinUtil.getFirstLetter(lineAlias.getLineAliasName(), "").toUpperCase();
hkl's avatar
hkl committed
54 55 56


        // 查询最大的编号
hkl's avatar
hkl committed
57
        String unitCode = lineAliasPinyin + "GG";
hkl's avatar
hkl committed
58 59 60 61 62 63 64 65 66 67 68
        RailManagement record = railManagementService.lambdaQuery()
                .likeRight(RailManagement::getUnitCode, unitCode)
                .orderByDesc(RailManagement::getUnitCode)
                .last("limit 1")
                .one();


        // 流水号初始化为1
        Integer serialNum = 1;
        if (null != record && record.getUnitCode() != null) {
            // 流水号+1
hkl's avatar
hkl committed
69
            serialNum = Integer.parseInt(record.getUnitCode().substring(record.getUnitCode().length() - 4)) + 1;
hkl's avatar
hkl committed
70 71 72 73 74 75 76 77
        }
        String serialNumStr = supplementZero(4, serialNum);

        return unitCode + serialNumStr;
    }

    /**
     * SQX - 竖曲线
hkl's avatar
hkl committed
78
     * 0001-流水号
hkl's avatar
hkl committed
79 80 81 82 83 84
     *
     * @param lineId 线别ID
     */
    public static String verticalCurveUnitCodeUtil(String lineId) {
        // 获取线别拼音
        LineAlias lineAlias = lineAliasService.getById(lineId);
hkl's avatar
hkl committed
85
        String lineAliasPinyin = PinyinUtil.getFirstLetter(lineAlias.getLineAliasName(), "").toUpperCase();
hkl's avatar
hkl committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100


        // 查询最大的编号
        String unitCode = lineAliasPinyin + "SQX";
        VerticalCurveManagement record = verticalCurveManagementService.lambdaQuery()
                .likeRight(VerticalCurveManagement::getUnitCode, unitCode)
                .orderByDesc(VerticalCurveManagement::getUnitCode)
                .last("limit 1")
                .one();


        // 流水号初始化为1
        Integer serialNum = 1;
        if (null != record && record.getUnitCode() != null) {
            // 流水号+1
hkl's avatar
hkl committed
101
            serialNum = Integer.parseInt(record.getUnitCode().substring(record.getUnitCode().length() - 4)) + 1;
hkl's avatar
hkl committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        }
        String serialNumStr = supplementZero(4, serialNum);

        return unitCode + serialNumStr;
    }

    /**
     * 曲线代码生成: L201QX0001
     * QX - 曲线
     * 000001-流水号
     *
     * @param lineId 线别ID
     */
    public static String curveUnitCodeUtil(String lineId) {
        // 获取线别拼音
        LineAlias lineAlias = lineAliasService.getById(lineId);
hkl's avatar
hkl committed
118
        String lineAliasPinyin = PinyinUtil.getFirstLetter(lineAlias.getLineAliasName(), "").toUpperCase();
hkl's avatar
hkl committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132


        // 查询最大的编号
        String unitCode = lineAliasPinyin + "QX";
        CurveManagement record = curveManagementService.lambdaQuery()
                .likeRight(CurveManagement::getUnitCode, unitCode)
                .orderByDesc(CurveManagement::getUnitCode)
                .last("limit 1")
                .one();

        // 流水号初始化为1
        Integer serialNum = 1;
        if (null != record && record.getUnitCode() != null) {
            // 流水号+1
hkl's avatar
hkl committed
133
            serialNum = Integer.parseInt(record.getUnitCode().substring(record.getUnitCode().length() - 4)) + 1;
hkl's avatar
hkl committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        }
        String serialNumStr = supplementZero(4, serialNum);

        return unitCode + serialNumStr;
    }

    /**
     * 道岔代码生成: 01DC0001
     * 01 - 上行;02-表示下行
     * DC - 道岔
     * 000001-流水号
     *
     * @param lineId 线别ID
     */
    public static String switchUnitCodeUtil(String lineId) {
        // 获取线别拼音
        LineAlias lineAlias = lineAliasService.getById(lineId);
hkl's avatar
hkl committed
151
        String lineAliasPinyin = PinyinUtil.getFirstLetter(lineAlias.getLineAliasName(), "").toUpperCase();
hkl's avatar
hkl committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165


        // 查询最大的编号
        String unitCode = lineAliasPinyin + "DC";
        SwitchManagement record = switchManagementService.lambdaQuery()
                .likeRight(SwitchManagement::getUnitCode, unitCode)
                .orderByDesc(SwitchManagement::getUnitCode)
                .last("limit 1")
                .one();

        // 流水号初始化为1
        Integer serialNum = 1;
        if (null != record && record.getUnitCode() != null) {
            // 流水号+1
hkl's avatar
hkl committed
166
            serialNum = Integer.parseInt(record.getUnitCode().substring(record.getUnitCode().length() - 4)) + 1;
hkl's avatar
hkl committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        }
        String serialNumStr = supplementZero(4, serialNum);

        return unitCode + serialNumStr;
    }


    /**
     * 将元数据前补零,补后的总长度为指定的长度,以宇符串的形式返回
     *
     * @param numLength 字符总长度
     * @param num       转值的数量
     */
    public static String supplementZero(int numLength, int num) {
        return String.format("%0" + numLength + "d", num);
    }
hkl's avatar
hkl committed
183

hkl's avatar
hkl committed
184
}