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:TransferNoticeMethodDict
 * @description:转让通知方式字典项
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年10月31日 上午10:03:51
 */
@Component
public class TransferNoticeMethodDict extends Dict {

    /** 编码 */
    public static String CODE = "transferNoticeMethod";
    /** 中文名称 */
    public static String CNAME = "转让通知方式";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 快递 **/
        express("express", "快递"),
        /** 挂号信 **/
        REGISTEREDLETTER("registeredLetter", "挂号信"),
        /** 债务人盖章确认" **/
        DEBTORSTAMPCONFIRMATION("debtorStampConfirmation", "债务人盖章确认"),
        /** 进口商盖章确认 **/
        IMPORTERSEALCONFIRMATION("importerSealConfirmation", "进口商盖章确认"),
        /** 公证送达 **/
        NOTARIZEDDELIVERY("notarizedDelivery", "公证送达"),
        /** 其他（见特殊约定） **/
        TRANSFERNOTICEMETHODOTHER("transferNoticeMethodOther", "其他（见特殊约定）");

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