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 org.springframework.stereotype.Component;

/**
 * @description:银行账户公司归属字典项
 * @author:吴端丽
 * @e-mail:1912942707@qq.com
 * @date:2018年4月27日 上午10:35:51
 */
@Component
public class CompanyOwnershipDict extends Dict {

    /** 编码 */
    public static String CODE = "companyOwnership";
    /** 中文名称 */
    public static String CNAME = "银行账户公司归属";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 资管 **/
        ZI_GUAN("zi_guan", "资管"),
        /** 保理 **/
        FACTORING("factoring", "保理"),
        /** 投资 **/
        INVESTMENT("investment", "投资"),
        /** 西安敖洪 **/
        XI_AN_AO_HONG("xi_an_ao_hong", "西安敖洪"),
        /** 上海敖洪 **/
        SHANG_HAI_AO_HONG("shang_hai_ao_hong", "上海敖洪");

        /** 编码 */
        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);
        if (domain == null) {
            throw new ValidatorException(CodeUtil.FAIL, "银行账户公司归属字典新增失败");
        }
        for (ChildEnum child : ChildEnum.values()) {
            addChildEnumDictionary(domain.getId(), child);
        }
    }

    private Dictionary addChildEnumDictionary(Long parentId, 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) {
        Dictionary dictionary = new Dictionary();
        dictionary.setParentId(parentId);
        dictionary.setName(CNAME);
        dictionary.setCode(CODE);
        dictionary.setGradation(gradation);
        return dictionaryService.add(dictionary);
    }
}
