package com.sunxung.factoring.dict.impl;

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 : L.Jia
 * @Date: 2021-09-07 11:46
 */
public class SalesContractStatusDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "salesContractStatus";
    /**
     * 中文名称
     */
    public static String CNAME = "销售合同状态";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 已创建
         */
        CREATE("sales_contract_create", "已创建"),
        /**
         * 已录入
         */
        INPUT("sales_contract_input", "已录入"),
        /**
         * 已驳回
         **/
        OVERRULE("sales_contract_overrule", "已驳回"),
        /**
         * 已审核
         */
        APPROVAL("sales_contract_approval", "已审核"),
        /**
         * 已录入(变更)
         */
        INPUT_CHANGE("sales_contract_input_change", "已录入(变更)"),
        /**
         * 已驳回(变更)
         */
        OVERRULE_CHANGE("sales_contract_overrule_change", "已驳回(变更)"),
        /**
         * 已审核(变更)
         */
        APPROVAL_CHANGE("sales_contract_approval_change", "已审核(变更)");

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