package com.fjhx.common.enums;

import com.fjhx.common.entity.coding.dto.DefaultRule;
import com.fjhx.common.entity.coding.dto.RuleVo;
import com.ruoyi.common.exception.ServiceException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
public enum CodingRuleEnum {

    /**
     * 外销合同
     */
    CONTRACT("contract", "外销合同", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "SC-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * 采购编码
     */
    PURCHASE("purchase", "采购订单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "PO-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * 对内报价单
     */
    SALE_QUOTATION("sale_quotation", "报价单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "GSQT-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * 对外报价单
     */
    EXT_SALE_QUOTATION("ext_sale_quotation", "对外报价单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "EXT-GSQT-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),

    /**
     * 申购单
     */
    SUBSCRIBE("subscribe", "申购单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "PR-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),

    /**
     * 采购退货
     */
    PURCHASE_BACK("purchase_back", "采购退货", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "PB-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),

    /**
     * 售后管理
     */
    AFTER_SALES("after_sales", "售后管理", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "AS-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),

    /**
     * 采购到货
     */
    ARRIVAL("arrival", "采购到货", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "AR-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    ;


    /**
     * 编码规则key
     */
    private final String key;

    /**
     * 功能名称
     */
    private final String name;

    /**
     * 默认规则
     */
    private final List<DefaultRule> defaultRuleList;

    private static final Map<String, CodingRuleEnum> map = new HashMap<>();

    static {
        for (CodingRuleEnum value : CodingRuleEnum.values()) {
            map.put(value.key, value);
        }
    }

    private static DefaultRule getDefaultRule(RuleTypeEnum ruleTypeEnum) {
        return new DefaultRule(ruleTypeEnum);
    }

    private static DefaultRule getDefaultRule(RuleTypeEnum ruleTypeEnum, String value) {
        return new DefaultRule(ruleTypeEnum, value);
    }

    public List<RuleVo> getDefaultRuleVoList() {
        return defaultRuleList.stream().map(item -> {
            RuleVo ruleVo = new RuleVo();
            ruleVo.setRuleType(item.getRuleTypeEnum().getType());
            ruleVo.setValue(item.getValue());
            return ruleVo;
        }).collect(Collectors.toList());
    }

    public static List<RuleVo> getDefaultRuleVoList(String key) {
        CodingRuleEnum codingRuleEnum = map.get(key);
        if (codingRuleEnum == null) {
            throw new ServiceException("编码规则key不存在");
        }
        return codingRuleEnum.getDefaultRuleVoList();
    }

}
