package cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.concrete;

import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.common.spring.SpringContextHolder;
import cn.superdesk.uniorder.dao.entity.invoice.supplier.InvoiceApplySupplier;
import cn.superdesk.uniorder.dto.param.invoice.supplier.status.InvoiceApplySupplierOpCommonDimAccountingOrderForm;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.context.InvoiceApplySupplierFieldPayoffAccountStatusContext;
import cn.superdesk.uniorder.pattern.state.invoice.supplier.invoiceApplySupplier.payoffAccountStatus.state.InvoiceApplySupplierFieldPayoffAccountStatusState;
import cn.superdesk.uniorder.service.invoice.supplier.InvoiceApplySupplierFieldPayoffAccountStatusService;
import cn.superdesk.uniorder.service.invoice.supplier.InvoiceApplySupplierService;
import lombok.AllArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Description: 简易状态机 星型结构
 * @Author: linjinyu
 * @CreateDate: 9/23/20 2:00 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 9/23/20 2:00 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@ToString
@AllArgsConstructor
public class InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_1_部分付款 implements InvoiceApplySupplierFieldPayoffAccountStatusState {

    private InvoiceApplySupplierService                         invoiceApplySupplierService;
    private InvoiceApplySupplierFieldPayoffAccountStatusService invoiceApplySupplierFieldPayoffAccountStatusService;

    private InvoiceApplySupplierFieldPayoffAccountStatusContext context;

    public InvoiceApplySupplierFieldPayoffAccountStatusConcreteState_1_部分付款(InvoiceApplySupplierFieldPayoffAccountStatusContext context) {
        this.context = context;
        this.invoiceApplySupplierService = (InvoiceApplySupplierService) SpringContextHolder.getBean(InvoiceApplySupplierService.class);
        this.invoiceApplySupplierFieldPayoffAccountStatusService = (InvoiceApplySupplierFieldPayoffAccountStatusService) SpringContextHolder.getBean(InvoiceApplySupplierFieldPayoffAccountStatusService.class);
    }

    // 侨城汇-运营 开票申请后初始状态 待付款 -> 状态 -> 待付款(0, "待付款")
    public int opInit() {

        log.info("侨城汇-运营 开票申请后初始状态 待付款 -> 状态 -> {} -> 待付款(0, \"待付款\")", getClass().getName());

        LoginSysUser loginSysUser = context.getLoginSysUser();

        InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
        switch (invoiceApplyDimEnum) {
            case 票申请:
                InvoiceApplySupplier entity = context.getEntity();
                return invoiceApplySupplierFieldPayoffAccountStatusService.opInitByStateMachine(entity, loginSysUser);
            case 对账单:
                return 0;
            default:
                return 0;
        }


    }

    // 侨城汇-运营 op发起付款 向上级请款 审批流程 例如 走OA审批流 -> 状态 -> 付款中(10, "付款中")
    public int opRegulatoryPipeline() {

        log.info("侨城汇-运营 op发起付款 向上级请款 审批流程 例如 走OA审批流 -> 状态 -> {} -> 付款中(10, \"付款中\")", getClass().getName());

        LoginSysUser loginSysUser = context.getLoginSysUser();

        InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
        switch (invoiceApplyDimEnum) {
            case 票申请:
                InvoiceApplySupplier entity = context.getEntity();
                return invoiceApplySupplierFieldPayoffAccountStatusService.opRegulatoryPipelineByStateMachine(entity, loginSysUser);
            case 对账单:
                return 0;
            default:
                return 0;
        }
    }

    // 侨城汇-运营 收到发票后 跟进付款 部分付款 - 预留 - 目前没有这逻辑,也不允许 -> 状态 -> 部分付款(1, "部分付款")
    public int opPart() {

        log.info("侨城汇-运营 收到发票后 跟进付款 部分付款 - 预留 - 目前没有这逻辑,也不允许 -> 状态 -> {} -> 部分付款(1, \"部分付款\")", getClass().getName());

        LoginSysUser loginSysUser = context.getLoginSysUser();

        InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
        switch (invoiceApplyDimEnum) {
            case 票申请:
                InvoiceApplySupplier entity = context.getEntity();
                return invoiceApplySupplierFieldPayoffAccountStatusService.opPartByStateMachine(entity, loginSysUser);
            case 对账单:
                return 0;
            default:
                return 0;
        }
    }

    // 侨城汇-运营 op确认付款 收到发票后 线下 或 走OA 请到付且付款后 已付款 -> 状态 -> 已付款(2, "已付款")
    public int opComplete() {

        log.info("侨城汇-运营 op确认付款 收到发票后 线下 或 走OA 请到付且付款后 已付款 -> 状态 -> {} -> 已付款(2, \"已付款\")", getClass().getName());

        LoginSysUser loginSysUser = context.getLoginSysUser();

        InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
        switch (invoiceApplyDimEnum) {
            case 票申请:
                InvoiceApplySupplier entity = context.getEntity();
                return invoiceApplySupplierFieldPayoffAccountStatusService.opCompleteByStateMachine(entity, loginSysUser);
            case 对账单:
                List<InvoiceApplySupplier> entitys = context.getEntitys();
                InvoiceApplySupplierOpCommonDimAccountingOrderForm form = context.getForm();
                return invoiceApplySupplierFieldPayoffAccountStatusService.opCompleteByStateMachine(entitys, form, loginSysUser);
            default:
                return 0;
        }
    }
}
