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;

/**
 * @Description : 现金流入账类型字典项
 * @Author : L.Jia
 * @Date: 2021-09-07 11:46
 */
public class CashFlowEntryTypeDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "cashFlowEntryType";
    /**
     * 中文名称
     */
    public static String CNAME = "现金流入账类型";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 现金
         */
        CASH("entryType_cash", "现金"),
        /**
         * 票据贴现
         */
        BILLS_DISCOUNTED("entryType_bills_discounted", "票据贴现");

        /**
         * 编码
         */
        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, "现金流入账类型字典项新增失败");
        }
        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);
    }
}
