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 liutao
 * @date 2021.09.17
 */
public class BusinessStageDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "businessStage";
    /**
     * 中文名称
     */
    public static String CNAME = "业务流程阶段";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 业务创建
         */
        CREATE("businessCreateStage", "业务创建"),
        /**
         * 模板审核
         */
        TEMPLATE_REVIEW("businessTemplateReviewStage", "模板审核"),
        /**
         * 保证方式确认
         **/
        GUARANTEE_MODE("businessGuaranteeModeStage", "保证方式确认"),
        /**
         * 保证金收款确认
         */
        BOND_COLLECTION("businessBondCollectionStage", "保证金收款确认"),
        /**
         * 中标确认
         */
        AWARD("businessAwardStage", "中标确认"),
        /**
         * 保证金退款确认
         **/
        REFUND_DEPOSIT("businessRefundDepositStage", "保证金退款确认"),
        /**
         * 无
         */
        EMPTY("businessEmptyStage", "无");
        /**
         * 编码
         */
        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);
    }
}
