package com.muchinfo.mtp3common.utils;

import com.muchinfo.mtp3common.vo.Calculator;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SerialNumberUtils {
    /**
     * 编号生成队列
     **/
    private static ConcurrentHashMap<String, SerialNumberHelper> requestMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<Long, Object> messageMap = new ConcurrentHashMap<>();
    public static Map<Long, Calculator> lockMap = new HashMap<>();
    public static String prefix_bank = "703";//银行服务
    public static String prefix_account = "701";//账户服务
    public static String prefix_Performance = "702";//履约服务
    public static String prefix_warehouse = "704";//仓单服务
    public static String prefix_login = "720"; //登录服务
    public static String prefix_trade = "712"; //交易服务
    public static String prefix_wroutin = "610";//仓单出入库

    public static String prefix_capacity = "180";//产能预售-远期订单管理端

    public static String prefix_capacitytwo = "181";//产能预售-预售管理

    public static String prefix_positiontransfer = "185";   // 持仓过户申请表
    /**
     * @param prefix 业务表示符号 3位数字字符
     * @return
     */
    public static synchronized Long getNewSerialNumber(String prefix) {
        int ran = (int) (Math.random() * 100);
        Long time = System.currentTimeMillis() - ran;
        String keys = prefix + time;
        Long key = Long.valueOf(keys);
        messageMap.put(key, -1);
        return Long.valueOf(keys);
    }

    /**
     * @param funcode 消息回应的funcode
     * @return Long
     * @throws
     * @Title: getNewSerialNumberByFuncode
     * @Description:
     */
    public static synchronized Long getNewSerialNumberByFuncode(Integer funcode) {
        StringBuffer sb = new StringBuffer();
        sb.append(funcode.toString());
        Long time = System.currentTimeMillis();
        sb.append(time.toString().substring(7));
        String keys = sb.toString();
        Long key = Long.valueOf(keys);
        messageMap.put(key, -1);
        return key;
    }
    public static void putMessageMap(Long uuid, Object o) {
        messageMap.put(uuid, o);
    }

    public static Object getMessageMap(Long uuid) {
        return messageMap.get(uuid);
    }

    public static void removeMessageMap(Long uuid) {
        if (messageMap.contains(uuid)) {
            messageMap.remove(uuid);
        }
    }
    /**
     * 创建计时器
     *
     * @param uuid
     * @return
     */
    public static Calculator createCalculator(Long uuid) {
        Calculator ca = new Calculator(uuid);
        lockMap.put(uuid, ca);
        return ca;
    }
    /**
     * 结束计时器
     *
     * @param uuid
     * @return
     */
    public static void endCalculator(Long uuid, Object obj) {
        Calculator ca = lockMap.remove(uuid);
        if (null != ca) {
            messageMap.put(uuid, obj);
            ca.start();
        }
    }

    public static synchronized Long getSerialNumber(String prefix) {
        String str = "000000";
        String timeStamp = DateTimeUtils.getNowTimeStamp();
        Long currCount;
        if (requestMap.containsKey(prefix)) {
            currCount = requestMap.get(prefix).getCount();
            if (requestMap.get(prefix).getTimeStamp().equals(timeStamp)) {
                currCount = currCount + 1;
                requestMap.get(prefix).setCount(currCount);
            } else {
                currCount = 1L;
                requestMap.get(prefix).setCount(currCount);
                requestMap.get(prefix).setTimeStamp(timeStamp);
            }
        } else {
            currCount = 1L;
            requestMap.put(prefix, new SerialNumberHelper(currCount, timeStamp));
        }

        String temp = prefix + timeStamp + str.substring(0, str.length() - currCount.toString().length()) + currCount.toString();
        requestMap.get(prefix).setTimeStamp(timeStamp);
        return Long.valueOf(temp);
    }
}
