package com.sckw.payment.service;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.sckw.core.exception.BusinessException;
import com.sckw.payment.model.constant.LedgerEnum;

import java.util.List;
import java.util.Objects;

/**
 * @author xucaiqin
 * @date 2023-07-13 15:40:37
 */
public abstract class AbsLedger {
    /**
     * 订单校验
     *
     * @param order 订单数据
     */
    public void orderCheck(Object order) {
        if (Objects.isNull(order)) {
            throw new BusinessException("对账单不存在!");
        }
    }

    /**
     * 驳回查询校验
     *
     * @param status 订单状态
     */
    public void backQueryCheck(Integer status) {
        if (Objects.nonNull(status) && LedgerEnum.BACK.getStatus() != status) {
            throw new BusinessException("只有【已退回】的单据才支持驳回查询!");
        }
    }

    /**
     * 更新对账单校验
     *
     * @param status 订单状态
     */
    public void updateCheck(Integer status) {
        List<Integer> objects = Lists.newArrayList(LedgerEnum.SAVE.getStatus(), LedgerEnum.BACK.getStatus(), LedgerEnum.REVOCATION.getStatus());
        if (Objects.nonNull(status) && !objects.contains(status)) {
            throw new BusinessException("只有【已保存】、【已撤回】和【已退回】的单据才支持修改!");
        }
    }

    /**
     * 驳回对账单校验
     *
     * @param status 订单状态
     */
    public void backCheck(Integer status) {
        if (Objects.nonNull(status) && LedgerEnum.TO_LEDGER.getStatus() != status) {
            throw new BusinessException("只有【待对账】的单据才支持对账驳回!");
        }
    }

    /**
     * 对账确认校验
     *
     * @param status 订单状态
     */
    public void confirmCheck(Integer status) {
        if (Objects.nonNull(status) && LedgerEnum.TO_LEDGER.getStatus() != status) {
            throw new BusinessException("只有【待对账】的单据才支持对账确认!");
        }
    }

    /**
     * 对账删除校验
     *
     * @param status 订单状态
     */
    public void deleteCheck(Integer status) {
        List<Integer> objects = Lists.newArrayList(LedgerEnum.SAVE.getStatus(), LedgerEnum.BACK.getStatus(), LedgerEnum.REVOCATION.getStatus());
        if (Objects.nonNull(status) && !objects.contains(status)) {
            throw new BusinessException("只有【已保存】、【已撤回】和【已退回】的单据才支持删除!");
        }
    }

    /**
     * 撤回订单校验
     *
     * @param status 订单状态
     */
    public void revokeCheck(Integer status) {
        if (Objects.nonNull(status) && LedgerEnum.TO_LEDGER.getStatus() != status) {
            throw new BusinessException("只有【待对账】的单据才支持撤销!");
        }
    }

    /**
     * 完结订单校验
     *
     * @param status 订单状态
     */
    public void successCheck(Integer status) {
        if (Objects.nonNull(status) && LedgerEnum.LEDGERED.getStatus() != status) {
            throw new BusinessException("只有【已对账】的订单支持完成对账!");
        }
    }
}
