package cn.superdesk.uniorder.controller.accounting.supplier;

import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplier;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.accounting.supplier.AccountingUnifiedSupplierFieldAccountingStatusDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 简易状态机 星型结构
 * @Author: linjinyu
 * @CreateDate: 10/12/20 4:28 PM
 * @UpdateUser: linjinyu
 * @UpdateDate: 10/12/20 4:28 PM
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "AccountingUnifiedSupplier - AccountingStatus", tags = {"AccountingUnifiedSupplier - AccountingStatus"})
@Slf4j
@RestController
@RequestMapping("/accounting/unified/supplier/statement/v2/state/accountingStatus")
public class AccountingUnifiedSupplierVer2FieldAccountingStatusController extends BaseController {

	@Autowired
	private AccountingUnifiedSupplierDubboConsumerService                      accountingUnifiedSupplierDubboConsumerService;
	@Autowired
	private AccountingUnifiedSupplierFieldAccountingStatusDubboConsumerService accountingUnifiedSupplierFieldAccountingStatusDubboConsumerService;

	@ApiOperation(value = "AccountingUnifiedSupplier - AccountingStatus - 操作 刚创建时初始化状态'待提交'或重置状态为'待提交' 状态改变为 待提交_3")
	@RequestMapping(value = "/opInitOrReset/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opInitOrReset(@PathVariable("id") Long id) {

		R<?> result = new R<>();

		try {
			AccountingUnifiedSupplier entity = accountingUnifiedSupplierDubboConsumerService.findById(id);
			accountingUnifiedSupplierFieldAccountingStatusDubboConsumerService.opInitOrReset(getToken(), entity);
			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	@ApiOperation(value = "AccountingUnifiedSupplier - AccountingStatus - 操作 刚创建时初始化状态'未确认'或重置状态为'未确认',内部_后端(待提交->已提交|未确认),外部_商户端(未确认|待确认) 状态改变为 未确认_0")
	@RequestMapping(value = "/opSubmit/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opSubmit(@PathVariable("id") Long id) {

		R<?> result = new R<>();

		try {
			AccountingUnifiedSupplier entity = accountingUnifiedSupplierDubboConsumerService.findById(id);
			accountingUnifiedSupplierFieldAccountingStatusDubboConsumerService.opSubmit(getToken(), entity);
			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	@ApiOperation(value = "AccountingUnifiedSupplier - AccountingStatus - 操作 op确认对账:外部_商户端(未确认|待确认->已确认) 状态改变为 已确认_1")
	@RequestMapping(value = "/opConfirm/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opConfirm(@PathVariable("id") Long id) {

		R<?> result = new R<>();

		try {
			AccountingUnifiedSupplier entity = accountingUnifiedSupplierDubboConsumerService.findById(id);
			accountingUnifiedSupplierFieldAccountingStatusDubboConsumerService.opConfirm(getToken(), entity);
			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	@ApiOperation(value = "AccountingUnifiedSupplier - AccountingStatus - 操作 op申斥对账:外部_商户端(未确认|待确认->已拒绝) 状态改变为 已拒绝_2")
	@RequestMapping(value = "/opReject/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opReject(@PathVariable("id") Long id) {

		R<?> result = new R<>();

		try {
			AccountingUnifiedSupplier entity = accountingUnifiedSupplierDubboConsumerService.findById(id);
			accountingUnifiedSupplierFieldAccountingStatusDubboConsumerService.opReject(getToken(), entity);
			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}

	@ApiOperation(value = "AccountingUnifiedSupplier - AccountingStatus - 操作 op取消对账|op同意驳回|op重新对账 状态改变为 已取消_-1")
	@RequestMapping(value = "/opCancel/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opCancel(@PathVariable("id") Long id) {

		R<?> result = new R<>();

		try {
			AccountingUnifiedSupplier entity = accountingUnifiedSupplierDubboConsumerService.findById(id);

			accountingUnifiedSupplierFieldAccountingStatusDubboConsumerService.opCancel(getToken(), entity);

			result.setSuccess(true);

		} catch (Exception e) {

			result.setSuccess(false);
			result.setMessage(ResponseMsgConstants.ERROR_UNKNOW);
			log.error(ExceptionUtils.getMessage(e));
			e.printStackTrace();
		}
		return result;
	}


}
