package net.lab1024.sa.admin.module.business.wms.inventory.itemKey.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.inventory.itemKey.dao.ItemKeyDao;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.entity.ItemKeyEntity;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.form.ItemKeyQueryForm;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.domain.vo.ItemKeyVO;
import net.lab1024.sa.admin.module.business.wms.inventory.itemKey.manager.ItemKeyManager;
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 ItemKeyQueryServiceImpl implements ItemKeyQueryService {

    @Resource
    private ItemKeyDao itemKeyDao;

    @Resource
    private ItemKeyManager itemKeyManager;

    /**
     * 分页查询
     */
    public PageResult<ItemKeyVO> queryPage(ItemKeyQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ItemKeyVO> list = itemKeyDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 根据物料属性ID集合查询物料属性信息
     *
     * @param itemKeyIdList 物料属性ID集合
     * @return Map<Long, ItemKeyEntity>
     */
    public Map<Long, ItemKeyEntity> queryByItemKeyIds(List<Long> itemKeyIdList) {
        if (CollectionUtils.isEmpty(itemKeyIdList)) {
            return Collections.emptyMap();
        }
        itemKeyIdList = itemKeyIdList.stream().filter(Objects::nonNull).distinct().toList();
        Map<Long, ItemKeyEntity> itemKeyMap = Maps.newHashMap();
        for (Long itemKeyId : itemKeyIdList) {
            ItemKeyEntity itemKey = itemKeyManager.queryItemKey(itemKeyId);
            if (itemKey != null) {
                itemKeyMap.put(itemKeyId, itemKey);
            }
        }
        return itemKeyMap;
    }

    public ItemKeyEntity queryItemKey(String orderNumber, Long itemId, String propC1) {
        LambdaQueryWrapper<ItemKeyEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ItemKeyEntity::getOrderNumber, orderNumber);
        queryWrapper.eq(ItemKeyEntity::getItemId, itemId);
        queryWrapper.eq(ItemKeyEntity::getPropC1, propC1);
        List<ItemKeyEntity> itemKeys = itemKeyManager.list(queryWrapper);
        if (CollectionUtils.isEmpty(itemKeys)) {
            return null;
        }
        return itemKeys.get(0);
    }

    public List<ItemKeyEntity>  queryItemKeys(List<Long> itemIds) {
        if (CollectionUtils.isEmpty(itemIds)){
            return Collections.emptyList();
        }
        itemIds = itemIds.stream().filter(Objects::nonNull).distinct().toList();
        LambdaQueryWrapper<ItemKeyEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ItemKeyEntity::getItemId, itemIds);
        return itemKeyManager.list(queryWrapper);
    }
}
