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:LogTypeDict
 * @description:登录日志的类型
 * @author:杜小姣
 * @e-mail:1261237207@qq.com
 * @date:2018年1月23日 上午11:35:44
 */
@Component
public class LogTypeDict extends Dict {

    /** 编码 **/
    public static String CODE = "logType";
    /** 中文名称 **/
    public static String CNAME = "登录日志";

    /**   子枚举 **/
    public static enum ChildEnum {
        /** 用户登录 **/
        USER_LOGIN("userLogin", "用户登录"),
        /** 用户自行注销 **/
        USER_SELF_CANCELLATION("userSelfCancellation", "用户自行注销"),
        /** 用户失效 **/
        USER_FAILED("userFailed", "用户失效"),
        /** 用户被管理员注销 **/
        USER_OFF_BY_ADMIN("userOffByAdmin", "用户被管理员注销");

        /** 编码 ***/
        private String code;
        /** 名称 **/
        private String cname;

        ChildEnum(String code, String cname){
            this.code = code;
            this.cname = cname;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getCname() {
            return cname;
        }

        public void setCname(String cname) {
            this.cname = 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);
    }
}
