package cn.superdesk.uniorder.service.order.base;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingLockingEnum;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingStateEnum;
import cn.superdesk.uniorder.common.enums.common.CommonDirectionEnum;
import cn.superdesk.uniorder.common.enums.common.CommonOrderGoodsUnitEnum;
import cn.superdesk.uniorder.common.enums.common.CommonOrderGoodsIdEnum;
import cn.superdesk.uniorder.common.enums.common.CommonOrderGoodsTypeEnum;
import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.entity.operation.UnifiedOrderIssues;
import cn.superdesk.uniorder.dto.param.setting.UnifiedServicePartGoodsTransportParam;
import cn.superdesk.uniorder.dto.receive.consumer.superdeskappservices.flashdeliver.FlashDeliverOrderReceiveParam;
import cn.superdesk.uniorder.service.setting.UnifiedServiceService;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Description: 处理杂费 形成 商品
 * @Author: linjinyu
 * @CreateDate: 2021/4/23 下午12:35
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/4/23 下午12:35
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class CommonOrderIncidentalExpensesProcessService {

	@Autowired
	private UnifiedServiceService unifiedServiceService;


	/**
	 * 核算 楼闪送 订单金额
	 *
	 * @param unifiedOrderEntity
	 * @param processUnifiedOrderGoodsList
	 */
	public UnifiedOrderIssues flashdeliverOrderAmountProcess(UnifiedOrder unifiedOrderEntity, List<UnifiedOrderGoods> processUnifiedOrderGoodsList) {

		log.info("{}", AnJsonUtils.toJson(processUnifiedOrderGoodsList));

		StringBuffer stringBuffer = new StringBuffer();

		if (AnCollectionUtils.isEmpty(processUnifiedOrderGoodsList)) {
			stringBuffer.append("原始订单商品Size:").append(processUnifiedOrderGoodsList.size()).append(";");
		}

		UnifiedOrderIssues unifiedOrderIssues = new UnifiedOrderIssues();
		unifiedOrderIssues.setIssuesComeFrom(1);
		unifiedOrderIssues.setUnifiedOrderId(unifiedOrderEntity.getId());
		unifiedOrderIssues.setOrderSn(unifiedOrderEntity.getOrderSn());
		unifiedOrderIssues.setIsProblem(0);
		unifiedOrderIssues.setIsProcess(0);
		unifiedOrderIssues.setIsResult(0);
		unifiedOrderIssues.setProblemReport("");
		unifiedOrderIssues.setProcessReport("");
		unifiedOrderIssues.setResultReport("");


		BigDecimal goodsAmount = processUnifiedOrderGoodsList
				.stream()
				.filter(o -> null != o.getGoodsType() && o.getGoodsType().compareTo(CommonOrderGoodsTypeEnum.商品.getKey()) == 0)
				.map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		BigDecimal goodsCostAmount = processUnifiedOrderGoodsList
				.stream()
				.filter(o -> null != o.getGoodsType() && o.getGoodsType().compareTo(CommonOrderGoodsTypeEnum.商品.getKey()) == 0)
				.map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		BigDecimal transportAmount = processUnifiedOrderGoodsList
				.stream()
				.filter(o -> null != o.getGoodsType() && o.getGoodsType().compareTo(CommonOrderGoodsTypeEnum.运费.getKey()) == 0)
				.map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		BigDecimal sundryFeesAmount = processUnifiedOrderGoodsList
				.stream()
				.filter(o -> null != o.getGoodsType() && o.getGoodsType().compareTo(CommonOrderGoodsTypeEnum.杂费.getKey()) == 0)
				.map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);


		// 这个是收客户的钱
		BigDecimal orderAmount     = goodsAmount.add(transportAmount).add(sundryFeesAmount);
		BigDecimal orderCostAmount = goodsCostAmount.add(transportAmount).add(sundryFeesAmount);


		if (unifiedOrderEntity.getAmount().compareTo(orderAmount) != 0) {

			stringBuffer.append("原始订单销售金额:").append(unifiedOrderEntity.getAmount()).append(";")
					.append("核算订单销售金额:").append(orderAmount).append(";");

//			unifiedOrderEntity.setAmount(orderAmount);
		}

		if (unifiedOrderEntity.getCostAmount().compareTo(orderCostAmount) != 0) {

			stringBuffer.append("原始订单成本金额:").append(unifiedOrderEntity.getCostAmount()).append(";")
					.append("核算订单成本金额:").append(orderCostAmount).append(";");

//			unifiedOrderEntity.setCostAmount(orderCostAmount);
		}


		/****************************************** 记录异常 start ******************************************/

		if (stringBuffer.length() > 0) {

			stringBuffer.insert(0, "69:异常订单数据;").append(unifiedOrderEntity.getRemarks());

			unifiedOrderIssues.setIsProblem(1);
			unifiedOrderIssues.setProblemReport(stringBuffer.toString());

//			unifiedOrderEntity.setRemarks(stringBuffer.toString());
		}

		unifiedOrderEntity.setGoodsAmount(goodsAmount);
		unifiedOrderEntity.setGoodsCostAmount(goodsCostAmount);

		return unifiedOrderIssues;

	}

	/**
	 * 供应商/客户 承担运费
	 *
	 * @param flashdeliverOrderReceiveParam
	 * @param processUnifiedOrderGoodsList
	 */
	public void flashdeliverOrderGoodsProcess(FlashDeliverOrderReceiveParam flashdeliverOrderReceiveParam, List<UnifiedOrderGoods> processUnifiedOrderGoodsList) {

		/**
		 ①费用定义:
		 ·用户配送费:用户支付的费用;
		 ·打包费:用户打包的费用;
		 ·商户配送费:商户需要支付给运营商的配送费,运营商与商户结算时需减掉商户配送费;
		 ·配送员配送费:配送员配送费用,商户与配送员结算;
		 ·目前商户无自配送业务,暂运营商无需结算商户自配送的费用给商户;

		 ②若有值需传值,无值即为传空,统一订单均解析为商品对账数据,费用字段数据需与业务方达成一致,统一订单需作数据标识,以便未来作主账单费用项细分;

		 ③用户配送费和打包费:此数据参与到统一订单对账中;

		 ④商户配送费和配送员配送费:
		 ·此数据不参与到统一订单对账中,无需对账;
		 ·此数据在统一订单仅作数据记录,不显示在"客户结算&供应商结算-未对账商品"列表中,未来有业务需要再根据业务调整;
		 **/

//		private BigDecimal transportAmountVirt;//快递运费(客户承担)		非必须	客户承担的快递运费
//
//		//
//		private BigDecimal packingAmount;//打包费 统一订单待补充字段
//		// 客户/供应商都不结算
//		private BigDecimal saleDeliveryAmount;//商户配送费		 统一订单待补充字段
//		// 客户/供应商都不结算
//		private BigDecimal deliveryPersonAmount;//配送员配送费 统一订单待补充字段

		UnifiedServicePartGoodsTransportParam defaultGoodsTransport = unifiedServiceService.getUnifiedServicePartGoodsTransport(flashdeliverOrderReceiveParam.getServiceNo());

		if (null != flashdeliverOrderReceiveParam.getTransportAmountVirt() && flashdeliverOrderReceiveParam.getTransportAmountVirt().compareTo(BigDecimal.ZERO) > 0) {

			UnifiedOrderGoods unifiedOrderGoods = new UnifiedOrderGoods();

			unifiedOrderGoods.setDirection(CommonDirectionEnum.正向.getKey());

			unifiedOrderGoods.setGoodsLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
			unifiedOrderGoods.setGoodsLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

			unifiedOrderGoods.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());
			unifiedOrderGoods.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());
			unifiedOrderGoods.setGoodsType(CommonOrderGoodsTypeEnum.运费.getKey());

			unifiedOrderGoods.setOrderSn(flashdeliverOrderReceiveParam.getOrderSn());

			unifiedOrderGoods.setGoodsId(CommonOrderGoodsIdEnum.用户配送费.getKey());
			unifiedOrderGoods.setGoodsName(CommonOrderGoodsIdEnum.用户配送费.getDesc());
			unifiedOrderGoods.setUnit(CommonOrderGoodsUnitEnum.项.getDesc());

			unifiedOrderGoods.setQuantity(1);
			unifiedOrderGoods.setPrice(flashdeliverOrderReceiveParam.getTransportAmountVirt());
			unifiedOrderGoods.setCostPrice(flashdeliverOrderReceiveParam.getTransportAmountVirt());

//			defaultGoodsTransport
			unifiedOrderGoods.setFiscalCode(defaultGoodsTransport.getGoodsTransportFiscalCode());
			unifiedOrderGoods.setTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			// 商户税率
			unifiedOrderGoods.setMerchTaxId(0L);
			unifiedOrderGoods.setMerchTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setMerchTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			// 平台税率
			unifiedOrderGoods.setOperationTaxId(0L);
			unifiedOrderGoods.setOperationTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setOperationTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			unifiedOrderGoods.setCreateTime(DateUtil.date());
			unifiedOrderGoods.setCreateUserId(1L);
			unifiedOrderGoods.setUpdateTime(DateUtil.date());
			unifiedOrderGoods.setUpdateUserId(1L);

			processUnifiedOrderGoodsList.add(unifiedOrderGoods);

		}

		if (null != flashdeliverOrderReceiveParam.getPackingAmount() && flashdeliverOrderReceiveParam.getPackingAmount().compareTo(BigDecimal.ZERO) > 0) {

			UnifiedOrderGoods unifiedOrderGoods = new UnifiedOrderGoods();

			unifiedOrderGoods.setDirection(CommonDirectionEnum.正向.getKey());

			unifiedOrderGoods.setGoodsLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
			unifiedOrderGoods.setGoodsLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

			unifiedOrderGoods.setAccountingStateCustomer(CommonAccountingStateEnum.未对账.getKey());
			unifiedOrderGoods.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());

			unifiedOrderGoods.setGoodsType(CommonOrderGoodsTypeEnum.杂费.getKey());

			unifiedOrderGoods.setOrderSn(flashdeliverOrderReceiveParam.getOrderSn());

			unifiedOrderGoods.setGoodsId(CommonOrderGoodsIdEnum.打包费.getKey());
			unifiedOrderGoods.setGoodsName(CommonOrderGoodsIdEnum.打包费.getDesc());
			unifiedOrderGoods.setUnit(CommonOrderGoodsUnitEnum.项.getDesc());

			unifiedOrderGoods.setQuantity(1);
			unifiedOrderGoods.setPrice(flashdeliverOrderReceiveParam.getPackingAmount());
			unifiedOrderGoods.setCostPrice(flashdeliverOrderReceiveParam.getPackingAmount());

//			defaultGoodsTransport
//			unifiedOrderGoods.setFiscalCode(defaultGoodsTransport.getGoodsTransportFiscalCode());
//			unifiedOrderGoods.setTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//			unifiedOrderGoods.setTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());
//
//			// 商户税率
//			unifiedOrderGoods.setMerchTaxId(0L);
//			unifiedOrderGoods.setMerchTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//			unifiedOrderGoods.setMerchTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());
//
//			// 平台税率
//			unifiedOrderGoods.setOperationTaxId(0L);
//			unifiedOrderGoods.setOperationTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//			unifiedOrderGoods.setOperationTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			unifiedOrderGoods.setCreateTime(DateUtil.date());
			unifiedOrderGoods.setCreateUserId(1L);
			unifiedOrderGoods.setUpdateTime(DateUtil.date());
			unifiedOrderGoods.setUpdateUserId(1L);

			processUnifiedOrderGoodsList.add(unifiedOrderGoods);

		}

		if (null != flashdeliverOrderReceiveParam.getSaleDeliveryAmount() && flashdeliverOrderReceiveParam.getSaleDeliveryAmount().compareTo(BigDecimal.ZERO) > 0) {

			UnifiedOrderGoods unifiedOrderGoods = new UnifiedOrderGoods();

			unifiedOrderGoods.setDirection(CommonDirectionEnum.正向.getKey());

			unifiedOrderGoods.setGoodsLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
			unifiedOrderGoods.setGoodsLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

			unifiedOrderGoods.setAccountingStateCustomer(CommonAccountingStateEnum.不对账.getKey());
			unifiedOrderGoods.setAccountingStateSupplier(CommonAccountingStateEnum.不对账.getKey());
			unifiedOrderGoods.setGoodsType(CommonOrderGoodsTypeEnum.杂费.getKey());

			unifiedOrderGoods.setOrderSn(flashdeliverOrderReceiveParam.getOrderSn());

			unifiedOrderGoods.setGoodsId(CommonOrderGoodsIdEnum.商户配送费.getKey());
			unifiedOrderGoods.setGoodsName(CommonOrderGoodsIdEnum.商户配送费.getDesc());
			unifiedOrderGoods.setUnit(CommonOrderGoodsUnitEnum.项.getDesc());

			unifiedOrderGoods.setQuantity(1);
			unifiedOrderGoods.setPrice(flashdeliverOrderReceiveParam.getSaleDeliveryAmount());
			unifiedOrderGoods.setCostPrice(flashdeliverOrderReceiveParam.getSaleDeliveryAmount());

//			defaultGoodsTransport
//			unifiedOrderGoods.setFiscalCode(defaultGoodsTransport.getGoodsTransportFiscalCode());
//			unifiedOrderGoods.setTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
//			unifiedOrderGoods.setTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());
//
//			商户税率
			unifiedOrderGoods.setMerchTaxId(0L);
			unifiedOrderGoods.setMerchTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setMerchTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

//			平台税率
			unifiedOrderGoods.setOperationTaxId(0L);
			unifiedOrderGoods.setOperationTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setOperationTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			unifiedOrderGoods.setCreateTime(DateUtil.date());
			unifiedOrderGoods.setCreateUserId(1L);
			unifiedOrderGoods.setUpdateTime(DateUtil.date());
			unifiedOrderGoods.setUpdateUserId(1L);

			processUnifiedOrderGoodsList.add(unifiedOrderGoods);

		}

		if (null != flashdeliverOrderReceiveParam.getDeliveryPersonAmount() && flashdeliverOrderReceiveParam.getDeliveryPersonAmount().compareTo(BigDecimal.ZERO) > 0) {

			UnifiedOrderGoods unifiedOrderGoods = new UnifiedOrderGoods();

			unifiedOrderGoods.setDirection(CommonDirectionEnum.正向.getKey());

			unifiedOrderGoods.setGoodsLockingCustomer(CommonAccountingLockingEnum.正常.getKey());
			unifiedOrderGoods.setGoodsLockingSupplier(CommonAccountingLockingEnum.正常.getKey());

			unifiedOrderGoods.setAccountingStateCustomer(CommonAccountingStateEnum.不对账.getKey());
			unifiedOrderGoods.setAccountingStateSupplier(CommonAccountingStateEnum.不对账.getKey());
			unifiedOrderGoods.setGoodsType(CommonOrderGoodsTypeEnum.杂费.getKey());

			unifiedOrderGoods.setOrderSn(flashdeliverOrderReceiveParam.getOrderSn());

			unifiedOrderGoods.setGoodsId(CommonOrderGoodsIdEnum.配送员配送费.getKey());
			unifiedOrderGoods.setGoodsName(CommonOrderGoodsIdEnum.配送员配送费.getDesc());
			unifiedOrderGoods.setUnit(CommonOrderGoodsUnitEnum.项.getDesc());

			unifiedOrderGoods.setQuantity(1);
			unifiedOrderGoods.setPrice(flashdeliverOrderReceiveParam.getDeliveryPersonAmount());
			unifiedOrderGoods.setCostPrice(flashdeliverOrderReceiveParam.getDeliveryPersonAmount());

//			defaultGoodsTransport
			unifiedOrderGoods.setFiscalCode(defaultGoodsTransport.getGoodsTransportFiscalCode());
			unifiedOrderGoods.setTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			// 商户税率
			unifiedOrderGoods.setMerchTaxId(0L);
			unifiedOrderGoods.setMerchTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setMerchTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			// 平台税率
			unifiedOrderGoods.setOperationTaxId(0L);
			unifiedOrderGoods.setOperationTaxCode(defaultGoodsTransport.getGoodsTransportTaxCode());
			unifiedOrderGoods.setOperationTaxRate(defaultGoodsTransport.getGoodsTransportTaxRate());

			unifiedOrderGoods.setCreateTime(DateUtil.date());
			unifiedOrderGoods.setCreateUserId(1L);
			unifiedOrderGoods.setUpdateTime(DateUtil.date());
			unifiedOrderGoods.setUpdateUserId(1L);

			processUnifiedOrderGoodsList.add(unifiedOrderGoods);

		}
	}
}
