package cn.superdesk.uniorder.service.accounting.unified.customer.impl;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingStateEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderDimCreateByEnum;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerXGoods;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrderGoods;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.customer.*;
import cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderGoodsMapperEx;
import cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapper;
import cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderMapperEx;
import cn.superdesk.uniorder.dao.mapper.base.UnifiedOrderReturnGoodsMapperEx;
import cn.superdesk.uniorder.dao.mapper.invoice.customer.InvoiceApplyMapperEx;
import cn.superdesk.uniorder.dao.mapper.organisation.SysEnterpriseMapperEx;
import cn.superdesk.uniorder.dao.mapper.organisation.SysOrganizationMapperEx;
import cn.superdesk.uniorder.dao.mapper.scheduler.SysQuartzScheduleTaskStatementOfAccountMapper;
import cn.superdesk.uniorder.dao.mapper.sys.SysConfigMapper;
import cn.superdesk.uniorder.dao.mapper.sys.SysOderUserMapperEx;
import cn.superdesk.uniorder.dao.mapper.sys.SysOrderMapper;
import cn.superdesk.uniorder.dao.mapper.invoice.common.InvoiceBankInfoMapperEx;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.service.accounting.unified.customer.*;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderCreditService;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderGoodsService;
import cn.superdesk.uniorder.service.order.base.UnifiedOrderService;
import cn.superdesk.uniorder.service.sys.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/1/20 下午5:03
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/1/20 下午5:03
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedCustomerServiceOpEditDimGoodsImpl implements AccountingUnifiedCustomerServiceOpEditDimGoods {

	@Autowired
	private AccountingUnifiedCustomerMapper      accountingUnifiedCustomerMapper;
	@Autowired
	private AccountingUnifiedCustomerMapperEx    accountingUnifiedCustomerMapperEx;
	@Autowired
	private AccountingUnifiedCustomerMapperExSys accountingUnifiedCustomerMapperExSys;

	@Autowired
	private AccountingUnifiedCustomerXGoodsMapper  accountingUnifiedCustomerXGoodsMapper;
	@Autowired
	private AccountingUnifiedCustomerXGoodsService accountingUnifiedCustomerXGoodsService;
	@Autowired
	private AccountingUnifiedCustomerServiceCommon accountingUnifiedCustomerServiceCommon;

	/****************************************** 对账单 订单 商品 表 将要弃用 start ******************************************/
	@Autowired
	private AccountingUnifiedCustomerOrderMapper   accountingUnifiedCustomerOrderMapper;
	@Autowired
	private AccountingUnifiedCustomerOrderMapperEx accountingUnifiedCustomerOrderMapperEx;

	@Autowired
	private AccountingUnifiedCustomerOrderGoodsMapper   accountingUnifiedCustomerOrderGoodsMapper;
	@Autowired
	private AccountingUnifiedCustomerOrderGoodsMapperEx accountingUnifiedCustomerOrderGoodsMapperEx;

	@Autowired
	private AccountingUnifiedCustomerOrderService      accountingUnifiedCustomerOrderService;
	@Autowired
	private AccountingUnifiedCustomerOrderGoodsService accountingUnifiedCustomerOrderGoodsService;

	/****************************************** 对账单 订单 商品 表 将要弃用   end ******************************************/

	@Autowired
	private InvoiceBankInfoMapperEx unifiedBankInfoMapper;

	@Autowired
	private SysConfigMapper     sysConfigMapper;
	@Autowired
	private SysOrderMapper      sysOrderMapper;
	@Autowired
	private SysOderUserMapperEx sysOderUserMapper;

	@Autowired
	private UnifiedOrderMapper              unifiedOrderMapper;
	@Autowired
	private UnifiedOrderMapperEx            unifiedOrderMapperEx;
	@Autowired
	private UnifiedOrderGoodsMapperEx       unifiedOrderGoodsMapperEx;
	@Autowired
	private UnifiedOrderReturnGoodsMapperEx unifiedOrderReturnGoodsMapperEx;


	@Autowired
	private UnifiedOrderService       unifiedOrderService;
	@Autowired
	private UnifiedOrderGoodsService  unifiedOrderGoodsService;
	@Autowired
	private UnifiedOrderCreditService unifiedOrderCreditService;

	@Autowired
	private SysQuartzScheduleTaskStatementOfAccountMapper sysQuartzScheduleTaskStatementOfAccountMapper;

	@Autowired
	private SysEnterpriseMapperEx   sysEnterpriseMapperEx;
	@Autowired
	private SysOrganizationMapperEx sysOrganizationMapper;

	@Autowired
	private InvoiceApplyMapperEx invoiceApplyMapperEx;

	@Autowired
	private SysConfigService sysConfigService;

	/****************************************** 对账单 编辑 start ******************************************/


	@Transactional
	@Override
	public void opEditAccountingDimGoods(AccountingUnifiedCustomer statementOfAccount, UnifiedOrder processUnifiedOrder, UnifiedOrderGoods processUnifiedOrderGoods, /* 方向 1 新增 非1 删除 加入 orderSn 对映的商品 */String add, LoginSysUser loginSysUser) {


		// 要动的表有三
		// AccountingUnifiedCustomerXGoods 订单 商品 维度都有
		// 订单
		// 商品

		List<String> processOrderSn = Arrays.asList(processUnifiedOrder.getOrderSn());

		List<UnifiedOrderGoods> unifiedOrderGoodsList          = null;
		List<Long>              processSysUnifiedOrderGoodsIds = null;

		// 0-订单;1-商品;2-流水; 默认为1;
		Integer modeBy = 1;

		// 如果为空 说明要以 订单维度去操作
		if (null == processUnifiedOrderGoods) {
			modeBy = 0;
			unifiedOrderGoodsList = unifiedOrderGoodsService.findByOrderSns(processOrderSn);

		} else {
			modeBy = 1;
			unifiedOrderGoodsList = Arrays.asList(processUnifiedOrderGoods);
		}

		processSysUnifiedOrderGoodsIds = unifiedOrderGoodsList.stream().map(o -> o.getId()).collect(Collectors.toList());


		if (add.equals("1")) {

			// 处理 对账单 X 订单 X 商品
			// 大概的作用类似批次
			Date processDate = DateUtil.date();

			List<AccountingUnifiedCustomerXGoods> processAccountingUnifiedCustomerXGoodsList = new ArrayList<>();

			for (UnifiedOrderGoods unifiedOrderGoods : unifiedOrderGoodsList) {


				/****************************************** 关联 start ******************************************/
				AccountingUnifiedCustomerXGoods xGoods = new AccountingUnifiedCustomerXGoods();

				xGoods.setDimCreateBy(AccountingOrderDimCreateByEnum.商品.getKey());

				xGoods.setAccountingOrderId(statementOfAccount.getId());
				xGoods.setAccountingOrderSn(statementOfAccount.getAccountingOrderSn());

				// 与创建时的差别
				xGoods.setUnifiedOrderId(processUnifiedOrder.getId());
				xGoods.setOrderSn(processUnifiedOrder.getOrderSn());

				xGoods.setUnifiedOrderGoodsId(unifiedOrderGoods.getId());
				xGoods.setGoodsId(unifiedOrderGoods.getGoodsId());
				xGoods.setOutSnapshotOrderGoodsId(unifiedOrderGoods.getOutSnapshotOrderGoodsId());

				xGoods.setCreateUserId(1L);
				xGoods.setCreateTime(processDate);
				xGoods.setUpdateUserId(1L);
				xGoods.setCreateTime(processDate);

				processAccountingUnifiedCustomerXGoodsList.add(xGoods);

				/****************************************** 关联   end ******************************************/
			}

			// 重构 再再 mark by linjinyu at 20210202 修改为 批量插入
			accountingUnifiedCustomerXGoodsService.insertBatch(processAccountingUnifiedCustomerXGoodsList);


			// 更新 订单 客户对账状态 -> 已对账
			unifiedOrderService.updateAccountingStateCustomerByOrderSns(processOrderSn, CommonAccountingStateEnum.已对账);

			// 更新 商品 客户对账状态 -> 已对账
			unifiedOrderGoodsService.updateAccountingStateCustomerByOrderSnAndSysUnifiedOrderGoodsIds(processOrderSn, processSysUnifiedOrderGoodsIds, CommonAccountingStateEnum.已对账);

			// 更新 流水 客户对账状态  -> 已对账 20200925 客户结算 创建对账单 如果 积分 流水 有的话 关联 更新状态 为 已对账状态
			// unifiedOrderCreditService.updateAccountingStateCustomerByOrderSnAndOutGoodsIds(processOrderSn, processOutGoodsIds, CommonAccountingStateEnum.已对账);


		} else {

			// 处理 对账单 X 订单 X 商品
			accountingUnifiedCustomerXGoodsService.deleteByAccountingOrderIdAndOrderSnAndSysUnifiedOrderGoodsIds(statementOfAccount.getId(), processUnifiedOrder.getOrderSn(), processSysUnifiedOrderGoodsIds);

			// 更新 订单 客户对账状态 -> 未对账
			unifiedOrderService.updateAccountingStateCustomerByOrderSns(processOrderSn, CommonAccountingStateEnum.未对账);

			// 更新 商品 客户对账状态 -> 未对账
			unifiedOrderGoodsService.updateAccountingStateCustomerByOrderSnAndSysUnifiedOrderGoodsIds(processOrderSn, processSysUnifiedOrderGoodsIds, CommonAccountingStateEnum.未对账);

			// 更新 流水 客户对账状态  -> 未对账 20200925 客户结算 创建对账单 如果 积分 流水 有的话 关联 更新状态 为 已对账状态
			// unifiedOrderCreditService.updateAccountingStateCustomerByOrderSnAndOutGoodsIds(processOrderSn, processOutGoodsIds, CommonAccountingStateEnum.未对账);

		}


		// TODO 是不需要更新对账单的订单数与金额
		accountingUnifiedCustomerServiceCommon.checkAndFixAccountingUnifiedCustomerOfAccountingAmountOfQuantityOrder(statementOfAccount);
	}

	/****************************************** 对账单 编辑   end ******************************************/
}
