package com.ywt.mg.services;

import com.ywt.mg.core.utils.StringHelper;
import com.ywt.mg.domain.entities.SequenceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class IdGenerator {

    /**
     * 订单号标志
     */
    private final String ORDER_NO_TAG_NAME = "orderno";

    public static final String REFUND_LOG_TABLE_NAME = "refund_log";

    @Autowired
    private SequenceRepository sequenceRepository;

    public Integer getWxPaymentLogsId() {
        return sequenceRepository.gen("wx_payment_logs");
    }

    public String generateOrderNo(String prefix) {
        if (StringHelper.isNullOrEmpty(prefix)) {
            prefix = "";
        }

        Date currentDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        int sn = sequenceRepository.generateSn(ORDER_NO_TAG_NAME);
        return prefix + format.format(currentDate) + String.format("%04d", sn);
    }

    public String generateRefundOrderNo() {
        return generateOrderNo("R");
    }

    public Integer getWechartCorppayLogsId() {
        return sequenceRepository.gen("wechart_corppay_logs");
    }

    public Integer genRefundLogId() {
        return sequenceRepository.gen(REFUND_LOG_TABLE_NAME);
    }

    /**
     * 下载记录表id
     * @return
     */
    public int genDownloadRecordId() {
        return sequenceRepository.gen("download_record");
    }
}
