package cn.superdesk.uniorder.dubbo.provider.invoice.customer.fundrestream;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.enums.invoice.customer.InvoiceApplyCustomerBackAccountStatusEnum;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceBankOffHxStatusEnum;
import cn.superdesk.uniorder.common.enums.invoice.InvoiceBankStatusEnum;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatement;
import cn.superdesk.uniorder.dao.entity.invoice.customer.fundrestream.InvoiceBankStatementOff;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedInvBankOffVo;
import cn.superdesk.uniorder.dubbo.facade.invoice.customer.fundrestream.DubboInvoiceBankStatementOffService;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.service.invoice.customer.fundrestream.InvoiceBankStatementService;
import cn.superdesk.uniorder.service.invoice.customer.fundrestream.InvoiceBankStatementOffService;
import cn.superdesk.uniorder.common.security.crypt.SuperdeskUniorderRsaUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboInvoiceBankStatementOffServiceImpl implements DubboInvoiceBankStatementOffService {

	@Autowired
	private InvoiceBankStatementService    invoiceBankStatementService;
	@Autowired
	private InvoiceBankStatementOffService invoiceBankStatementOffService;

	@Autowired
	private InvoiceApplyCustomerService invoiceApplyService;

	@Override
	public List<InvoiceBankStatement> getInvoiceBankData(Map<String, Object> params) {
		return invoiceBankStatementService.findAll(params);
	}

	@Override
	public List<InvoiceApply> getInvoiceApplyData(Map<String, Object> params) {
		return invoiceApplyService.findAll(params);
	}

	/**
	 * opt 核销(提交)
	 *
	 * @param invoiceBankStatementOff
	 * @return
	 */
	@Override
	public Map<String, Object> addObjTran(InvoiceBankStatementOff invoiceBankStatementOff) {

		Map<String, Object> resultMap = new HashMap<>();

		// 核销操作

		/**
		 * 银行流水
		 */
		String[] invoiceBankIdsArray = invoiceBankStatementOff.getInvBankIds().split(",");
		/**
		 * 发票
		 */
		String[] invoiceApplyIdsArray = invoiceBankStatementOff.getInvIds().split(",");

		List<InvoiceBankStatement> invoiceBankEntityTodoList  = new ArrayList<>(); // 银行流水
		List<InvoiceApply>         invoiceApplyEntityTodoList = new ArrayList<>(); // 发票流水
		List<UnifiedOrder>         unifiedOrderEntityTodoList = new ArrayList<>(); // 订单流水


		// 收款金额 = 发票金额
		if (invoiceBankStatementOff.getInvAmount().doubleValue() == invoiceBankStatementOff.getInvBankAmount().doubleValue()) {

			// 1 更新银行流水
			for (int i = 0; i < invoiceBankIdsArray.length; i++) {

				InvoiceBankStatement invoiceBank = new InvoiceBankStatement();
				invoiceBank.setId(Long.parseLong(invoiceBankIdsArray[i]));
				invoiceBank.setNoCheckCount(new BigDecimal(0));
				invoiceBank.setStatus(InvoiceBankStatusEnum.全部核销.getKey());
				invoiceBankEntityTodoList.add(invoiceBank);
			}

			// 2 更新发票流水-订单流水
			for (int i = 0; i < invoiceApplyIdsArray.length; i++) {

				InvoiceApply invoiceApply = new InvoiceApply();

				invoiceApply.setId(Long.parseLong(invoiceApplyIdsArray[i]));
				invoiceApply.setNoCheckCount(new BigDecimal(0));
				invoiceApply.setBackAccountStatus(InvoiceApplyCustomerBackAccountStatusEnum.已回款.getKey());
				invoiceApplyEntityTodoList.add(invoiceApply);

				// TODO 未修正 先屏蔽
//				List<Long> orderIds = unifiedInvBankOffService.getOrderIdsByInvoiceApplyId(Long.parseLong(invoiceApplyIdsArray[i]));
//				for (int j = 0; j < orderIds.size(); j++) {
//					UnifiedOrder unifiedOrder = new UnifiedOrder();
//					unifiedOrder.setId(orderIds.get(i));
//					// TODO 写死
//					unifiedOrder.setPayState(1);
//					unifiedOrderEntityTodoList.add(unifiedOrder);
//				}
			}

			// 3 记录核销操作
			invoiceBankStatementOff.setHxStatus(InvoiceBankOffHxStatusEnum.完成.getKey());
			invoiceBankStatementOff.setHxSn("HX" + SuperdeskUniorderRsaUtils.genId("MB"));

		}
		// 收款金额 < 发票金额
		else if (invoiceBankStatementOff.getInvAmount().doubleValue() > invoiceBankStatementOff.getInvBankAmount().doubleValue()) {


			// 为流水全额
			double tempAmount = invoiceBankStatementOff.getInvBankAmount().doubleValue();

			// 2 更新发票流水-订单流水
			for (int i = 0; i < invoiceApplyIdsArray.length; i++) {

				if (tempAmount == 0) {
					break;
				}

				InvoiceApply invoiceApplySearch = invoiceApplyService.findById(Long.parseLong(invoiceApplyIdsArray[i]));

				InvoiceApply invoiceApply = new InvoiceApply();

				invoiceApply.setId(Long.parseLong(invoiceApplyIdsArray[i]));

				if (tempAmount > invoiceApplySearch.getNoCheckCount().doubleValue()) {

					invoiceApply.setNoCheckCount(new BigDecimal(0));
					tempAmount -= invoiceApplySearch.getNoCheckCount().doubleValue();
					invoiceApply.setBackAccountStatus(InvoiceApplyCustomerBackAccountStatusEnum.已回款.getKey());

					// TODO 未修正 先屏蔽
//					List<Long> orderIds = unifiedInvBankOffService.getOrderIdsByInvoiceApplyId(Long.parseLong(invoiceApplyIdsArray[i]));
//					for (int j = 0; j < orderIds.size(); j++) {
//						UnifiedOrder unifiedOrder = new UnifiedOrder();
//						unifiedOrder.setId(orderIds.get(i));
//						// TODO 写死
//						unifiedOrder.setPayState(1);
//						unifiedOrderEntityTodoList.add(unifiedOrder);
//					}

				} else {
					invoiceApply.setNoCheckCount(new BigDecimal(invoiceApplySearch.getNoCheckCount().doubleValue()-tempAmount));
					tempAmount = 0;
					invoiceApply.setBackAccountStatus(InvoiceApplyCustomerBackAccountStatusEnum.部分回款.getKey());

					// TODO 未修正 先屏蔽
					// ------- 逻辑是错的 这个支付状态不能混用 TODO 要为订单新增一个字段 回款状态 start
//					List<Long> orderIds = unifiedInvBankOffService.getOrderIdsByInvoiceApplyId(Long.parseLong(invoiceApplyIdsArray[i]));
//					for (int j = 0; j < orderIds.size(); j++) {
//						UnifiedOrder unifiedOrder = new UnifiedOrder();
//
//						unifiedOrder.setId(orderIds.get(i));
//						unifiedOrder.setPayState(3);
//						unifiedOrderEntityTodoList.add(unifiedOrder);
//					}
					// ------- 逻辑是错的 这个支付状态不能混用 TODO 要为订单新增一个字段 回款状态 end
				}

				invoiceApplyEntityTodoList.add(invoiceApply);
			}

			// 1 更新银行流水
			for (int i = 0; i < invoiceBankIdsArray.length; i++) {

				InvoiceBankStatement invoiceBank = new InvoiceBankStatement();

				invoiceBank.setId(Long.parseLong(invoiceBankIdsArray[i]));
				invoiceBank.setNoCheckCount(new BigDecimal(0));
				invoiceBank.setStatus(InvoiceBankStatusEnum.全部核销.getKey());

				invoiceBankEntityTodoList.add(invoiceBank);
			}

			// 3 记录核销操作
			invoiceBankStatementOff.setHxStatus(InvoiceBankOffHxStatusEnum.完成.getKey());
			invoiceBankStatementOff.setHxSn("HX" + SuperdeskUniorderRsaUtils.genId("MB"));

		}
		// 收款金额 > 发票金额
		else {


			double tempAmount = invoiceBankStatementOff.getInvAmount().doubleValue();

			// 1 更新银行流水
			for (int i = 0; i < invoiceBankIdsArray.length; i++) {

				if (tempAmount == 0) {
					break;
				}

				InvoiceBankStatement invoiceBank       = new InvoiceBankStatement();
				InvoiceBankStatement invoiceBankSearch = invoiceBankStatementService.findById(Long.parseLong(invoiceBankIdsArray[i]));

				if (tempAmount > invoiceBankSearch.getNoCheckCount().doubleValue()) {
					invoiceBank.setNoCheckCount(new BigDecimal(0));
					tempAmount -= invoiceBankSearch.getNoCheckCount().doubleValue();
					invoiceBank.setStatus(InvoiceBankStatusEnum.全部核销.getKey());
				} else {

					// 发票的总金额 < 第N条流水的未核销金额
					// 所以 更新 流水未核销金额 = 原来的 流水未核销金额-发票的总金额
					invoiceBank.setNoCheckCount(new BigDecimal(invoiceBankSearch.getNoCheckCount().doubleValue()-tempAmount));
					// 发票的总金额 = 0
					tempAmount = 0;
					invoiceBank.setStatus(InvoiceBankStatusEnum.部分核销.getKey());
				}

				invoiceBank.setId(Long.parseLong(invoiceBankIdsArray[i]));
				invoiceBankEntityTodoList.add(invoiceBank);

			}

			// 2 更新发票流水-订单流水
			for (int i = 0; i < invoiceApplyIdsArray.length; i++) {

				InvoiceApply invoiceApply = new InvoiceApply();

				invoiceApply.setId(Long.parseLong(invoiceApplyIdsArray[i]));
				invoiceApply.setNoCheckCount(new BigDecimal(0));
				invoiceApply.setBackAccountStatus(InvoiceApplyCustomerBackAccountStatusEnum.已回款.getKey());

				invoiceApplyEntityTodoList.add(invoiceApply);

				// TODO 未修正 先屏蔽
				// ------- 逻辑是错的 这个支付状态不能混用 TODO 要为订单新增一个字段 回款状态 start

//				List<Long> orderIds = unifiedInvBankOffService.getOrderIdsByInvoiceApplyId(Long.parseLong(invoiceApplyIdsArray[i]));
//				for (int j = 0; j < orderIds.size(); j++) {
//					UnifiedOrder unifiedOrder = new UnifiedOrder();
//					unifiedOrder.setId(orderIds.get(i));
//					unifiedOrder.setPayState(1);
//					unifiedOrderEntityTodoList.add(unifiedOrder);
//				}
				// ------- 逻辑是错的 这个支付状态不能混用 TODO 要为订单新增一个字段 回款状态 end
			}

			// 3 记录核销操作
			invoiceBankStatementOff.setHxStatus(InvoiceBankOffHxStatusEnum.完成.getKey());
			invoiceBankStatementOff.setHxSn("HX" + SuperdeskUniorderRsaUtils.genId("MB"));

		}

		InvoiceBankStatementOff resInvBankOff = invoiceBankStatementOffService.addObjTran(
				invoiceBankStatementOff,
				invoiceBankEntityTodoList,
				invoiceApplyEntityTodoList,
				unifiedOrderEntityTodoList
		);

		if (resInvBankOff.getId() > 0) {

			resultMap.put("code", "1");
			resultMap.put("data", resInvBankOff);

			return resultMap;
		} else {

			resultMap.put("code", "400");
			resultMap.put("msg", "核销失败,请检查数据！");

			return resultMap;
		}

	}

	@Override
	public R<List<UnifiedInvBankOffVo>> getInvoiceBankOffDataByPage(Map<String, Object> params, int pageNum, int pageSize) {
		return invoiceBankStatementOffService.getInvoiceBankOffDataByPage(params, pageNum, pageSize);
	}
}
