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:wanxiangZhou
 * @E-mail:zhouyu@sunxung.cn
 * @Date:2018年6月13日 上午10:20:22
 */
@Component
public class ReviewPayBackMethodDict extends Dict {

    /**
     * 编码
     **/
    public static String CODE = "reviewPayBackMethod";
    /**
     * 中文名称
     **/
    public static String CNAME = "额度支用还款方式";

    /**
     * 子枚举
     **/
    public static enum ChildEnum {
        /**
         * 前收息
         **/
        PRE_INTEREST("pre_interest", "前收息"),
        /**
         * 后收息
         **/
        AFTER_INTEREST("after_interest", "后收息"),
        /**
         * 管理费前收利息后收
         **/
        PRE_MANAGEMENT_AFTER_INTEREST("pre_management_after_interest", "管理费前收利息后收");
        /**
         * 编码
         ***/
        private String code;
        /**
         * 名称
         **/
        private String cname;

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

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getCname() {
            return cname;
        }

        public void setCname(String cname) {
            this.cname = 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);
    }
}
