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


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.service.setting.UnifiedSnConfigService;
import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingStateEnum;
import cn.superdesk.uniorder.common.enums.accounting.customer.AccountingOrderCustomerAccountingStatusEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderBackAccountStatusEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderDimCreateByEnum;
import cn.superdesk.uniorder.common.enums.accounting.AccountingOrderInvoiceApplyStatusEnum;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerOrder;
import cn.superdesk.uniorder.dao.entity.accounting.unified.customer.AccountingUnifiedCustomerOrderGoods;
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.entity.sys.SysOrder;
import cn.superdesk.uniorder.dao.entity.invoice.common.InvoiceBankInfo;
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.dto.param.accounting.unified.customer.UnifiedCustomerCreateDimOrderParam;
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.invoice.common.InvoiceBankInfoService;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;


/**
 * @Description: 客户结算 对账单 重构 拆分 创建部分
 * @Author: linjinyu
 * @CreateDate: 2021/1/20 下午3:40
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/1/20 下午3:40
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedCustomerServiceOpCreateDimOrderImpl implements AccountingUnifiedCustomerServiceOpCreateDimOrder {

	@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 InvoiceBankInfoService unifiedBankInfoService;

//	@Autowired
//	private SysConfigService sysConfigService;

	@Autowired
	private UnifiedSnConfigService unifiedSnConfigService;

	/****************************************** 对账单 创建 start ******************************************/
	/**
	 * 这个是老的生成对账单
	 *
	 * @param accountingUnifiedCustomer
	 * @param accountingUnifiedCustomerOrderList
	 * @return
	 */
	@Transactional
	@Override
	public AccountingUnifiedCustomer addTran(AccountingUnifiedCustomer accountingUnifiedCustomer, List<AccountingUnifiedCustomerOrder> accountingUnifiedCustomerOrderList) {


		accountingUnifiedCustomer.setAccountingOrderSn(unifiedSnConfigService.getCustomerStatementOfAccountSnBySnowflake(""));

		accountingUnifiedCustomerMapper.insert(accountingUnifiedCustomer);

		for (AccountingUnifiedCustomerOrder dzOrder : accountingUnifiedCustomerOrderList) {

			dzOrder.setDzId(accountingUnifiedCustomer.getId());
			accountingUnifiedCustomerOrderMapper.insert(dzOrder);

			// 更新订单对账状态
			SysOrder sysOrder = new SysOrder();
			sysOrder.setId(dzOrder.getOrderId());
			sysOrder.setIsGenBill(Byte.valueOf("1"));
			sysOrderMapper.updateByPrimaryKeySelective(sysOrder);


			// 这个字段是重构前是真的,但对映的数据库字段是没有的
			// 整个 弃了吧 start
//			Map<String, Object> queryMap = new HashMap<>();
//			queryMap.put("sysOrderId", dzOrder.getMyOrderId());
//			List<SysGood> goods = dzOrderGoodMapper.findAllFromGood(queryMap);
//
//			for (SysGood sysGood : goods) {
//				AccountingUnifiedCustomerOrderGoods orderGood = AnConvertBeanUtil.copyBean(sysGood, AccountingUnifiedCustomerOrderGoods.class);
//				orderGood.setDzOrderId(dzOrder.getId());
//				orderGood.setGoodId(sysGood.getId());
//
//				dzOrderGoodMapper.insert(orderGood);
//			}
			// 整个 弃了吧 end
		}

		// 更新账单号
//		sysConfigService.updateSysConfigFieldDataIntByCode(codeMap);

		return accountingUnifiedCustomer;
	}


	@Override
	public AccountingUnifiedCustomer opCreateAccountingDimOrderPreProcess(Long userId, List<UnifiedOrder> unifiedOrderList, UnifiedCustomerCreateDimOrderParam unifiedCustomerCreateDimOrderParam) {

		/****************************************** 添加检查 mark by linjinyu at 20210125 start ******************************************/
		if (AnCollectionUtils.isEmpty(unifiedOrderList)) {
			// TODO 记录错误-传过来的数据为空
			return null;
		}

		/****************************************** 过虑要对账商品中是否有已对账单 mark by linjinyu at 20210125 start ******************************************/
		List<String>            processUnifiedOrderSns       = unifiedOrderList.stream().filter(AnLamdaUtils.distinctByKey(o -> o.getOrderSn())).map(o -> o.getOrderSn()).collect(Collectors.toList());
		List<UnifiedOrderGoods> processUnifiedOrderGoodsList = unifiedOrderGoodsService.findByOrderSns(processUnifiedOrderSns);

		List<Predicate<UnifiedOrderGoods>> predicatesUnifiedOrderGoodsByAccountingStateCustomerEq1 = new ArrayList<>();
		predicatesUnifiedOrderGoodsByAccountingStateCustomerEq1.add(r -> r.getAccountingStateCustomer() != null && r.getAccountingStateCustomer() == 1);
		List<UnifiedOrderGoods> predicatesUnifiedOrderGoodsByAccountingStateCustomerEq1Filter = AnLamdaUtils.filters(processUnifiedOrderGoodsList, predicatesUnifiedOrderGoodsByAccountingStateCustomerEq1);

		if (AnCollectionUtils.isNotEmpty(predicatesUnifiedOrderGoodsByAccountingStateCustomerEq1Filter)) {
			// TODO 记录错误-传过来的数据-包含已对账商品
			return null;
		}


		/****************************************** 核算 对账单 总金额 mark by linjinyu at 20210125 start ******************************************/
		BigDecimal goodsAmount = processUnifiedOrderGoodsList
				.stream()
				.map(item -> item.getPrice() == null ? BigDecimal.ZERO : item.getPrice().multiply(new BigDecimal(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);
		BigDecimal goodsCostAmount = processUnifiedOrderGoodsList
				.stream()
				.map(item -> item.getCostPrice() == null ? BigDecimal.ZERO : item.getCostPrice().multiply(new BigDecimal(item.getQuantity())))
				.reduce(BigDecimal.ZERO, BigDecimal::add);

		unifiedCustomerCreateDimOrderParam.setDzMoney(goodsAmount);

		/****************************************** 核算 对账单 订单数 mark by linjinyu at 20210125 start ******************************************/
		unifiedCustomerCreateDimOrderParam.setOrderQuantity(processUnifiedOrderSns.size());

		/****************************************** 核算 对账单 联系人信息 mark by linjinyu at 20210125 start ******************************************/

		if (null == unifiedCustomerCreateDimOrderParam.getPurchaseUserId()) {
			UnifiedOrder sample = unifiedOrderList.get(0);

			unifiedCustomerCreateDimOrderParam.setServiceNo(sample.getServiceNo());
			unifiedCustomerCreateDimOrderParam.setPurchaseUserId(sample.getPurchaseUserId());
			unifiedCustomerCreateDimOrderParam.setPurchaseUserName(sample.getPurchaseUserName());
			unifiedCustomerCreateDimOrderParam.setPurchasePhone(sample.getPurchasePhone());

		}


		/****************************************** 核算 bankinfo mark by linjinyu at 20210125 start ******************************************/

		if (ObjectUtil.isNull(unifiedCustomerCreateDimOrderParam.getBankInfoId())) {

			InvoiceBankInfo invoiceBankInfoEntity = unifiedBankInfoService.getByDefault();

			if (ObjectUtil.isNull(invoiceBankInfoEntity)) {
				// TODO 记录错误-传过来的数据-没有Bankinfo
				return null;
			} else {
				unifiedCustomerCreateDimOrderParam.setBankInfoId(invoiceBankInfoEntity.getId());
			}
		}

		/****************************************** 处理 AccountingUnifiedCustomerOrder start ******************************************/

		List<AccountingUnifiedCustomerOrder> accountingUnifiedCustomerOrderList = new ArrayList<>();

		for (UnifiedOrder unifiedOrder : unifiedOrderList) {

			AccountingUnifiedCustomerOrder accountingUnifiedCustomerOrder = new AccountingUnifiedCustomerOrder();

			// AnConvertBeanUtil.copyBean(order, DzOrder.class); 弃用 字段差得太多
			accountingUnifiedCustomerServiceCommon.convertUnifiedOrder2AccountingUnifiedCustomerOrder(accountingUnifiedCustomerOrder, unifiedOrder);

			accountingUnifiedCustomerOrderList.add(accountingUnifiedCustomerOrder);
		}
		/****************************************** 处理 AccountingUnifiedCustomerOrder   end ******************************************/


		/****************************************** 处理 AccountingUnifiedCustomer start ******************************************/

		AccountingUnifiedCustomer accountingUnifiedCustomer = new AccountingUnifiedCustomer();

		// TODO 写死
		accountingUnifiedCustomer.setDateType(2);
//		accountingUnifiedCustomer.setStartTime(sdf.parse(unifiedCustomerCreateDimOrderParam.getStartTime()));
//		accountingUnifiedCustomer.setEndTime(sdf.parse(unifiedCustomerCreateDimOrderParam.getEndTime()));
		accountingUnifiedCustomer.setStartTime(unifiedCustomerCreateDimOrderParam.getStartTime());
		accountingUnifiedCustomer.setEndTime(unifiedCustomerCreateDimOrderParam.getEndTime());

		accountingUnifiedCustomer.setCycleBeginTime(unifiedCustomerCreateDimOrderParam.getStartTime());
		accountingUnifiedCustomer.setCycleEndTime(unifiedCustomerCreateDimOrderParam.getEndTime());


		// TODO 错错错 哪就不是错
		accountingUnifiedCustomer.setEnterpriseId(unifiedOrderList.get(0).getPurchaseVirtId());

		accountingUnifiedCustomer.setIsSendEmail(unifiedCustomerCreateDimOrderParam.getIsSendEmail());
		accountingUnifiedCustomer.setSendEmailTime(DateUtil.date());

		accountingUnifiedCustomer.setCreateTime(DateUtil.date());
		accountingUnifiedCustomer.setCreateUserId(userId);
		accountingUnifiedCustomer.setUpdateTime(DateUtil.date());
		accountingUnifiedCustomer.setUpdateUserId(userId);

		// add 20200110
		// TODO 写死 支付类型
		accountingUnifiedCustomer.setPayType(0);
		// TODO 写死 支付状态
		accountingUnifiedCustomer.setPayStatus(2);


		accountingUnifiedCustomer.setServiceNo(unifiedCustomerCreateDimOrderParam.getServiceNo());

		accountingUnifiedCustomer.setPurchaseUserName(unifiedCustomerCreateDimOrderParam.getPurchaseUserName());
		accountingUnifiedCustomer.setPurchasePhone(unifiedCustomerCreateDimOrderParam.getPurchasePhone());
		accountingUnifiedCustomer.setPurchaseUserId(unifiedCustomerCreateDimOrderParam.getPurchaseUserId());

		// add
		accountingUnifiedCustomer.setBankInfoId(unifiedCustomerCreateDimOrderParam.getBankInfoId());

		accountingUnifiedCustomer.setAccountingAmount(unifiedCustomerCreateDimOrderParam.getDzMoney());
		accountingUnifiedCustomer.setQuantityOrder(unifiedCustomerCreateDimOrderParam.getOrderQuantity());

		accountingUnifiedCustomer.setInvoiceStatus(AccountingOrderInvoiceApplyStatusEnum.未申请.getKey());
		// 新版 3-待提交
		accountingUnifiedCustomer.setAccountingStatus(AccountingOrderCustomerAccountingStatusEnum.待提交.getKey());
		accountingUnifiedCustomer.setBackAccountStatus(AccountingOrderBackAccountStatusEnum.未回款.getKey());

		// accountingUnifiedCustomer.setInvoiceCompanyName(orders.get(0).get());
		// accountingUnifiedCustomer.setInvoicetype(orders.get(0).getInvoicetype());
		// accountingUnifiedCustomer.setInvoiceTaxpayersIDCode(orders.get(0).getTaxpayersidcode());

		// 此地址订单上也没有 是下单人的地址
		// accountingUnifiedCustomer.setPurchaseAddress(orders.get(0).getAddressAddress());


		/****************************************** 处理 AccountingUnifiedCustomer   end ******************************************/

		return opCreateAccountingDimOrder(accountingUnifiedCustomer, accountingUnifiedCustomerOrderList);
	}

	/**
	 * 新版 对账单
	 *
	 * @param accountingUnifiedCustomer
	 * @param accountingUnifiedCustomerOrderList
	 * @return
	 */
	@Transactional
	@Override
	public AccountingUnifiedCustomer opCreateAccountingDimOrder(AccountingUnifiedCustomer accountingUnifiedCustomer, List<AccountingUnifiedCustomerOrder> accountingUnifiedCustomerOrderList) {


		accountingUnifiedCustomer.setDimCreateBy(AccountingOrderDimCreateByEnum.订单.getKey());
		// 用于标记生成逻辑:10-早期(加字段时已经默认);20-改版202012
		accountingUnifiedCustomer.setLogicMode(20);


		accountingUnifiedCustomer.setAccountingOrderSn(unifiedSnConfigService.getCustomerStatementOfAccountSnBySnowflake(""));

		accountingUnifiedCustomerMapper.insert(accountingUnifiedCustomer);

		/****************************************** 初始化 start ******************************************/
		// 大概的作用类似批次
		Date processDate = DateUtil.date();
		// 大概是能再放上一层
		List<AccountingUnifiedCustomerXGoods> processAccountingUnifiedCustomerXGoodsList = new ArrayList<>();
		/****************************************** 初始化   end ******************************************/

		List<String> processOrderSns = accountingUnifiedCustomerOrderList.stream().filter(AnLamdaUtils.distinctByKey(o -> o.getOrderSn())).map(o -> o.getOrderSn()).collect(Collectors.toList());

		/**
		 * 通过上一步 处理过的 accountingUnifiedCustomerOrderList 再来 返过来再 处理 unified_order 更新 客户结算状态  unified_order_goods
		 */
		for (AccountingUnifiedCustomerOrder accountingUnifiedCustomerOrder : accountingUnifiedCustomerOrderList) {

			// TODO 没有判定就入库 mark by linjinyu at 20201228
			accountingUnifiedCustomerOrder.setDzId(accountingUnifiedCustomer.getId());

			accountingUnifiedCustomerOrderMapper.insert(accountingUnifiedCustomerOrder);


			/****************************************** 单个订单 客户结算状态 更新 重构 改成批量 mark by linjinyu at 202012281757 start ******************************************/
//			UnifiedOrder order = new UnifiedOrder();
//			order.setId(accountingUnifiedCustomerOrder.getOrderId());
//			// 更新订单对账状态
//			// TODO 写死
//			order.setAccountingStateCustomer(1);
//			unifiedOrderMapper.updateByPrimaryKeySelective(order);
			/****************************************** 单个订单 客户结算状态 更新 重构 改成批量 mark by linjinyu at 202012281757   end ******************************************/


			/****************************************** 处理 正的商品 start ******************************************/
			// TODO 迷题已经解开 TAPD_ID_1000492 当时此代码屏蔽 但相关 逻辑并未修改 所以 会有可能 有些对账单 没有详情
			// change 20200302 抛弃对账下货品信息
			// TODO 20200428 linjinyu 打开 start ====================
//			Map<String, Object> queryMap = new HashMap<>();
//			queryMap.put("orderSn", accountingUnifiedCustomerOrder.getOrderSn());
//			List<UnifiedOrderGoods> unifiedOrderGoodsList = unifiedOrderGoodsMapperEx.findAll(queryMap);

			/****************************************** 重构 mark by linjinyu at 20210113 start ******************************************/
			List<UnifiedOrderGoods> unifiedOrderGoodsList = new ArrayList<>();
			unifiedOrderGoodsList = unifiedOrderGoodsService.findByOrderSn(accountingUnifiedCustomerOrder.getOrderSn());


			// 已完成  还要 更新 unified_order_goods 的客户对账状态 mark by linjinyu at 202012241719


			for (UnifiedOrderGoods unifiedOrderGoods : unifiedOrderGoodsList) {

				/****************************************** 关联 start ******************************************/
				// 重构 mark by linjinyu at 20210113

				processAccountingUnifiedCustomerXGoodsList.add(accountingUnifiedCustomerXGoodsService.composeAccountingUnifiedCustomerXGoods02(accountingUnifiedCustomer, accountingUnifiedCustomerOrder, unifiedOrderGoods, AccountingOrderDimCreateByEnum.订单, processDate));
				/****************************************** 关联   end ******************************************/

				AccountingUnifiedCustomerOrderGoods accountingUnifiedCustomerOrderGoods = new AccountingUnifiedCustomerOrderGoods();
				// AnConvertBeanUtil.copyBean(sysGood, DzOrderGood.class); 弃用
				accountingUnifiedCustomerServiceCommon.convertUnifiedOrderGoods2AccountingUnifiedCustomerOrderGoods(accountingUnifiedCustomerOrderGoods, unifiedOrderGoods, accountingUnifiedCustomerOrder.getId());
				accountingUnifiedCustomerOrderGoodsMapper.insert(accountingUnifiedCustomerOrderGoods);
			}


			// TODO 20200428 linjinyu 打开  end  ====================


			/****************************************** 处理 正的商品   end ******************************************/


			/****************************************** 处理 运费 这个已经处理过 大概是要屏蔽 start ******************************************/
			// 判断是否运费//change 20190925
			// TODO 20200428 linjinyu 打开 start ====================
			// TODO mark by linjinyu 运费作为 商品 插入一行
			// TODO mark by linjinyu at 20201203 这个是不是有逻辑问题呀,为什么这里又加一个
//			if (accountingUnifiedCustomerOrder.getDispatchPrice().doubleValue() > 0) {
//
//				AccountingUnifiedCustomerOrderGoods accountingUnifiedCustomerOrderGoods = new AccountingUnifiedCustomerOrderGoods();
//
//				accountingUnifiedCustomerOrderGoods.setDzOrderId(accountingUnifiedCustomerOrder.getId());
//
//				accountingUnifiedCustomerOrderGoods.setOptionname("运费");
//				accountingUnifiedCustomerOrderGoods.setPrice(accountingUnifiedCustomerOrder.getDispatchPrice());
//				accountingUnifiedCustomerOrderGoods.setTotal(1);
//				accountingUnifiedCustomerOrderGoods.setTotalPrice(accountingUnifiedCustomerOrder.getDispatchPrice());
//				accountingUnifiedCustomerOrderGoods.setUnitPrice(accountingUnifiedCustomerOrder.getDispatchPrice());
//
//				accountingUnifiedCustomerOrderGoodsMapper.insert(accountingUnifiedCustomerOrderGoods);
//
//			}
			// TODO 20200428 linjinyu 打开  end  ====================
			/****************************************** 处理 运费 这个已经处理过 大概是要屏蔽   end ******************************************/


			/****************************************** 处理 退单 这个已经处理过 大概是要屏蔽 start ******************************************/
			// 判断是否有退单 change 2019-09-25
			// TODO 20200428 linjinyu 打开 start ====================
			// 查询退单 start ================================================================================
//			Map<String, Object> returnOrderSearchParams = new HashMap<>();
//			returnOrderSearchParams.put("orderSn", accountingUnifiedCustomerOrder.getOrderSn());
//			List<ViewUnifiedOrderReturn> rs = unifiedOrderReturnGoodsMapperEx.findView(returnOrderSearchParams);
//			// 查询退单 end   ================================================================================
//
//
//			// 处理退单 start ================================================================================
//			if (rs != null && rs.size() > 0) {
//
//				for (ViewUnifiedOrderReturn vr : rs) {
//
//					AccountingUnifiedCustomerOrderGoods accountingUnifiedCustomerOrderGoods = new AccountingUnifiedCustomerOrderGoods();
//
//					accountingUnifiedCustomerOrderGoods.setOptionname(vr.getGoodsName());
//					accountingUnifiedCustomerOrderGoods.setPrice(new BigDecimal(0-(vr.getQuantity() * vr.getPrice().doubleValue())));
//					accountingUnifiedCustomerOrderGoods.setTotal(0-vr.getQuantity());
//					accountingUnifiedCustomerOrderGoods.setTotalPrice(new BigDecimal(0-(vr.getQuantity() * vr.getPrice().doubleValue())));
//					accountingUnifiedCustomerOrderGoods.setUnitPrice(vr.getPrice());
//					accountingUnifiedCustomerOrderGoods.setDzOrderId(accountingUnifiedCustomerOrder.getId());
//
//					accountingUnifiedCustomerOrderGoodsMapper.insert(accountingUnifiedCustomerOrderGoods);
//				}
//
//			}
			// 处理退单 end   ================================================================================
			// TODO 20200428 linjinyu 打开  end  ====================
			/****************************************** 处理 退单 这个已经处理过 大概是要屏蔽   end ******************************************/
		}

		// store 对账单 X 订单 X 商品
		accountingUnifiedCustomerXGoodsService.insertBatch(processAccountingUnifiedCustomerXGoodsList);

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

		// 更新 商品 客户对账状态
		unifiedOrderGoodsService.updateAccountingStateCustomerByOrderSn(processOrderSns, CommonAccountingStateEnum.已对账);

		// 更新 流水 客户对账状态
//		unifiedOrderCreditService.updateAccountingStateCustomerByOrderSn(processOrderSn, CommonAccountingStateEnum.已对账);

		return accountingUnifiedCustomer;
	}
	/****************************************** 对账单 创建   end ******************************************/
}
