package com.kxs.common.core.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 系统模块 操作
 *
 * @author 没秃顶的码农
 * @date 2024-01-11
 */
public class SysUtils {

    /**
     * 通过用户的pidPath查找最近所属的上级
     *
     * @param pidPaths PID 路径
     * @param userIds  用户 ID
     * @return {@link Boolean}
     */
    public static Integer checkUserIsChildren(String[] pidPaths, List<Integer> userIds){
        // 倒序循环数组
        for (int i = pidPaths.length - 1; i >= 0; i--) {
            boolean contains = userIds.contains(Integer.valueOf(pidPaths[i]));
            if(contains){
                return Integer.valueOf(pidPaths[i]);
            }
        }
        return null;
    }

    /**
     * 交易流水号 ID
     *
     * @param id ID
     * @return {@link String}
     */
    public static String generatePaymentId(Long id) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String timestamp = sdf.format(new Date());

        // 生成一个6位随机数作为流水号的后缀
        int randomNum = (int) (Math.random() * 1000000);
        String paymentId = timestamp + String.format("%06d", randomNum);

        return paymentId + id;
    }
}
