| | |
| | | import com.ruoyi.common.config.FrameworkConfig; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.utils.ShiroUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import org.apache.commons.lang3.RandomUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.commons.lang3.time.DateFormatUtils; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.atomic.AtomicLong; |
| | | |
| | | /** |
| | | * 系统公用方法 |
| | |
| | | */ |
| | | public class ContextUtil { |
| | | |
| | | public static Map<String, Boolean> l = new HashMap<>(); |
| | | private static final AtomicLong sequence = new AtomicLong(0); |
| | | private static String lastTimestamp = ""; |
| | | |
| | | |
| | | //全局用户实时坐在部门 |
| | | public static Map<String, String> contextUserDept = new HashMap<>(); |
| | | |
| | | /** |
| | | * 全局命令ID |
| | | * 全局用于存放SN与组织编码的关系,例如分机SN和组织编码关系 |
| | | */ |
| | | public static Map<String, Integer> contextOrderId = new HashMap<>(); |
| | | public static Map<String, String> contextSnCompanyIdMap = new HashMap<>(); |
| | | |
| | | |
| | | /** |
| | | * UUID生成规则 |
| | | * |
| | | * @return |
| | | * 生成顺序ID:年月日时分秒毫秒(17位) + 序列号(4位) = 21位 |
| | | */ |
| | | public static String getUUID() { |
| | | return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase(); |
| | | } |
| | | |
| | | public static String getBusinessID() { |
| | | return DateFormatUtils.format(new Date(), "yyyyMMdd") + String.format("%04d", RandomUtils.nextInt(0, 10000)); |
| | | } |
| | | |
| | | public static String getCurTimeMillis() { |
| | | return System.currentTimeMillis() + ""; |
| | | } |
| | | |
| | | public static String getTimeId() { |
| | | return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"); |
| | | } |
| | | |
| | | public static String getTimeId(int start, int end) { |
| | | return DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + RandomUtils.nextInt(start, end); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取当前年份 |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getYear() { |
| | | return DateFormatUtils.format(new Date(), "yyyyMM"); |
| | | public static String generateId() { |
| | | String pattern = "yyyyMMddHHmmssSSS"; |
| | | return generateId(null, pattern); |
| | | } |
| | | |
| | | /** |
| | | * 生成资产编码ID |
| | | * @return |
| | | */ |
| | | public static String createAssetId() { |
| | | return "NO" + randomChar(4) + getYear() + randomNum(6); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 生成业务ID |
| | | * 生成单据编码, (格式: yyyyMMddHHmmss + 4位随机数) |
| | | * @param prefix |
| | | * @return |
| | | */ |
| | | public static String getBizId(String prefix) { |
| | | if (prefix == null) prefix = "ID"; |
| | | return prefix + getTimeId() + randomNum(4); |
| | | public static String generateOrderId(String prefix) { |
| | | String pattern = "yyyyMMddHHmmss"; |
| | | return generateId(prefix, pattern); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 随机生成大写字母 |
| | | * |
| | | * @param length 长度 |
| | | * @return |
| | | */ |
| | | public static String randomChar(int length) { |
| | | Random random = new Random(); |
| | | StringBuffer sb = new StringBuffer(); |
| | | for (int i = 0; i < length; i++) { |
| | | char randomLetter = (char) (random.nextInt(26) + 'A'); // 65-90 |
| | | sb.append(randomLetter); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 随机生成数字 |
| | | * @param length |
| | | * @return |
| | | */ |
| | | public static String randomNum(int length) { |
| | | Random random = new Random(); |
| | | StringBuffer sb = new StringBuffer(); |
| | | for (int i = 0; i < length; i++) { |
| | | int digit = random.nextInt(10); // 生成0-9的随机数 |
| | | sb.append(digit); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | |
| | | |
| | | public static String getDefaultBatchId() { |
| | | return DateFormatUtils.format(new Date(), "yyyyMMddHHmm"); |
| | | } |
| | | |
| | | /** |
| | | * 根据当前登陆人获取组织编号,如果没有登陆人获取系统默认的组织编号 |
| | | * |
| | | * @return |
| | | */ |
| | | private static String generateId(String prefix, String pattern) { |
| | | if (null == pattern) pattern = "yyyyMMddHHmmss"; |
| | | String currentTimestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern)); |
| | | long seq; |
| | | synchronized (ContextUtil.class) { |
| | | if (currentTimestamp.equals(lastTimestamp)) { |
| | | seq = sequence.incrementAndGet(); |
| | | } else { |
| | | sequence.set(0); |
| | | seq = 0; |
| | | lastTimestamp = currentTimestamp; |
| | | } |
| | | } |
| | | |
| | | if (null == prefix) return String.format("%s%04d", currentTimestamp, seq); |
| | | return prefix + "_" + String.format("%s%04d", currentTimestamp, seq); |
| | | } |
| | | |
| | | |
| | | public static String UUID() { |
| | | return UUID.randomUUID().toString().replaceAll("-", ""); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 默认获取登录人所在组织,如果未登录则获取系统默认组织 |
| | | * @return 组织ID |
| | | */ |
| | | public static String getCompanyId() { |
| | | return ShiroUtils.getLoginUserCompanyId(); |
| | | } |
| | | |
| | | /** |
| | | * 获取系统默认的=companyId |
| | | * |
| | | * @return |
| | | */ |
| | | public static String getDefaultCompanyId() { |
| | | SysUser user = ShiroUtils.getSysUser(); |
| | | if (null != user) { |
| | | if (null != user) return user.getCompanyId(); |
| | | return FrameworkConfig.getCompanyId(); |
| | | } |
| | | |
| | | public static String getLoginUserName() { |
| | | SysUser user = ShiroUtils.getSysUser(); |
| | | return null == user ? "系统" : user.getUserName(); |
| | | } |
| | | |
| | | public static SysUser getLoginUser() { |
| | | return ShiroUtils.getSysUser(); |
| | | } |
| | | |
| | | public static String subDeptId(SysUser user) { |
| | | if (null == user) { |
| | | user = getLoginUser(); |
| | | } |
| | | |
| | | if (null == user) { |
| | | return getCompanyId() + "001"; |
| | | } |
| | | |
| | | //从全局获取,如果有则取全局的默认,如果没有则取自己所属 |
| | | if (null != contextUserDept.get(user.getLoginName())) { |
| | | return contextUserDept.get(user.getLoginName()); |
| | | } |
| | | |
| | | if (null == user.getDeptId()) { |
| | | return user.getCompanyId(); |
| | | } else { |
| | | return FrameworkConfig.getCompanyId(); |
| | | } |
| | | return user.getDeptId().toString(); |
| | | } |
| | | |
| | | public static void updateSubDept(String userId, String deptId) { |
| | | if (null == deptId) return; |
| | | contextUserDept.put(userId, deptId); |
| | | } |
| | | |
| | | /** |
| | | * 最高的编码为:组织编码,然后逐级往下,如:5013,5013_001,5013_002,5013_001_001 |
| | | * |
| | | * @param companyId 必须 |
| | | * @param parentId 可空 |
| | | * @param endId 可空 |
| | | * @param format 必须 三位传1000,四位传10000 |
| | | * 获取顺序号 |
| | | * @param oldOrderId 原有顺序号 |
| | | * @param num 位数 |
| | | * @return |
| | | */ |
| | | public static String getNextId(String companyId, String parentId, |
| | | String endId, int format) { |
| | | if (parentId == null) { |
| | | return companyId; |
| | | public static String getOrderId(String oldOrderId, Integer num) { |
| | | Integer index3 = 1001; |
| | | Integer index4 = 10001; |
| | | Integer index5 = 100001; |
| | | Integer index = 0; |
| | | String orderId = "001"; |
| | | if (StringUtils.isNotEmpty(oldOrderId)) { |
| | | index = Integer.valueOf(oldOrderId); |
| | | } |
| | | try { |
| | | int endNum = 0; |
| | | if (StringUtils.isNotEmpty(endId)) { |
| | | endNum = Integer.valueOf(endId.substring(endId.length() - 3)); |
| | | } |
| | | String endStr = ("" + (endNum + format + 1)).substring(1); |
| | | |
| | | if (parentId.equals(companyId)) { |
| | | return companyId + "_" + endStr; |
| | | } |
| | | parentId = parentId.substring(parentId.lastIndexOf("_") + 1); |
| | | return companyId + "_" + parentId + "_" + endStr; |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | if (3 == num) { |
| | | orderId = ((index3 + index) + "").substring(1); |
| | | } |
| | | if (4 == num) { |
| | | orderId = ((index4 + index) + "").substring(1); |
| | | } |
| | | if (5 == num) { |
| | | orderId = ((index5 + index) + "").substring(1); |
| | | } |
| | | return orderId; |
| | | } |
| | | |
| | | /** |
| | | * 获取仓库编码 格式: |
| | | * 存放SN与所属组织的关系 |
| | | * |
| | | * @param deptId 5102_001_001 5102_001_002 |
| | | * 必须 分库编码 |
| | | * @param endId 必须 最后的仓库编码 |
| | | * @param format 必须 三位传1000,四位传10000,默认都传1000 |
| | | * @param sn |
| | | * @param companyId |
| | | */ |
| | | public static void addSerCompany(String sn, String companyId) { |
| | | contextSnCompanyIdMap.put(sn, companyId); |
| | | } |
| | | |
| | | /** |
| | | * 通过SN获取当前SN所属的组织 |
| | | * |
| | | * @param sn |
| | | * @return |
| | | */ |
| | | public static String getNextDepotId(String deptId, String endId, int format) { |
| | | try { |
| | | int endNum = 0; |
| | | if (StringUtils.isNotEmpty(endId)) { |
| | | endNum = Integer.valueOf(endId.substring(endId.length() - 3)); |
| | | } |
| | | String endStr = ("" + (endNum + format + 1)).substring(1); |
| | | |
| | | endStr = deptId.substring(deptId.indexOf("_") + 1) + "_" + endStr; |
| | | |
| | | return endStr.replaceAll("_", ""); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | public static String getCompanyIdBySn(String sn) { |
| | | return contextSnCompanyIdMap.get(sn); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取删除注释 |
| | | * |
| | | * @author sgj |
| | | * @date 2025/08/05 |
| | | * @param desc 删除文案描述 |
| | | */ |
| | | public static String getDeleteDesc(String desc) { |
| | | if (StringUtils.isEmpty(desc)) { |
| | | desc = "删除数据"; |
| | | } |
| | | return desc + "_" + ShiroUtils.getLoginName() + "_" + new Date(); |
| | | } |
| | | |
| | | |
| | | } |