package com.inkasso.factoring.common.dict.impl;

import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.auth.exception.ValidatorException;
import com.inkasso.factoring.common.dict.Dict;
import com.inkasso.factoring.dict.entity.Dictionary;
import org.springframework.stereotype.Component;

/**
 * @className:IncomeExpenditureTypeDict
 * @description:收支类型字典项
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年8月13日 上午11:36:04
 */
@Component
public class IncomeExpenditureTypeDict extends Dict {

    /** 编码 */
    public static String CODE = "incormeExpenditureType";
    /** 中文名称 */
    public static String CNAME = "收支类型";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 本金 **/
        PRINCIPAL("principal", "本金"),
        /** 利息 **/
        INTEREST("interest", "利息"),
        /** 保证金 **/
        MARGIN("margin", "保证金"),
        /** 管理费 **/
        MANAGEMENT_FEE("management_fee", "管理费"),
        /** 逾期利息 **/
        OVERDUE_INTEREST("overdue_interest", "逾期利息"),
        /** 其他费 **/
        OTHER_FEE("other_fee", "其他费用"),
        /** 垫款 **/
        MONEY_ADVANCED("money_advanced", "垫款");
        /** 编码 */
        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);
    }
}
