package com.sunxung.factoring.dict.impl;

import org.springframework.stereotype.Component;

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 com.sunxung.factoring.service.sys.DictionaryService;

/**
 * @description:消息类型字典项
 * @author:吴端丽
 * @e-mail:1912942707@qq.com
 * @date:2018年3月21日 上午10:44:48
 */
@Component
public class MsgTypeDict extends Dict {

    /** 编码 */
    public static String CODE = "msgType";
    /** 中文名称 */
    public static String CNAME = "消息类型";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 系统消息 */
        SYSTEMMESSAGE("system_message", "系统消息"),
        /** 待办消息 */
        PENDINGNEWS("pending_news", "待办消息"),
        /** 知晓消息 */
        KNOWNEWS("know_news", "知晓消息"),
        /** 正常催收 */
        NORMALCOLLECTION("normal_collection", "正常催收"),
        /** 逾期催收 */
        OVERDUECOLLECTION("overdue_collection", "逾期催收"),
        /** 信保提醒 */
        LETTERGUARANTEEREMINDER("letter_guarantee_reminder", "信保提醒"),
        /** 用户消息 */
        USERMESSAGE("user_message", "用户消息");

        /** 编码 */
        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, dictionaryService);
        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, DictionaryService dictionaryService) {
        Dictionary dictionary = new Dictionary();
        dictionary.setParentId(parentId);
        dictionary.setName(CNAME);
        dictionary.setCode(CODE);
        dictionary.setGradation(gradation);
        return dictionaryService.add(dictionary);
    }
}
