package com.fjhx.victoriatourist.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fjhx.victoriatourist.service.abnormal.AbnormalInfoService;
import com.fjhx.victoriatourist.service.jd.JdBackQualityDetailsService;
import com.fjhx.victoriatourist.service.order.OrderInfoService;
import com.fjhx.victoriatourist.service.stock.StockTransferService;
import com.ruoyi.common.exception.ServiceException;
import lombok.Getter;

import java.util.Date;
import java.util.Map;

@Getter
public enum CodeEnum {

    //调仓
    STOCK_TRANSFER("SM", "yyMM-", "code", 3, StockTransferService.class),
    // 销售订单
    ORDER_SALES("SO", null, "code", 5, OrderInfoService.class),
    //京东退货质检
    JD_BACK_QUALITY("PA", "yyMM-", "code", 3, JdBackQualityDetailsService.class),
    //异常记录
    ABNORMAL("PO", "yyMM-", "link_code", 3, AbnormalInfoService.class);

    CodeEnum(String prefix, String dateFormat, String codeFieldName, Integer length, Class<? extends IService<?>> serviceCls) {
        this.prefix = prefix;
        this.dateFormat = dateFormat;
        this.length = length;
        this.codeFieldName = codeFieldName;
        this.service = SpringUtil.getBean(serviceCls);
    }

    // 编码前缀
    private final String prefix;
    // 编码加日期规则
    private final String dateFormat;
    // 长度
    private final Integer length;
    // 编码字段名
    private final String codeFieldName;
    // service
    private final IService<?> service;

    /**
     * 获取键值对
     */
    public String getCode() {
        String itemPrefix;

        if (ObjectUtil.isNotEmpty(dateFormat)) {
            Date date = new Date();
            String format = DateUtil.format(date, dateFormat);
            itemPrefix = prefix + format;
        } else {
            itemPrefix = prefix;
        }

        Object obj = service.query()
                .likeRight(codeFieldName, itemPrefix)
                .orderByDesc(codeFieldName)
                .last("limit 1")
                .one();

        if (obj == null) {
            return itemPrefix + autoGenericCode(length, 0);
        }

        Map<String, Object> map = Convert.toMap(String.class, Object.class, obj);

        String code = Convert.toStr(map.get(CharSequenceUtil.toCamelCase(codeFieldName)));
        Integer codeNum = Convert.toInt(code.substring(itemPrefix.length()));
        if (ObjectUtil.isEmpty(codeNum)) {
            throw new ServiceException("自定义编码与系统编码生成规则冲突，暂时无法生成编码，请联系管理员");
        }

        return itemPrefix + autoGenericCode(length, codeNum);
    }

    /**
     * 获取键值对
     */
    public String getCode(String code) {
        if (ObjectUtil.isNotEmpty(code)) {
            Long count = service.query().eq(codeFieldName, code).count();
            if (count != 0) {
                throw new ServiceException("编码已存在");
            }
            return code;
        } else {
            return getCode();
        }
    }

    /**
     * 不够位数的在前面补0，保留num的长度位数字
     */
    private static String autoGenericCode(int length, Integer codeNum) {
        return String.format("%0" + length + "d", codeNum + 1);
    }

}
