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 CashFlowOutTypeDict extends Dict {
    /**
     * 编码
     */
    public static String CODE = "cashFlowOutType";
    /**
     * 中文名称
     */
    public static String CNAME = "现金流出账类型";

    /**
     * 子项枚举
     */
    public static enum ChildEnum {
        /**
         * 保理融资本金
         */
        FACTORING_PRINCIPAL("outType_factoring_principal", "保理融资本金"),

        /**
         * 保理融资利息
         */
        FACTORING_INTEREST("outType_factoring_interest", "保理融资利息"),

        /**
         * 保理融资管理费
         */
        FINANCING_MANAGEMENT_FEE("outType_financing_management_fee", "保理融资管理费"),

        /**
         * 保理融资逾期利息
         */
        FINANCING_OVERDUE_INTEREST("outType_financing_overdue_interest", "保理融资逾期利息"),

        /**
         * 保理融资款项
         */
        FINANCING_AMOUNT("outType_financing_amount", "保理融资款项"),

        /**
         * 平台利润
         */
        PROFIT("outType_profit", "平台利润"),

        /**
         * 实退金额
         */
        DISCOUNT("outType_discount", "实退金额"),
        /**
         * 资金占用费
         */
        FUNDS_USED_COST("outType_funds_used_cost", "资金占用费"),
        /**
         * 放款
         */
        LOAN_AMOUNT("outType_loan_amount", "放款"),

        /**
         * 履约保证金
         */
        PERFORMANCE_BOND("performanceBond", "履约保证金"),

        /**
         * 质保金
         */
        RETENTION_MONEY("retentionMoney", "质保金"),

        /**
         * 应退利息
         */
        REFUND_INTEREST("refundInterest","应退利息"),

        /**
         * 应退管理费
         */
        REFUND_MANAGEMENT_FEE("refundManagementFee","应退管理费");

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