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;

/**
 * 工作任务完成状态字典类
 */
@Component
public class WorkStateDict extends Dict {

    /** 编码 */
    public static String CODE = "workState";
    /** 中文名称 */
    public static String CNAME = "工作任务完成状态";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 进行中 */
        WORKING("workState_working", "进行中"),
        /** 完成 */
        FINSH("workState_finsh", "完成"),
        /** 下周继续 */
        CONTINUE_NEXT_WEEK("workState_continue_next_week", "下周继续"),
        /** 暂停 */
        PAUSE("workState_pause", "暂停"),
        /** 终止 */
        STOP("workState_stop", "终止"),
        /** 逾期 */
        OVERDUE("workState_overdue", "逾期");

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