package com.scm.contract.tools;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scm.contract.service.apply.LabelApplicationService;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;


/**
 * @author zuo
 * @date 2024/6/19 15:45
 */
@Component
public class OutSnowflakeIdGenerator {

    @Autowired
    private LabelApplicationService labelApplicationService;

    private long lastStamp = -1L;
    private long sequence = 0L;

    public void into(){
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MILLISECOND, 0);
        long currentStamp = ca.getTime().getTime();

        if (lastStamp == -1L) {
            String orderNo = labelApplicationService.getMaxOrderNo();
            if (StringUtils.isBlank(orderNo)) {
                lastStamp = currentStamp;
                sequence = 0L;
            } else {
                String dayStr = orderNo.substring(0, 6);
                String number = orderNo.substring(6);
                try {
                    lastStamp = DateUtils.parseDate(dayStr, "yyMMdd").getTime();
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                sequence = Long.parseLong(number);
            }
        }
    }

    public synchronized String generateId() {
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.SECOND, 0);
        ca.set(Calendar.MINUTE, 0);
        ca.set(Calendar.MILLISECOND, 0);
        long currentStamp = ca.getTime().getTime();

        if (currentStamp < lastStamp) {
            throw new RuntimeException("Clock moved backwards. Refusing to generate id");
        }

        if (currentStamp == lastStamp) {
            sequence = sequence + 1;
        } else {
            sequence = 1L;
        }

        lastStamp = currentStamp;


        String day = DateFormatUtils.format(new Date(lastStamp), "yyMMdd");
        String number = String.format("%04d", sequence);

        return day + number;
    }

    private long getNextMill() {
        long mill = System.currentTimeMillis();
        while (mill <= lastStamp) {
            mill = System.currentTimeMillis();
        }
        return mill;
    }


//    public SnowflakeIdGenerator() {
//        Date date = new Date();
//        Calendar ca = Calendar.getInstance();
//        ca.setTime(date);
//        ca.set(Calendar.HOUR_OF_DAY, 0);
//        ca.set(Calendar.MINUTE, 0);
//        ca.set(Calendar.SECOND, 0);
//        ca.set(Calendar.MILLISECOND, 0);
//
//        String orderNo = labelApplicationService.getMaxOrderNo();
//        if (StringUtils.isBlank(orderNo)) {
//            lastStamp = ca.getTime().getTime();
//            sequence = 0L;
//        } else {
//            String dayStr = orderNo.substring(0, 6);
//            String number = orderNo.substring(6);
//            String[] dateFormats = {"yyMMdd"};
//            Date dayTime = DateUtils.parseDate(dayStr, dateFormats);
//            lastStamp = dayTime.getTime();
//            sequence = Long.getLong(number);
//        }
//    }

//    public static void main(String[] args) {
//        SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator();
//        for (int i = 0; i < 100; i++) {
//            System.out.println(idGenerator.generateId());
//        }
//    }
}

