package com.sw.mes.sap.reconciliation.warehouse;

import com.sw.mes.core.api.system.StoreFeignClients;
import com.sw.mes.core.dto.store.*;
import com.sw.mes.core.response.Result;
import com.sw.mes.sap.reconciliation.framework.IntermediateResult;
import com.sw.mes.sap.reconciliation.IntermediateResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class LineWarehouseDataImpl implements LineWarehouseData {
    @Autowired
    private StoreFeignClients storeFeignClients;

    //获取入库数据
    @Override
    public IntermediateResult<WarehouseInterResult> getWarehouseInputBillByCode(String code) {
        Result<List<WarehouseInputBillDetailBo>> result = storeFeignClients.findWarehouseInputBillDetailsByMaterialCode(code);
        if (null != result && result.getCode().equals(200)) {
            WarehouseInterResult warehouseInterResult = new WarehouseInterResult();
            IntermediateResult<WarehouseInterResult> resultIntermediateResult = new IntermediateResult<>();
            List<WarehouseInputBillDetailBo> warehouseInputBillBos = result.getData();
            BigDecimal quantity = new BigDecimal(0);
            if (null != warehouseInputBillBos && warehouseInputBillBos.size() > 0) {
                for (WarehouseInputBillDetailBo warehouseInputBill : warehouseInputBillBos) {
                    if (null != warehouseInputBill.getCount()) {
                        quantity = quantity.add(warehouseInputBill.getCount());
                    }
                }
            }
            warehouseInterResult.setType(0);
            warehouseInterResult.setQuantity(quantity);
            warehouseInterResult.setMaterialCode(code);
            resultIntermediateResult.setCode(IntermediateResultEnum.INPUT.getCode());
            resultIntermediateResult.setData(warehouseInterResult);
            return resultIntermediateResult;
        }
        return null;
    }

    //获取库存数据
    @Override
    public IntermediateResult<WarehouseInterResult> getWarehouseInventoryByCode(String code) {
        Result<List<WarehouseInventoryDto>> result = storeFeignClients.findWarehouseInventoryByMaterialCode(code);
        if (null != result && result.getCode().equals(200)) {
            WarehouseInterResult warehouseInterResult = new WarehouseInterResult();
            IntermediateResult<WarehouseInterResult> resultIntermediateResult = new IntermediateResult<>();
            List<WarehouseInventoryDto> warehouseInventoryDtos = result.getData();
            BigDecimal quantity = new BigDecimal(0);
            if (null != warehouseInventoryDtos && warehouseInventoryDtos.size() > 0) {
                for (WarehouseInventoryDto warehouseInventoryDto : warehouseInventoryDtos) {
                    if (null != warehouseInventoryDto.getQuantity()) {
                        quantity = quantity.add(warehouseInventoryDto.getQuantity());
                    }
                }
            }
            warehouseInterResult.setType(0);
            warehouseInterResult.setQuantity(quantity);
            warehouseInterResult.setMaterialCode(code);
            resultIntermediateResult.setCode(IntermediateResultEnum.INVENTORY.getCode());
            resultIntermediateResult.setData(warehouseInterResult);
            return resultIntermediateResult;
        }
        return null;
    }

    //获取出库数据
    @Override
    public IntermediateResult<WarehouseInterResult> getWarehouseOutputBillByCode(String code) {
        Result<List<WarehouseOutputBillDetailBo>> result = storeFeignClients.findWarehouseOutputBillDetailByMaterialCode(code);
        if (null != result && result.getCode().equals(200)) {
            WarehouseInterResult warehouseInterResult = new WarehouseInterResult();
            IntermediateResult<WarehouseInterResult> resultIntermediateResult = new IntermediateResult<>();
            List<WarehouseOutputBillDetailBo> warehouseOutputBillDetailBos = result.getData();
            BigDecimal quantity = new BigDecimal(0);
            if (null != warehouseOutputBillDetailBos && warehouseOutputBillDetailBos.size() > 0) {
                for (WarehouseOutputBillDetailBo warehouseOutputBillDetailBo : warehouseOutputBillDetailBos) {
                    BigDecimal outputCount = warehouseOutputBillDetailBo.getOutputCount(); //审核出库时，出库人填写的真实要出库的数量
                    BigDecimal requireCount = warehouseOutputBillDetailBo.getRequireCount(); //需求数量，是创建出库单时，需要的数量
                    if (outputCount == null || outputCount.compareTo(new BigDecimal(0)) == 0) {
                        //如果出库数量为空，则默认出库数量为需求数量(正常情况下，二者是相等的)
                        outputCount = requireCount;
                    }
                    if (requireCount == null || requireCount.compareTo(new BigDecimal(0)) == 0) {
                        requireCount = outputCount;
                    }
                    quantity = quantity.add(requireCount);
                }
            }
            warehouseInterResult.setType(0);
            warehouseInterResult.setQuantity(quantity);
            warehouseInterResult.setMaterialCode(code);
            resultIntermediateResult.setCode(IntermediateResultEnum.OUTPUT.getCode());
            resultIntermediateResult.setData(warehouseInterResult);
            return resultIntermediateResult;
        }
        return null;
    }

    @Override
    public Set<String> getDistinctMaterialCode(int page, int pageSize) {
        Result<Set<String>> result = storeFeignClients.getDistinctMaterialCode(page, pageSize);
        if (null != result && result.getCode() == 200) {
            return result.getData();
        }
        return Collections.emptySet();
    }
}
