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")
    )),
    /**
     * 样品单
     */
    SAMPLE("sample", "样品单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "YP-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * EHSD采购编码
     */
    EHSD_PURCHASE("ehsd_purchase", "EHSD采购单", 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")
    )),
    /**
     * 包装
     */
    PACK("pack", "包装", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "PACK-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * 采购编码
     */
    PURCHASE("purchase", "采购单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "SC-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),

    /**
     * 服务合同
     */
    SERVICE_CONTRACT("service_contract", "服务合同", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "PO-"),
            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")
    )),
    /**
     * 到货
     */
    ARRIVAL("arrival", "到货", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "PA-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * 质检
     */
    QUALITY("quality", "质检", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "ZJ-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * 退货
     */
    SALES_RETURN("sales_return", "退货", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "TH-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    /**
     * EHSD到货检验编码
     */
    EHSD_QUALITY("ehsd_quality", "到货检验编码", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "EHSD-QUALITY-"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyyyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),

    //---------------------------嘉兴双拓-----------------------------
    JXST_SALES_CONTRACT("jsxt_sales_contract", "嘉兴双拓销售合同", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "SC"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    JXST_WORK_ORDER("jsxt_work_order", "嘉兴双拓工单", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "WO"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    JXST_PRODUCTION_PLAN("jsxt_production_plan", "嘉兴双拓生产计划", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "WP"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    JXST_PRODUCTION_TASK("jsxt_production_task", "嘉兴双拓生产任务", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "WT"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyMM-"),
            getDefaultRule(RuleTypeEnum.AUTOINCREMENT, "3")
    )),
    JXST_AFTER_SALES("jsxt_after_sales", "嘉兴双拓售后", Arrays.asList(
            getDefaultRule(RuleTypeEnum.CUSTOMIZE, "AS"),
            getDefaultRule(RuleTypeEnum.DATE_FORMAT, "yyMM-"),
            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();
    }

}
