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 com.inkasso.factoring.dict.service.DictionaryService;

/**
 * @author liutao
 * @date 2021.09.24
 */
public class SettlementDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "billStatus";
    /**
     * 中文名称
     */
    public static String CNAME = "结算单状态";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 已创建
         */
        CREATE("bill_create", "已创建"),
        /**
         * 已录入
         */
        ENTRY("bill_entry", "已录入"),
        /**
         * 已驳回
         **/
        OVERRULE("bill_overrule", "已驳回"),
        /**
         * 待签署
         */
        NEED_SIGN("bill_need_sign", "待签署"),
        /**
         * 已签署
         **/
        SIGN("bill_sign", "已签署");
        /**
         * 编码
         */
        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 (FileModuleDict.ChildEnum child : FileModuleDict.ChildEnum.values()) {
            addChildEnumDictionary(domain.getId(), child);
        }
    }

    private Dictionary addChildEnumDictionary(Long parentId, FileModuleDict.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);
    }
}
