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

import cn.superdesk.uniorder.common.enums.invoice.InvoiceApplyDimEnum;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dto.param.invoice.customer.status.OpBackAccountStatusForm;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.backAccountStatus.context.InvoiceApplyFieldBackAccountStatusContext;
import cn.superdesk.uniorder.pattern.state.invoice.customer.invoiceApply.backAccountStatus.state.InvoiceApplyFieldBackAccountStatusState;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldBackAccountStatusService;
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;

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 InvoiceApplyFieldBackAccountStatusConcreteState_2_已回款 implements InvoiceApplyFieldBackAccountStatusState {

	private InvoiceApplyCustomerService                       invoiceApplyService;
	private InvoiceApplyCustomerFieldBackAccountStatusService invoiceApplyFieldBackAccountStatusService;

	private InvoiceApplyFieldBackAccountStatusContext context;

	public InvoiceApplyFieldBackAccountStatusConcreteState_2_已回款(InvoiceApplyFieldBackAccountStatusContext context) {
		this.context = context;
		this.invoiceApplyService = (InvoiceApplyCustomerService) SpringContextHolder.getBean(InvoiceApplyCustomerService.class);
		this.invoiceApplyFieldBackAccountStatusService = (InvoiceApplyCustomerFieldBackAccountStatusService) SpringContextHolder.getBean(InvoiceApplyCustomerFieldBackAccountStatusService.class);
	}

	// 运营领票后跟进回款 -> 状态 -> 未回款(0, "未回款")
	public void opInit() {

		log.info("运营领票后跟进回款 -> 状态 -> {} -> 未回款(0, \"未回款\")", getClass().getName());

		InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
		LoginSysUser        loginSysUser        = context.getLoginSysUser();


		switch (invoiceApplyDimEnum) {
			case 票申请:
				InvoiceApply entity = context.getEntity();
				invoiceApplyFieldBackAccountStatusService.opInitByStateMachine(entity, loginSysUser);
				break;
			case 对账单:
				break;
		}
	}

	// 运营领票后跟进回款 部分回款 -> 状态 -> 部分回款(1, "部分回款")
	public void opPart() {

		log.info("运营领票后跟进回款 部分回款 -> 状态 -> {} -> 部分回款(1, \"部分回款\")", getClass().getName());

		InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
		LoginSysUser        loginSysUser        = context.getLoginSysUser();


		switch (invoiceApplyDimEnum) {
			case 票申请:
				InvoiceApply entity = context.getEntity();
				invoiceApplyFieldBackAccountStatusService.opPartByStateMachine(entity, loginSysUser);
				break;
			case 对账单:
				break;
		}
	}

	// 运营收款后登记回款 全部回款 -> 状态 -> 已回款(2, "已回款")
	public void opComplete() {

		log.info("运营收款后登记回款 全部回款 -> 状态 -> {} -> 已回款(2, \"已回款\")", getClass().getName());

		InvoiceApplyDimEnum invoiceApplyDimEnum = context.getInvoiceApplyDimEnum();
		LoginSysUser        loginSysUser        = context.getLoginSysUser();


		switch (invoiceApplyDimEnum) {
			case 票申请:

				InvoiceApply entity = context.getEntity();
				invoiceApplyFieldBackAccountStatusService.opCompleteByStateMachine(entity, loginSysUser);
				break;
			case 对账单:

				List<InvoiceApply> entitys = context.getEntitys();
				OpBackAccountStatusForm form = context.getForm();
				invoiceApplyFieldBackAccountStatusService.opCompleteByStateMachine(entitys, form, loginSysUser);
				break;
		}
	}

}