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

import cn.hutool.core.lang.Snowflake;
import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.constants.ResponseMsgConstants;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceInvoicedIsInvalidEnum;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerBackAccountStatusEnum;
import cn.superdesk.uniorder.common.exception.utils.ExceptionUtils;
import cn.superdesk.uniorder.common.mq.kafka.producer.FakeSuperdeskUniOrderProducerHandler;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dto.param.invoice.customer.invoiced.InvoiceInvoicedQueryCriteria;
import cn.superdesk.uniorder.dto.vo.invoice.customer.invoiced.InvoiceInvoicedExcelExportVo;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.invoice.customer.invoiced.InvoiceInvoicedFieldIsOverdueDubboConsumerService;
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;
import java.util.Arrays;
import java.util.List;

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

	@Autowired
	private InvoiceInvoicedDubboConsumerService               invoiceInvoicedDubboConsumerService;
	@Autowired
	private InvoiceInvoicedFieldIsOverdueDubboConsumerService invoiceInvoicedFieldIsOverdueDubboConsumerService;

	@Resource
	private FakeSuperdeskUniOrderProducerHandler fakeSuperdeskUniOrderProducerHandler;

	@Resource
	private Snowflake snowflake;

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

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

		try {
			invoiceInvoicedFieldIsOverdueDubboConsumerService.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-IsOverdue-操作 计划任务检查-已逾期 状态改变为 逾期_1")
	@RequestMapping(value = "/opOverdued", method = RequestMethod.POST)
	@ResponseBody
	public R<?> opOverdued(@RequestBody InvoiceInvoicedExcelExportVo form) {

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

		try {
			invoiceInvoicedFieldIsOverdueDubboConsumerService.opOverdued(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;
	}


	/**
	 * http://192.168.6.223:8089/web/invoiceInvoiced/state/isOverdue/overdueTask
	 * <p>
	 * SELECT * FROM `invoice_invoiced` WHERE update_user_id = 1 ORDER BY invoiced_date ASC
	 * <p>
	 * SELECT * FROM `invoice_invoiced` WHERE `back_collection_amount_wait_for` > 0
	 *
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "应收账款-逾期查询-测试-主动调用")
	@RequestMapping(value = "/overdueTask", method = RequestMethod.POST)
	@ResponseBody
	public R<List<InvoiceInvoicedExcelExportVo>> pageQueryInvoiceInvoicedsOverdueTask(@RequestBody PageQueryGenericParam<InvoiceInvoicedQueryCriteria> param) {

		// 假定这个企业账期 30天
//		param.getConditions().setOverdueDateSub(30);
		// 是否作废-是否作废:0-正常;1-作废;为空-全部;
		param.getConditions().setIsInvalid(InvoiceInvoicedIsInvalidEnum.正常.getKey());
		param.getConditions().setIsBackCollectionIds(Arrays.asList(
				InvoiceApplyCustomerBackAccountStatusEnum.未回款.getKey(),
				InvoiceApplyCustomerBackAccountStatusEnum.部分回款.getKey()));

		/****************************************** 条件 start ******************************************/

		// 是否回款_回款状态:0-未回款;1-部分回款;2-全部回款;69-红冲发票;
		// is_back_collection
		// 回款时间 eg 2021-03-03 10:02:34
		// back_collection_time 如果是空的
		// 已回款金额 = 0
		// back_collection_amount_already
		// 待回款金额
		// back_collection_amount_wait_for
		// 是否逾期 0<=逾期天数 =IF(0<=B917,已逾期,未逾期):0-未逾期;1-已逾期;
		// is_overdue
		// 逾期天数 当前日期-开票日期-30
		// overdue_days
		// 是否作废:0-正常;1-作废
		// is_invalid
		// 软删除:0-未删除;1-已删除
		// deleted

		/****************************************** 推导 start ******************************************/

		// 更新的是
		// 是否逾期 0<=逾期天数 =IF(0<=B917,已逾期,未逾期):0-未逾期;1-已逾期;
		// is_overdue
		// 逾期天数 当前日期-开票日期-30
		// overdue_days


		log.info("pageQueryInvoiceInvoiced:\n{}", JSONUtil.toJsonStr(param));

		R<List<InvoiceInvoicedExcelExportVo>> result = new R<>();

		// TODO 参数合法性判定

		List<InvoiceInvoicedExcelExportVo> todoList = invoiceInvoicedDubboConsumerService.queryByCriteria(param.getConditions());

		String batchId = snowflake.nextIdStr();
		// 走kafka
		todoList.forEach(taskParams -> {
			taskParams.setBatchId(batchId);
			fakeSuperdeskUniOrderProducerHandler.kafkaInvoiceInvoicedsOverdueTask(taskParams);
		});

		// 直接处理
//		todoList.forEach(taskParams -> {
//			taskParams.setBatchId(batchId);
//			invoiceInvoicedFieldIsOverdueDubboConsumerService.processInvoiceInvoicedsOverdueTask(getToken(), taskParams);
//		});

		result.setData(todoList);

		return result;
	}


}
