package org.dromara.system.service.impl;

import cn.hutool.core.date.DateUtil;
import org.dromara.system.domain.BpmOrderNum;
import org.dromara.system.mapper.CommonMapper;
import org.dromara.system.service.IOrderNum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author sunzexing
 * @version 1.0
 * @title BpmOrderNumImpl
 * @description
 * @create 2024-02-20 18:17
 */

@Service
public class BpmOrderNumImpl implements IOrderNum {

    private Map<String, Long> cacheMap = Collections.synchronizedMap(new HashMap<>());

    @Resource
    private CommonMapper commonMapper;

    private static  String PRI = "NUM_";
    @Override
    public synchronized  String   getOrderNum(String bpmType,int size ) throws Exception {

       String  date =  DateUtil.format(new Date(), DateTimeFormatter.ofPattern("yyyyMMdd"));
        Long num =  cacheMap.get(PRI+bpmType+"_"+date);
        BpmOrderNum orderNum = new BpmOrderNum();
        orderNum.setV_process_name(bpmType);
        orderNum.setDtm_time(date);

        if(num == null){
            BpmOrderNum  resultOrder = this.commonMapper.selectOrderNumByBpmDefInfo(orderNum);
            if(resultOrder == null){
                orderNum.setI_max_num(1L);
                this.commonMapper.insertIntoOrderNum(orderNum);
                cacheMap.put(PRI+bpmType+"_"+date,1L);
            }else{
               long i =  resultOrder.getI_max_num()+1;
               this.getNextNumber(orderNum,i);
            }
        }else{
            num += 1;
            this.getNextNumber(orderNum,num);
        }

        return String.format("%0" + size + "d", orderNum.getI_max_num());
    }


    @Override
    public synchronized  Long   getOrderLongNum(String bpmType,int size ) throws Exception {

//        String  date =  DateUtil.format(new Date(), DateTimeFormatter.ofPattern("yyyyMMdd"));
        String date = "20240816";
        Long num =  cacheMap.get(PRI+bpmType+"_"+date);
        BpmOrderNum orderNum = new BpmOrderNum();
        orderNum.setV_process_name(bpmType);
        orderNum.setDtm_time(date);

        if(num == null){
            BpmOrderNum  resultOrder = this.commonMapper.selectOrderNumByBpmDefInfo(orderNum);
            if(resultOrder == null){
                orderNum.setI_max_num(1L);
                this.commonMapper.insertIntoOrderNum(orderNum);
                cacheMap.put(PRI+bpmType+"_"+date,1L);
            }else{
                long i =  resultOrder.getI_max_num()+1;
                this.getNextNumber(orderNum,i);
            }
        }else{
            num += 1;
            this.getNextNumber(orderNum,num);
        }

        return  orderNum.getI_max_num();
    }


    private void getNextNumber(BpmOrderNum orderNum, long i) throws Exception {

        this.cacheMap.put(PRI+orderNum.getV_process_name()+"_"+orderNum.getDtm_time(), i);
        orderNum.setI_max_num(i);
        this.commonMapper.updateOrderNumByProName(orderNum);
    }
}

