package net.lab1024.sa.admin.module.business.wms.inventory.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.wms.base.item.service.ItemQueryService;
import net.lab1024.sa.admin.module.business.wms.base.location.domain.entity.LocationEntity;
import net.lab1024.sa.admin.module.business.wms.base.location.service.LocationQueryService;
import net.lab1024.sa.admin.module.business.wms.base.stock.domain.entity.StockEntity;
import net.lab1024.sa.admin.module.business.wms.base.stock.service.StockQueryService;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.dao.InventoryDao;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.domain.entity.InventoryEntity;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.domain.form.InventoryQueryForm;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.domain.vo.InventoryVO;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.manager.InventoryManager;
import net.lab1024.sa.admin.module.business.wms.inventory.inventory.service.InventoryQueryService;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.entity.ItemKeyEntity;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.service.ItemKeyQueryService;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class InventoryQueryServiceImpl implements InventoryQueryService {

    @Resource
    private InventoryDao inventoryDao;

    @Resource
    private InventoryManager inventoryManager;

    @Resource
    private ItemQueryService itemQueryService;

    @Resource
    private StockQueryService stockQueryService;

    @Resource
    private LocationQueryService locationQueryService;

    @Resource
    private ItemKeyQueryService itemKeyQueryService;


    /**
     * 分页查询
     */
    public PageResult<InventoryVO> queryPage(InventoryQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<InventoryVO> list = inventoryDao.queryPage(page, queryForm);
        // 查询物料属性
        List<Long> itemKeyIds = list.stream().map(InventoryVO::getItemKeyId).toList();
        Map<Long, ItemKeyEntity> itemKeyMap = itemKeyQueryService.queryByItemKeyIds(itemKeyIds);

        //查询物料
        List<Long> itemIds = itemKeyMap.values().stream().map(ItemKeyEntity::getItemId).toList();
        Map<Long, ItemEntity> itemMap = itemQueryService.queryByItemIdsToMap(itemIds);

        //查询容器
        List<Long> stockIds = list.stream().map(InventoryVO::getStockId).toList();
        Map<Long, StockEntity> stockMap = stockQueryService.queryByStockIdsToMap(stockIds);

        //查询库位
        List<Long> locationIds = list.stream().map(InventoryVO::getLocationId).toList();
        Map<Long, LocationEntity> locationMap = locationQueryService.queryByLocationIdsToMap(locationIds);

        list.forEach(InventoryVO -> {
            ItemKeyEntity itemKey = itemKeyMap.get(InventoryVO.getItemKeyId());
            if (itemKey != null) {
                ItemEntity item = itemMap.get(itemKey.getItemId());
                if (item != null) {
                    InventoryVO.setItemCode(item.getItemCode());
                    InventoryVO.setItemName(item.getItemName());
                }
            }
            StockEntity stock = stockMap.get(InventoryVO.getStockId());
            if (stock != null) {
                InventoryVO.setStockCode(stock.getStockCode());
            }
            LocationEntity location = locationMap.get(InventoryVO.getLocationId());
            if (location != null) {
                InventoryVO.setLocationCode(location.getLocationCode());
            }
        });
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * @param ItemKeyId 物料属性
     * @return InventoryEntity
     */
    public InventoryEntity queryInventoryByItemKeyId(Long ItemKeyId) {
        LambdaQueryWrapper<InventoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InventoryEntity::getItemKeyId, ItemKeyId);
        return inventoryManager.getOne(queryWrapper);
    }

    /**
     * @param itemIds 物料
     * @return List<InventoryEntity>
     */
    public List<InventoryEntity> queryInventoryByItemIds(List<Long> itemIds) {
        if (CollectionUtils.isEmpty(itemIds)) {
            return Collections.emptyList();
        }
        List<ItemKeyEntity> itemKeys = itemKeyQueryService.queryItemKeys(itemIds);
        if (CollectionUtils.isEmpty(itemKeys)) {
            return Collections.emptyList();
        }
        List<Long> itemKeyIds = itemKeys.stream().map(ItemKeyEntity::getItemKeyId).toList();
        LambdaQueryWrapper<InventoryEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(InventoryEntity::getItemKeyId, itemKeyIds).apply("quantity - queued_quantity > 0");;
        return inventoryManager.list(queryWrapper);
    }
}
