package com.sunxung.factoring.dict.impl;

import org.springframework.stereotype.Component;

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:付款申请状态
 * @Author:wanxiangZhou
 * @E-mail:zhouyu@sunxung.cn
 * @Date:2018年9月10日 下午7:17:47
 */
@Component
public class PaymentApplyStatusDict extends Dict {

    /** 编码 **/
    public static String CODE = "paymentApplyStatus";
    /** 中文名称 **/
    public static String CNAME = "付款申请状态";

    /**   子枚举 **/
    public static enum ChildEnum {
        /** 放款申请中 **/
        APPLY("payment_apply", "放款申请中"),
        /** 放款审批中 **/
        APPROVAL("payment_approval", "放款审批中"),
        /** 放款执行中 **/
        EXECUTING("payment_executing", "放款执行中"),
        /** 已完成 **/
        COMPLETED("payment_completed", "已完成"),
        /** 已终止 **/
        TERMINATED("payment_terminated", "已终止");// 未来扩展业务，可能称之为“放款执行终止”，这种情况下需释放额度

        /** 编码 ***/
        private String code;
        /** 名称 **/
        private String cname;

        ChildEnum(String code, String cname){
            this.code = code;
            this.cname = cname;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getCname() {
            return cname;
        }

        public void setCname(String cname) {
            this.cname = cname;
        }
    }

    @Override
    public void add(int gradation) {
        Dictionary domain = addDomainDictionary(null, gradation);
        if (domain == null) {
            throw new ValidatorException(CodeUtil.FAIL, "付款申请状态数据字典新增失败");
        }
        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);
    }
}
