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;

/**
 * @Description OA流程配置付款事项类型字典项
 * @Author Yafan Wang
 * @Date 2023/9/6 11:43
 */
public class OaFlowPaymentMatterTypeDic extends Dict {

    /** 编码 */
    public static String CODE = "oaFlowPaymentMatterType";
    /** 中文名称 */
    public static String CNAME = "OA流程配置付款事项类型";

    /** 子项枚举 */
    public static enum ChildEnum {
        /** 退款类（包含退利息管理费、（履约）保证金、支付保证金等） **/
        OA_REFUND_MATTER("oaRefundMatter", "退款类（包含退利息管理费、（履约）保证金、支付保证金等）"),
        /** 再融资付款类（归还本金、利息、支付再融资相关费用、支付保证金、支付长银系借款类） **/
        OA_FLOW_PAYMENT_MATTER_TYPE("oaRefinancingPaymentsMatter", "再融资付款类（归还本金、利息、支付再融资相关费用、支付保证金、支付长银系借款类）"),
        /** 其他付款（公对公支付、备用金、重卡业务结清等） **/
        OA_OTHER_PAYMENT_MATTER("oaOtherPaymentMatter", "('其他付款（公对公支付、备用金、重卡业务结清等）"),
        /** 其他付款（误转退回） **/
        OA_RETURN_PAYMENT_MATTER("oaReturnPaymentMatter", "其他付款（误转退回）"),
        /** 跟投付款 **/
        OA_FLOW_PAYMENT_MATTER("oaFlowPaymentMatter", "跟投付款"),
        /** 归还保理融资款（本金及利息） **/
        OA_RETURN_FACTORING_PAYMENT_MATTER("oaReturnFactoringPaymentMatter", "归还保理融资款（本金及利息）"),
        /** 上游货款支付 **/
        OA_UPSTREAM_GOODS_PAYMENT_MATTER("oaUpstreamGoodsPaymentMatter", "上游货款支付"),
        /** 保理融资本金 **/
        FACTORING_FINANCING_PRINCIPAL_PAYMENT_MATTER("factoringFinancingPrincipalPaymentMatter","保理款项支付申请");

        /** 编码 */
        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);
        if (domain == null) {
            throw new ValidatorException(CodeUtil.FAIL, "OA流程配置付款事项类型字典新增失败");
        }
        for (ChildEnum child : ChildEnum.values()) {
            addChildEnumDictionary(domain.getId(), child);
        }
    }

    private Dictionary addChildEnumDictionary(Long parentId, 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) {
        Dictionary dictionary = new Dictionary();
        dictionary.setParentId(parentId);
        dictionary.setName(CNAME);
        dictionary.setCode(CODE);
        dictionary.setGradation(gradation);
        return dictionaryService.add(dictionary);
    }

}
