package com.neusoft.smart.pos.inventory.service;

import com.neusoft.smart.pos.dto.inventory.request.SellAndWarehouseOutRequest;
import com.neusoft.smart.pos.inventory.constants.InventoryConstant;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsConfig;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsConfigExample;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsInfo;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryGoodsLimit;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatch;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreBatchExample;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryGoodsConfigMapper;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryStoreBatchMapper;
import org.springframework.data.util.Pair;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 库存限制
 */
@Service
public class WarehouseOutStorageLimit implements WarehouseOutLimitInterface {
    
    @Resource
    private InventoryGoodsConfigMapper inventoryGoodsConfigMapper;
    
    @Resource
    private InventoryStoreBatchMapper inventoryStoreBatchMapper;
    
    @Override
    public Pair<Boolean, String> checkLimit(InventoryGoodsLimit limit, InventoryGoodsInfo inventoryGoodsInfo, SellAndWarehouseOutRequest.OrderItem orderItem) {
        //查询限制配置,是否可以负库存
        InventoryGoodsConfigExample example = new InventoryGoodsConfigExample();
        example.createCriteria().andShopIdEqualTo(null == orderItem.getShopId() ? 0 : orderItem.getShopId()).
                andOrgIdEqualTo(orderItem.getOrgId());
        List<InventoryGoodsConfig> inventoryGoodsConfigs = inventoryGoodsConfigMapper.selectByExample(example);
        if (inventoryGoodsConfigs.isEmpty() || inventoryGoodsConfigs.get(0).getCanNegativeStorage() == 1) {
            return Pair.of(true, "");
        }
        //查询商品大于0的库存
        InventoryStoreBatchExample example2 = new InventoryStoreBatchExample();
        example2.createCriteria().andGoodsIdEqualTo(orderItem.getGoodsId()).andLockedStorageCountGreaterThan(0).
                andStatusEqualTo(InventoryConstant.USABLE_BATCH);
        List<InventoryStoreBatch> inventoryStoreBatchList = inventoryStoreBatchMapper.selectByExample(example2);
        if (inventoryStoreBatchList.isEmpty()) {
            return Pair.of(false, inventoryGoodsInfo.getName() + "售罄或者已下架");
        }
        BigDecimal sum = new BigDecimal("0");
        for (InventoryStoreBatch batch : inventoryStoreBatchList) {
            BigDecimal aBatch = new BigDecimal(batch.getLockedStorageCount());
            sum = sum.add(aBatch);
        }
        if (sum.compareTo(orderItem.getStorageNum().multiply(new BigDecimal(Math.pow(10, orderItem.getScale())))) < 0) {
            return Pair.of(false, inventoryGoodsInfo.getName() + "剩余库存不足");
        }
        return Pair.of(true, "");
    }
}
