package cn.superdesk.uniorder.service.accounting.unified.supplier.Impl;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.common.CommonAccountingStateEnum;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplier;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierOrder;
import cn.superdesk.uniorder.dao.entity.accounting.unified.supplier.AccountingUnifiedSupplierOrderExample;
import cn.superdesk.uniorder.dao.entity.base.UnifiedOrder;
import cn.superdesk.uniorder.dao.mapper.accounting.unified.supplier.*;
import cn.superdesk.uniorder.dao.mapper.base.*;
import cn.superdesk.uniorder.service.accounting.unified.supplier.AccountingUnifiedSupplierServiceOpEditDimOrder;
import cn.superdesk.uniorder.service.accounting.unified.supplier.AccountingUnifiedSupplierXGoodsService;
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.setting.UnifiedSnConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @Description: Java类作用描述
 * @Author: linjinyu
 * @CreateDate: 2021/10/26 上午10:28
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/10/26 上午10:28
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class AccountingUnifiedSupplierServiceOpEditDimOrderImpl implements AccountingUnifiedSupplierServiceOpEditDimOrder {

	@Autowired
	private UnifiedOrderMapper                   unifiedOrderMapper;
	@Autowired
	private UnifiedOrderMapperEx                 unifiedOrderMapperEx;
	@Autowired
	private UnifiedOrderMapperExSupplierDimOrder unifiedOrderMapperExSupplierDimOrder;
	@Autowired
	private UnifiedOrderService                  unifiedOrderService;

	@Autowired
	private UnifiedOrderGoodsMapper   unifiedOrderGoodsMapper;
	@Autowired
	private UnifiedOrderGoodsMapperEx unifiedOrderGoodsMapperEx;
	@Autowired
	private UnifiedOrderGoodsService  unifiedOrderGoodsService;

	@Autowired
	private UnifiedOrderCreditMapper  unifiedOrderCreditMapper;
	@Autowired
	private UnifiedOrderCreditService unifiedOrderCreditService;

	@Autowired
	private AccountingUnifiedSupplierMapper   accountingUnifiedSupplierMapper;
	@Autowired
	private AccountingUnifiedSupplierMapperEx accountingUnifiedSupplierMapperEx;

	@Autowired
	private AccountingUnifiedSupplierOrderMapper    accountingUnifiedSupplierOrderMapper;
	@Autowired
	private AccountingUnifiedSupplierXOrderMapperEx accountingUnifiedSupplierXOrderMapperEx;

	@Autowired
	private AccountingUnifiedSupplierXGoodsMapper   accountingUnifiedSupplierXGoodsMapper;
	@Autowired
	private AccountingUnifiedSupplierXGoodsMapperEx accountingUnifiedSupplierXGoodsMapperEx;

	@Autowired
	private AccountingUnifiedSupplierXGoodsService accountingUnifiedSupplierXGoodsService;

	@Autowired
	private UnifiedSnConfigService unifiedSnConfigService;

	/**
	 * 这个是用于 编辑 对账单 一条一条
	 *
	 * @param statementOfAccount
	 * @param order
	 * @param add
	 * @param loginSysUser
	 */
	@Transactional
	@Override
	public void opEditAccountingDimOrder(
			AccountingUnifiedSupplier statementOfAccount,
			UnifiedOrder order,
			/* 方向 1 新增 非1 删除 加入 orderSn 对映的商品 */ String add,
			LoginSysUser loginSysUser) {

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

			// 恢复 订单为 已对账订单
			UnifiedOrder unifiedOrder = new UnifiedOrder();

			unifiedOrder.setId(order.getId());
			unifiedOrder.setAccountingStateSupplier(CommonAccountingStateEnum.已对账.getKey());
			unifiedOrder.setUpdateTime(DateUtil.date());
			unifiedOrder.setUpdateUserId(loginSysUser.getSysUserId());

			unifiedOrderMapper.updateByPrimaryKeySelective(unifiedOrder);

			// 添加 对账单 X 订单关系
			AccountingUnifiedSupplierOrder accountingUnifiedSupplierOrder = new AccountingUnifiedSupplierOrder();
			accountingUnifiedSupplierOrder.setAmount(order.getAmount());
			accountingUnifiedSupplierOrder.setCostAmount(order.getCostAmount());
			accountingUnifiedSupplierOrder.setOrderSn(order.getOrderSn());
			accountingUnifiedSupplierOrder.setAccountingOrderSn(statementOfAccount.getAccountingOrderSn());

			accountingUnifiedSupplierOrderMapper.insert(accountingUnifiedSupplierOrder);

			// TODO 是不需要更新对账单的订单数与金额

		} else {

			// 恢复 订单为 未对账订单
			UnifiedOrder unifiedOrder = new UnifiedOrder();

			unifiedOrder.setId(order.getId());
			unifiedOrder.setAccountingStateSupplier(CommonAccountingStateEnum.未对账.getKey());
			unifiedOrder.setUpdateTime(DateUtil.date());
			unifiedOrder.setUpdateUserId(loginSysUser.getSysUserId());

			unifiedOrderMapper.updateByPrimaryKeySelective(unifiedOrder);

			// 删除 对账单 X 订单关系
			AccountingUnifiedSupplierOrderExample deleteByExample = new AccountingUnifiedSupplierOrderExample();
			deleteByExample.createCriteria()
					.andOrderSnEqualTo(order.getOrderSn())
					.andAccountingOrderSnEqualTo(statementOfAccount.getAccountingOrderSn());
			accountingUnifiedSupplierOrderMapper.deleteByExample(deleteByExample);

			// TODO 是不需要更新对账单的订单数与金额
		}

	}
}
