package com.zfire.mall.manager.logic.k3;

import cn.hutool.core.date.DateUtil;
import com.zfire.mall.manager.bean.k3.bill.SalesOutStockBean;
import com.zfire.mall.manager.bean.k3.bill.SalesOutStockItemBean;
import com.zfire.mall.manager.bean.k3.bill.result.BillItem;
import com.zfire.mall.manager.bean.k3.bill.result.ResultEntity;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.logic.k3.bill.SalesOrderLogic;
import com.zfire.mall.manager.logic.k3.bill.SalesOutStockLogic;
import com.zfire.mall.manager.plus.entity.DirectTransferOrder;
import com.zfire.mall.manager.plus.entity.DirectTransferOrderItem;
import com.zfire.mall.manager.plus.service.DirectTransferOrderItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class K3CommonMethodLogic {

    @Resource
    SalesOrderLogic salesOrderLogic;
    @Resource
    SalesOutStockLogic salesOutStockLogic;
    @Resource
    DirectTransferOrderItemService directTransferOrderItemService;

    public void syncK3SalesOutStockOrder(DirectTransferOrder directTransferOrder, List<DirectTransferOrderItem> directTransferOrderItems) {
        this.toK3SalesOutStockOrder(directTransferOrder, directTransferOrderItems);

        // 更新直调发货单信息
        directTransferOrder.updateById();
        directTransferOrderItemService.saveOrUpdateBatch(directTransferOrderItems);
    }

    private ResultEntity toK3SalesOutStockOrder(DirectTransferOrder directTransferOrder, List<DirectTransferOrderItem> directTransferOrderItems) {
        // 下推
        ResultEntity resultEntity = salesOrderLogic.pushOutStock(directTransferOrder.getOrderNo(), directTransferOrderItems.stream()
                .map(DirectTransferOrderItem::getRefKingDeeEntryId).collect(Collectors.toList()));

        if(Objects.isNull(resultEntity)) {
            throw new RemoteServiceException("同步金蝶单据失败");
        }

        // 注入销售出库单id
        directTransferOrder.setKingDeeId(resultEntity.getId());

        // 准备更新数据
        SalesOutStockBean salesOutStockBean = new SalesOutStockBean();
        salesOutStockBean.setId(resultEntity.getId());
        salesOutStockBean.setBillNo(directTransferOrder.getId());
        salesOutStockBean.setDate(DateUtil.formatDate(directTransferOrder.getDeliverDate()));
        salesOutStockBean.setRemarks(directTransferOrder.getRemark());

        Map<String, BillItem> billItemMap = resultEntity.getItems().stream()
                .collect(Collectors.toMap(BillItem::getSupplyColVal, Function.identity()));
        List<SalesOutStockItemBean> salesOutStockItemBeans = new ArrayList<>(10);
        for (DirectTransferOrderItem item : directTransferOrderItems) {
            BillItem billItem = billItemMap.get(item.getRefOrderItemId());
            // 注入销售出库单明细内码
            item.setKingDeeSeq(billItem.getSeq());
            item.setKingDeeEntryId(billItem.getEntryId());

            SalesOutStockItemBean salesOutStockItemBean = new SalesOutStockItemBean();
            salesOutStockItemBean.setEntryId(billItem.getEntryId());
            salesOutStockItemBean.setStock(item.getStockNumber());
            salesOutStockItemBean.setRealQty(BigDecimal.valueOf(item.getQty()));
            salesOutStockItemBeans.add(salesOutStockItemBean);
        }
        salesOutStockBean.setEntry(salesOutStockItemBeans);

        return salesOutStockLogic.syncPushBillData(salesOutStockBean);
    }
}
