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 ProjectTypeDict extends Dict {

    /** 编码 */
    public static String CODE = "projectType";
    /** 中文名称 */
    public static String CNAME = "出入账项目类型";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 非业务 **/
        NONBUSINESS("non_business", "非业务"),
        /** 保理业务 **/
        FACTORING_BUSINESS("factoring_business", "保理业务"),
        /** 资管业务 **/
        MANAGEMENT_BUSINESS("management_business", "资管业务"),
        /** 再融资业务 **/
        REFINANCING_BUSINESS("refinancing_business", "再融资业务"),
        /** 其他业务 **/
        OTHER_BUSINESS("other_business", "其他业务");

        /** 编码 */
        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);
    }
}
