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;

/**
 * @Author Yafan Wang
 * @Date 2024/1/7 11:49
 */
public class SalesContractPersonTypeDict extends Dict {

    /**
     * 编码
     */
    public static String CODE = "salesContractPersonType";
    /**
     * 中文名称
     */
    public static String CNAME = "销售合同人员类型";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {

        /** 合同签署人 **/
        SALESCONTRACTPERSONTYPE_SIGN("salesContractPersonType_sign", "合同签署人"),
        /** 验收经办人 **/
        SALESCONTRACTPERSONTYPE_ACCEPTANCE("salesContractPersonType_acceptance", "验收经办人"),
        /** 结算经办人 **/
        SALESCONTRACTPERSONTYPE_SETTLEMENT("salesContractPersonType_settlement", "结算经办人");

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

}
