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;

/**
 * @className:FactoringServiceScope
 * @description:服务范围字典项
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年10月31日 上午9:56:27
 */
@Component
public class FactoringServiceScopeDict extends Dict {

    /** 编码 */
    public static String CODE = "serviceScope";
    /** 中文名称 */
    public static String CNAME = "服务范围";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 应收账款融资 **/
        ACCOUNTRECEIVABLEFINANCING("accountReceivableFinancing", "应收账款融资"),
        /** 应收账款管理 **/
        ACCOUNTRECEIVABLEMANAGEMENT("accountReceivableManagement", "应收账款管理"),
        /** 应收账款催收 **/
        COLLECTIONRECEIVABLES("collectionReceivables", "应收账款催收"),
        /** 信用风险担保 **/
        CREDITRISKGUARANTEE("creditRiskGuarantee", "信用风险担保"),
        /** 其他（见特殊约定） **/
        accountReceivableOther("accountReceivableOther", "坏账担保");

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