package cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.makeInvoiceStatus.state.concrete;

import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.singleBilling.SzhtxxInvoiceFptxxOpSingleBillingForm;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.makeInvoiceStatus.context.InvoiceApplyFieldMakeInvoiceStatusContext;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.makeInvoiceStatus.state.InvoiceApplyFieldMakeInvoiceStatusState;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.common.spring.SpringContextHolder;
import lombok.AllArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * @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 InvoiceApplyFieldMakeInvoiceStatusConcreteState_1_已开票 implements InvoiceApplyFieldMakeInvoiceStatusState {

    private InvoiceApplyCustomerService                       invoiceApplyService;
    private InvoiceApplyCustomerFieldMakeInvoiceStatusService invoiceApplyFieldInvoiceProgressingStatusService;

    private InvoiceApplyFieldMakeInvoiceStatusContext context;

    public InvoiceApplyFieldMakeInvoiceStatusConcreteState_1_已开票(InvoiceApplyFieldMakeInvoiceStatusContext context) {
        this.context = context;
        this.invoiceApplyService = (InvoiceApplyCustomerService) SpringContextHolder.getBean(InvoiceApplyCustomerService.class);
        this.invoiceApplyFieldInvoiceProgressingStatusService = (InvoiceApplyCustomerFieldMakeInvoiceStatusService) SpringContextHolder.getBean(InvoiceApplyCustomerFieldMakeInvoiceStatusService.class);
    }

    // 财务-待开票 -> 状态 -> 待开票(0, "待开票")
    public SysNotify opInit() {

        log.info("财务-待开票 -> 状态 -> {} -> 待开票(0, \"待开票\")", getClass().getName());

        InvoiceApply entity       = context.getEntity();
        LoginSysUser loginSysUser = context.getLoginSysUser();

        return invoiceApplyFieldInvoiceProgressingStatusService.opInitByStateMachine(entity, loginSysUser);
    }

    // 财务-作废票 -> 状态 -> 已作废(2, "已作废")
    public SysNotify opBecomeInvalid() {

        log.info("财务-作废票 -> 状态 -> {} -> 已作废(2, \"已作废\")", getClass().getName());

        InvoiceApply entity       = context.getEntity();
        LoginSysUser loginSysUser = context.getLoginSysUser();

        return invoiceApplyFieldInvoiceProgressingStatusService.opBecomeInvalidByStateMachine(entity, loginSysUser);
    }

    // 财务-发起开票 -> 状态 -> 开票中(10, "开票中")
    public SysNotify opOngoing() {

        log.info("财务-发起开票 -> 状态 -> {} -> 开票中(10, \"开票中\")", getClass().getName());

        SzhtxxInvoiceFptxxOpSingleBillingForm form   = context.getForm();
        InvoiceApply                                    entity = context.getEntity();
        LoginSysUser                              loginSysUser = context.getLoginSysUser();

        return invoiceApplyFieldInvoiceProgressingStatusService.opOngoingByStateMachine(form, entity, loginSysUser);
    }

    // 财务-确认开票 -> 状态 -> 已开票(11, "已开票")
    public SysNotify opComplete() {

        log.info("财务-确认开票 -> 状态 -> {} -> 已开票(11, \"已开票\")", getClass().getName());

        InvoiceApply entity       = context.getEntity();
        LoginSysUser loginSysUser = context.getLoginSysUser();

        return invoiceApplyFieldInvoiceProgressingStatusService.opCompleteByStateMachine(entity, loginSysUser);
    }

}
