package com.sunxung.factoring.service.sys.impl;

import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.DateUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.sys.SystemSerialNum;
import com.sunxung.factoring.service.sys.SerialNumberGenerator;
import com.sunxung.factoring.service.sys.SystemSerialNumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class SerialNumberGeneratorImpl implements SerialNumberGenerator {

    /**
     * 客户营销业务编号后缀默认初始值
     **/
    private static final String CUSTOMER_MARKETING_SUFFIX = "0000";
    /**
     * 评审会-额度支用-主合同信息: 保理合同编号后缀默认初始值
     **/
    private static final String REVIEW_MASTER_CONTRACT_SUFFIX = "000";
    /**
     * 付款/放款申请编号
     **/
    private static final String PAYMENT_APPLY_SUFFIX = "000";

    /**
     * 业务编号
     */
    private static final String BUSINESS_BEGIN_NUM = "0000";
    /**
     * 验收单编号
     */
    private static final String ACCEPTANCE_SHEET_BEGIN_NUM = "0000";
    /**
     * 结算单编号
     */
    private static final String BILL_BEGIN_NUM = "0000";
    /**
     * 底层编号
     */
    private static final String MORTGAGE_NUM = "0000";
    /**
     * 发票编号
     */
    private static final String INVOICE_NUM = "0000";
    /**
     * 分账编号
     */
    private static final String LEDGER_NUM = "0000";
    /**
     * 退款编号
     */
    private static final String REFUND_NUM = "0000";
    @Autowired
    private SystemSerialNumService sysSerialNumService;

    @Override
    @Transactional
    public String nextValue(String prefixType) {
        //获取前置字符
        String prefix = getCurrentPrefix(prefixType);
        //获取当前数据库中对应SystemSerialNum
        SystemSerialNum sysSerialNum = sysSerialNumService.getByName(prefix);
        if (null == sysSerialNum && StringUtil.isNotEmpty(prefixType)) {
            if (prefixType.equals(ConstantUtil.BUSINESS_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "业务编号"));
            } else if (prefixType.equals(ConstantUtil.ACCEPTANCE_SHEET_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "验收单编号"));
            } else if (prefixType.equals(ConstantUtil.BILL_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "结算单编号"));
            } else if (prefixType.equals(ConstantUtil.MORTGAGE_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "底层编号"));
            } else if (prefixType.equals(ConstantUtil.INVOICE_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "销售发票编号"));
            } else if (prefixType.equals(ConstantUtil.LEDGER_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "分账编号"));
            } else if (prefixType.equals(ConstantUtil.REVIEW_MASTER_CONTRACT_DOMESTIC_CODE)) {
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "额度支用-主合同信息-国内保理"));
            } else if (prefixType.equals(ConstantUtil.REFUND_MANAGEMENT_CODE)){
                sysSerialNum = sysSerialNumService.add(createInitialNo(prefix, "退款编号"));
            }
        }
        if (null != sysSerialNum) {
            String curNum = "";
            if (prefixType.equals(ConstantUtil.BUSINESS_CODE)) {
                curNum = getCurNum(sysSerialNum, BUSINESS_BEGIN_NUM);
            } else if (prefixType.equals(ConstantUtil.ACCEPTANCE_SHEET_CODE)) {
                curNum = getCurNum(sysSerialNum, ACCEPTANCE_SHEET_BEGIN_NUM);
            } else if (prefixType.equals(ConstantUtil.BILL_CODE)) {
                curNum = getCurNum(sysSerialNum, BILL_BEGIN_NUM);
            } else if (prefixType.equals(ConstantUtil.MORTGAGE_CODE)) {
                curNum = getCurNum(sysSerialNum, MORTGAGE_NUM);
            } else if (prefixType.equals(ConstantUtil.INVOICE_CODE)) {
                curNum = getCurNum(sysSerialNum, INVOICE_NUM);
            } else if (prefixType.equals(ConstantUtil.LEDGER_CODE)) {
                curNum = getCurNum(sysSerialNum, LEDGER_NUM);
            } else if (prefixType.equals(ConstantUtil.REVIEW_MASTER_CONTRACT_DOMESTIC_CODE)) {
                curNum = getCurNum(sysSerialNum, REVIEW_MASTER_CONTRACT_SUFFIX);
            } else if (prefixType.equals(ConstantUtil.REFUND_MANAGEMENT_CODE)){
                curNum = getCurNum(sysSerialNum, REFUND_NUM);
            }
            if (StringUtil.isNotEmpty(curNum)) {
                if (prefixType.equals(ConstantUtil.BUSINESS_CODE) || prefixType.equals(ConstantUtil.ACCEPTANCE_SHEET_CODE)
                        || prefixType.equals(ConstantUtil.BILL_CODE) || prefixType.equals(ConstantUtil.MORTGAGE_CODE)
                        || prefixType.equals(ConstantUtil.INVOICE_CODE) || prefixType.equals(ConstantUtil.LEDGER_CODE)
                        ||prefixType.equals(ConstantUtil.REFUND_MANAGEMENT_CODE) ) {
                    return DateUtil.getDayStringFormatYMD(new Date()) + prefixType + curNum;
                } else {
                    return prefix + curNum;
                }
            }
        }
        return null;
    }

    private String getCurNum(SystemSerialNum sysSerialNum, String suffix) {
        sysSerialNum.setCurNum(sysSerialNum.getCurNum() + sysSerialNum.getStep());
        sysSerialNumService.update(sysSerialNum);
        String curNum = String.valueOf(sysSerialNum.getCurNum());
        int length = suffix.length();
        if (StringUtil.isNotEmpty(curNum) && curNum.length() > length) {
            curNum = curNum.substring(1, length);// 目前仅支持日增9999条记录，如何设计更可扩展的编号，待设计……
        } else {
            curNum = suffix.substring(0, length - curNum.length()) + curNum;
        }
        return curNum;
    }

    /**
     * 初始化编号
     *
     * @param prefix 编号名称（标识、编码）
     * @param remark 编号备注说明
     * @return
     */
    private SystemSerialNum createInitialNo(String prefix, String remark) {
        return new SystemSerialNum(prefix, 0L, 1, remark);
    }

    /**
     * 获取当前日期的编号名称
     *
     * @param prefixType 编号类型编码 INVOICE_CODE
     * @return
     */
    private String getCurrentPrefix(String prefixType) {
        if (StringUtil.isNotEmpty(prefixType)) {
            if (prefixType.equals(ConstantUtil.BUSINESS_CODE) || prefixType.equals(ConstantUtil.ACCEPTANCE_SHEET_CODE)
                    || prefixType.equals(ConstantUtil.BILL_CODE) || prefixType.equals(ConstantUtil.INVOICE_CODE) || prefixType.equals(ConstantUtil.MORTGAGE_CODE)
                    || prefixType.equals(ConstantUtil.LEDGER_CODE) || prefixType.equals(ConstantUtil.REFUND_MANAGEMENT_CODE)) {
                return DateUtil.getDayStringFormatYMD(new Date()) + prefixType;
            } else if (prefixType.equals(ConstantUtil.REVIEW_MASTER_CONTRACT_DOMESTIC_CODE)) {
                return prefixType + "-" + DateUtil.getYearStringFormatYear(new Date());
            }
        }
        return null;
    }
}
