package net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.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.impl.ItemQueryServiceImpl;
import net.lab1024.sa.admin.module.business.wms.receive.asnDetail.domain.vo.AsnDetailVO;
import net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.dao.PickDetailDao;
import net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.domain.entity.PickDetailEntity;
import net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.domain.form.PickDetailQueryForm;
import net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.domain.vo.PickDetailVO;
import net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.manager.PickDetailManager;
import net.lab1024.sa.admin.module.business.wms.shipping.pickDetail.service.PickDetailQueryService;
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 PickDetailQueryServiceImpl implements PickDetailQueryService {

    @Resource
    private PickDetailDao pickDetailDao;

    @Resource
    private PickDetailManager pickDetailManager;

    @Resource
    private ItemQueryServiceImpl itemQueryService;

    /**
     * 分页查询
     */
    public PageResult<PickDetailVO> queryPage(PickDetailQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<PickDetailVO> list = pickDetailDao.queryPage(page, queryForm);
        // 查询物料名称
        List<Long> itemIdList = list.stream().map(PickDetailVO::getItemId).toList();
        Map<Long, ItemEntity> itemMap = itemQueryService.queryByItemIdsToMap(itemIdList);
        list.forEach(pickDetailVO -> {
            ItemEntity item = itemMap.get(pickDetailVO.getItemId());
            if (item != null) {
                pickDetailVO.setItemCode(item.getItemCode());
                pickDetailVO.setItemName(item.getItemName());
            }
        });
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 根据入库单id查询出库单详情
     *
     * @param pickId 出库单id
     * @return List<PickDetailEntity>
     */
    public List<PickDetailEntity> queryPickDetailByPickId(Long pickId) {
        if (pickId == null) {
            return null;
        }
        LambdaQueryWrapper<PickDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PickDetailEntity::getPickId, pickId);
        return pickDetailManager.list(queryWrapper);
    }

    /**
     * 根据入库单ids查询出库单详情
     *
     * @param pickIds 出库单id
     * @return List<PickDetailEntity>
     */
    public List<PickDetailEntity> queryPickDetailByPickIds(List<Long> pickIds) {
        if (CollectionUtils.isEmpty(pickIds)) {
            return Collections.emptyList();
        }
        //去重
        pickIds = pickIds.stream().filter(Objects::nonNull).distinct().toList();
        LambdaQueryWrapper<PickDetailEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PickDetailEntity::getPickId, pickIds);
        return pickDetailManager.list(queryWrapper);
    }

    /**
     * 根据ids查询出库明细
     *
     * @param idList 出库明细集合
     * @return List<PickDetailEntity>
     */
    public List<PickDetailEntity> queryByPickDetailIds(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }
        //去重
        idList = idList.stream().filter(Objects::nonNull).distinct().toList();
        return pickDetailManager.listByIds(idList);
    }

    /**
     * 根据明细集合查询明细信息
     *
     * @param idList 明细集合
     * @return Map<String, AsnDetailEntity>
     */
    public Map<Long, PickDetailEntity> queryByPickDetailIdsToMap(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyMap();
        }
        //查询明细
        List<PickDetailEntity> pickDetails = queryByPickDetailIds(idList);
        //封装map
        Map<Long, PickDetailEntity> pickDetailMap = Maps.newHashMap();
        for (PickDetailEntity pickDetail : pickDetails) {
            pickDetailMap.put(pickDetail.getPickDetailId(), pickDetail);
        }
        return pickDetailMap;
    }


}
