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;

/**
 * @Description 客户基础资料审核状态字典类
 * @Author Yafan Wang
 * @Date 2023/2/13 16:44
 */
public class FinancialCustomerStatusDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "financialCustomerStatus";
    /**
     * 中文名称
     */
    public static String CNAME = "客户基础资料审核状态";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 待编辑
         */
        FINANCIAL_CUSTOMER_TO_BE_EDIT("financial_customer_to_be_edit", "待编辑"),
        /**
         * 待审核
         */
        FINANCIAL_CUSTOMER_UNDER_APPROVAL("financial_customer_under_approval", "待审核"),
        /**
         * 通过
         */
        FINANCIAL_CUSTOMER_PASS("financial_customer_pass", "通过"),
        /**
         * 拒绝
         **/
        FINANCIAL_CUSTOMER_REFUSE("financial_customer_refuse", "拒绝");

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