package com.sunxung.factoring.dict.impl;

import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.dict.Dict;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.service.sys.DictionaryService;

/**
 * @author liutao
 * @date 2021.09.17
 */
public class EquityHierarchyDetailTwoDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "equityHierarchyDetail_2";
    /**
     * 中文名称
     */
    public static String CNAME = "B端_股权层级小类_非建筑类公司";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 本级
         */
        ONE("equityHierarchyDetail_2_A", "本级"),
        /**
         * 一级
         */
        TWO("equityHierarchyDetail_2_B", "一级"),
        /**
         * 二级
         */
        THREE("equityHierarchyDetail_2_C", "二级"),
        /**
         * 三级
         */
        FORE("equityHierarchyDetail_2_D", "三级"),
        /**
         * 三级以下
         */
        FIVE("equityHierarchyDetail_2_E", "三级以下"),
        /**
         * 民营公司
         */
        SIX("equityHierarchyDetail_2_F", "事业单位");
        /**
         * 编码
         */
        private String code;
        /**
         * 名称
         */
        private String cname;

        ChildEnum(String code, String cname) {
            this.code = code;
            this.cname = cname;
        }

        public String getCode() {
            return code;
        }

        public String getCname() {
            return cname;
        }
    }

    @Override
    public void add(int gradation) {
        Dictionary domain = addDomainDictionary(null, gradation, dictionaryService);
        if (domain == null) {
            throw new ValidatorException(CodeUtil.FAIL, "B端_股权层级字典新增失败");
        }
        for (FileModuleDict.ChildEnum child : FileModuleDict.ChildEnum.values()) {
            addChildEnumDictionary(domain.getId(), child);
        }
    }

    private Dictionary addChildEnumDictionary(Long parentId, FileModuleDict.ChildEnum child) {
        Dictionary dictionary = new Dictionary();
        dictionary.setParentId(parentId);
        dictionary.setName(child.getCname());
        dictionary.setCode(child.getCode());
        dictionary.setGradation(child.ordinal());
        return dictionaryService.add(dictionary);
    }

    private Dictionary addDomainDictionary(Long parentId, int gradation, DictionaryService dictionaryService) {
        Dictionary dictionary = new Dictionary();
        dictionary.setParentId(parentId);
        dictionary.setName(CNAME);
        dictionary.setCode(CODE);
        dictionary.setGradation(gradation);
        return dictionaryService.add(dictionary);
    }
}
