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;

/**
 * @Description:立项状态数据字典（说明：其他业务均可使用此定义的状态数据字典）
 * @Author:wanxiangZhou
 * @E-mail:zhouyu@sunxung.cn
 * @Date:2018年6月13日 上午10:20:22
 */
@Component
public class ProjectInitiationStatusDict extends Dict {

    /** 编码 **/
    public static String CODE = "projectInitiationStatus";
    /** 中文名称 **/
    public static String CNAME = "立项状态";

    /**   子枚举 **/
    public static enum ChildEnum {
        /** 处理中 **/
        PROCESSING("project_init_processing", "处理中"),
        /** 已完成 **/
        COMPLETED("project_init_completed", "已完成"),
        /** 已终止 **/
        TERMINATED("project_init_terminated", "已终止"),
        /** 已解除 **/
        CANCELLED("project_init_cancelled", "已解除");

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