package cn.superdesk.uniorder.controller.invoice.customer.invoiced;

import cn.hutool.core.lang.Snowflake;
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.dto.param.invoice.excel.InvoiceInvoicedOpBackCollectionForm;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedFieldIsBackCollectionDubboConsumerService;
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.*;

import javax.annotation.Resource;

/**
 * @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 = "InvoiceInvoiced-IsBackCollection", tags = {"InvoiceInvoiced-IsBackCollection"})
@Slf4j
@RestController
@RequestMapping("/invoiceInvoiced/state/isBackCollection")
public class InvoiceInvoicedFieldIsBackCollectionController extends BaseController {

	@Autowired
	private InvoiceInvoicedDubboConsumerService                      invoiceInvoicedDubboConsumerService;
	@Autowired
	private InvoiceInvoicedFieldIsBackCollectionDubboConsumerService invoiceInvoicedFieldIsBackCollectionDubboConsumerService;

	@Resource
	private Snowflake snowflake;

	@ApiOperation(value = "InvoiceInvoiced-IsBackCollection-操作 初始化或重置 状态改变为 未回款_0")
	@RequestMapping(value = "/opInitOrReset", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opInitOrReset(@RequestBody InvoiceInvoicedOpBackCollectionForm form) {

		R<?> result = new R<>();
		form.setBatchId(snowflake.nextIdStr());

		try {
//			InvoiceInvoiced entity = invoiceInvoicedDubboConsumerService.findById(id);
			invoiceInvoicedFieldIsBackCollectionDubboConsumerService.opInitOrReset(getToken(), form);
			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 = "InvoiceInvoiced-IsBackCollection-操作 登记回款-部分回款 状态改变为 部分回款_1")
	@RequestMapping(value = "/opPart", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opPart(@RequestBody InvoiceInvoicedOpBackCollectionForm form) {

		R<?> result = new R<>();
		form.setBatchId(snowflake.nextIdStr());

		try {
//			InvoiceInvoiced entity = invoiceInvoicedDubboConsumerService.findById(id);
			invoiceInvoicedFieldIsBackCollectionDubboConsumerService.opPart(getToken(), form);
			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 = "InvoiceInvoiced-IsBackCollection-操作 登记回款-全部回款-已回款 状态改变为 全部回款-已回款_2")
	@RequestMapping(value = "/opComplete", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opComplete(@RequestBody InvoiceInvoicedOpBackCollectionForm form) {

		R<?> result = new R<>();
		form.setBatchId(snowflake.nextIdStr());

		try {
//			InvoiceInvoiced entity = invoiceInvoicedDubboConsumerService.findById(id);
			invoiceInvoicedFieldIsBackCollectionDubboConsumerService.opComplete(getToken(), form);
			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 = "InvoiceInvoiced-IsBackCollection-操作 红冲发票 状态改变为 红冲发票_69")
	@RequestMapping(value = "/opRedWriteOff", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opRedWriteOff(@RequestBody InvoiceInvoicedOpBackCollectionForm form) {

		R<?> result = new R<>();
		form.setBatchId(snowflake.nextIdStr());

		try {
//			InvoiceInvoiced entity = invoiceInvoicedDubboConsumerService.findById(id);
			invoiceInvoicedFieldIsBackCollectionDubboConsumerService.opRedWriteOff(getToken(), form);
			result.setSuccess(true);

		} catch (Exception e) {

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


}
